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 .GameResultListResponse ;
19+ import io .f1 .backend .domain .game .dto .response .GameResultResponse ;
1820import io .f1 .backend .domain .game .event .GameCorrectAnswerEvent ;
1921import io .f1 .backend .domain .game .event .GameTimeoutEvent ;
2022import io .f1 .backend .domain .game .event .RoomUpdatedEvent ;
23+ import io .f1 .backend .domain .game .model .ConnectionState ;
2124import io .f1 .backend .domain .game .model .Player ;
2225import io .f1 .backend .domain .game .model .Room ;
2326import io .f1 .backend .domain .game .model .RoomState ;
2629import io .f1 .backend .domain .question .entity .Question ;
2730import io .f1 .backend .domain .quiz .app .QuizService ;
2831import io .f1 .backend .domain .quiz .entity .Quiz ;
32+ import io .f1 .backend .domain .stat .app .StatService ;
2933import io .f1 .backend .domain .user .dto .UserPrincipal ;
3034import io .f1 .backend .global .exception .CustomException ;
3135import io .f1 .backend .global .exception .errorcode .GameErrorCode ;
@@ -52,6 +56,7 @@ 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 ;
@@ -171,10 +176,15 @@ public void gameEnd(Room room) {
171176 Map <Long , Player > playerMap = room .getPlayerMap ();
172177
173178 // TODO : 랭킹 정보 업데이트
179+ GameResultListResponse gameResultListResponse = toGameResultListResponse (playerMap ,
180+ room .getGameSetting ().getRound ());
181+
174182 messageSender .sendBroadcast (
175183 destination ,
176- MessageType .GAME_RESULT ,
177- toGameResultListResponse (playerMap , room .getGameSetting ().getRound ()));
184+ MessageType .GAME_RESULT ,gameResultListResponse
185+ );
186+
187+ updateRank (room , gameResultListResponse );
178188
179189 room .initializeRound ();
180190 room .initializePlayers ();
@@ -200,6 +210,32 @@ public void gameEnd(Room room) {
200210 destination , MessageType .ROOM_SETTING , toRoomSettingResponse (room ));
201211 }
202212
213+ private void updateRank (Room room , GameResultListResponse gameResultListResponse ) {
214+
215+ List <GameResultResponse > result = gameResultListResponse .result ();
216+
217+ for (GameResultResponse gameResultResponse : result ) {
218+ Long playerId = gameResultResponse .id ();
219+ int rank = gameResultResponse .rank ();
220+ int score = gameResultResponse .score ();
221+
222+ Player player = room .getPlayerByUserId (playerId );
223+
224+ if (Objects .equals (player .getState (), ConnectionState .DISCONNECTED )) {
225+ statService .updateRank (playerId , false , 0 );
226+ continue ;
227+ }
228+
229+ if (rank == 1 ) {
230+ statService .updateRank (playerId , true , score );
231+ continue ;
232+ }
233+
234+ statService .updateRank (playerId , false , score );
235+
236+ }
237+ }
238+
203239 @ DistributedLock (prefix = "room" , key = "#roomId" )
204240 public void handlePlayerReady (Long roomId , UserPrincipal userPrincipal ) {
205241
0 commit comments