1515import io .f1 .backend .domain .game .dto .MessageType ;
1616import io .f1 .backend .domain .game .dto .RoomEventType ;
1717import io .f1 .backend .domain .game .dto .request .GameSettingChanger ;
18- import io .f1 .backend .domain .game .dto .response .PlayerListResponse ;
18+ import io .f1 .backend .domain .game .dto .response .GameResultListResponse ;
19+ import io .f1 .backend .domain .game .dto .response .GameResultResponse ;
1920import io .f1 .backend .domain .game .event .GameCorrectAnswerEvent ;
2021import io .f1 .backend .domain .game .event .GameTimeoutEvent ;
2122import io .f1 .backend .domain .game .event .RoomUpdatedEvent ;
23+ import io .f1 .backend .domain .game .model .ConnectionState ;
2224import io .f1 .backend .domain .game .model .Player ;
2325import io .f1 .backend .domain .game .model .Room ;
2426import io .f1 .backend .domain .game .model .RoomState ;
2729import io .f1 .backend .domain .question .entity .Question ;
2830import io .f1 .backend .domain .quiz .app .QuizService ;
2931import io .f1 .backend .domain .quiz .entity .Quiz ;
32+ import io .f1 .backend .domain .stat .app .StatService ;
3033import io .f1 .backend .domain .user .dto .UserPrincipal ;
3134import io .f1 .backend .global .exception .CustomException ;
3235import io .f1 .backend .global .exception .errorcode .GameErrorCode ;
3336import io .f1 .backend .global .exception .errorcode .RoomErrorCode ;
37+ import io .f1 .backend .global .lock .DistributedLock ;
3438
3539import lombok .RequiredArgsConstructor ;
3640import lombok .extern .slf4j .Slf4j ;
@@ -52,13 +56,15 @@ public class GameService {
5256 private static final int CONTINUE_DELAY = 3 ;
5357 private static final String NONE_CORRECT_USER = "" ;
5458
59+ private final StatService statService ;
5560 private final QuizService quizService ;
5661 private final RoomService roomService ;
5762 private final TimerService timerService ;
5863 private final MessageSender messageSender ;
5964 private final RoomRepository roomRepository ;
6065 private final ApplicationEventPublisher eventPublisher ;
6166
67+ @ DistributedLock (prefix = "room" , key = "#roomId" )
6268 public void gameStart (Long roomId , UserPrincipal principal ) {
6369
6470 String destination = getDestination (roomId );
@@ -97,13 +103,13 @@ public void onCorrectAnswer(GameCorrectAnswerEvent event) {
97103
98104 Room room = event .room ();
99105 log .debug (room .getId () + "๋ฒ ๋ฐฉ ์ฑํ
์ผ๋ก ์ ๋ต! ํ์ฌ ๋ผ์ด๋ : " + room .getCurrentRound ());
100- String sessionId = event .sessionId ();
106+ Long userId = event .userId ();
101107 ChatMessage chatMessage = event .chatMessage ();
102108 String answer = event .answer ();
103109
104110 String destination = getDestination (room .getId ());
105111
106- room .increasePlayerCorrectCount (sessionId );
112+ room .increasePlayerCorrectCount (userId );
107113
108114 messageSender .sendBroadcast (
109115 destination ,
@@ -167,13 +173,14 @@ public void gameEnd(Room room) {
167173 Long roomId = room .getId ();
168174 String destination = getDestination (roomId );
169175
170- Map <String , Player > playerSessionMap = room .getPlayerSessionMap ();
176+ Map <Long , Player > playerMap = room .getPlayerMap ();
171177
172- // TODO : ๋ญํน ์ ๋ณด ์
๋ฐ์ดํธ
173- messageSender .sendBroadcast (
174- destination ,
175- MessageType .GAME_RESULT ,
176- toGameResultListResponse (playerSessionMap , room .getGameSetting ().getRound ()));
178+ GameResultListResponse gameResultListResponse =
179+ toGameResultListResponse (playerMap , room .getGameSetting ().getRound ());
180+
181+ messageSender .sendBroadcast (destination , MessageType .GAME_RESULT , gameResultListResponse );
182+
183+ updateRank (room , gameResultListResponse );
177184
178185 room .initializeRound ();
179186 room .initializePlayers ();
@@ -199,19 +206,44 @@ public void gameEnd(Room room) {
199206 destination , MessageType .ROOM_SETTING , toRoomSettingResponse (room ));
200207 }
201208
202- public void handlePlayerReady (Long roomId , String sessionId ) {
209+ private void updateRank (Room room , GameResultListResponse gameResultListResponse ) {
210+
211+ List <GameResultResponse > result = gameResultListResponse .result ();
212+
213+ for (GameResultResponse gameResultResponse : result ) {
214+ Long playerId = gameResultResponse .id ();
215+ int rank = gameResultResponse .rank ();
216+ int score = gameResultResponse .score ();
217+
218+ Player player = room .getPlayerByUserId (playerId );
219+
220+ if (room .isPlayerInState (playerId , ConnectionState .DISCONNECTED )) {
221+ statService .updateRank (playerId , false , 0 );
222+ continue ;
223+ }
224+
225+ if (rank == 1 ) {
226+ statService .updateRank (playerId , true , score );
227+ continue ;
228+ }
229+
230+ statService .updateRank (playerId , false , score );
231+ }
232+ }
233+
234+ @ DistributedLock (prefix = "room" , key = "#roomId" )
235+ public void handlePlayerReady (Long roomId , UserPrincipal userPrincipal ) {
203236
204237 Room room = findRoom (roomId );
205238
206- Player player = room .getPlayerBySessionId ( sessionId );
239+ Player player = room .getPlayerByUserId ( userPrincipal . getUserId () );
207240
208241 toggleReadyIfPossible (room , player );
209242
210243 String destination = getDestination (roomId );
211244
212- PlayerListResponse playerListResponse = toPlayerListResponse (room );
213- log .info (playerListResponse .toString ());
214- messageSender .sendBroadcast (destination , MessageType .PLAYER_LIST , playerListResponse );
245+ messageSender .sendBroadcast (
246+ destination , MessageType .PLAYER_LIST , toPlayerListResponse (room ));
215247 }
216248
217249 public void changeGameSetting (
@@ -244,7 +276,7 @@ private void validateRoomStart(Room room, UserPrincipal principal) {
244276 // ๋ผ์ด๋ ์๋งํผ ๋๋ค Question ์ถ์ถ
245277 private List <Question > prepareQuestions (Room room , Quiz quiz ) {
246278 Long quizId = quiz .getId ();
247- Integer round = room .getGameSetting (). getRound ();
279+ Integer round = room .getRound ();
248280 return quizService .getRandomQuestionsWithoutAnswer (quizId , round );
249281 }
250282
0 commit comments