From 2e50259dd6bd7968c5a9b54a1775aba318718dc1 Mon Sep 17 00:00:00 2001 From: yhpark95 <98851575+yhpark95@users.noreply.github.com> Date: Tue, 7 Nov 2023 15:30:35 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EC=A2=85?= =?UTF-8?q?=EB=A3=8C=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20#78?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../game/controller/GameController.java | 5 +++++ .../game/dto/response/PlayerAsset.java | 16 ++++++++++++++++ .../game/dto/response/UserRankingResponse.java | 16 ++++++++++++++++ .../gaemimarble/game/entity/GameStatus.java | 4 ++++ .../gaemimarble/game/entity/TypeConstants.java | 1 + .../gaemimarble/game/service/GameService.java | 18 ++++++++++++++++++ 6 files changed, 60 insertions(+) create mode 100644 be/src/main/java/codesquad/gaemimarble/game/dto/response/PlayerAsset.java create mode 100644 be/src/main/java/codesquad/gaemimarble/game/dto/response/UserRankingResponse.java diff --git a/be/src/main/java/codesquad/gaemimarble/game/controller/GameController.java b/be/src/main/java/codesquad/gaemimarble/game/controller/GameController.java index eb6a430..0141e1c 100644 --- a/be/src/main/java/codesquad/gaemimarble/game/controller/GameController.java +++ b/be/src/main/java/codesquad/gaemimarble/game/controller/GameController.java @@ -99,6 +99,11 @@ private void sendEventResult(GameEventResultRequest gameEventResultRequest) { gameEventNameResponse)); socketDataSender.send(gameEventResultRequest.getGameId(), new ResponseDTO<>(TypeConstants.STATUS_BOARD, gameService.proceedEvent(gameEventNameResponse.getName(), gameEventResultRequest.getGameId()))); + if (gameService.checkGameOver(gameEventResultRequest.getGameId())) { + socketDataSender.send( + gameEventResultRequest.getGameId(), new ResponseDTO<>(TypeConstants.GAME_OVER, + gameService.createUserRanking(gameEventResultRequest.getGameId()))); + } } @PostMapping("/api/games") diff --git a/be/src/main/java/codesquad/gaemimarble/game/dto/response/PlayerAsset.java b/be/src/main/java/codesquad/gaemimarble/game/dto/response/PlayerAsset.java new file mode 100644 index 0000000..772cfe8 --- /dev/null +++ b/be/src/main/java/codesquad/gaemimarble/game/dto/response/PlayerAsset.java @@ -0,0 +1,16 @@ +package codesquad.gaemimarble.game.dto.response; + +import lombok.Builder; +import lombok.Getter; + +@Getter +public class PlayerAsset { + private final String playerId; + private final Integer totalAsset; + + @Builder + private PlayerAsset(String playerId, Integer totalAsset) { + this.playerId = playerId; + this.totalAsset = totalAsset; + } +} diff --git a/be/src/main/java/codesquad/gaemimarble/game/dto/response/UserRankingResponse.java b/be/src/main/java/codesquad/gaemimarble/game/dto/response/UserRankingResponse.java new file mode 100644 index 0000000..fc5a725 --- /dev/null +++ b/be/src/main/java/codesquad/gaemimarble/game/dto/response/UserRankingResponse.java @@ -0,0 +1,16 @@ +package codesquad.gaemimarble.game.dto.response; + +import java.util.List; + +import lombok.Builder; +import lombok.Getter; + +@Getter +public class UserRankingResponse { + List ranking; + + @Builder + private UserRankingResponse(List ranking) { + this.ranking = ranking; + } +} diff --git a/be/src/main/java/codesquad/gaemimarble/game/entity/GameStatus.java b/be/src/main/java/codesquad/gaemimarble/game/entity/GameStatus.java index 3851260..7ab4a37 100644 --- a/be/src/main/java/codesquad/gaemimarble/game/entity/GameStatus.java +++ b/be/src/main/java/codesquad/gaemimarble/game/entity/GameStatus.java @@ -47,4 +47,8 @@ public Player getPlayer(String playerId) { .findFirst() .orElseThrow(() -> new IllegalArgumentException("해당하는 플레이어가 없습니다.")); } + + public void incrementRoundCount() { + roundCount++; + } } diff --git a/be/src/main/java/codesquad/gaemimarble/game/entity/TypeConstants.java b/be/src/main/java/codesquad/gaemimarble/game/entity/TypeConstants.java index 53101aa..dae9aa0 100644 --- a/be/src/main/java/codesquad/gaemimarble/game/entity/TypeConstants.java +++ b/be/src/main/java/codesquad/gaemimarble/game/entity/TypeConstants.java @@ -20,4 +20,5 @@ public final class TypeConstants { public static final String GOLD_CARD = "goldCard"; public static final String ERROR = "error"; public static final String ROB = "rob"; + public static final String GAME_OVER = "gameOver"; } diff --git a/be/src/main/java/codesquad/gaemimarble/game/service/GameService.java b/be/src/main/java/codesquad/gaemimarble/game/service/GameService.java index 016239e..134223d 100644 --- a/be/src/main/java/codesquad/gaemimarble/game/service/GameService.java +++ b/be/src/main/java/codesquad/gaemimarble/game/service/GameService.java @@ -1,6 +1,7 @@ package codesquad.gaemimarble.game.service; import java.util.ArrayList; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -32,6 +33,8 @@ import codesquad.gaemimarble.game.dto.response.GamePrisonDiceResponse; import codesquad.gaemimarble.game.dto.response.GameReadyResponse; import codesquad.gaemimarble.game.dto.response.GameRoomCreateResponse; +import codesquad.gaemimarble.game.dto.response.PlayerAsset; +import codesquad.gaemimarble.game.dto.response.UserRankingResponse; import codesquad.gaemimarble.game.dto.response.generalStatusBoard.GameStatusBoardResponse; import codesquad.gaemimarble.game.dto.response.userStatusBoard.GameUserBoardResponse; import codesquad.gaemimarble.game.entity.Board; @@ -215,6 +218,7 @@ public GameStatusBoardResponse proceedEvent(String eventName, Long gameId) { } } updatePlayersAsset(gameStatus.getPlayers(), stockList); + gameStatus.incrementRoundCount(); return createGameStatusBoardResponse(gameId); } @@ -323,6 +327,7 @@ public GameEndTurnResponse endTurn(GameEndTurnRequest gameEndTurnRequest) { currentPlayerInfo.update(player); } } + return GameEndTurnResponse.builder().nextPlayerId(null).build(); } @@ -408,4 +413,17 @@ public List rob(GameRobRequest gameRobRequest) { target.addCashAsset(-10_000_000); return List.of(taker, target); } + + public boolean checkGameOver(Long gameId) { + GameStatus gameStatus = gameRepository.getGameStatus(gameId); + return gameStatus.getRoundCount() > 15; + } + + public UserRankingResponse createUserRanking(Long gameId) { + return UserRankingResponse.builder().ranking(gameRepository.getAllPlayer(gameId) + .stream() + .sorted(Comparator.comparing(Player::getTotalAsset).reversed()) + .map(p -> PlayerAsset.builder().playerId(p.getPlayerId()).totalAsset(p.getTotalAsset()).build()) + .collect(Collectors.toList())).build(); + } } From 9914a663eb204b64227612dec0cde9ad464cbe5b Mon Sep 17 00:00:00 2001 From: yhpark95 <98851575+yhpark95@users.noreply.github.com> Date: Tue, 7 Nov 2023 16:57:50 +0900 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20=EC=98=88=EC=99=B8=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EA=B8=B0=EB=8A=A5-1=20#78?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/CustomException.java | 15 ++++++++++ .../exception/GlobalExceptionHandler.java | 21 ++++++++++++++ .../game/controller/SocketDataSender.java | 28 ++++++++++++------- .../game/dto/SocketErrorResponse.java | 4 +-- .../gaemimarble/game/service/GameService.java | 7 +++-- 5 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 be/src/main/java/codesquad/gaemimarble/exception/CustomException.java create mode 100644 be/src/main/java/codesquad/gaemimarble/exception/GlobalExceptionHandler.java diff --git a/be/src/main/java/codesquad/gaemimarble/exception/CustomException.java b/be/src/main/java/codesquad/gaemimarble/exception/CustomException.java new file mode 100644 index 0000000..ed29430 --- /dev/null +++ b/be/src/main/java/codesquad/gaemimarble/exception/CustomException.java @@ -0,0 +1,15 @@ +package codesquad.gaemimarble.exception; + +import lombok.Getter; + +@Getter +public class CustomException extends RuntimeException { + private final String playerId; + private final Long gameId; + + public CustomException(String message, String playerId, Long gameId) { + super(message); + this.playerId = playerId; + this.gameId = gameId; + } +} diff --git a/be/src/main/java/codesquad/gaemimarble/exception/GlobalExceptionHandler.java b/be/src/main/java/codesquad/gaemimarble/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..8f1c846 --- /dev/null +++ b/be/src/main/java/codesquad/gaemimarble/exception/GlobalExceptionHandler.java @@ -0,0 +1,21 @@ +package codesquad.gaemimarble.exception; + +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import codesquad.gaemimarble.game.controller.SocketDataSender; + +@RestControllerAdvice +public class GlobalExceptionHandler { + + private final SocketDataSender socketDataSender; + + public GlobalExceptionHandler(SocketDataSender socketDataSender) { + this.socketDataSender = socketDataSender; + } + + @ExceptionHandler(CustomException.class) + public void handleCustomException(CustomException ex) { + socketDataSender.sendErrorMessage(ex.getGameId(), ex.getPlayerId(), ex.getMessage()); + } +} diff --git a/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java b/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java index 1c80040..d401bc5 100644 --- a/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java +++ b/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java @@ -10,7 +10,6 @@ import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import codesquad.gaemimarble.game.dto.ResponseDTO; @@ -23,24 +22,24 @@ @RequiredArgsConstructor @Slf4j public class SocketDataSender { - private final ConcurrentMap> gameSocketMap = new ConcurrentHashMap<>(); + private final ConcurrentMap> gameSocketMap = new ConcurrentHashMap<>(); private final ObjectMapper objectMapper; public void createRoom(Long gameRoomId) { - gameSocketMap.put(gameRoomId, new HashSet<>()); + gameSocketMap.put(gameRoomId, new ConcurrentHashMap<>()); } public boolean saveSocket(Long gameId, String playerId, WebSocketSession session) { - Set sessions = gameSocketMap.computeIfAbsent(gameId, key -> ConcurrentHashMap.newKeySet()); + ConcurrentMap socketMap = gameSocketMap.get(gameId); try { - if (sessions.size() == 4) { + if (socketMap.values().size() == 4) { session.sendMessage(new TextMessage(objectMapper.writeValueAsString( - new ResponseDTO<>(TypeConstants.ERROR, new SocketErrorResponse("full", "인원이 가득 찼습니다."))))); + new ResponseDTO<>(TypeConstants.ERROR, new SocketErrorResponse("인원이 가득 찼습니다."))))); + session.close(); return false; } - boolean isDuplicate = sessions.stream() - .anyMatch(s -> s.getAttributes().get("playerId").equals(playerId)); + boolean isDuplicate = socketMap.containsKey(playerId); if (!isDuplicate) { session.getAttributes().put("playerId", playerId); @@ -48,7 +47,7 @@ public boolean saveSocket(Long gameId, String playerId, WebSocketSession session return true; } else { session.sendMessage(new TextMessage(objectMapper.writeValueAsString( - new ResponseDTO<>(TypeConstants.ERROR, new SocketErrorResponse("duplicate", "이미 접속한 플레이어입니다."))))); + new ResponseDTO<>(TypeConstants.ERROR, new SocketErrorResponse("이미 접속한 플레이어입니다."))))); return false; } } catch (IOException e) { @@ -58,7 +57,7 @@ public boolean saveSocket(Long gameId, String playerId, WebSocketSession session } public void send(Long gameId, T object) { - for (WebSocketSession session : gameSocketMap.get(gameId)) { + for (WebSocketSession session : gameSocketMap.get(gameId).values()) { try { session.sendMessage(new TextMessage(objectMapper.writeValueAsString(object))); } catch (IOException e) { @@ -67,4 +66,13 @@ public void send(Long gameId, T object) { } System.out.println("전송 완료"); } + + public void sendErrorMessage(Long gameId, String playerId, String message) { + try { + gameSocketMap.get(gameId).get(playerId).sendMessage(new TextMessage(objectMapper.writeValueAsString( + new ResponseDTO<>(TypeConstants.ERROR, new SocketErrorResponse(message))))); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + } } diff --git a/be/src/main/java/codesquad/gaemimarble/game/dto/SocketErrorResponse.java b/be/src/main/java/codesquad/gaemimarble/game/dto/SocketErrorResponse.java index bafcc83..cfddab9 100644 --- a/be/src/main/java/codesquad/gaemimarble/game/dto/SocketErrorResponse.java +++ b/be/src/main/java/codesquad/gaemimarble/game/dto/SocketErrorResponse.java @@ -5,12 +5,10 @@ @Getter public class SocketErrorResponse { - private String errorType; // 나중에 에러 코드로 변경 private String message; @Builder - public SocketErrorResponse(String errorType, String message) { - this.errorType = errorType; + public SocketErrorResponse(String message) { this.message = message; } } diff --git a/be/src/main/java/codesquad/gaemimarble/game/service/GameService.java b/be/src/main/java/codesquad/gaemimarble/game/service/GameService.java index 134223d..fe18148 100644 --- a/be/src/main/java/codesquad/gaemimarble/game/service/GameService.java +++ b/be/src/main/java/codesquad/gaemimarble/game/service/GameService.java @@ -10,6 +10,7 @@ import org.springframework.stereotype.Service; +import codesquad.gaemimarble.exception.CustomException; import codesquad.gaemimarble.game.dto.GameMapper; import codesquad.gaemimarble.game.dto.request.GameEndTurnRequest; import codesquad.gaemimarble.game.dto.request.GameEventResultRequest; @@ -249,10 +250,12 @@ public GameUserBoardResponse buyStock(GameStockBuyRequest gameStockBuyRequest) { .stream() .filter(s -> s.getName().equals(gameStockBuyRequest.getStockName())) .findFirst() - .orElseThrow(() -> new RuntimeException("존재하지 않는 주식이름입니다")); + .orElseThrow(() -> new CustomException("존재하지 않는 주식이름입니다", gameStockBuyRequest.getPlayerId(), + gameStockBuyRequest.getGameId())); if (stock.getRemainingStock() < gameStockBuyRequest.getQuantity() | player.getCashAsset() < stock.getCurrentPrice() * gameStockBuyRequest.getQuantity()) { - throw new RuntimeException("구매할 수량이 부족하거나, 플레이어 보유 캐쉬가 부족합니다"); + throw new CustomException("구매할 수량이 부족하거나, 플레이어 보유 캐쉬가 부족합니다", gameStockBuyRequest.getPlayerId(), + gameStockBuyRequest.getGameId()); } player.buy(stock, gameStockBuyRequest.getQuantity()); stock.decrementQuantity(gameStockBuyRequest.getQuantity()); From 7b6786a574170ff71698c07c7f0200782bc58527 Mon Sep 17 00:00:00 2001 From: yhpark95 <98851575+yhpark95@users.noreply.github.com> Date: Tue, 7 Nov 2023 16:58:07 +0900 Subject: [PATCH 3/5] =?UTF-8?q?feat:=20=EC=98=88=EC=99=B8=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EA=B8=B0=EB=8A=A5-1=20#78?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gaemimarble/game/controller/SocketDataSender.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java b/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java index d401bc5..dd8695d 100644 --- a/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java +++ b/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java @@ -1,8 +1,6 @@ package codesquad.gaemimarble.game.controller; import java.io.IOException; -import java.util.HashSet; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -42,8 +40,7 @@ public boolean saveSocket(Long gameId, String playerId, WebSocketSession session boolean isDuplicate = socketMap.containsKey(playerId); if (!isDuplicate) { - session.getAttributes().put("playerId", playerId); - sessions.add(session); + socketMap.put(playerId, session); return true; } else { session.sendMessage(new TextMessage(objectMapper.writeValueAsString( From a600e60bdaa0f8d65b8b8dffadf7746409ddadf6 Mon Sep 17 00:00:00 2001 From: HyowonSin Date: Tue, 7 Nov 2023 17:30:35 +0900 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20=EC=86=8C=EC=BC=93=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/GlobalExceptionHandler.java | 21 ------------------- .../gaemimarble/filter/WebSocketHandler.java | 9 +++++++- .../game/controller/SocketDataSender.java | 4 ++++ .../gaemimarble/game/service/GameService.java | 10 +++++---- 4 files changed, 18 insertions(+), 26 deletions(-) delete mode 100644 be/src/main/java/codesquad/gaemimarble/exception/GlobalExceptionHandler.java diff --git a/be/src/main/java/codesquad/gaemimarble/exception/GlobalExceptionHandler.java b/be/src/main/java/codesquad/gaemimarble/exception/GlobalExceptionHandler.java deleted file mode 100644 index 8f1c846..0000000 --- a/be/src/main/java/codesquad/gaemimarble/exception/GlobalExceptionHandler.java +++ /dev/null @@ -1,21 +0,0 @@ -package codesquad.gaemimarble.exception; - -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.RestControllerAdvice; - -import codesquad.gaemimarble.game.controller.SocketDataSender; - -@RestControllerAdvice -public class GlobalExceptionHandler { - - private final SocketDataSender socketDataSender; - - public GlobalExceptionHandler(SocketDataSender socketDataSender) { - this.socketDataSender = socketDataSender; - } - - @ExceptionHandler(CustomException.class) - public void handleCustomException(CustomException ex) { - socketDataSender.sendErrorMessage(ex.getGameId(), ex.getPlayerId(), ex.getMessage()); - } -} diff --git a/be/src/main/java/codesquad/gaemimarble/filter/WebSocketHandler.java b/be/src/main/java/codesquad/gaemimarble/filter/WebSocketHandler.java index 0e6021e..cb318ed 100644 --- a/be/src/main/java/codesquad/gaemimarble/filter/WebSocketHandler.java +++ b/be/src/main/java/codesquad/gaemimarble/filter/WebSocketHandler.java @@ -7,6 +7,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; +import codesquad.gaemimarble.exception.CustomException; +import codesquad.gaemimarble.game.controller.SocketDataSender; import codesquad.gaemimarble.game.dto.request.GameMessage; import codesquad.gaemimarble.game.controller.GameController; import lombok.RequiredArgsConstructor; @@ -18,6 +20,7 @@ public class WebSocketHandler extends TextWebSocketHandler { private final ObjectMapper objectMapper; private final GameController gameController; + private final SocketDataSender socketDataSender; @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { @@ -37,7 +40,11 @@ protected void handleTextMessage(WebSocketSession session, TextMessage message) payload, expectedClass); log.info("payload:{}", payload); log.info("className:{}", mappedRequest.getClass().cast(mappedRequest)); - gameController.handleRequest(mappedRequest); + try { + gameController.handleRequest(mappedRequest); + } catch (CustomException ex) { + socketDataSender.sendErrorMessage(ex.getGameId(), ex.getPlayerId(), ex.getMessage()); + } } private Long extractGameIdFromUri(String uri) { diff --git a/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java b/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java index dd8695d..b761a61 100644 --- a/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java +++ b/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java @@ -66,6 +66,10 @@ public void send(Long gameId, T object) { public void sendErrorMessage(Long gameId, String playerId, String message) { try { + if (playerId == null) { + send(gameId, new ResponseDTO<>(TypeConstants.ERROR, new SocketErrorResponse(message))); + return; + } gameSocketMap.get(gameId).get(playerId).sendMessage(new TextMessage(objectMapper.writeValueAsString( new ResponseDTO<>(TypeConstants.ERROR, new SocketErrorResponse(message))))); } catch (IOException e) { diff --git a/be/src/main/java/codesquad/gaemimarble/game/service/GameService.java b/be/src/main/java/codesquad/gaemimarble/game/service/GameService.java index fe18148..40d75f8 100644 --- a/be/src/main/java/codesquad/gaemimarble/game/service/GameService.java +++ b/be/src/main/java/codesquad/gaemimarble/game/service/GameService.java @@ -205,7 +205,7 @@ public GameStatusBoardResponse proceedEvent(String eventName, Long gameId) { } } if (eventToProceed == null) { - throw new RuntimeException("이벤트 이름이 맞지 않습니다"); + throw new CustomException("이벤트 이름이 맞지 않습니다", null, gameId); } GameStatus gameStatus = gameRepository.getGameStatus(gameId); Map impactMap = eventToProceed.getImpact(); @@ -288,7 +288,8 @@ public GameUserBoardResponse sellStock(GameSellStockRequest gameSellStockRequest } for (String stockName : sellingStockInfoMap.keySet()) { if (player.getMyStocks().get(stockName) < sellingStockInfoMap.get(stockName)) { - throw new RuntimeException("플레이어가 보유한 주식보다 더 많이 팔수는 없습니다"); + throw new CustomException("플레이어가 보유한 주식보다 더 많이 팔수는 없습니다", gameSellStockRequest.getPlayerId(), + gameSellStockRequest.getGameId()); } } @@ -337,7 +338,8 @@ public GameEndTurnResponse endTurn(GameEndTurnRequest gameEndTurnRequest) { public void teleport(GameTeleportRequest gameTeleportRequest) { Player player = gameRepository.getPlayer(gameTeleportRequest.getGameId(), gameTeleportRequest.getPlayerId()); if (gameTeleportRequest.getLocation().equals(player.getLocation()) && player.getLocation() == 18) { - throw new RuntimeException("순간이동 칸으로 이동 할 수 없습니다"); + throw new CustomException("순간이동 칸으로 이동 할 수 없습니다", gameTeleportRequest.getPlayerId(), + gameTeleportRequest.getGameId()); } player.setLocation( gameTeleportRequest.getLocation() > player.getLocation() ? gameTeleportRequest.getLocation() : @@ -349,7 +351,7 @@ public GameStatusBoardResponse increaseCompanyStock(Long gameId, Integer locatio String shareName = gameStatus.getBoard().getBoard().get(location); Stock stock = gameStatus.getStocks().stream() .filter(s -> s.getName().equals(shareName)).findFirst() - .orElseThrow(() -> new RuntimeException("존재하지 않는 주식입니다.")); + .orElseThrow(() -> new CustomException("존재하지 않는 주식입니다.", null, gameId)); if (stock.getWasBought()) { stock.changePrice(10); } From 0d65a2c06267bebc5540205fc91b8dfb29827a70 Mon Sep 17 00:00:00 2001 From: HyowonSin Date: Tue, 7 Nov 2023 17:34:06 +0900 Subject: [PATCH 5/5] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EC=A2=85?= =?UTF-8?q?=EB=A3=8C=20=EC=8B=9C=20=EC=84=B8=EC=85=98=20=ED=95=B4=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gaemimarble/game/controller/GameController.java | 1 + .../gaemimarble/game/controller/SocketDataSender.java | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/be/src/main/java/codesquad/gaemimarble/game/controller/GameController.java b/be/src/main/java/codesquad/gaemimarble/game/controller/GameController.java index 0141e1c..cc43adf 100644 --- a/be/src/main/java/codesquad/gaemimarble/game/controller/GameController.java +++ b/be/src/main/java/codesquad/gaemimarble/game/controller/GameController.java @@ -103,6 +103,7 @@ private void sendEventResult(GameEventResultRequest gameEventResultRequest) { socketDataSender.send( gameEventResultRequest.getGameId(), new ResponseDTO<>(TypeConstants.GAME_OVER, gameService.createUserRanking(gameEventResultRequest.getGameId()))); + socketDataSender.close(gameEventResultRequest.getGameId()); } } diff --git a/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java b/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java index b761a61..77c791a 100644 --- a/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java +++ b/be/src/main/java/codesquad/gaemimarble/game/controller/SocketDataSender.java @@ -76,4 +76,15 @@ public void sendErrorMessage(Long gameId, String playerId, String message) { log.error(e.getMessage(), e); } } + + public void close(Long gameId) { + for (WebSocketSession session : gameSocketMap.get(gameId).values()) { + try { + session.close(); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + } + gameSocketMap.remove(gameId); + } }