|
| 1 | +package com.luox6.battleship; |
| 2 | + |
| 3 | +import com.luox6.battleship.gui.GUIController; |
| 4 | +import com.luox6.battleship.gui.GUIViewer; |
| 5 | +import com.luox6.battleship.gui.factory.Dialog; |
| 6 | +import com.luox6.battleship.gui.model.PlayerBoard; |
| 7 | +import com.luox6.battleship.gui.model.UserSetting; |
| 8 | +import com.luox6.battleship.model.GameBoard; |
| 9 | +import com.luox6.battleship.model.ship.StandbyShip; |
| 10 | +import com.luox6.battleship.network.Client; |
| 11 | +import com.luox6.battleship.network.Connectable; |
| 12 | +import com.luox6.battleship.network.Protocol; |
| 13 | +import com.luox6.battleship.network.Server; |
| 14 | + |
| 15 | +import javax.swing.*; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.concurrent.Callable; |
| 19 | +import java.util.concurrent.ExecutorService; |
| 20 | +import java.util.concurrent.Executors; |
| 21 | +import java.util.concurrent.Future; |
| 22 | + |
| 23 | +public class Main { |
| 24 | + /** |
| 25 | + * This main method is main for test purpose. |
| 26 | + * @param args |
| 27 | + */ |
| 28 | + public static void main(String[] args) { |
| 29 | + // We need to gather: mode, server name, etc. |
| 30 | + final Protocol.Mode m = Dialog.selectMode(null); |
| 31 | + final String name; |
| 32 | + final Integer port; |
| 33 | + final String address; |
| 34 | + |
| 35 | + if (m == null) { |
| 36 | + // Cancelled |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + // Server specific info |
| 41 | + Object[] config = null; |
| 42 | + if (m == Protocol.Mode.SERVER) { |
| 43 | + while (config == null) { |
| 44 | + config = getServerInfo(); |
| 45 | + } |
| 46 | + port = (Integer) config[0]; |
| 47 | + name = (String) config[1]; |
| 48 | + address = null; |
| 49 | + } else { |
| 50 | + while (config == null) { |
| 51 | + config = getClientInfo(); |
| 52 | + } |
| 53 | + address = (String) config[0]; |
| 54 | + port = (Integer) config[1]; |
| 55 | + name = (String) config[2]; |
| 56 | + } |
| 57 | + |
| 58 | + // Initialize Network |
| 59 | + JDialog initDialog = new JDialog(null, "Busy", java.awt.Dialog.ModalityType.MODELESS); |
| 60 | + JLabel indicator = new JLabel("Initializing..."); |
| 61 | + initDialog.add(indicator); |
| 62 | + initDialog.setSize(400, 150); // For more words |
| 63 | + final PlayerBoard[] pb = new PlayerBoard[1]; |
| 64 | + final Protocol[] pr = new Protocol[1]; |
| 65 | + final Connectable[] c = new Connectable[1]; |
| 66 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 67 | + Future<Boolean> res = executor.submit(() -> { |
| 68 | + try { |
| 69 | + if (m == Protocol.Mode.SERVER) { |
| 70 | + c[0] = new Server(port); |
| 71 | + indicator.setText("Waiting for Client..."); |
| 72 | + } else { |
| 73 | + c[0] = new Client(address, port); |
| 74 | + indicator.setText("Try Connecting to Server..."); |
| 75 | + } |
| 76 | + c[0].connect(); |
| 77 | + } catch (Exception e) { |
| 78 | + Dialog.genericWarningDialog(null, e); |
| 79 | + e.printStackTrace(); |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + if (m == Protocol.Mode.SERVER) { |
| 84 | + indicator.setText("Client Connected! Initializing settings..."); |
| 85 | + int row = UserSetting.getGridRow(); |
| 86 | + int col = UserSetting.getGridCol(); |
| 87 | + |
| 88 | + List<StandbyShip> ships = new ArrayList<>(); |
| 89 | + ships.add(new StandbyShip(5)); // Carrier |
| 90 | + ships.add(new StandbyShip(4)); // Battleship |
| 91 | + ships.add(new StandbyShip(3)); // Destroyer |
| 92 | + ships.add(new StandbyShip(3)); // Submarine |
| 93 | + ships.add(new StandbyShip(2)); // Patrol Boat |
| 94 | + |
| 95 | + pb[0] = new PlayerBoard(row, col, UserSetting.getTimeLimit(), ships); |
| 96 | + pb[0].setSelfName(name); |
| 97 | + pr[0] = new Protocol(pb[0]); |
| 98 | + |
| 99 | + indicator.setText("Syncing basic settings with Client..."); |
| 100 | + awaitSync(c, pr); |
| 101 | + |
| 102 | + // we need to have opposite's name |
| 103 | + c[0].send("%s#%s".formatted(Protocol.GameCommand.WHAT, Protocol.GameCommand.NAME)); |
| 104 | + pr[0].process(c[0].receive()); |
| 105 | + c[0].send(Protocol.GameCommand.NEXT.toString()); |
| 106 | + } else { |
| 107 | + // Acquire information |
| 108 | + indicator.setText("Server Connected! Initializing Game setting"); |
| 109 | + pb[0] = new PlayerBoard(); |
| 110 | + pr[0] = new Protocol(pb[0]); |
| 111 | + // Set basic info |
| 112 | + pb[0].setSelfName(name); |
| 113 | + |
| 114 | + indicator.setText("Waiting for server echo..."); |
| 115 | + // Wait for Server ready |
| 116 | + do { |
| 117 | + String command = c[0].receive(); |
| 118 | + if (command.equals(Protocol.GameCommand.NEXT.toString())) { |
| 119 | + break; |
| 120 | + } |
| 121 | + } while (true); |
| 122 | + |
| 123 | + indicator.setText("Syncing with Server..."); |
| 124 | + // Ask for data |
| 125 | + pr[0].init(c[0]); |
| 126 | + indicator.setText("Waiting Server..."); |
| 127 | + awaitSync(c, pr); |
| 128 | + } |
| 129 | + indicator.setText("Sync complete! Starting GUI..."); |
| 130 | + initDialog.dispose(); |
| 131 | + return true; |
| 132 | + }); |
| 133 | + |
| 134 | + initDialog.setVisible(true); |
| 135 | + |
| 136 | + try { |
| 137 | + Boolean r = res.get(); |
| 138 | + if (!r) { |
| 139 | + System.exit(1); |
| 140 | + } |
| 141 | + } catch (Exception e) { |
| 142 | + e.printStackTrace(); |
| 143 | + } |
| 144 | + |
| 145 | + System.out.printf("Ready: Enemy Player is %s", pb[0].getEnemyName()); |
| 146 | + |
| 147 | + // Initialize GUI |
| 148 | + // Controller |
| 149 | + GUIController guiController = new GUIController(pb[0], pr[0], c[0]); |
| 150 | + // View |
| 151 | + GUIViewer GUIViewer = new GUIViewer(guiController); |
| 152 | + guiController.setGuiViewer(GUIViewer); |
| 153 | + |
| 154 | + // Message Broker |
| 155 | + executor.submit(() -> { |
| 156 | + do { |
| 157 | + String command = c[0].receive(); |
| 158 | + String response = pr[0].process(command); |
| 159 | + if (response != null) { |
| 160 | + c[0].send(response); |
| 161 | + } |
| 162 | + } while (true); |
| 163 | + }); |
| 164 | + // Launch GUI |
| 165 | + guiController.start(); |
| 166 | + } |
| 167 | + |
| 168 | + private static void awaitSync(Connectable[] c, Protocol[] pr) { |
| 169 | + c[0].send(Protocol.GameCommand.NEXT.toString()); |
| 170 | + do { |
| 171 | + String command = c[0].receive(); |
| 172 | + if (command.equals(Protocol.GameCommand.NEXT.toString())) { |
| 173 | + break; |
| 174 | + } |
| 175 | + String response = pr[0].process(command); |
| 176 | + if (response != null) |
| 177 | + c[0].send(response); |
| 178 | + } while (true); |
| 179 | + } |
| 180 | + |
| 181 | + public static Object[] getClientInfo() { |
| 182 | + String[] config = Dialog.clientInitialDialog(null); |
| 183 | + if (config == null) { |
| 184 | + System.exit(1); |
| 185 | + } |
| 186 | + |
| 187 | + try { |
| 188 | + String address = config[0]; |
| 189 | + int port = Integer.parseInt(config[1]); |
| 190 | + String name = config[2]; |
| 191 | + |
| 192 | + if (name.length() < 1) { |
| 193 | + throw new Exception("Name must have at least 1 characters"); |
| 194 | + } |
| 195 | + |
| 196 | + Object[] res = new Object[3]; |
| 197 | + res[0] = address; |
| 198 | + res[1] = port; |
| 199 | + res[2] = name; |
| 200 | + return res; |
| 201 | + } catch (NumberFormatException e) { |
| 202 | + Dialog.numberParseDialog(null, e); |
| 203 | + return null; |
| 204 | + } catch (Exception e) { |
| 205 | + Dialog.genericWarningDialog(null, e); |
| 206 | + return null; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + public static Object[] getServerInfo() { |
| 211 | + String[] config = Dialog.serverInitialDialog(null); |
| 212 | + if (config == null) { |
| 213 | + System.exit(1); |
| 214 | + } |
| 215 | + |
| 216 | + try { |
| 217 | + int port = Integer.parseInt(config[0]); |
| 218 | + String name = config[1]; |
| 219 | + |
| 220 | + if (name.length() > 0) { |
| 221 | + Object[] res = new Object[2]; |
| 222 | + res[0] = port; |
| 223 | + res[1] = name; |
| 224 | + |
| 225 | + return res; |
| 226 | + } |
| 227 | + |
| 228 | + throw new Exception("Name must be at least 1 character"); |
| 229 | + } catch (NumberFormatException e) { |
| 230 | + Dialog.numberParseDialog(null, e); |
| 231 | + return null; |
| 232 | + } catch (Exception e) { |
| 233 | + Dialog.genericWarningDialog(null, e); |
| 234 | + return null; |
| 235 | + } |
| 236 | + } |
| 237 | +} |
0 commit comments