From 2b3b8fa9e63a6e003de21cc811d7cf8f605333d8 Mon Sep 17 00:00:00 2001 From: wzp Date: Thu, 19 Dec 2024 20:14:40 +0800 Subject: [PATCH] fix: fix cannot reset room's bug --- .../mmga/clubs/controller/ChessController.java | 2 ++ .../java/org/mmga/clubs/entities/chess/Room.java | 15 ++++++++++++--- .../entities/chess/packet/RoomInfoPacket.java | 3 ++- .../org/mmga/clubs/entities/user/UserLoginVo.java | 5 ++++- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/mmga/clubs/controller/ChessController.java b/src/main/java/org/mmga/clubs/controller/ChessController.java index 845b3c0..c9aa809 100644 --- a/src/main/java/org/mmga/clubs/controller/ChessController.java +++ b/src/main/java/org/mmga/clubs/controller/ChessController.java @@ -197,6 +197,8 @@ public class ChessController { WebSocketUtils.sendPacketIfPossible(new PlayerWinPacket(), roomByPlayer.getBlackSession()); WebSocketUtils.sendPacketIfPossible(new PlayerLosePacket(), roomByPlayer.getWhiteSession()); } + roomByPlayer.setState(Room.RoomState.FINISHED); + roomByPlayer.broadcast(roomByPlayer.getRoomInfo(users)); }); } if (payload instanceof ResetRoomRequest) { diff --git a/src/main/java/org/mmga/clubs/entities/chess/Room.java b/src/main/java/org/mmga/clubs/entities/chess/Room.java index 8122188..7cb260a 100644 --- a/src/main/java/org/mmga/clubs/entities/chess/Room.java +++ b/src/main/java/org/mmga/clubs/entities/chess/Room.java @@ -64,11 +64,14 @@ public class Room { WebSocketUtils.sendPacket(new PlayerSideAllocationPacket(false), session); WebSocketUtils.sendPacketIfPossible(new PlayerJoinRoomPacket(user, this), whiteSession); this.blackSession = session; - this.setState(RoomState.GAMING); } else { WebSocketUtils.sendPacket(new PlayerSideAllocationPacket(true), session); WebSocketUtils.sendPacketIfPossible(new PlayerJoinRoomPacket(user, this), blackSession); this.whiteSession = session; + } + if (this.isFull()) { + this.setState(RoomState.GAMING); + } else { this.setState(RoomState.WAITING); } return Optional.empty(); @@ -96,7 +99,7 @@ public class Room { Session blackSession = this.getBlackSession(); User whiteUser = whiteSession != null ? users.get(whiteSession.getId()) : null; User blackUser = blackSession != null ? users.get(blackSession.getId()) : null; - return new RoomInfoPacket(this.id, whiteUser, blackUser, this.state, pieces); + return new RoomInfoPacket(this.id, whiteUser, blackUser, this.state, pieces, this.isWhiteAcceptRestart, this.isBlackAcceptRestart, this.canWhiteDown); } public void broadcast(Object packet) { @@ -164,6 +167,11 @@ public class Room { } if (isBlackAcceptRestart && isWhiteAcceptRestart) { this.resetPieces(); + if (whiteSession == null || blackSession == null) { + this.setState(RoomState.WAITING); + return; + } + this.setState(RoomState.GAMING); } } @@ -183,7 +191,8 @@ public class Room { public enum RoomState { CREATED, WAITING, - GAMING + GAMING, + FINISHED } @RequiredArgsConstructor diff --git a/src/main/java/org/mmga/clubs/entities/chess/packet/RoomInfoPacket.java b/src/main/java/org/mmga/clubs/entities/chess/packet/RoomInfoPacket.java index 1fc8099..7fb86ab 100644 --- a/src/main/java/org/mmga/clubs/entities/chess/packet/RoomInfoPacket.java +++ b/src/main/java/org/mmga/clubs/entities/chess/packet/RoomInfoPacket.java @@ -5,5 +5,6 @@ import org.mmga.clubs.entities.user.User; import java.util.UUID; -public record RoomInfoPacket(UUID roomId, User whiteUser, User blackUser, Room.RoomState state, byte[][] pieces) { +public record RoomInfoPacket(UUID roomId, User whiteUser, User blackUser, Room.RoomState state, byte[][] pieces, + boolean whiteRequestRestart, boolean blackRequestRestart, boolean canWhiteDown) { } diff --git a/src/main/java/org/mmga/clubs/entities/user/UserLoginVo.java b/src/main/java/org/mmga/clubs/entities/user/UserLoginVo.java index 69084a2..7431061 100644 --- a/src/main/java/org/mmga/clubs/entities/user/UserLoginVo.java +++ b/src/main/java/org/mmga/clubs/entities/user/UserLoginVo.java @@ -3,5 +3,8 @@ package org.mmga.clubs.entities.user; import io.swagger.v3.oas.annotations.media.Schema; @Schema(description = "用户登录参数类") -public record UserLoginVo(@Schema(description = "用户名", requiredMode = Schema.RequiredMode.REQUIRED) String username, @Schema(description = "用户密码(使用MD5摘要后的hex字符串)") String password/*, @Schema(description = "验证码密钥") String key, @Schema(description = "验证码") String code*/) { +public record UserLoginVo(@Schema(description = "用户名", requiredMode = Schema.RequiredMode.REQUIRED) String username, + @Schema(description = "用户密码(使用MD5摘要后的hex字符串)") String password, + @Schema(description = "验证码密钥") String key, + @Schema(description = "验证码") String code) { }