4444import io .f1 .backend .global .exception .CustomException ;
4545import io .f1 .backend .global .exception .errorcode .RoomErrorCode ;
4646import io .f1 .backend .global .exception .errorcode .UserErrorCode ;
47+
48+ import lombok .RequiredArgsConstructor ;
49+ import lombok .extern .slf4j .Slf4j ;
50+
51+ import org .springframework .context .ApplicationEventPublisher ;
52+ import org .springframework .stereotype .Service ;
53+
4754import java .util .List ;
4855import java .util .Map ;
4956import java .util .Optional ;
5057import java .util .concurrent .ConcurrentHashMap ;
5158import java .util .concurrent .atomic .AtomicLong ;
52- import lombok .RequiredArgsConstructor ;
53- import lombok .extern .slf4j .Slf4j ;
54- import org .springframework .context .ApplicationEventPublisher ;
55- import org .springframework .stereotype .Service ;
5659
5760@ Slf4j
5861@ Service
@@ -129,7 +132,7 @@ public void enterRoom(RoomValidationRequest request) {
129132 }
130133
131134 if (room .getRoomSetting ().locked ()
132- && !room .getRoomSetting ().password ().equals (request .password ())) {
135+ && !room .getRoomSetting ().password ().equals (request .password ())) {
133136 throw new CustomException (RoomErrorCode .WRONG_PASSWORD );
134137 }
135138
@@ -175,19 +178,19 @@ public void initializeRoomSocket(Long roomId, UserPrincipal principal) {
175178 Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
176179
177180 GameSettingResponse gameSettingResponse =
178- toGameSettingResponse (room .getGameSetting (), quiz );
181+ toGameSettingResponse (room .getGameSetting (), quiz );
179182
180183 PlayerListResponse playerListResponse = toPlayerListResponse (room );
181184
182185 SystemNoticeResponse systemNoticeResponse =
183- ofPlayerEvent (player .getNickname (), RoomEventType .ENTER );
186+ ofPlayerEvent (player .getNickname (), RoomEventType .ENTER );
184187
185188 String destination = getDestination (roomId );
186189
187190 userRoomRepository .addUser (player , room );
188191
189192 messageSender .sendPersonal (
190- getUserDestination (), MessageType .GAME_SETTING , gameSettingResponse , principal );
193+ getUserDestination (), MessageType .GAME_SETTING , gameSettingResponse , principal );
191194
192195 messageSender .sendBroadcast (destination , MessageType .ROOM_SETTING , roomSettingResponse );
193196 messageSender .sendBroadcast (destination , MessageType .PLAYER_LIST , playerListResponse );
@@ -212,34 +215,34 @@ public void exitRoom(Long roomId, UserPrincipal principal) {
212215 cleanRoom (room , removePlayer );
213216
214217 messageSender .sendPersonal (
215- getUserDestination (),
216- MessageType .EXIT_SUCCESS ,
217- new ExitSuccessResponse (true ),
218- principal );
218+ getUserDestination (),
219+ MessageType .EXIT_SUCCESS ,
220+ new ExitSuccessResponse (true ),
221+ principal );
219222
220223 SystemNoticeResponse systemNoticeResponse =
221- ofPlayerEvent (removePlayer .nickname , RoomEventType .EXIT );
224+ ofPlayerEvent (removePlayer .nickname , RoomEventType .EXIT );
222225
223226 PlayerListResponse playerListResponse = toPlayerListResponse (room );
224227
225228 messageSender .sendBroadcast (destination , MessageType .PLAYER_LIST , playerListResponse );
226229 messageSender .sendBroadcast (
227- destination , MessageType .SYSTEM_NOTICE , systemNoticeResponse );
230+ destination , MessageType .SYSTEM_NOTICE , systemNoticeResponse );
228231 }
229232 }
230233
231234 public RoomListResponse getAllRooms () {
232235 List <Room > rooms = roomRepository .findAll ();
233236 List <RoomResponse > roomResponses =
234- rooms .stream ()
235- .map (
236- room -> {
237- Long quizId = room .getGameSetting ().getQuizId ();
238- Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
239-
240- return toRoomResponse (room , quiz );
241- })
242- .toList ();
237+ rooms .stream ()
238+ .map (
239+ room -> {
240+ Long quizId = room .getGameSetting ().getQuizId ();
241+ Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
242+
243+ return toRoomResponse (room , quiz );
244+ })
245+ .toList ();
243246 return new RoomListResponse (roomResponses );
244247 }
245248
@@ -250,27 +253,27 @@ public void reconnectSendResponse(Long roomId, UserPrincipal principal) {
250253 String userDestination = getUserDestination ();
251254
252255 messageSender .sendBroadcast (
253- destination ,
254- MessageType .SYSTEM_NOTICE ,
255- ofPlayerEvent (principal .getUserNickname (), RoomEventType .RECONNECT ));
256+ destination ,
257+ MessageType .SYSTEM_NOTICE ,
258+ ofPlayerEvent (principal .getUserNickname (), RoomEventType .RECONNECT ));
256259
257260 if (room .isPlaying ()) {
258261 messageSender .sendPersonal (
259- userDestination ,
260- MessageType .SYSTEM_NOTICE ,
261- ofPlayerEvent (
262- principal .getUserNickname (), RoomEventType .RECONNECT_PRIVATE_NOTICE ),
263- principal );
262+ userDestination ,
263+ MessageType .SYSTEM_NOTICE ,
264+ ofPlayerEvent (
265+ principal .getUserNickname (), RoomEventType .RECONNECT_PRIVATE_NOTICE ),
266+ principal );
264267 messageSender .sendPersonal (
265- userDestination ,
266- MessageType .RANK_UPDATE ,
267- toRankUpdateResponse (room ),
268- principal );
268+ userDestination ,
269+ MessageType .RANK_UPDATE ,
270+ toRankUpdateResponse (room ),
271+ principal );
269272 messageSender .sendPersonal (
270- userDestination ,
271- MessageType .GAME_START ,
272- toGameStartResponse (room .getQuestions ()),
273- principal );
273+ userDestination ,
274+ MessageType .GAME_START ,
275+ toGameStartResponse (room .getQuestions ()),
276+ principal );
274277 } else {
275278 RoomSettingResponse roomSettingResponse = toRoomSettingResponse (room );
276279
@@ -279,16 +282,16 @@ public void reconnectSendResponse(Long roomId, UserPrincipal principal) {
279282 Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
280283
281284 GameSettingResponse gameSettingResponse =
282- toGameSettingResponse (room .getGameSetting (), quiz );
285+ toGameSettingResponse (room .getGameSetting (), quiz );
283286
284287 PlayerListResponse playerListResponse = toPlayerListResponse (room );
285288
286289 messageSender .sendPersonal (
287- userDestination , MessageType .ROOM_SETTING , roomSettingResponse , principal );
290+ userDestination , MessageType .ROOM_SETTING , roomSettingResponse , principal );
288291 messageSender .sendPersonal (
289- userDestination , MessageType .PLAYER_LIST , playerListResponse , principal );
292+ userDestination , MessageType .PLAYER_LIST , playerListResponse , principal );
290293 messageSender .sendPersonal (
291- userDestination , MessageType .GAME_SETTING , gameSettingResponse , principal );
294+ userDestination , MessageType .GAME_SETTING , gameSettingResponse , principal );
292295 }
293296 }
294297
@@ -324,8 +327,8 @@ private Player createPlayer() {
324327
325328 public Room findRoom (Long roomId ) {
326329 return roomRepository
327- .findRoom (roomId )
328- .orElseThrow (() -> new CustomException (RoomErrorCode .ROOM_NOT_FOUND ));
330+ .findRoom (roomId )
331+ .orElseThrow (() -> new CustomException (RoomErrorCode .ROOM_NOT_FOUND ));
329332 }
330333
331334 private void removeRoom (Room room ) {
@@ -339,14 +342,14 @@ private void changeHost(Room room, Player host) {
339342 Map <Long , Player > playerMap = room .getPlayerMap ();
340343
341344 Optional <Player > nextHost =
342- playerMap .entrySet ().stream ()
343- .filter (entry -> !entry .getKey ().equals (host .getId ()))
344- .filter (entry -> entry .getValue ().getState () == ConnectionState .CONNECTED )
345- .map (Map .Entry ::getValue )
346- .findFirst ();
345+ playerMap .entrySet ().stream ()
346+ .filter (entry -> !entry .getKey ().equals (host .getId ()))
347+ .filter (entry -> entry .getValue ().getState () == ConnectionState .CONNECTED )
348+ .map (Map .Entry ::getValue )
349+ .findFirst ();
347350
348351 room .updateHost (
349- nextHost .orElseThrow (() -> new CustomException (RoomErrorCode .PLAYER_NOT_FOUND )));
352+ nextHost .orElseThrow (() -> new CustomException (RoomErrorCode .PLAYER_NOT_FOUND )));
350353 }
351354
352355 private String getUserDestination () {
@@ -366,12 +369,12 @@ public void exitRoomForDisconnectedPlayer(Long roomId, Player player) {
366369 String destination = getDestination (roomId );
367370
368371 SystemNoticeResponse systemNoticeResponse =
369- ofPlayerEvent (player .nickname , RoomEventType .EXIT );
372+ ofPlayerEvent (player .nickname , RoomEventType .EXIT );
370373
371374 messageSender .sendBroadcast (
372- destination , MessageType .SYSTEM_NOTICE , systemNoticeResponse );
375+ destination , MessageType .SYSTEM_NOTICE , systemNoticeResponse );
373376 messageSender .sendBroadcast (
374- destination , MessageType .PLAYER_LIST , toPlayerListResponse (room ));
377+ destination , MessageType .PLAYER_LIST , toPlayerListResponse (room ));
375378 }
376379 }
377380
0 commit comments