Loading res/save/save.json 0 → 100644 +1 −0 Changes for res/save/save.json: 1 added line, 0 removed lines. Original line number Diff line number Diff line {"Game":{"chessboard":{"chessList":[{"chessColor":0,"chessType":0,"location":{"plate":2,"x":3,"y":0},"reversal":false,"selected":false,"id":0},{"chessColor":0,"chessType":0,"location":{"plate":2,"x":3,"y":4},"reversal":false,"selected":false,"id":1},{"chessColor":0,"chessType":1,"location":{"plate":2,"x":1,"y":5},"reversal":false,"selected":false,"id":2},{"chessColor":0,"chessType":2,"location":{"plate":2,"x":2,"y":6},"reversal":false,"selected":false,"id":3},{"chessColor":0,"chessType":2,"location":{"plate":2,"x":1,"y":1},"reversal":false,"selected":false,"id":4},{"chessColor":0,"chessType":3,"location":{"plate":2,"x":3,"y":6},"reversal":false,"selected":false,"id":5},{"chessColor":0,"chessType":3,"location":{"plate":2,"x":2,"y":2},"reversal":false,"selected":false,"id":6},{"chessColor":0,"chessType":4,"location":{"plate":2,"x":0,"y":3},"reversal":false,"selected":false,"id":7},{"chessColor":0,"chessType":4,"location":{"plate":2,"x":2,"y":7},"reversal":false,"selected":false,"id":8},{"chessColor":0,"chessType":5,"location":{"plate":2,"x":1,"y":2},"reversal":false,"selected":false,"id":9},{"chessColor":0,"chessType":5,"location":{"plate":2,"x":0,"y":1},"reversal":false,"selected":false,"id":10},{"chessColor":0,"chessType":6,"location":{"plate":2,"x":3,"y":1},"reversal":false,"selected":false,"id":11},{"chessColor":0,"chessType":6,"location":{"plate":2,"x":1,"y":7},"reversal":false,"selected":false,"id":12},{"chessColor":0,"chessType":6,"location":{"plate":2,"x":1,"y":6},"reversal":false,"selected":false,"id":13},{"chessColor":0,"chessType":6,"location":{"plate":2,"x":2,"y":0},"reversal":false,"selected":false,"id":14},{"chessColor":0,"chessType":6,"location":{"plate":2,"x":0,"y":4},"reversal":false,"selected":false,"id":15},{"chessColor":1,"chessType":0,"location":{"plate":2,"x":2,"y":1},"reversal":false,"selected":false,"id":16},{"chessColor":1,"chessType":0,"location":{"plate":2,"x":1,"y":3},"reversal":false,"selected":false,"id":17},{"chessColor":1,"chessType":1,"location":{"plate":2,"x":0,"y":0},"reversal":false,"selected":false,"id":18},{"chessColor":1,"chessType":2,"location":{"plate":2,"x":2,"y":3},"reversal":false,"selected":false,"id":19},{"chessColor":1,"chessType":2,"location":{"plate":2,"x":0,"y":5},"reversal":false,"selected":false,"id":20},{"chessColor":1,"chessType":3,"location":{"plate":2,"x":0,"y":7},"reversal":false,"selected":false,"id":21},{"chessColor":1,"chessType":3,"location":{"plate":2,"x":3,"y":5},"reversal":false,"selected":false,"id":22},{"chessColor":1,"chessType":4,"location":{"plate":2,"x":3,"y":7},"reversal":false,"selected":false,"id":23},{"chessColor":1,"chessType":4,"location":{"plate":2,"x":0,"y":2},"reversal":false,"selected":false,"id":24},{"chessColor":1,"chessType":5,"location":{"plate":2,"x":3,"y":3},"reversal":false,"selected":false,"id":25},{"chessColor":1,"chessType":5,"location":{"plate":2,"x":1,"y":0},"reversal":false,"selected":false,"id":26},{"chessColor":1,"chessType":6,"location":{"plate":2,"x":2,"y":5},"reversal":false,"selected":false,"id":27},{"chessColor":1,"chessType":6,"location":{"plate":2,"x":2,"y":4},"reversal":false,"selected":false,"id":28},{"chessColor":1,"chessType":6,"location":{"plate":2,"x":0,"y":6},"reversal":false,"selected":false,"id":29},{"chessColor":1,"chessType":6,"location":{"plate":2,"x":3,"y":2},"reversal":false,"selected":false,"id":30},{"chessColor":1,"chessType":6,"location":{"plate":2,"x":1,"y":4},"reversal":true,"selected":false,"id":31}],"idGrid":[[18,26,14,0],[10,4,16,11],[24,9,6,30],[7,17,19,25],[15,31,28,1],[20,2,27,22],[29,13,3,5],[21,12,8,23]],"sideStacks":[{"chessColor":0},{"chessColor":1}],"squareList":[{"id":32,"location":{"plate":0,"x":0,"y":0}},{"id":33,"location":{"plate":0,"x":1,"y":0}},{"id":34,"location":{"plate":0,"x":2,"y":0}},{"id":35,"location":{"plate":0,"x":3,"y":0}},{"id":36,"location":{"plate":0,"x":4,"y":0}},{"id":37,"location":{"plate":0,"x":0,"y":1}},{"id":38,"location":{"plate":0,"x":1,"y":1}},{"id":39,"location":{"plate":0,"x":2,"y":1}},{"id":40,"location":{"plate":0,"x":3,"y":1}},{"id":41,"location":{"plate":0,"x":4,"y":1}},{"id":42,"location":{"plate":0,"x":0,"y":2}},{"id":43,"location":{"plate":0,"x":1,"y":2}},{"id":44,"location":{"plate":0,"x":2,"y":2}},{"id":45,"location":{"plate":0,"x":3,"y":2}},{"id":46,"location":{"plate":0,"x":4,"y":2}},{"id":47,"location":{"plate":0,"x":0,"y":3}},{"id":48,"location":{"plate":0,"x":1,"y":3}},{"id":49,"location":{"plate":0,"x":2,"y":3}},{"id":50,"location":{"plate":0,"x":3,"y":3}},{"id":51,"location":{"plate":0,"x":4,"y":3}},{"id":52,"location":{"plate":0,"x":0,"y":4}},{"id":53,"location":{"plate":0,"x":1,"y":4}},{"id":54,"location":{"plate":0,"x":2,"y":4}},{"id":55,"location":{"plate":0,"x":3,"y":4}},{"id":56,"location":{"plate":0,"x":4,"y":4}},{"id":57,"location":{"plate":0,"x":0,"y":5}},{"id":58,"location":{"plate":0,"x":1,"y":5}},{"id":59,"location":{"plate":0,"x":2,"y":5}},{"id":60,"location":{"plate":0,"x":3,"y":5}},{"id":61,"location":{"plate":0,"x":4,"y":5}},{"id":62,"location":{"plate":0,"x":0,"y":6}},{"id":63,"location":{"plate":0,"x":1,"y":6}},{"id":64,"location":{"plate":0,"x":2,"y":6}},{"id":65,"location":{"plate":0,"x":3,"y":6}},{"id":66,"location":{"plate":0,"x":4,"y":6}},{"id":67,"location":{"plate":1,"x":0,"y":0}},{"id":68,"location":{"plate":1,"x":1,"y":0}},{"id":69,"location":{"plate":1,"x":2,"y":0}},{"id":70,"location":{"plate":1,"x":3,"y":0}},{"id":71,"location":{"plate":1,"x":4,"y":0}},{"id":72,"location":{"plate":1,"x":0,"y":1}},{"id":73,"location":{"plate":1,"x":1,"y":1}},{"id":74,"location":{"plate":1,"x":2,"y":1}},{"id":75,"location":{"plate":1,"x":3,"y":1}},{"id":76,"location":{"plate":1,"x":4,"y":1}},{"id":77,"location":{"plate":1,"x":0,"y":2}},{"id":78,"location":{"plate":1,"x":1,"y":2}},{"id":79,"location":{"plate":1,"x":2,"y":2}},{"id":80,"location":{"plate":1,"x":3,"y":2}},{"id":81,"location":{"plate":1,"x":4,"y":2}},{"id":82,"location":{"plate":1,"x":0,"y":3}},{"id":83,"location":{"plate":1,"x":1,"y":3}},{"id":84,"location":{"plate":1,"x":2,"y":3}},{"id":85,"location":{"plate":1,"x":3,"y":3}},{"id":86,"location":{"plate":1,"x":4,"y":3}},{"id":87,"location":{"plate":1,"x":0,"y":4}},{"id":88,"location":{"plate":1,"x":1,"y":4}},{"id":89,"location":{"plate":1,"x":2,"y":4}},{"id":90,"location":{"plate":1,"x":3,"y":4}},{"id":91,"location":{"plate":1,"x":4,"y":4}},{"id":92,"location":{"plate":1,"x":0,"y":5}},{"id":93,"location":{"plate":1,"x":1,"y":5}},{"id":94,"location":{"plate":1,"x":2,"y":5}},{"id":95,"location":{"plate":1,"x":3,"y":5}},{"id":96,"location":{"plate":1,"x":4,"y":5}},{"id":97,"location":{"plate":1,"x":0,"y":6}},{"id":98,"location":{"plate":1,"x":1,"y":6}},{"id":99,"location":{"plate":1,"x":2,"y":6}},{"id":100,"location":{"plate":1,"x":3,"y":6}},{"id":101,"location":{"plate":1,"x":4,"y":6}}]},"currentColor":0,"gameType":0,"hostColor":0,"msgList":[],"scoreboard":[0,0]},"History":{"steps":[{"srcChessId":31}]}} No newline at end of file src/controller/AppController.java +99 −1 Changes for src/controller/AppController.java: 99 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -4,8 +4,12 @@ import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONException; import com.alibaba.fastjson2.JSONObject; import model.History; import model.Room; import model.dataType.GameType; import model.game.Game; import network.Client; import network.Server; import network.dataType.OnlineStatus; import view.Frame.AppFrame; import view.Frame.ChessGameFrame; import view.RoomFrame; Loading @@ -13,7 +17,9 @@ import view.RoomFrame; import javax.swing.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.IOException; import java.net.UnknownHostException; import java.nio.file.Files; import java.nio.file.Path; Loading @@ -30,7 +36,7 @@ public class AppController { private Game savedGame; public static final String savePath = "/res/save/save.json"; public static final String savePath = "res/save/save.json"; public AppController() { try { Loading Loading @@ -166,6 +172,94 @@ public class AppController { this.switchStatus(status1, false); } public void goRooms() { SwingUtilities.invokeLater(() -> { if (status == AppStatus.START) AppFrame.instance.dispose(); else if (status == AppStatus.GAME) ChessGameFrame.instance.dispose(); else return; RoomFrame.instance = new RoomFrame(); RoomFrame.instance.setVisible(true); status = AppStatus.ROOM; }); } public void goOfflineGame(boolean useSave, GameType type) { if (status == AppStatus.START) { SwingUtilities.invokeLater(() -> { AppFrame.instance.dispose(); ChessGameFrame.instance = new ChessGameFrame(); ChessGameFrame.instance.setVisible(true); }); if (useSave) { Game.instance = savedGame; History.instance = savedHistory; } else { Game.instance = new Game(type); History.instance = new History(Game.instance.clone()); } status = AppStatus.GAME; } } public void goOnlineGame(OnlineStatus onlineMode, Room room) { if (status == AppStatus.ROOM) { Game.instance = new Game(GameType.ONLINE); History.instance = new History(Game.instance.clone()); if (onlineMode == OnlineStatus.SERVER_WAITING) { try { Server.start(); } catch (UnknownHostException e) { JOptionPane.showMessageDialog(null, "房间创建失败", "提示", JOptionPane.WARNING_MESSAGE); return; } } else { boolean isPlayer = onlineMode == OnlineStatus.CLIENT_PLAYING; RoomFrame.instance.dispose(); Client.connect(room.ip, isPlayer ? room.playerPort : room.watcherPort, isPlayer, () -> { SwingUtilities.invokeLater(() -> { ChessGameFrame.instance = new ChessGameFrame(); ChessGameFrame.instance.setVisible(true); }); Game.instance = new Game(GameType.ONLINE); History.instance = new History(Game.instance.clone()); status = AppStatus.GAME; }, () -> { JOptionPane.showMessageDialog(null, "连接失败", "提示", JOptionPane.WARNING_MESSAGE); goRooms(); }, () -> { JOptionPane.showMessageDialog(null, "连接已断开", "提示", JOptionPane.WARNING_MESSAGE); goRooms(); }); } } } public void backToStart() { if (status == AppStatus.ROOM) RoomFrame.instance.dispose(); else if (status == AppStatus.GAME) ChessGameFrame.instance.dispose(); if(Game.instance != null && Game.instance.gameType != GameType.ONLINE){ try { write(savePath); } catch (IOException e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "阿哦,保存时发生错误了:" + e.getMessage(), "提示", JOptionPane.WARNING_MESSAGE); } } else return; SwingUtilities.invokeLater(() -> { AppFrame.instance = new AppFrame(); AppFrame.instance.setVisible(true); }); status = AppStatus.START; } public void read(String path) throws IOException, JSONException { JSONObject jsonObject = JSONObject.parse(Files.readString(Path.of(path))); savedGame = jsonObject.getObject("Game", Game.class); Loading @@ -176,6 +270,10 @@ public class AppController { JSONObject jsonObject = new JSONObject(); jsonObject.put("Game", Game.instance); jsonObject.put("History", History.instance); File save = new File(path); if(!save.exists()){ save.createNewFile(); } Files.writeString(Path.of(path), JSON.toJSONString(jsonObject)); } Loading src/network/Client.java +3 −3 Changes for src/network/Client.java: 3 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -43,15 +43,15 @@ public class Client { /** * 连接(断线重连),是阻塞方法,成功会把对象赋给instance * * @param host 地址 * @param hostIP 地址 * @param port 端口 */ public static void connect(String host, int port, boolean isPlayer, public static void connect(String hostIP, int port, boolean isPlayer, Runnable onSuccess, Runnable onFail, Runnable onClose) { if (instance != null) disconnect(); new Thread(() -> { try { instance = new Client(host, port, isPlayer, onSuccess, onClose); instance = new Client(hostIP, port, isPlayer, onSuccess, onClose); } catch (IOException e) { onFail.run(); disconnect(); Loading src/network/Responser.java +7 −2 Changes for src/network/Responser.java: 7 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONException; import com.alibaba.fastjson2.JSONObject; import model.Account; import network.dataType.OnlineStatus; import java.io.IOException; import java.net.DatagramPacket; Loading Loading @@ -63,13 +64,15 @@ public class Responser { * "playerPort": port, * "watcherPort": port, * "host": Account Object, * "isPlaying": boolean, * "watcherNum": int, * } * </pre> */ public void recvCallback(DatagramPacket packet) { try { JSONObject recvData = JSON.parseObject(new String(packet.getData(), 0, packet.getLength())); String ip = packet.getAddress().getHostAddress(); String srcIP = packet.getAddress().getHostAddress(); if(!recvData.get("type").equals("Searching")) return; String id = recvData.getString("id"); int port = recvData.getIntValue("replyTo"); Loading @@ -81,10 +84,12 @@ public class Responser { responseData.put("playerPort", Server.playerPort); responseData.put("watcherPort", Server.watcherPort); responseData.put("host", Account.SELF); responseData.put("isPlaying", OnlineInfo.status == OnlineStatus.SERVER_PLAYING); responseData.put("watcherNum", OnlineInfo.watcherAccounts.size()); String dataString = responseData.toString(); try { DatagramPacket responsePack = new DatagramPacket( dataString.getBytes(), 0, dataString.length(), InetAddress.getByName(ip), port); dataString.getBytes(), 0, dataString.length(), InetAddress.getByName(srcIP), port); DatagramSocket responseSocket = new DatagramSocket(RESPONSE_PORT); responseSocket.send(responsePack); } catch (IOException ignored) {} Loading src/network/Searcher.java +5 −1 Changes for src/network/Searcher.java: 5 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -88,6 +88,8 @@ public class Searcher { * "playerPort": port, * "watcherPort": port, * "host": Account Object, * "isPlaying": boolean, * "watcherNum": int, * } * </pre> */ Loading @@ -102,8 +104,10 @@ public class Searcher { int playerPort = recvData.getIntValue("playerPort"); int watcherPort = recvData.getIntValue("watcherPort"); Account host = recvData.getObject("host", Account.class); boolean isPlaying = recvData.getBoolean("isPlaying"); int watcherNum = recvData.getIntValue("watcherNum"); RoomFrame.instance.addRoom(new Room(ip, playerPort, watcherPort, host, false, 666)); } catch (JSONException ignored){} } catch (JSONException e){e.printStackTrace();} } public static void stop() { Loading Loading
res/save/save.json 0 → 100644 +1 −0 Changes for res/save/save.json: 1 added line, 0 removed lines. Original line number Diff line number Diff line {"Game":{"chessboard":{"chessList":[{"chessColor":0,"chessType":0,"location":{"plate":2,"x":3,"y":0},"reversal":false,"selected":false,"id":0},{"chessColor":0,"chessType":0,"location":{"plate":2,"x":3,"y":4},"reversal":false,"selected":false,"id":1},{"chessColor":0,"chessType":1,"location":{"plate":2,"x":1,"y":5},"reversal":false,"selected":false,"id":2},{"chessColor":0,"chessType":2,"location":{"plate":2,"x":2,"y":6},"reversal":false,"selected":false,"id":3},{"chessColor":0,"chessType":2,"location":{"plate":2,"x":1,"y":1},"reversal":false,"selected":false,"id":4},{"chessColor":0,"chessType":3,"location":{"plate":2,"x":3,"y":6},"reversal":false,"selected":false,"id":5},{"chessColor":0,"chessType":3,"location":{"plate":2,"x":2,"y":2},"reversal":false,"selected":false,"id":6},{"chessColor":0,"chessType":4,"location":{"plate":2,"x":0,"y":3},"reversal":false,"selected":false,"id":7},{"chessColor":0,"chessType":4,"location":{"plate":2,"x":2,"y":7},"reversal":false,"selected":false,"id":8},{"chessColor":0,"chessType":5,"location":{"plate":2,"x":1,"y":2},"reversal":false,"selected":false,"id":9},{"chessColor":0,"chessType":5,"location":{"plate":2,"x":0,"y":1},"reversal":false,"selected":false,"id":10},{"chessColor":0,"chessType":6,"location":{"plate":2,"x":3,"y":1},"reversal":false,"selected":false,"id":11},{"chessColor":0,"chessType":6,"location":{"plate":2,"x":1,"y":7},"reversal":false,"selected":false,"id":12},{"chessColor":0,"chessType":6,"location":{"plate":2,"x":1,"y":6},"reversal":false,"selected":false,"id":13},{"chessColor":0,"chessType":6,"location":{"plate":2,"x":2,"y":0},"reversal":false,"selected":false,"id":14},{"chessColor":0,"chessType":6,"location":{"plate":2,"x":0,"y":4},"reversal":false,"selected":false,"id":15},{"chessColor":1,"chessType":0,"location":{"plate":2,"x":2,"y":1},"reversal":false,"selected":false,"id":16},{"chessColor":1,"chessType":0,"location":{"plate":2,"x":1,"y":3},"reversal":false,"selected":false,"id":17},{"chessColor":1,"chessType":1,"location":{"plate":2,"x":0,"y":0},"reversal":false,"selected":false,"id":18},{"chessColor":1,"chessType":2,"location":{"plate":2,"x":2,"y":3},"reversal":false,"selected":false,"id":19},{"chessColor":1,"chessType":2,"location":{"plate":2,"x":0,"y":5},"reversal":false,"selected":false,"id":20},{"chessColor":1,"chessType":3,"location":{"plate":2,"x":0,"y":7},"reversal":false,"selected":false,"id":21},{"chessColor":1,"chessType":3,"location":{"plate":2,"x":3,"y":5},"reversal":false,"selected":false,"id":22},{"chessColor":1,"chessType":4,"location":{"plate":2,"x":3,"y":7},"reversal":false,"selected":false,"id":23},{"chessColor":1,"chessType":4,"location":{"plate":2,"x":0,"y":2},"reversal":false,"selected":false,"id":24},{"chessColor":1,"chessType":5,"location":{"plate":2,"x":3,"y":3},"reversal":false,"selected":false,"id":25},{"chessColor":1,"chessType":5,"location":{"plate":2,"x":1,"y":0},"reversal":false,"selected":false,"id":26},{"chessColor":1,"chessType":6,"location":{"plate":2,"x":2,"y":5},"reversal":false,"selected":false,"id":27},{"chessColor":1,"chessType":6,"location":{"plate":2,"x":2,"y":4},"reversal":false,"selected":false,"id":28},{"chessColor":1,"chessType":6,"location":{"plate":2,"x":0,"y":6},"reversal":false,"selected":false,"id":29},{"chessColor":1,"chessType":6,"location":{"plate":2,"x":3,"y":2},"reversal":false,"selected":false,"id":30},{"chessColor":1,"chessType":6,"location":{"plate":2,"x":1,"y":4},"reversal":true,"selected":false,"id":31}],"idGrid":[[18,26,14,0],[10,4,16,11],[24,9,6,30],[7,17,19,25],[15,31,28,1],[20,2,27,22],[29,13,3,5],[21,12,8,23]],"sideStacks":[{"chessColor":0},{"chessColor":1}],"squareList":[{"id":32,"location":{"plate":0,"x":0,"y":0}},{"id":33,"location":{"plate":0,"x":1,"y":0}},{"id":34,"location":{"plate":0,"x":2,"y":0}},{"id":35,"location":{"plate":0,"x":3,"y":0}},{"id":36,"location":{"plate":0,"x":4,"y":0}},{"id":37,"location":{"plate":0,"x":0,"y":1}},{"id":38,"location":{"plate":0,"x":1,"y":1}},{"id":39,"location":{"plate":0,"x":2,"y":1}},{"id":40,"location":{"plate":0,"x":3,"y":1}},{"id":41,"location":{"plate":0,"x":4,"y":1}},{"id":42,"location":{"plate":0,"x":0,"y":2}},{"id":43,"location":{"plate":0,"x":1,"y":2}},{"id":44,"location":{"plate":0,"x":2,"y":2}},{"id":45,"location":{"plate":0,"x":3,"y":2}},{"id":46,"location":{"plate":0,"x":4,"y":2}},{"id":47,"location":{"plate":0,"x":0,"y":3}},{"id":48,"location":{"plate":0,"x":1,"y":3}},{"id":49,"location":{"plate":0,"x":2,"y":3}},{"id":50,"location":{"plate":0,"x":3,"y":3}},{"id":51,"location":{"plate":0,"x":4,"y":3}},{"id":52,"location":{"plate":0,"x":0,"y":4}},{"id":53,"location":{"plate":0,"x":1,"y":4}},{"id":54,"location":{"plate":0,"x":2,"y":4}},{"id":55,"location":{"plate":0,"x":3,"y":4}},{"id":56,"location":{"plate":0,"x":4,"y":4}},{"id":57,"location":{"plate":0,"x":0,"y":5}},{"id":58,"location":{"plate":0,"x":1,"y":5}},{"id":59,"location":{"plate":0,"x":2,"y":5}},{"id":60,"location":{"plate":0,"x":3,"y":5}},{"id":61,"location":{"plate":0,"x":4,"y":5}},{"id":62,"location":{"plate":0,"x":0,"y":6}},{"id":63,"location":{"plate":0,"x":1,"y":6}},{"id":64,"location":{"plate":0,"x":2,"y":6}},{"id":65,"location":{"plate":0,"x":3,"y":6}},{"id":66,"location":{"plate":0,"x":4,"y":6}},{"id":67,"location":{"plate":1,"x":0,"y":0}},{"id":68,"location":{"plate":1,"x":1,"y":0}},{"id":69,"location":{"plate":1,"x":2,"y":0}},{"id":70,"location":{"plate":1,"x":3,"y":0}},{"id":71,"location":{"plate":1,"x":4,"y":0}},{"id":72,"location":{"plate":1,"x":0,"y":1}},{"id":73,"location":{"plate":1,"x":1,"y":1}},{"id":74,"location":{"plate":1,"x":2,"y":1}},{"id":75,"location":{"plate":1,"x":3,"y":1}},{"id":76,"location":{"plate":1,"x":4,"y":1}},{"id":77,"location":{"plate":1,"x":0,"y":2}},{"id":78,"location":{"plate":1,"x":1,"y":2}},{"id":79,"location":{"plate":1,"x":2,"y":2}},{"id":80,"location":{"plate":1,"x":3,"y":2}},{"id":81,"location":{"plate":1,"x":4,"y":2}},{"id":82,"location":{"plate":1,"x":0,"y":3}},{"id":83,"location":{"plate":1,"x":1,"y":3}},{"id":84,"location":{"plate":1,"x":2,"y":3}},{"id":85,"location":{"plate":1,"x":3,"y":3}},{"id":86,"location":{"plate":1,"x":4,"y":3}},{"id":87,"location":{"plate":1,"x":0,"y":4}},{"id":88,"location":{"plate":1,"x":1,"y":4}},{"id":89,"location":{"plate":1,"x":2,"y":4}},{"id":90,"location":{"plate":1,"x":3,"y":4}},{"id":91,"location":{"plate":1,"x":4,"y":4}},{"id":92,"location":{"plate":1,"x":0,"y":5}},{"id":93,"location":{"plate":1,"x":1,"y":5}},{"id":94,"location":{"plate":1,"x":2,"y":5}},{"id":95,"location":{"plate":1,"x":3,"y":5}},{"id":96,"location":{"plate":1,"x":4,"y":5}},{"id":97,"location":{"plate":1,"x":0,"y":6}},{"id":98,"location":{"plate":1,"x":1,"y":6}},{"id":99,"location":{"plate":1,"x":2,"y":6}},{"id":100,"location":{"plate":1,"x":3,"y":6}},{"id":101,"location":{"plate":1,"x":4,"y":6}}]},"currentColor":0,"gameType":0,"hostColor":0,"msgList":[],"scoreboard":[0,0]},"History":{"steps":[{"srcChessId":31}]}} No newline at end of file
src/controller/AppController.java +99 −1 Changes for src/controller/AppController.java: 99 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -4,8 +4,12 @@ import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONException; import com.alibaba.fastjson2.JSONObject; import model.History; import model.Room; import model.dataType.GameType; import model.game.Game; import network.Client; import network.Server; import network.dataType.OnlineStatus; import view.Frame.AppFrame; import view.Frame.ChessGameFrame; import view.RoomFrame; Loading @@ -13,7 +17,9 @@ import view.RoomFrame; import javax.swing.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.IOException; import java.net.UnknownHostException; import java.nio.file.Files; import java.nio.file.Path; Loading @@ -30,7 +36,7 @@ public class AppController { private Game savedGame; public static final String savePath = "/res/save/save.json"; public static final String savePath = "res/save/save.json"; public AppController() { try { Loading Loading @@ -166,6 +172,94 @@ public class AppController { this.switchStatus(status1, false); } public void goRooms() { SwingUtilities.invokeLater(() -> { if (status == AppStatus.START) AppFrame.instance.dispose(); else if (status == AppStatus.GAME) ChessGameFrame.instance.dispose(); else return; RoomFrame.instance = new RoomFrame(); RoomFrame.instance.setVisible(true); status = AppStatus.ROOM; }); } public void goOfflineGame(boolean useSave, GameType type) { if (status == AppStatus.START) { SwingUtilities.invokeLater(() -> { AppFrame.instance.dispose(); ChessGameFrame.instance = new ChessGameFrame(); ChessGameFrame.instance.setVisible(true); }); if (useSave) { Game.instance = savedGame; History.instance = savedHistory; } else { Game.instance = new Game(type); History.instance = new History(Game.instance.clone()); } status = AppStatus.GAME; } } public void goOnlineGame(OnlineStatus onlineMode, Room room) { if (status == AppStatus.ROOM) { Game.instance = new Game(GameType.ONLINE); History.instance = new History(Game.instance.clone()); if (onlineMode == OnlineStatus.SERVER_WAITING) { try { Server.start(); } catch (UnknownHostException e) { JOptionPane.showMessageDialog(null, "房间创建失败", "提示", JOptionPane.WARNING_MESSAGE); return; } } else { boolean isPlayer = onlineMode == OnlineStatus.CLIENT_PLAYING; RoomFrame.instance.dispose(); Client.connect(room.ip, isPlayer ? room.playerPort : room.watcherPort, isPlayer, () -> { SwingUtilities.invokeLater(() -> { ChessGameFrame.instance = new ChessGameFrame(); ChessGameFrame.instance.setVisible(true); }); Game.instance = new Game(GameType.ONLINE); History.instance = new History(Game.instance.clone()); status = AppStatus.GAME; }, () -> { JOptionPane.showMessageDialog(null, "连接失败", "提示", JOptionPane.WARNING_MESSAGE); goRooms(); }, () -> { JOptionPane.showMessageDialog(null, "连接已断开", "提示", JOptionPane.WARNING_MESSAGE); goRooms(); }); } } } public void backToStart() { if (status == AppStatus.ROOM) RoomFrame.instance.dispose(); else if (status == AppStatus.GAME) ChessGameFrame.instance.dispose(); if(Game.instance != null && Game.instance.gameType != GameType.ONLINE){ try { write(savePath); } catch (IOException e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "阿哦,保存时发生错误了:" + e.getMessage(), "提示", JOptionPane.WARNING_MESSAGE); } } else return; SwingUtilities.invokeLater(() -> { AppFrame.instance = new AppFrame(); AppFrame.instance.setVisible(true); }); status = AppStatus.START; } public void read(String path) throws IOException, JSONException { JSONObject jsonObject = JSONObject.parse(Files.readString(Path.of(path))); savedGame = jsonObject.getObject("Game", Game.class); Loading @@ -176,6 +270,10 @@ public class AppController { JSONObject jsonObject = new JSONObject(); jsonObject.put("Game", Game.instance); jsonObject.put("History", History.instance); File save = new File(path); if(!save.exists()){ save.createNewFile(); } Files.writeString(Path.of(path), JSON.toJSONString(jsonObject)); } Loading
src/network/Client.java +3 −3 Changes for src/network/Client.java: 3 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -43,15 +43,15 @@ public class Client { /** * 连接(断线重连),是阻塞方法,成功会把对象赋给instance * * @param host 地址 * @param hostIP 地址 * @param port 端口 */ public static void connect(String host, int port, boolean isPlayer, public static void connect(String hostIP, int port, boolean isPlayer, Runnable onSuccess, Runnable onFail, Runnable onClose) { if (instance != null) disconnect(); new Thread(() -> { try { instance = new Client(host, port, isPlayer, onSuccess, onClose); instance = new Client(hostIP, port, isPlayer, onSuccess, onClose); } catch (IOException e) { onFail.run(); disconnect(); Loading
src/network/Responser.java +7 −2 Changes for src/network/Responser.java: 7 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONException; import com.alibaba.fastjson2.JSONObject; import model.Account; import network.dataType.OnlineStatus; import java.io.IOException; import java.net.DatagramPacket; Loading Loading @@ -63,13 +64,15 @@ public class Responser { * "playerPort": port, * "watcherPort": port, * "host": Account Object, * "isPlaying": boolean, * "watcherNum": int, * } * </pre> */ public void recvCallback(DatagramPacket packet) { try { JSONObject recvData = JSON.parseObject(new String(packet.getData(), 0, packet.getLength())); String ip = packet.getAddress().getHostAddress(); String srcIP = packet.getAddress().getHostAddress(); if(!recvData.get("type").equals("Searching")) return; String id = recvData.getString("id"); int port = recvData.getIntValue("replyTo"); Loading @@ -81,10 +84,12 @@ public class Responser { responseData.put("playerPort", Server.playerPort); responseData.put("watcherPort", Server.watcherPort); responseData.put("host", Account.SELF); responseData.put("isPlaying", OnlineInfo.status == OnlineStatus.SERVER_PLAYING); responseData.put("watcherNum", OnlineInfo.watcherAccounts.size()); String dataString = responseData.toString(); try { DatagramPacket responsePack = new DatagramPacket( dataString.getBytes(), 0, dataString.length(), InetAddress.getByName(ip), port); dataString.getBytes(), 0, dataString.length(), InetAddress.getByName(srcIP), port); DatagramSocket responseSocket = new DatagramSocket(RESPONSE_PORT); responseSocket.send(responsePack); } catch (IOException ignored) {} Loading
src/network/Searcher.java +5 −1 Changes for src/network/Searcher.java: 5 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -88,6 +88,8 @@ public class Searcher { * "playerPort": port, * "watcherPort": port, * "host": Account Object, * "isPlaying": boolean, * "watcherNum": int, * } * </pre> */ Loading @@ -102,8 +104,10 @@ public class Searcher { int playerPort = recvData.getIntValue("playerPort"); int watcherPort = recvData.getIntValue("watcherPort"); Account host = recvData.getObject("host", Account.class); boolean isPlaying = recvData.getBoolean("isPlaying"); int watcherNum = recvData.getIntValue("watcherNum"); RoomFrame.instance.addRoom(new Room(ip, playerPort, watcherPort, host, false, 666)); } catch (JSONException ignored){} } catch (JSONException e){e.printStackTrace();} } public static void stop() { Loading