fix: 修改房间逻辑
This commit is contained in:
@@ -338,6 +338,14 @@ public class GameServiceImpl implements GameService {
|
||||
return newStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始游戏(HTTP 端点保留,实际开始游戏由 WebSocket 处理)
|
||||
*
|
||||
* WS 的 "start" 消息会触发 GomokuGameServiceImpl.startGame(),
|
||||
* 包含完整校验 + GameRecord 创建 + WS BOARD_SYNC + SSE ROOM_UPDATED。
|
||||
*
|
||||
* TODO: 金币结算逻辑预留,后期处理
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public void startGame(String roomId, String ownerId) {
|
||||
@@ -360,43 +368,20 @@ public class GameServiceImpl implements GameService {
|
||||
throw new BusinessException(ErrorCode.PLAYER_COUNT_INSUFFICIENT);
|
||||
}
|
||||
|
||||
// 校验所有玩家已准备(房主除外,房主默认准备)
|
||||
// 校验所有玩家已准备
|
||||
for (RoomPlayerEntity player : players) {
|
||||
if (!player.getReadyStatus() && !player.getUserId().equals(room.getCreatorId())) {
|
||||
if (!player.getReadyStatus()) {
|
||||
throw new BusinessException(ErrorCode.NOT_ALL_READY);
|
||||
}
|
||||
}
|
||||
|
||||
// 扣除房主底注
|
||||
UserEntity ownerUser = userRepository.findByAccountId(ownerId)
|
||||
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
|
||||
if (ownerUser.getBalance() < room.getStakes()) {
|
||||
throw new BusinessException(ErrorCode.BALANCE_INSUFFICIENT);
|
||||
}
|
||||
ownerUser.setBalance(ownerUser.getBalance() - room.getStakes());
|
||||
userRepository.save(ownerUser);
|
||||
|
||||
// 扣除其他玩家底注
|
||||
for (RoomPlayerEntity player : players) {
|
||||
if (!player.getUserId().equals(ownerId)) {
|
||||
UserEntity user = userRepository.findByAccountId(player.getUserId())
|
||||
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
|
||||
if (user.getBalance() < room.getStakes()) {
|
||||
throw new BusinessException(ErrorCode.BALANCE_INSUFFICIENT,
|
||||
"玩家 " + user.getNickName() + " 余额不足");
|
||||
}
|
||||
user.setBalance(user.getBalance() - room.getStakes());
|
||||
userRepository.save(user);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新房间状态为进行中
|
||||
room.setStatus("playing");
|
||||
gameRoomRepository.save(room);
|
||||
|
||||
log.info("[游戏服务] 游戏开始, roomId={}, 参与人数={}, 底注={}", roomId, players.size(), room.getStakes());
|
||||
log.info("[游戏服务] 游戏开始, roomId={}, 参与人数={}", roomId, players.size());
|
||||
|
||||
// 通知房间状态更新
|
||||
// 通知房间状态更新(大厅刷新)
|
||||
List<RoomPlayerResponse> allPlayers = buildRoomPlayers(roomId);
|
||||
GameConfigEntity gameConfig = gameConfigRepository.findByGameId(room.getGameId())
|
||||
.orElse(null);
|
||||
@@ -404,28 +389,6 @@ public class GameServiceImpl implements GameService {
|
||||
String creatorName = getNickname(room.getCreatorId());
|
||||
RoomItemResponse response = RoomItemResponse.from(room, allPlayers, icon, creatorName);
|
||||
sseEventPublisher.broadcast(SseEventType.ROOM_UPDATED, response);
|
||||
|
||||
// 通知房间内每个玩家游戏已开始
|
||||
for (RoomPlayerEntity player : players) {
|
||||
sseEventPublisher.sendTo(player.getUserId(), SseEventType.GAME_STARTED,
|
||||
Map.of("roomId", roomId));
|
||||
}
|
||||
|
||||
// 通知余额变动(房主已在前面扣除了底注)
|
||||
ownerUser = userRepository.findByAccountId(ownerId).orElse(null);
|
||||
if (ownerUser != null) {
|
||||
sseEventPublisher.sendTo(ownerId, SseEventType.BALANCE_CHG,
|
||||
Map.of("balance", ownerUser.getBalance(), "delta", -room.getStakes()));
|
||||
}
|
||||
for (RoomPlayerEntity player : players) {
|
||||
if (!player.getUserId().equals(ownerId)) {
|
||||
UserEntity user = userRepository.findByAccountId(player.getUserId()).orElse(null);
|
||||
if (user != null) {
|
||||
sseEventPublisher.sendTo(player.getUserId(), SseEventType.BALANCE_CHG,
|
||||
Map.of("balance", user.getBalance(), "delta", -room.getStakes()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user