From 50a2dce59fc36253dd9bacab243280d4b7573845 Mon Sep 17 00:00:00 2001 From: leestana01 Date: Tue, 6 Aug 2024 22:46:33 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20#5=20=EB=B0=98=ED=99=98=EA=B0=92=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/GameController.java | 6 +- .../runwithmate/domain/game/GameInfo.java | 5 ++ .../domain/game/GameInfoForUser.java | 1 + .../domain/game/dto/GameFinishResDto.java | 5 +- .../domain/game/type/FinishType.java | 2 +- .../runwithmate/service/GameRoomService.java | 4 +- .../lgtu/runwithmate/service/GameService.java | 62 ++++++++++++------- 7 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/main/java/likelion/hufsglobal/lgtu/runwithmate/controller/GameController.java b/src/main/java/likelion/hufsglobal/lgtu/runwithmate/controller/GameController.java index c3cb1ba..d5afb33 100644 --- a/src/main/java/likelion/hufsglobal/lgtu/runwithmate/controller/GameController.java +++ b/src/main/java/likelion/hufsglobal/lgtu/runwithmate/controller/GameController.java @@ -34,10 +34,10 @@ public StartCheckResDto checkStart(@DestinationVariable String roomId, UserPosit } @MessageMapping("/update_position/{roomId}") - @SendTo("/room/{roomId}") - public PositionUpdateResDto updatePosition(@DestinationVariable String roomId, UserPosition userPosition, Authentication authentication) { +// @SendTo("/room/{roomId}") + public void updatePosition(@DestinationVariable String roomId, UserPosition userPosition, Authentication authentication) { String userId = authentication.getName(); - return gameService.updatePosition(roomId, userId, userPosition); + gameService.updatePosition(roomId, userId, userPosition); } @MessageMapping("/surrender/{roomId}") diff --git a/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/GameInfo.java b/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/GameInfo.java index f11ff37..bf1dbd9 100644 --- a/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/GameInfo.java +++ b/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/GameInfo.java @@ -1,6 +1,7 @@ package likelion.hufsglobal.lgtu.runwithmate.domain.game; import jakarta.persistence.*; +import likelion.hufsglobal.lgtu.runwithmate.domain.game.type.FinishType; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -15,8 +16,12 @@ public class GameInfo { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + private String type="game_finished"; + private FinishType finishType; private String roomId; private Long betPoint; + private String winnerId; + private String winnerName; @OneToMany(cascade = CascadeType.ALL) private List usersInfo = new ArrayList<>(); } diff --git a/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/GameInfoForUser.java b/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/GameInfoForUser.java index 881c583..b43e55b 100644 --- a/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/GameInfoForUser.java +++ b/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/GameInfoForUser.java @@ -19,6 +19,7 @@ public class GameInfoForUser { private Long id; private String userId; + private String userName; private Long dopamine; private Long point; } \ No newline at end of file diff --git a/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/dto/GameFinishResDto.java b/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/dto/GameFinishResDto.java index 7ec2319..79768ee 100644 --- a/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/dto/GameFinishResDto.java +++ b/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/dto/GameFinishResDto.java @@ -15,7 +15,10 @@ public class GameFinishResDto { private final String type = "game_finished"; private FinishType finishType; // 두 개로 해야하는디 흠 - private String winner; + private String roomId; + private Long betPoint; + private String winnerId; + private String winnerName; private List usersInfo; } diff --git a/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/type/FinishType.java b/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/type/FinishType.java index 87e9a44..6389135 100644 --- a/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/type/FinishType.java +++ b/src/main/java/likelion/hufsglobal/lgtu/runwithmate/domain/game/type/FinishType.java @@ -1,5 +1,5 @@ package likelion.hufsglobal.lgtu.runwithmate.domain.game.type; public enum FinishType { - TIME_EXCEED, PLAYER_SURRENDER + TIME_OVER, PLAYER_SURRENDER } \ No newline at end of file diff --git a/src/main/java/likelion/hufsglobal/lgtu/runwithmate/service/GameRoomService.java b/src/main/java/likelion/hufsglobal/lgtu/runwithmate/service/GameRoomService.java index b820a5c..cb000b0 100644 --- a/src/main/java/likelion/hufsglobal/lgtu/runwithmate/service/GameRoomService.java +++ b/src/main/java/likelion/hufsglobal/lgtu/runwithmate/service/GameRoomService.java @@ -19,8 +19,8 @@ public class GameRoomService { private final RedisTemplate redisTemplate; - private static final long DEFAULT_BET_POINT = 1000L; - private static final long DEFAULT_TIME_LIMIT = 60L; + private static final int DEFAULT_BET_POINT = 1000; + private static final int DEFAULT_TIME_LIMIT = 60; private final UserRepository userRepository; diff --git a/src/main/java/likelion/hufsglobal/lgtu/runwithmate/service/GameService.java b/src/main/java/likelion/hufsglobal/lgtu/runwithmate/service/GameService.java index f3f3f25..679912c 100644 --- a/src/main/java/likelion/hufsglobal/lgtu/runwithmate/service/GameService.java +++ b/src/main/java/likelion/hufsglobal/lgtu/runwithmate/service/GameService.java @@ -168,28 +168,26 @@ private BoxInfo createBox(BoxType boxType, Double range, Double division, long i } private Long calcRunTime(String roomId){ + ObjectMapper mapper = new ObjectMapper(); Object startTimeObject = redisTemplate.opsForHash().get("game_rooms:" + roomId, "start_time"); - if (startTimeObject instanceof String) { - // 저장된 문자열을 LocalDateTime으로 변환 - LocalDateTime startTime = LocalDateTime.parse((String) startTimeObject, formatter); - LocalDateTime currentTime = LocalDateTime.now(); - Duration duration = Duration.between(startTime, currentTime); - return duration.getSeconds(); - } else { - throw new IllegalStateException("게임 시작 시간을 인식할 수 없습니다. String 타입이 아닙니다."); - } + LocalDateTime startTime = LocalDateTime.parse(mapper.convertValue(startTimeObject, String.class), formatter); + LocalDateTime currentTime = LocalDateTime.now(); + Duration duration = Duration.between(startTime, currentTime); + return duration.getSeconds(); } private Long calcTimeLeft(String roomId){ Long runTime = calcRunTime(roomId); - Long timeLimit = Long.valueOf((Integer) redisTemplate.opsForHash().get("game_rooms:" + roomId, "time_limit"));; - return timeLimit - runTime; + ObjectMapper mapper = new ObjectMapper(); + + Object object = redisTemplate.opsForHash().get("game_rooms:" + roomId, "time_limit");; + return mapper.convertValue(object, Long.class) - runTime; } // ------------------------------------------------- - public PositionUpdateResDto updatePosition(String roomId, String userId, UserPosition position) { + public void updatePosition(String roomId, String userId, UserPosition position) { /** * 1. 해당 플레이어 위치를 `player_position:방번호`방번호 에서 찾음 * 2. (선택) 현재 위치와 이전 위치의 오차를 계산하고, 비정상적인 변동인지 파악함 @@ -204,7 +202,11 @@ public PositionUpdateResDto updatePosition(String roomId, String userId, UserPos positionUpdateResDto.setPosition(position); positionUpdateResDto.setTimeLeft(calcTimeLeft(roomId)); - return positionUpdateResDto; + messagingTemplate.convertAndSend("/room/" + roomId, positionUpdateResDto); + // 시간 종료 체크 + if (calcTimeLeft(roomId) <= 0) { + messagingTemplate.convertAndSend("/room/" + roomId, finishGame(roomId, FinishType.TIME_OVER, "")); + } } // ------------------------------------------------- @@ -327,14 +329,18 @@ public GameFinishResDto finishGame(String roomId, FinishType finishType, String String userTwoId = (String) redisTemplate.opsForHash().get("game_rooms:"+roomId,"user2_id"); // user1 인게임 도파민/포인트 빼오기 - Map userOnePoints = (Map) redisTemplate.opsForHash().get("player_points:" + roomId, userOneId); - Long userOneDopamine = userOnePoints.get("dopamine"); - Long userOneGamePoint = userOnePoints.get("point"); + ObjectMapper mapper = new ObjectMapper(); + Object object; + object = redisTemplate.opsForHash().get("player_points:" + roomId, userOneId); + Map userOnePoints = mapper.convertValue(object, Map.class); + Long userOneDopamine = Long.valueOf((Integer)userOnePoints.get("dopamine")); + Long userOneGamePoint = Long.valueOf((Integer)userOnePoints.get("point")); // user2 인게임 도파민/포인트 빼오기 - Map userTwoPoints = (Map) redisTemplate.opsForHash().get("player_points:" + roomId, userTwoId); - Long userTwoDopamine = userTwoPoints.get("dopamine"); - Long userTwoGamePoint = userTwoPoints.get("point"); + object = redisTemplate.opsForHash().get("player_points:" + roomId, userTwoId); + Map userTwoPoints = mapper.convertValue(object, Map.class); + Long userTwoDopamine = Long.valueOf((Integer)userTwoPoints.get("dopamine")); + Long userTwoGamePoint = Long.valueOf((Integer)userTwoPoints.get("point")); boolean isUserOneWin = userOneDopamine >= userTwoDopamine; @@ -342,9 +348,16 @@ public GameFinishResDto finishGame(String roomId, FinishType finishType, String isUserOneWin = !surrenderId.equals(userOneId); } + // --- Response DTO 만들기 --- + GameFinishResDto gameFinishResDto = new GameFinishResDto(); gameFinishResDto.setFinishType(finishType); - gameFinishResDto.setWinner(isUserOneWin ? userOneId : userTwoId); + gameFinishResDto.setRoomId(roomId); + gameFinishResDto.setBetPoint(Long.valueOf((Integer) redisTemplate.opsForHash().get("game_rooms:" + roomId, "bet_point"))); + + String winnerId = isUserOneWin ? userOneId : userTwoId; + gameFinishResDto.setWinnerId(isUserOneWin ? userOneId : userTwoId); + gameFinishResDto.setWinnerName(userRepository.findByUserId(winnerId).orElseThrow(() -> new IllegalArgumentException("User not found")).getNickname()); User userOne = userRepository.findByUserId(userOneId).orElseThrow(() -> new IllegalArgumentException("User not found")); String userOneName = userOne.getNickname(); @@ -371,12 +384,14 @@ public GameFinishResDto finishGame(String roomId, FinishType finishType, String // user1 GameInfoForUser에 저장 GameInfoForUser newGameInfoForUser1 = new GameInfoForUser(); newGameInfoForUser1.setUserId(userOneId); + newGameInfoForUser1.setUserName(userOneName); newGameInfoForUser1.setDopamine(userOneDopamine); newGameInfoForUser1.setPoint(userOneGamePoint); // user2 GameInfoForUser에 저장 GameInfoForUser newGameInfoForUser2 = new GameInfoForUser(); newGameInfoForUser2.setUserId(userTwoId); + newGameInfoForUser2.setUserName(userTwoName); newGameInfoForUser2.setDopamine(userTwoDopamine); newGameInfoForUser2.setPoint(userTwoGamePoint); @@ -385,12 +400,13 @@ public GameFinishResDto finishGame(String roomId, FinishType finishType, String newUsersInfo.add(newGameInfoForUser1); newUsersInfo.add(newGameInfoForUser2); - // 배팅 포인트 가져오기 - Long betPoint = (Long) redisTemplate.opsForHash().get("game_rooms:" + roomId, "bet_point"); - + Long betPoint = Long.valueOf((Integer) redisTemplate.opsForHash().get("game_rooms:" + roomId, "bet_point")); GameInfo newGameInfo = new GameInfo(); + newGameInfo.setFinishType(finishType); newGameInfo.setRoomId(roomId); newGameInfo.setBetPoint(betPoint); + newGameInfo.setWinnerId(winnerId); + newGameInfo.setWinnerName(userRepository.findByUserId(winnerId).orElseThrow(() -> new IllegalArgumentException("User not found")).getNickname()); newGameInfo.setUsersInfo(newUsersInfo); // mysql에 데이터 저장하기