4545import io .f1 .backend .domain .user .dto .UserPrincipal ;
4646import io .f1 .backend .global .exception .CustomException ;
4747import io .f1 .backend .global .exception .errorcode .RoomErrorCode ;
48+
49+ import lombok .RequiredArgsConstructor ;
50+ import lombok .extern .slf4j .Slf4j ;
51+
52+ import org .springframework .context .ApplicationEventPublisher ;
53+ import org .springframework .stereotype .Service ;
54+
4855import java .util .List ;
4956import java .util .Map ;
5057import java .util .Optional ;
5158import java .util .concurrent .ConcurrentHashMap ;
5259import java .util .concurrent .atomic .AtomicLong ;
53- import lombok .RequiredArgsConstructor ;
54- import lombok .extern .slf4j .Slf4j ;
55- import org .springframework .context .ApplicationEventPublisher ;
56- import org .springframework .stereotype .Service ;
5760
5861@ Slf4j
5962@ Service
@@ -138,17 +141,17 @@ public void initializeRoomSocket(Long roomId, String sessionId, UserPrincipal pr
138141 Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
139142
140143 GameSettingResponse gameSettingResponse =
141- toGameSettingResponse (room .getGameSetting (), quiz );
144+ toGameSettingResponse (room .getGameSetting (), quiz );
142145
143146 PlayerListResponse playerListResponse = toPlayerListResponse (room );
144147
145148 SystemNoticeResponse systemNoticeResponse =
146- ofPlayerEvent (player .getNickname (), RoomEventType .ENTER );
149+ ofPlayerEvent (player .getNickname (), RoomEventType .ENTER );
147150
148151 String destination = getDestination (roomId );
149152
150- messageSender .sendPersonal (getUserDestination (), MessageType . GAME_SETTING , gameSettingResponse ,
151- principal );
153+ messageSender .sendPersonal (
154+ getUserDestination (), MessageType . GAME_SETTING , gameSettingResponse , principal );
152155
153156 messageSender .sendBroadcast (destination , MessageType .ROOM_SETTING , roomSettingResponse );
154157 messageSender .sendBroadcast (destination , MessageType .PLAYER_LIST , playerListResponse );
@@ -166,8 +169,11 @@ public void exitRoom(Long roomId, String sessionId, UserPrincipal principal) {
166169
167170 String destination = getDestination (roomId );
168171
169- messageSender .sendPersonal (getUserDestination (), MessageType .EXIT_SUCCESS ,
170- new ExitSuccessResponse (true ), principal );
172+ messageSender .sendPersonal (
173+ getUserDestination (),
174+ MessageType .EXIT_SUCCESS ,
175+ new ExitSuccessResponse (true ),
176+ principal );
171177
172178 /* 방 삭제 */
173179 if (room .isLastPlayer (sessionId )) {
@@ -184,15 +190,14 @@ public void exitRoom(Long roomId, String sessionId, UserPrincipal principal) {
184190 removePlayer (room , sessionId , removePlayer );
185191
186192 SystemNoticeResponse systemNoticeResponse =
187- ofPlayerEvent (removePlayer .nickname , RoomEventType .EXIT );
193+ ofPlayerEvent (removePlayer .nickname , RoomEventType .EXIT );
188194
189195 PlayerListResponse playerListResponse = toPlayerListResponse (room );
190196
191197 messageSender .sendBroadcast (destination , MessageType .PLAYER_LIST , playerListResponse );
192- messageSender .sendBroadcast (destination , MessageType . SYSTEM_NOTICE ,
193- systemNoticeResponse );
198+ messageSender .sendBroadcast (
199+ destination , MessageType . SYSTEM_NOTICE , systemNoticeResponse );
194200 }
195-
196201 }
197202
198203 public void handlePlayerReady (Long roomId , String sessionId ) {
@@ -207,22 +212,22 @@ public void handlePlayerReady(Long roomId, String sessionId) {
207212
208213 String destination = getDestination (roomId );
209214
210- messageSender .sendBroadcast (destination , MessageType . PLAYER_LIST ,
211- toPlayerListResponse (room ));
215+ messageSender .sendBroadcast (
216+ destination , MessageType . PLAYER_LIST , toPlayerListResponse (room ));
212217 }
213218
214219 public RoomListResponse getAllRooms () {
215220 List <Room > rooms = roomRepository .findAll ();
216221 List <RoomResponse > roomResponses =
217- rooms .stream ()
218- .map (
219- room -> {
220- Long quizId = room .getGameSetting ().getQuizId ();
221- Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
222-
223- return toRoomResponse (room , quiz );
224- })
225- .toList ();
222+ rooms .stream ()
223+ .map (
224+ room -> {
225+ Long quizId = room .getGameSetting ().getQuizId ();
226+ Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
227+
228+ return toRoomResponse (room , quiz );
229+ })
230+ .toList ();
226231 return new RoomListResponse (roomResponses );
227232 }
228233
@@ -246,15 +251,15 @@ public void chat(Long roomId, String sessionId, ChatMessage chatMessage) {
246251 room .increasePlayerCorrectCount (sessionId );
247252
248253 messageSender .sendBroadcast (
249- destination ,
250- MessageType .QUESTION_RESULT ,
251- toQuestionResultResponse (chatMessage .nickname (), answer ));
252- messageSender .sendBroadcast (destination , MessageType .RANK_UPDATE ,
253- toRankUpdateResponse (room ));
254+ destination ,
255+ MessageType .QUESTION_RESULT ,
256+ toQuestionResultResponse (chatMessage .nickname (), answer ));
254257 messageSender .sendBroadcast (
255- destination ,
256- MessageType .SYSTEM_NOTICE ,
257- ofPlayerEvent (chatMessage .nickname (), RoomEventType .CORRECT_ANSWER ));
258+ destination , MessageType .RANK_UPDATE , toRankUpdateResponse (room ));
259+ messageSender .sendBroadcast (
260+ destination ,
261+ MessageType .SYSTEM_NOTICE ,
262+ ofPlayerEvent (chatMessage .nickname (), RoomEventType .CORRECT_ANSWER ));
258263
259264 timerService .cancelTimer (room );
260265
@@ -269,35 +274,41 @@ public void chat(Long roomId, String sessionId, ChatMessage chatMessage) {
269274 // 타이머 추가하기
270275 timerService .startTimer (room , CONTINUE_DELAY );
271276 messageSender .sendBroadcast (
272- destination ,
273- MessageType .QUESTION_START ,
274- toQuestionStartResponse (room , CONTINUE_DELAY ));
277+ destination ,
278+ MessageType .QUESTION_START ,
279+ toQuestionStartResponse (room , CONTINUE_DELAY ));
275280 }
276281 }
277282
278283 public void reconnectSession (
279- Long roomId , String oldSessionId , String newSessionId , UserPrincipal principal ) {
284+ Long roomId , String oldSessionId , String newSessionId , UserPrincipal principal ) {
280285 Room room = findRoom (roomId );
281286 room .reconnectSession (oldSessionId , newSessionId );
282287
283288 String destination = getDestination (roomId );
284289 String userDestination = getUserDestination ();
285290
286291 messageSender .sendBroadcast (
287- destination ,
288- MessageType .SYSTEM_NOTICE ,
289- ofPlayerEvent (principal .getUserNickname (), RoomEventType .RECONNECT ));
292+ destination ,
293+ MessageType .SYSTEM_NOTICE ,
294+ ofPlayerEvent (principal .getUserNickname (), RoomEventType .RECONNECT ));
290295
291296 if (room .isPlaying ()) {
292- messageSender .sendPersonal (userDestination , MessageType .RANK_UPDATE ,
293- toRankUpdateResponse (room ), principal );
294297 messageSender .sendPersonal (
295- userDestination , MessageType .GAME_START , toGameStartResponse (room .getQuestions ()),
296- principal );
298+ userDestination ,
299+ MessageType .RANK_UPDATE ,
300+ toRankUpdateResponse (room ),
301+ principal );
297302 messageSender .sendPersonal (
298- userDestination ,
299- MessageType .QUESTION_START ,
300- toQuestionStartResponse (room , START_DELAY ), principal );
303+ userDestination ,
304+ MessageType .GAME_START ,
305+ toGameStartResponse (room .getQuestions ()),
306+ principal );
307+ messageSender .sendPersonal (
308+ userDestination ,
309+ MessageType .QUESTION_START ,
310+ toQuestionStartResponse (room , START_DELAY ),
311+ principal );
301312 } else {
302313 RoomSettingResponse roomSettingResponse = toRoomSettingResponse (room );
303314
@@ -306,16 +317,16 @@ userDestination, MessageType.GAME_START, toGameStartResponse(room.getQuestions()
306317 Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
307318
308319 GameSettingResponse gameSettingResponse =
309- toGameSettingResponse (room .getGameSetting (), quiz );
320+ toGameSettingResponse (room .getGameSetting (), quiz );
310321
311322 PlayerListResponse playerListResponse = toPlayerListResponse (room );
312323
313- messageSender .sendPersonal (userDestination , MessageType . ROOM_SETTING ,
314- roomSettingResponse , principal );
315- messageSender .sendPersonal (userDestination , MessageType . PLAYER_LIST , playerListResponse ,
316- principal );
317- messageSender .sendPersonal (userDestination , MessageType . GAME_SETTING ,
318- gameSettingResponse , principal );
324+ messageSender .sendPersonal (
325+ userDestination , MessageType . ROOM_SETTING , roomSettingResponse , principal );
326+ messageSender .sendPersonal (
327+ userDestination , MessageType . PLAYER_LIST , playerListResponse , principal );
328+ messageSender .sendPersonal (
329+ userDestination , MessageType . GAME_SETTING , gameSettingResponse , principal );
319330 }
320331 }
321332
@@ -370,11 +381,11 @@ private void changeHost(Room room, String hostSessionId) {
370381 Map <String , Player > playerSessionMap = room .getPlayerSessionMap ();
371382
372383 Optional <String > nextHostSessionId =
373- playerSessionMap .entrySet ().stream ()
374- .filter (entry -> !entry .getKey ().equals (hostSessionId ))
375- .filter (entry -> entry .getValue ().getState () == ConnectionState .CONNECTED )
376- .map (Map .Entry ::getKey )
377- .findFirst ();
384+ playerSessionMap .entrySet ().stream ()
385+ .filter (entry -> !entry .getKey ().equals (hostSessionId ))
386+ .filter (entry -> entry .getValue ().getState () == ConnectionState .CONNECTED )
387+ .map (Map .Entry ::getKey )
388+ .findFirst ();
378389
379390 Player nextHost =
380391 playerSessionMap .get (
0 commit comments