feat: 重构游戏房间架构

This commit is contained in:
2026-06-30 10:32:29 +08:00
parent ff284e2841
commit c1134f48e0
4 changed files with 17 additions and 62 deletions

View File

@@ -14,6 +14,7 @@ import com.webgame.webgamebackend.service.engine.*;
import com.webgame.webgamebackend.ws.RoomSessionManager;
import com.webgame.webgamebackend.ws.dto.WsMessage;
import com.webgame.webgamebackend.ws.dto.WsOutboundMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@@ -45,6 +46,7 @@ import java.util.stream.Collectors;
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class RoomManager {
private final GameConfigRepository gameConfigRepository;
@@ -84,28 +86,6 @@ public class RoomManager {
RoomRuntime(GameEngine gameEngine) { this.gameEngine = gameEngine; }
}
public RoomManager(GameConfigRepository gameConfigRepository,
GameRoomRepository gameRoomRepository,
RoomPlayerRepository roomPlayerRepository,
UserRepository userRepository,
GameRecordRepository gameRecordRepository,
GameMoveRepository gameMoveRepository,
RoomSessionManager sessionManager,
GameEngineRegistry engineRegistry,
SseEventPublisher sseEventPublisher,
RoomEventBus eventBus) {
this.gameConfigRepository = gameConfigRepository;
this.gameRoomRepository = gameRoomRepository;
this.roomPlayerRepository = roomPlayerRepository;
this.userRepository = userRepository;
this.gameRecordRepository = gameRecordRepository;
this.gameMoveRepository = gameMoveRepository;
this.sessionManager = sessionManager;
this.engineRegistry = engineRegistry;
this.sseEventPublisher = sseEventPublisher;
this.eventBus = eventBus;
}
// ==================== 房间 CRUD ====================
/**

View File

@@ -1,11 +1,18 @@
package com.webgame.webgamebackend.service.engine;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
/**
* 游戏状态快照
*
* 由 GameEngine.getState() 返回,用于重连时同步完整游戏状态给前端。
* 具体游戏引擎可以扩展此类,添加游戏特定的状态字段。
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class GameState {
/** 游戏是否已结束 */
@@ -22,30 +29,4 @@ public class GameState {
/** 游戏特定数据JSON 字符串),由各引擎自行填充 */
private String gameData;
public GameState() {}
public GameState(boolean finished, String winnerId, String resultType,
String currentTurn, String gameData) {
this.finished = finished;
this.winnerId = winnerId;
this.resultType = resultType;
this.currentTurn = currentTurn;
this.gameData = gameData;
}
public boolean isFinished() { return finished; }
public void setFinished(boolean finished) { this.finished = finished; }
public String getWinnerId() { return winnerId; }
public void setWinnerId(String winnerId) { this.winnerId = winnerId; }
public String getResultType() { return resultType; }
public void setResultType(String resultType) { this.resultType = resultType; }
public String getCurrentTurn() { return currentTurn; }
public void setCurrentTurn(String currentTurn) { this.currentTurn = currentTurn; }
public String getGameData() { return gameData; }
public void setGameData(String gameData) { this.gameData = gameData; }
}

View File

@@ -8,6 +8,7 @@ import com.webgame.webgamebackend.entities.GameMoveEntity;
import com.webgame.webgamebackend.repository.GameMoveRepository;
import com.webgame.webgamebackend.ws.dto.WsMessage;
import com.webgame.webgamebackend.ws.dto.WsOutboundMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
@@ -30,6 +31,7 @@ import java.util.concurrent.*;
* <p>不关心房间管理(加入/离开/踢人/房主转移),只关心棋盘上的规则。</p>
*/
@Slf4j
@RequiredArgsConstructor
public class GomokuEngine implements GameEngine {
/** 棋盘大小 */
@@ -55,10 +57,6 @@ public class GomokuEngine implements GameEngine {
/** 断线倒计时: userId → Future */
private final ConcurrentHashMap<String, ScheduledFuture<?>> disconnectTimers = new ConcurrentHashMap<>();
public GomokuEngine(GameMoveRepository moveRepository) {
this.moveRepository = moveRepository;
}
@Override
public void init(RoomContext context) {
this.context = context;

View File

@@ -1,6 +1,7 @@
package com.webgame.webgamebackend.service.engine;
import com.webgame.webgamebackend.ws.dto.WsMessage;
import lombok.Getter;
import java.util.List;
import java.util.function.Consumer;
@@ -11,6 +12,7 @@ import java.util.function.Consumer;
* 游戏引擎初始化时由 RoomManager 传入,包含引擎所需的房间信息和回调接口。
* 引擎不应持有对 RoomManager 的直接引用,所有与房间层的交互通过此上下文完成。
*/
@Getter
public class RoomContext {
/** 房间 ID */
@@ -19,7 +21,7 @@ public class RoomContext {
/** 游戏 ID */
private final String gameId;
/** 参与对局的玩家列表(按座位号排序) */
/** 参与对局的玩家列表(按座位号排序,不可变 */
private final List<PlayerInfo> players;
/** 玩法模式 */
@@ -28,13 +30,13 @@ public class RoomContext {
/** 底注金额 */
private final int stakes;
/** 广播消息到房间内所有 WebSocket 会话 */
/** 广播消息到房间内所有 WebSocket 会话(不暴露到 getter */
private final Consumer<WsMessage> broadcaster;
/** 私有消息:向指定用户发送消息 */
/** 私有消息发送器(不暴露到 getter */
private final PrivateMessageSender privateSender;
/** 游戏结束回调 */
/** 游戏结束回调(不暴露到 getter */
private final GameOverCallback onGameOver;
public RoomContext(String roomId, String gameId, List<PlayerInfo> players,
@@ -52,12 +54,6 @@ public class RoomContext {
this.onGameOver = onGameOver;
}
public String getRoomId() { return roomId; }
public String getGameId() { return gameId; }
public List<PlayerInfo> getPlayers() { return players; }
public String getMode() { return mode; }
public int getStakes() { return stakes; }
/**
* 广播消息到房间内所有人
*/