4545import io .f1 .backend .global .exception .CustomException ;
4646import io .f1 .backend .global .exception .errorcode .RoomErrorCode ;
4747import io .f1 .backend .global .exception .errorcode .UserErrorCode ;
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
@@ -98,7 +101,6 @@ public RoomCreateResponse saveRoom(RoomCreateRequest request) {
98101 eventPublisher .publishEvent (new RoomCreatedEvent (room , quiz ));
99102
100103 return new RoomCreateResponse (newId );
101-
102104 }
103105
104106 public void enterRoom (RoomValidationRequest request ) {
@@ -131,7 +133,7 @@ public void enterRoom(RoomValidationRequest request) {
131133 }
132134
133135 if (room .getRoomSetting ().locked ()
134- && !room .getRoomSetting ().password ().equals (request .password ())) {
136+ && !room .getRoomSetting ().password ().equals (request .password ())) {
135137 throw new CustomException (RoomErrorCode .WRONG_PASSWORD );
136138 }
137139
@@ -143,14 +145,14 @@ private void exitIfInAnotherRoom(Room room, Long userId) {
143145
144146 Long joinedRoomId = userRoomRepository .getRoomId (userId );
145147
146- if (joinedRoomId != null && !room .getId ().equals (joinedRoomId )) {
148+ if (joinedRoomId != null && !room .getId ().equals (joinedRoomId )) {
147149
148- if (room .isPlaying ()){
149- changeConnectedStatus (userId , ConnectionState .DISCONNECTED );
150- } else {
151- exitRoom (joinedRoomId ,getCurrentUserPrincipal ());
152- }
153- }
150+ if (room .isPlaying ()) {
151+ changeConnectedStatus (userId , ConnectionState .DISCONNECTED );
152+ } else {
153+ exitRoom (joinedRoomId , getCurrentUserPrincipal ());
154+ }
155+ }
154156 }
155157
156158 public void initializeRoomSocket (Long roomId , UserPrincipal principal ) {
@@ -163,10 +165,10 @@ public void initializeRoomSocket(Long roomId, UserPrincipal principal) {
163165 }
164166
165167 /* 재연결 */
166- if (room .getPlayerState (userId ).equals (ConnectionState .DISCONNECTED )) {
167- changeConnectedStatus (userId ,ConnectionState .CONNECTED );
168+ if (room .getPlayerState (userId ).equals (ConnectionState .DISCONNECTED )) {
169+ changeConnectedStatus (userId , ConnectionState .CONNECTED );
168170 cancelTask (userId );
169- reconnectSendResponse (roomId ,principal );
171+ reconnectSendResponse (roomId , principal );
170172 return ;
171173 }
172174
@@ -178,19 +180,19 @@ public void initializeRoomSocket(Long roomId, UserPrincipal principal) {
178180 Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
179181
180182 GameSettingResponse gameSettingResponse =
181- toGameSettingResponse (room .getGameSetting (), quiz );
183+ toGameSettingResponse (room .getGameSetting (), quiz );
182184
183185 PlayerListResponse playerListResponse = toPlayerListResponse (room );
184186
185187 SystemNoticeResponse systemNoticeResponse =
186- ofPlayerEvent (player .getNickname (), RoomEventType .ENTER );
188+ ofPlayerEvent (player .getNickname (), RoomEventType .ENTER );
187189
188190 String destination = getDestination (roomId );
189191
190192 userRoomRepository .addUser (player , room );
191193
192194 messageSender .sendPersonal (
193- getUserDestination (), MessageType .GAME_SETTING , gameSettingResponse , principal );
195+ getUserDestination (), MessageType .GAME_SETTING , gameSettingResponse , principal );
194196
195197 messageSender .sendBroadcast (destination , MessageType .ROOM_SETTING , roomSettingResponse );
196198 messageSender .sendBroadcast (destination , MessageType .PLAYER_LIST , playerListResponse );
@@ -215,66 +217,65 @@ public void exitRoom(Long roomId, UserPrincipal principal) {
215217 cleanRoom (room , removePlayer );
216218
217219 messageSender .sendPersonal (
218- getUserDestination (),
219- MessageType .EXIT_SUCCESS ,
220- new ExitSuccessResponse (true ),
221- principal );
220+ getUserDestination (),
221+ MessageType .EXIT_SUCCESS ,
222+ new ExitSuccessResponse (true ),
223+ principal );
222224
223225 SystemNoticeResponse systemNoticeResponse =
224- ofPlayerEvent (removePlayer .nickname , RoomEventType .EXIT );
226+ ofPlayerEvent (removePlayer .nickname , RoomEventType .EXIT );
225227
226228 PlayerListResponse playerListResponse = toPlayerListResponse (room );
227229
228230 messageSender .sendBroadcast (destination , MessageType .PLAYER_LIST , playerListResponse );
229231 messageSender .sendBroadcast (
230- destination , MessageType .SYSTEM_NOTICE , systemNoticeResponse );
232+ destination , MessageType .SYSTEM_NOTICE , systemNoticeResponse );
231233 }
232234 }
233235
234236 public RoomListResponse getAllRooms () {
235237 List <Room > rooms = roomRepository .findAll ();
236238 List <RoomResponse > roomResponses =
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 ();
239+ rooms .stream ()
240+ .map (
241+ room -> {
242+ Long quizId = room .getGameSetting ().getQuizId ();
243+ Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
244+
245+ return toRoomResponse (room , quiz );
246+ })
247+ .toList ();
246248 return new RoomListResponse (roomResponses );
247249 }
248250
249- public void reconnectSendResponse (
250- Long roomId , UserPrincipal principal ) {
251+ public void reconnectSendResponse (Long roomId , UserPrincipal principal ) {
251252 Room room = findRoom (roomId );
252253
253254 String destination = getDestination (roomId );
254255 String userDestination = getUserDestination ();
255256
256257 messageSender .sendBroadcast (
257- destination ,
258- MessageType .SYSTEM_NOTICE ,
259- ofPlayerEvent (principal .getUserNickname (), RoomEventType .RECONNECT ));
258+ destination ,
259+ MessageType .SYSTEM_NOTICE ,
260+ ofPlayerEvent (principal .getUserNickname (), RoomEventType .RECONNECT ));
260261
261262 if (room .isPlaying ()) {
262263 messageSender .sendPersonal (
263- userDestination ,
264- MessageType .SYSTEM_NOTICE ,
265- ofPlayerEvent (
266- principal .getUserNickname (), RoomEventType .RECONNECT_PRIVATE_NOTICE ),
267- principal );
264+ userDestination ,
265+ MessageType .SYSTEM_NOTICE ,
266+ ofPlayerEvent (
267+ principal .getUserNickname (), RoomEventType .RECONNECT_PRIVATE_NOTICE ),
268+ principal );
268269 messageSender .sendPersonal (
269- userDestination ,
270- MessageType .RANK_UPDATE ,
271- toRankUpdateResponse (room ),
272- principal );
270+ userDestination ,
271+ MessageType .RANK_UPDATE ,
272+ toRankUpdateResponse (room ),
273+ principal );
273274 messageSender .sendPersonal (
274- userDestination ,
275- MessageType .GAME_START ,
276- toGameStartResponse (room .getQuestions ()),
277- principal );
275+ userDestination ,
276+ MessageType .GAME_START ,
277+ toGameStartResponse (room .getQuestions ()),
278+ principal );
278279 } else {
279280 RoomSettingResponse roomSettingResponse = toRoomSettingResponse (room );
280281
@@ -283,16 +284,16 @@ public void reconnectSendResponse(
283284 Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
284285
285286 GameSettingResponse gameSettingResponse =
286- toGameSettingResponse (room .getGameSetting (), quiz );
287+ toGameSettingResponse (room .getGameSetting (), quiz );
287288
288289 PlayerListResponse playerListResponse = toPlayerListResponse (room );
289290
290291 messageSender .sendPersonal (
291- userDestination , MessageType .ROOM_SETTING , roomSettingResponse , principal );
292+ userDestination , MessageType .ROOM_SETTING , roomSettingResponse , principal );
292293 messageSender .sendPersonal (
293- userDestination , MessageType .PLAYER_LIST , playerListResponse , principal );
294+ userDestination , MessageType .PLAYER_LIST , playerListResponse , principal );
294295 messageSender .sendPersonal (
295- userDestination , MessageType .GAME_SETTING , gameSettingResponse , principal );
296+ userDestination , MessageType .GAME_SETTING , gameSettingResponse , principal );
296297 }
297298 }
298299
@@ -305,15 +306,15 @@ public Long changeConnectedStatus(Long userId, ConnectionState newState) {
305306 return roomId ;
306307 }
307308
308- public void cancelTask (Long userId ){
309+ public void cancelTask (Long userId ) {
309310 disconnectTasks .cancelDisconnectTask (userId );
310311 }
311312
312313 public void exitIfNotPlaying (Long roomId , UserPrincipal principal ) {
313314 Room room = findRoom (roomId );
314315 if (room .isPlaying ()) {
315316 removeUserRepository (principal .getUserId (), roomId );
316- }else {
317+ } else {
317318 exitRoom (roomId , principal );
318319 }
319320 }
@@ -328,8 +329,8 @@ private Player createPlayer() {
328329
329330 public Room findRoom (Long roomId ) {
330331 return roomRepository
331- .findRoom (roomId )
332- .orElseThrow (() -> new CustomException (RoomErrorCode .ROOM_NOT_FOUND ));
332+ .findRoom (roomId )
333+ .orElseThrow (() -> new CustomException (RoomErrorCode .ROOM_NOT_FOUND ));
333334 }
334335
335336 private void removeRoom (Room room ) {
@@ -342,14 +343,15 @@ private void removeRoom(Room room) {
342343 private void changeHost (Room room , Player host ) {
343344 Map <Long , Player > playerMap = room .getPlayerMap ();
344345
345- Optional <Player > nextHost = 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 ();
346+ Optional <Player > nextHost =
347+ playerMap .entrySet ().stream ()
348+ .filter (entry -> !entry .getKey ().equals (host .getId ()))
349+ .filter (entry -> entry .getValue ().getState () == ConnectionState .CONNECTED )
350+ .map (Map .Entry ::getValue )
351+ .findFirst ();
350352
351353 room .updateHost (
352- nextHost .orElseThrow (() -> new CustomException (RoomErrorCode .PLAYER_NOT_FOUND )));
354+ nextHost .orElseThrow (() -> new CustomException (RoomErrorCode .PLAYER_NOT_FOUND )));
353355 }
354356
355357 private String getUserDestination () {
@@ -369,12 +371,12 @@ public void exitRoomForDisconnectedPlayer(Long roomId, Player player) {
369371 String destination = getDestination (roomId );
370372
371373 SystemNoticeResponse systemNoticeResponse =
372- ofPlayerEvent (player .nickname , RoomEventType .EXIT );
374+ ofPlayerEvent (player .nickname , RoomEventType .EXIT );
373375
374376 messageSender .sendBroadcast (
375- destination , MessageType .SYSTEM_NOTICE , systemNoticeResponse );
377+ destination , MessageType .SYSTEM_NOTICE , systemNoticeResponse );
376378 messageSender .sendBroadcast (
377- destination , MessageType .PLAYER_LIST , toPlayerListResponse (room ));
379+ destination , MessageType .PLAYER_LIST , toPlayerListResponse (room ));
378380 }
379381 }
380382
@@ -417,7 +419,7 @@ public void removeUserRepository(Long userId, Long roomId) {
417419 userRoomRepository .removeUser (userId , roomId );
418420 }
419421
420- public boolean isUserInAnyRoom (Long userId ){
422+ public boolean isUserInAnyRoom (Long userId ) {
421423 return userRoomRepository .isUserInAnyRoom (userId );
422424 }
423425}
0 commit comments