99import static io .f1 .backend .domain .game .mapper .RoomMapper .toRoomSetting ;
1010import static io .f1 .backend .domain .game .mapper .RoomMapper .toRoomSettingResponse ;
1111import static io .f1 .backend .domain .game .websocket .WebSocketUtils .getDestination ;
12+ import static io .f1 .backend .domain .game .websocket .WebSocketUtils .getUserDestination ;
1213import static io .f1 .backend .domain .quiz .mapper .QuizMapper .toGameStartResponse ;
1314import static io .f1 .backend .global .security .util .SecurityUtils .getCurrentUserId ;
1415import static io .f1 .backend .global .security .util .SecurityUtils .getCurrentUserNickname ;
4445import io .f1 .backend .global .exception .CustomException ;
4546import io .f1 .backend .global .exception .errorcode .RoomErrorCode ;
4647import 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-
5448import java .util .List ;
5549import java .util .Map ;
5650import java .util .Optional ;
5751import java .util .concurrent .ConcurrentHashMap ;
5852import 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 ;
5957
6058@ Slf4j
6159@ Service
@@ -93,7 +91,7 @@ public RoomCreateResponse saveRoom(RoomCreateRequest request) {
9391 roomRepository .saveRoom (room );
9492
9593 /* 다른 방 접속 시 기존 방은 exit 처리 - 탭 동시 로그인 시 (disconnected 리스너 작동x) */
96- exitIfInAnotherRoom (room , host . getId ());
94+ exitIfInAnotherRoom (room , getCurrentUserPrincipal ());
9795
9896 eventPublisher .publishEvent (new RoomCreatedEvent (room , quiz ));
9997
@@ -112,7 +110,7 @@ public void enterRoom(RoomValidationRequest request) {
112110 Long userId = getCurrentUserId ();
113111
114112 /* 다른 방 접속 시 기존 방은 exit 처리 - 탭 동시 로그인 시 (disconnected 리스너 작동x) */
115- exitIfInAnotherRoom (room , userId );
113+ exitIfInAnotherRoom (room , getCurrentUserPrincipal () );
116114
117115 /* reconnect */
118116 if (room .hasPlayer (userId )) {
@@ -137,16 +135,12 @@ public void enterRoom(RoomValidationRequest request) {
137135 }
138136 }
139137
140- private void exitIfInAnotherRoom (Room room , Long userId ) {
141-
142- Long joinedRoomId = userRoomRepository . getRoomId (userId );
138+ private void exitIfInAnotherRoom (Room room ,UserPrincipal userPrincipal ) {
139+ Long userId = userPrincipal . getUserId ();
140+ Long joinedRoomId = getRoomIdByUserId (userId );
143141
144142 if (joinedRoomId != null && !room .isSameRoom (joinedRoomId )) {
145- if (room .isPlaying ()) {
146- changeConnectedStatus (userId , ConnectionState .DISCONNECTED );
147- } else {
148- exitRoom (joinedRoomId , getCurrentUserPrincipal ());
149- }
143+ disconnectOrExitRoom (joinedRoomId , userPrincipal );
150144 }
151145 }
152146
@@ -161,7 +155,7 @@ public void initializeRoomSocket(Long roomId, UserPrincipal principal) {
161155
162156 /* 재연결 */
163157 if (room .isPlayerInState (userId , ConnectionState .DISCONNECTED )) {
164- changeConnectedStatus (userId , ConnectionState .CONNECTED );
158+ changeConnectedStatus (roomId , userId , ConnectionState .CONNECTED );
165159 cancelTask (userId );
166160 reconnectSendResponse (roomId , principal );
167161 return ;
@@ -187,7 +181,7 @@ public void initializeRoomSocket(Long roomId, UserPrincipal principal) {
187181 userRoomRepository .addUser (player , room );
188182
189183 messageSender .sendPersonal (
190- getUserDestination (), MessageType .GAME_SETTING , gameSettingResponse , principal );
184+ getUserDestination (), MessageType .GAME_SETTING , gameSettingResponse , principal . getName () );
191185
192186 messageSender .sendBroadcast (destination , MessageType .ROOM_SETTING , roomSettingResponse );
193187 messageSender .sendBroadcast (destination , MessageType .PLAYER_LIST , playerListResponse );
@@ -215,7 +209,7 @@ public void exitRoom(Long roomId, UserPrincipal principal) {
215209 getUserDestination (),
216210 MessageType .EXIT_SUCCESS ,
217211 new ExitSuccessResponse (true ),
218- principal );
212+ principal . getName () );
219213
220214 SystemNoticeResponse systemNoticeResponse =
221215 ofPlayerEvent (removePlayer .nickname , RoomEventType .EXIT );
@@ -260,17 +254,17 @@ public void reconnectSendResponse(Long roomId, UserPrincipal principal) {
260254 MessageType .SYSTEM_NOTICE ,
261255 ofPlayerEvent (
262256 principal .getUserNickname (), RoomEventType .RECONNECT_PRIVATE_NOTICE ),
263- principal );
257+ principal . getName () );
264258 messageSender .sendPersonal (
265259 userDestination ,
266260 MessageType .RANK_UPDATE ,
267261 toRankUpdateResponse (room ),
268- principal );
262+ principal . getName () );
269263 messageSender .sendPersonal (
270264 userDestination ,
271265 MessageType .GAME_START ,
272266 toGameStartResponse (room .getQuestions ()),
273- principal );
267+ principal . getName () );
274268 } else {
275269 RoomSettingResponse roomSettingResponse = toRoomSettingResponse (room );
276270
@@ -284,33 +278,30 @@ public void reconnectSendResponse(Long roomId, UserPrincipal principal) {
284278 PlayerListResponse playerListResponse = toPlayerListResponse (room );
285279
286280 messageSender .sendPersonal (
287- userDestination , MessageType .ROOM_SETTING , roomSettingResponse , principal );
281+ userDestination , MessageType .ROOM_SETTING , roomSettingResponse , principal . getName () );
288282 messageSender .sendPersonal (
289- userDestination , MessageType .PLAYER_LIST , playerListResponse , principal );
283+ userDestination , MessageType .PLAYER_LIST , playerListResponse , principal . getName () );
290284 messageSender .sendPersonal (
291- userDestination , MessageType .GAME_SETTING , gameSettingResponse , principal );
285+ userDestination , MessageType .GAME_SETTING , gameSettingResponse , principal . getName () );
292286 }
293287 }
294288
295- public Long changeConnectedStatus (Long userId , ConnectionState newState ) {
296- Long roomId = userRoomRepository .getRoomId (userId );
289+ public void changeConnectedStatus (Long roomId ,Long userId , ConnectionState newState ) {
297290 Room room = findRoom (roomId );
298-
299291 room .updatePlayerConnectionState (userId , newState );
300-
301- return roomId ;
302292 }
303293
304294 public void cancelTask (Long userId ) {
305295 disconnectTasks .cancelDisconnectTask (userId );
306296 }
307297
308- public void exitIfNotPlaying (Long roomId , UserPrincipal principal ) {
298+ public void disconnectOrExitRoom (Long roomId , UserPrincipal principal ) {
309299 Room room = findRoom (roomId );
310300 if (room .isPlaying ()) {
301+ changeConnectedStatus (room .getId (), principal .getUserId (), ConnectionState .DISCONNECTED );
311302 removeUserRepository (principal .getUserId (), roomId );
312303 } else {
313- exitRoom (roomId , principal );
304+ exitRoom (room . getId () , principal );
314305 }
315306 }
316307
@@ -349,10 +340,6 @@ private void changeHost(Room room, Player host) {
349340 nextHost .orElseThrow (() -> new CustomException (RoomErrorCode .PLAYER_NOT_FOUND )));
350341 }
351342
352- private String getUserDestination () {
353- return "/queue" ;
354- }
355-
356343 public void exitRoomForDisconnectedPlayer (Long roomId , Player player ) {
357344
358345 Object lock = roomLocks .computeIfAbsent (roomId , k -> new Object ());
@@ -417,4 +404,8 @@ public void removeUserRepository(Long userId, Long roomId) {
417404 public boolean isUserInAnyRoom (Long userId ) {
418405 return userRoomRepository .isUserInAnyRoom (userId );
419406 }
407+
408+ public Long getRoomIdByUserId (Long userId ) {
409+ return userRoomRepository .getRoomId (userId );
410+ }
420411}
0 commit comments