From 6bc7bbcdbed4cfb7abd942a3fc5442df503e38ad Mon Sep 17 00:00:00 2001 From: ooni Date: Wed, 22 Jan 2025 17:59:22 +0900 Subject: [PATCH] =?UTF-8?q?Feature(#11)=20:=20=EC=A0=90=EC=88=98=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20=EB=A1=9C=EC=A7=81=EC=97=90=EC=84=9C=20red?= =?UTF-8?q?is=20=EC=A1=B0=ED=9A=8C=EC=98=A4=EB=A5=98=20@Indexed=EB=A1=9C?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/game/service/GameServiceImpl.java | 17 +++++++++++++++-- .../domain/round/entity/Answer.java | 4 ++++ .../round/repository/AnswerRepository.java | 2 +- .../domain/round/service/RoundServiceImpl.java | 2 +- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/urdego/urdego_game_service/domain/game/service/GameServiceImpl.java b/src/main/java/io/urdego/urdego_game_service/domain/game/service/GameServiceImpl.java index f980578..a73430f 100644 --- a/src/main/java/io/urdego/urdego_game_service/domain/game/service/GameServiceImpl.java +++ b/src/main/java/io/urdego/urdego_game_service/domain/game/service/GameServiceImpl.java @@ -16,12 +16,14 @@ import io.urdego.urdego_game_service.domain.round.entity.Question; import io.urdego.urdego_game_service.domain.round.service.RoundService; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.Instant; import java.util.*; +@Slf4j @Service @Transactional @RequiredArgsConstructor @@ -76,6 +78,8 @@ public ScoreRes giveScores(ScoreReq request) { gameRepository.save(game); + log.info("게임 점수 정보 : roundScores={}, totalScores={}", game.getRoundScores(), game.getTotalScores()); + return ScoreRes.from(game); } @@ -119,6 +123,8 @@ public Game findGameById(String gameId) { game.setTotalScores(new HashMap<>()); } + gameRepository.save(game); + return game; } @@ -126,16 +132,21 @@ public Game findGameById(String gameId) { private void updateRoundScores(Game game, int roundNum, List answers) { Map> roundScores = game.getRoundScores(); - if (!roundScores.containsKey(roundNum)) { - roundScores.put(roundNum, new HashMap<>()); + if (roundScores == null) { + roundScores = new HashMap<>(); } + roundScores.putIfAbsent(roundNum, new HashMap<>()); + Map roundScore = roundScores.get(roundNum); for (Answer answer : answers) { + log.info("Answer: userId={}, score={}", answer.getUserId(), answer.getScore()); roundScore.put(answer.getUserId(), answer.getScore()); } game.setRoundScores(roundScores); + + log.info("{}라운드 점수가 업데이트 되었습니다. : {}", roundNum, game.getRoundScores()); } // 전체 점수 업뎃 @@ -149,5 +160,7 @@ private void updateTotalScores(Game game) { }); game.setTotalScores(totalScores); + + log.info("전체 점수가 업데이트 되었습니다. : {}", game.getTotalScores()); } } diff --git a/src/main/java/io/urdego/urdego_game_service/domain/round/entity/Answer.java b/src/main/java/io/urdego/urdego_game_service/domain/round/entity/Answer.java index 7e87b86..6010139 100644 --- a/src/main/java/io/urdego/urdego_game_service/domain/round/entity/Answer.java +++ b/src/main/java/io/urdego/urdego_game_service/domain/round/entity/Answer.java @@ -6,6 +6,7 @@ import lombok.NoArgsConstructor; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; +import org.springframework.data.redis.core.index.Indexed; @Getter @RedisHash(value = "answer", timeToLive = 3600) @@ -14,7 +15,10 @@ public class Answer { @Id private String answerId; + + @Indexed private String questionId; + private String userId; private double latitude; private double longitude; diff --git a/src/main/java/io/urdego/urdego_game_service/domain/round/repository/AnswerRepository.java b/src/main/java/io/urdego/urdego_game_service/domain/round/repository/AnswerRepository.java index 2906e53..6902f12 100644 --- a/src/main/java/io/urdego/urdego_game_service/domain/round/repository/AnswerRepository.java +++ b/src/main/java/io/urdego/urdego_game_service/domain/round/repository/AnswerRepository.java @@ -8,5 +8,5 @@ @Repository public interface AnswerRepository extends CrudRepository { - List findByQuestionId(String questionId); + List findAllByQuestionId(String questionId); } diff --git a/src/main/java/io/urdego/urdego_game_service/domain/round/service/RoundServiceImpl.java b/src/main/java/io/urdego/urdego_game_service/domain/round/service/RoundServiceImpl.java index 6b82c76..8900171 100644 --- a/src/main/java/io/urdego/urdego_game_service/domain/round/service/RoundServiceImpl.java +++ b/src/main/java/io/urdego/urdego_game_service/domain/round/service/RoundServiceImpl.java @@ -117,7 +117,7 @@ public AnswerRes submitAnswer(AnswerReq request) { @Override @Transactional(readOnly = true) public List findAnswersByQuestionId(String questionId) { - return answerRepository.findByQuestionId(questionId); + return answerRepository.findAllByQuestionId(questionId); } // roomId로 문제 정보 조회