3838import io .f1 .backend .domain .user .dto .UserPrincipal ;
3939import io .f1 .backend .global .exception .CustomException ;
4040import io .f1 .backend .global .exception .errorcode .RoomErrorCode ;
41+
42+ import lombok .RequiredArgsConstructor ;
43+ import lombok .extern .slf4j .Slf4j ;
44+
45+ import org .springframework .context .ApplicationEventPublisher ;
46+ import org .springframework .stereotype .Service ;
47+
4148import java .util .List ;
4249import java .util .Map ;
4350import java .util .Optional ;
4451import java .util .concurrent .ConcurrentHashMap ;
4552import java .util .concurrent .atomic .AtomicLong ;
46- import lombok .RequiredArgsConstructor ;
47- import lombok .extern .slf4j .Slf4j ;
48- import org .springframework .context .ApplicationEventPublisher ;
49- import org .springframework .stereotype .Service ;
5053
5154@ Slf4j
5255@ Service
@@ -105,16 +108,15 @@ public void enterRoom(RoomValidationRequest request) {
105108 }
106109
107110 if (room .getRoomSetting ().locked ()
108- && !room .getRoomSetting ().password ().equals (request .password ())) {
111+ && !room .getRoomSetting ().password ().equals (request .password ())) {
109112 throw new CustomException (RoomErrorCode .WRONG_PASSWORD );
110113 }
111114
112115 room .getUserIdSessionMap ().put (getCurrentUserId (), PENDING_SESSION_ID );
113116 }
114117 }
115118
116- public void initializeRoomSocket (
117- Long roomId , String sessionId , UserPrincipal principal ) {
119+ public void initializeRoomSocket (Long roomId , String sessionId , UserPrincipal principal ) {
118120
119121 Room room = findRoom (roomId );
120122
@@ -140,23 +142,19 @@ public void initializeRoomSocket(
140142 Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
141143
142144 GameSettingResponse gameSettingResponse =
143- toGameSettingResponse (room .getGameSetting (), quiz );
145+ toGameSettingResponse (room .getGameSetting (), quiz );
144146
145147 PlayerListResponse playerListResponse = toPlayerListResponse (room );
146148
147149 SystemNoticeResponse systemNoticeResponse =
148- ofPlayerEvent (player .getNickname (), RoomEventType .ENTER );
150+ ofPlayerEvent (player .getNickname (), RoomEventType .ENTER );
149151
150152 String destination = getDestination (roomId );
151153
152- messageSender .send (
153- destination , MessageType .ROOM_SETTING , roomSettingResponse );
154- messageSender .send (
155- destination , MessageType .GAME_SETTING , gameSettingResponse );
156- messageSender .send (
157- destination , MessageType .PLAYER_LIST , playerListResponse );
158- messageSender .send (
159- destination , MessageType .SYSTEM_NOTICE , systemNoticeResponse );
154+ messageSender .send (destination , MessageType .ROOM_SETTING , roomSettingResponse );
155+ messageSender .send (destination , MessageType .GAME_SETTING , gameSettingResponse );
156+ messageSender .send (destination , MessageType .PLAYER_LIST , playerListResponse );
157+ messageSender .send (destination , MessageType .SYSTEM_NOTICE , systemNoticeResponse );
160158 }
161159
162160 public void exitRoom (Long roomId , String sessionId , UserPrincipal principal ) {
@@ -183,25 +181,22 @@ public void exitRoom(Long roomId, String sessionId, UserPrincipal principal) {
183181 removePlayer (room , sessionId , removePlayer );
184182
185183 SystemNoticeResponse systemNoticeResponse =
186- ofPlayerEvent (removePlayer .nickname , RoomEventType .EXIT );
184+ ofPlayerEvent (removePlayer .nickname , RoomEventType .EXIT );
187185
188186 PlayerListResponse playerListResponse = toPlayerListResponse (room );
189187
190188 String destination = getDestination (roomId );
191189
192- messageSender .send (
193- destination , MessageType .PLAYER_LIST , playerListResponse );
194- messageSender .send (
195- destination , MessageType .SYSTEM_NOTICE , systemNoticeResponse );
196-
190+ messageSender .send (destination , MessageType .PLAYER_LIST , playerListResponse );
191+ messageSender .send (destination , MessageType .SYSTEM_NOTICE , systemNoticeResponse );
197192 }
198193 }
199194
200195 public void handlePlayerReady (Long roomId , String sessionId ) {
201196 Player player =
202- roomRepository
203- .findPlayerInRoomBySessionId (roomId , sessionId )
204- .orElseThrow (() -> new CustomException (RoomErrorCode .PLAYER_NOT_FOUND ));
197+ roomRepository
198+ .findPlayerInRoomBySessionId (roomId , sessionId )
199+ .orElseThrow (() -> new CustomException (RoomErrorCode .PLAYER_NOT_FOUND ));
205200
206201 player .toggleReady ();
207202
@@ -210,21 +205,20 @@ public void handlePlayerReady(Long roomId, String sessionId) {
210205 String destination = getDestination (roomId );
211206
212207 messageSender .send (destination , MessageType .PLAYER_LIST , toPlayerListResponse (room ));
213-
214208 }
215209
216210 public RoomListResponse getAllRooms () {
217211 List <Room > rooms = roomRepository .findAll ();
218212 List <RoomResponse > roomResponses =
219- rooms .stream ()
220- .map (
221- room -> {
222- Long quizId = room .getGameSetting ().getQuizId ();
223- Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
224-
225- return toRoomResponse (room , quiz );
226- })
227- .toList ();
213+ rooms .stream ()
214+ .map (
215+ room -> {
216+ Long quizId = room .getGameSetting ().getQuizId ();
217+ Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
218+
219+ return toRoomResponse (room , quiz );
220+ })
221+ .toList ();
228222 return new RoomListResponse (roomResponses );
229223 }
230224
@@ -248,12 +242,14 @@ public void chat(Long roomId, String sessionId, ChatMessage chatMessage) {
248242 room .increasePlayerCorrectCount (sessionId );
249243
250244 messageSender .send (
251- destination , MessageType .QUESTION_RESULT ,
252- toQuestionResultResponse (currentQuestion .getId (), chatMessage , answer ));
245+ destination ,
246+ MessageType .QUESTION_RESULT ,
247+ toQuestionResultResponse (currentQuestion .getId (), chatMessage , answer ));
253248 messageSender .send (destination , MessageType .RANK_UPDATE , toRankUpdateResponse (room ));
254249 messageSender .send (
255- destination , MessageType .SYSTEM_NOTICE ,
256- ofPlayerEvent (chatMessage .nickname (), RoomEventType .ENTER ));
250+ destination ,
251+ MessageType .SYSTEM_NOTICE ,
252+ ofPlayerEvent (chatMessage .nickname (), RoomEventType .ENTER ));
257253 }
258254 }
259255
@@ -276,8 +272,8 @@ private Player createPlayer() {
276272
277273 private Room findRoom (Long roomId ) {
278274 return roomRepository
279- .findRoom (roomId )
280- .orElseThrow (() -> new CustomException (RoomErrorCode .ROOM_NOT_FOUND ));
275+ .findRoom (roomId )
276+ .orElseThrow (() -> new CustomException (RoomErrorCode .ROOM_NOT_FOUND ));
281277 }
282278
283279 private boolean isLastPlayer (Room room , String sessionId ) {
@@ -296,14 +292,14 @@ private void changeHost(Room room, String hostSessionId) {
296292 Map <String , Player > playerSessionMap = room .getPlayerSessionMap ();
297293
298294 Optional <String > nextHostSessionId =
299- playerSessionMap .keySet ().stream ()
300- .filter (key -> !key .equals (hostSessionId ))
301- .findFirst ();
295+ playerSessionMap .keySet ().stream ()
296+ .filter (key -> !key .equals (hostSessionId ))
297+ .findFirst ();
302298
303299 Player nextHost =
304- playerSessionMap .get (
305- nextHostSessionId .orElseThrow (
306- () -> new CustomException (RoomErrorCode .SOCKET_SESSION_NOT_FOUND )));
300+ playerSessionMap .get (
301+ nextHostSessionId .orElseThrow (
302+ () -> new CustomException (RoomErrorCode .SOCKET_SESSION_NOT_FOUND )));
307303
308304 room .updateHost (nextHost );
309305 log .info ("user_id:{} 방장 변경 완료 " , nextHost .getId ());
@@ -314,7 +310,6 @@ private void removePlayer(Room room, String sessionId, Player removePlayer) {
314310 room .removeSessionId (sessionId );
315311 }
316312
317-
318313 private String getDestination (Long roomId ) {
319314 return "/sub/room/" + roomId ;
320315 }
0 commit comments