Skip to content

Commit 1b54ded

Browse files
committed
Merge remote-tracking branch 'origin/feat/34' into feat/34
2 parents 31d1f76 + 147ab05 commit 1b54ded

File tree

4 files changed

+54
-59
lines changed

4 files changed

+54
-59
lines changed

backend/src/main/java/io/f1/backend/domain/game/app/RoomService.java

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void enterRoom(RoomValidationRequest request) {
119119
}
120120

121121
if (room.getRoomSetting().locked()
122-
&& !room.getRoomSetting().password().equals(request.password())) {
122+
&& !room.getRoomSetting().password().equals(request.password())) {
123123
throw new CustomException(RoomErrorCode.WRONG_PASSWORD);
124124
}
125125

@@ -141,12 +141,12 @@ public void initializeRoomSocket(Long roomId, String sessionId, UserPrincipal pr
141141
Quiz quiz = quizService.getQuizWithQuestionsById(quizId);
142142

143143
GameSettingResponse gameSettingResponse =
144-
toGameSettingResponse(room.getGameSetting(), quiz);
144+
toGameSettingResponse(room.getGameSetting(), quiz);
145145

146146
PlayerListResponse playerListResponse = toPlayerListResponse(room);
147147

148148
SystemNoticeResponse systemNoticeResponse =
149-
ofPlayerEvent(player.getNickname(), RoomEventType.ENTER);
149+
ofPlayerEvent(player.getNickname(), RoomEventType.ENTER);
150150

151151
String destination = getDestination(roomId);
152152

@@ -182,7 +182,7 @@ public void exitRoom(Long roomId, String sessionId, UserPrincipal principal) {
182182
removePlayer(room, sessionId, removePlayer);
183183

184184
SystemNoticeResponse systemNoticeResponse =
185-
ofPlayerEvent(removePlayer.nickname, RoomEventType.EXIT);
185+
ofPlayerEvent(removePlayer.nickname, RoomEventType.EXIT);
186186

187187
PlayerListResponse playerListResponse = toPlayerListResponse(room);
188188

@@ -193,9 +193,9 @@ public void exitRoom(Long roomId, String sessionId, UserPrincipal principal) {
193193

194194
public void handlePlayerReady(Long roomId, String sessionId) {
195195
Player player =
196-
roomRepository
197-
.findPlayerInRoomBySessionId(roomId, sessionId)
198-
.orElseThrow(() -> new CustomException(RoomErrorCode.PLAYER_NOT_FOUND));
196+
roomRepository
197+
.findPlayerInRoomBySessionId(roomId, sessionId)
198+
.orElseThrow(() -> new CustomException(RoomErrorCode.PLAYER_NOT_FOUND));
199199

200200
player.toggleReady();
201201

@@ -209,15 +209,15 @@ public void handlePlayerReady(Long roomId, String sessionId) {
209209
public RoomListResponse getAllRooms() {
210210
List<Room> rooms = roomRepository.findAll();
211211
List<RoomResponse> roomResponses =
212-
rooms.stream()
213-
.map(
214-
room -> {
215-
Long quizId = room.getGameSetting().getQuizId();
216-
Quiz quiz = quizService.getQuizWithQuestionsById(quizId);
217-
218-
return toRoomResponse(room, quiz);
219-
})
220-
.toList();
212+
rooms.stream()
213+
.map(
214+
room -> {
215+
Long quizId = room.getGameSetting().getQuizId();
216+
Quiz quiz = quizService.getQuizWithQuestionsById(quizId);
217+
218+
return toRoomResponse(room, quiz);
219+
})
220+
.toList();
221221
return new RoomListResponse(roomResponses);
222222
}
223223

@@ -242,14 +242,14 @@ public void chat(Long roomId, String sessionId, ChatMessage chatMessage) {
242242
room.increasePlayerCorrectCount(sessionId);
243243

244244
messageSender.send(
245-
destination,
246-
MessageType.QUESTION_RESULT,
247-
toQuestionResultResponse(chatMessage.nickname(), answer));
245+
destination,
246+
MessageType.QUESTION_RESULT,
247+
toQuestionResultResponse(chatMessage.nickname(), answer));
248248
messageSender.send(destination, MessageType.RANK_UPDATE, toRankUpdateResponse(room));
249249
messageSender.send(
250-
destination,
251-
MessageType.SYSTEM_NOTICE,
252-
ofPlayerEvent(chatMessage.nickname(), RoomEventType.CORRECT_ANSWER));
250+
destination,
251+
MessageType.SYSTEM_NOTICE,
252+
ofPlayerEvent(chatMessage.nickname(), RoomEventType.CORRECT_ANSWER));
253253

254254
timerService.cancelTimer(room);
255255

@@ -264,50 +264,46 @@ public void chat(Long roomId, String sessionId, ChatMessage chatMessage) {
264264
// 타이머 추가하기
265265
timerService.startTimer(room, CONTINUE_DELAY);
266266
messageSender.send(
267-
destination,
268-
MessageType.QUESTION_START,
269-
toQuestionStartResponse(room, CONTINUE_DELAY));
267+
destination,
268+
MessageType.QUESTION_START,
269+
toQuestionStartResponse(room, CONTINUE_DELAY));
270270
}
271271
}
272272

273-
public void reconnectSession(Long roomId, String oldSessionId, String newSessionId,
274-
UserPrincipal principal) {
273+
public void reconnectSession(
274+
Long roomId, String oldSessionId, String newSessionId, UserPrincipal principal) {
275275
Room room = findRoom(roomId);
276276
room.reconnectSession(oldSessionId, newSessionId);
277277

278278
String destination = getDestination(roomId);
279279

280-
281-
282-
283280
messageSender.send(
284-
destination,
285-
MessageType.SYSTEM_NOTICE,
286-
ofPlayerEvent(principal.getUserNickname(), RoomEventType.RECONNECT));
281+
destination,
282+
MessageType.SYSTEM_NOTICE,
283+
ofPlayerEvent(principal.getUserNickname(), RoomEventType.RECONNECT));
287284

288285
if (room.isPlaying()) {
289-
//todo 현재 round 및 타이머 ..
290-
//todo 랭킹 리스트
291-
messageSender.send(destination, MessageType.GAME_START, toGameStartResponse(room.getQuestions()));
286+
// todo 현재 round 및 타이머 ..
287+
// todo 랭킹 리스트
288+
messageSender.send(
289+
destination, MessageType.GAME_START, toGameStartResponse(room.getQuestions()));
292290

293-
}else {
291+
} else {
294292

295293
RoomSettingResponse roomSettingResponse = toRoomSettingResponse(room);
296294

297295
Long quizId = room.getGameSetting().getQuizId();
298296
Quiz quiz = quizService.getQuizWithQuestionsById(quizId);
299297

300298
GameSettingResponse gameSettingResponse =
301-
toGameSettingResponse(room.getGameSetting(), quiz);
299+
toGameSettingResponse(room.getGameSetting(), quiz);
302300

303301
PlayerListResponse playerListResponse = toPlayerListResponse(room);
304302

305303
messageSender.send(destination, MessageType.ROOM_SETTING, roomSettingResponse);
306304
messageSender.send(destination, MessageType.GAME_SETTING, gameSettingResponse);
307305
messageSender.send(destination, MessageType.PLAYER_LIST, playerListResponse);
308306
}
309-
310-
311307
}
312308

313309
public void changeConnectedStatus(Long roomId, String sessionId, ConnectionState newState) {
@@ -327,7 +323,6 @@ public void exitIfNotPlaying(Long roomId, String sessionId, UserPrincipal princi
327323
}
328324
}
329325

330-
331326
private Player getRemovePlayer(Room room, String sessionId, UserPrincipal principal) {
332327
Player removePlayer = room.getPlayerSessionMap().get(sessionId);
333328
if (removePlayer == null) {
@@ -347,8 +342,8 @@ private Player createPlayer() {
347342

348343
private Room findRoom(Long roomId) {
349344
return roomRepository
350-
.findRoom(roomId)
351-
.orElseThrow(() -> new CustomException(RoomErrorCode.ROOM_NOT_FOUND));
345+
.findRoom(roomId)
346+
.orElseThrow(() -> new CustomException(RoomErrorCode.ROOM_NOT_FOUND));
352347
}
353348

354349
private boolean isLastPlayer(Room room, String sessionId) {
@@ -367,14 +362,14 @@ private void changeHost(Room room, String hostSessionId) {
367362
Map<String, Player> playerSessionMap = room.getPlayerSessionMap();
368363

369364
Optional<String> nextHostSessionId =
370-
playerSessionMap.keySet().stream()
371-
.filter(key -> !key.equals(hostSessionId))
372-
.findFirst();
365+
playerSessionMap.keySet().stream()
366+
.filter(key -> !key.equals(hostSessionId))
367+
.findFirst();
373368

374369
Player nextHost =
375-
playerSessionMap.get(
376-
nextHostSessionId.orElseThrow(
377-
() -> new CustomException(RoomErrorCode.SOCKET_SESSION_NOT_FOUND)));
370+
playerSessionMap.get(
371+
nextHostSessionId.orElseThrow(
372+
() -> new CustomException(RoomErrorCode.SOCKET_SESSION_NOT_FOUND)));
378373

379374
room.updateHost(nextHost);
380375
log.info("user_id:{} 방장 변경 완료 ", nextHost.getId());

backend/src/main/java/io/f1/backend/domain/game/websocket/eventlistener/WebsocketEventListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void handleSubscribeListener(SessionSubscribeEvent event) {
5656
if (subscribeType[2].equals("room")) {
5757
Long roomId = Long.parseLong(subscribeType[3]);
5858
sessionService.addRoomId(roomId, sessionId);
59-
sessionService.handleUserReconnect(roomId, sessionId,principal);
59+
sessionService.handleUserReconnect(roomId, sessionId, principal);
6060
}
6161
}
6262

backend/src/main/java/io/f1/backend/domain/game/websocket/service/SessionService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public void handleUserReconnect(Long roomId, String newSessionId, UserPrincipal
4141
if (userIdLatestSession.get(userId) != null) {
4242
String oldSessionId = userIdLatestSession.get(userId);
4343
/* room 재연결 대상인지 아닌지 판별 */
44-
if (!roomService.isExit(oldSessionId,roomId)) {
45-
roomService.reconnectSession(roomId, oldSessionId, newSessionId,principal);
44+
if (!roomService.isExit(oldSessionId, roomId)) {
45+
roomService.reconnectSession(roomId, oldSessionId, newSessionId, principal);
4646
}
4747
}
4848
}
@@ -75,7 +75,7 @@ public void handleUserDisconnect(String sessionId, UserPrincipal principal) {
7575

7676
public void removeSession(String sessionId, Long userId) {
7777

78-
if(userIdSession.get(userId).equals(sessionId)) {
78+
if (userIdSession.get(userId).equals(sessionId)) {
7979
userIdSession.remove(userId);
8080
}
8181
sessionIdUser.remove(sessionId);

backend/src/test/java/io/f1/backend/domain/game/websocket/SessionServiceTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ void handleUserReconnect_shouldReconnectIfTarget() {
9898
User user = new User("provider", "providerId", LocalDateTime.now());
9999
user.setId(userId1);
100100
UserPrincipal principal = new UserPrincipal(user, new HashMap<>());
101-
sessionService.handleUserReconnect(roomId1, sessionId2,principal);
101+
sessionService.handleUserReconnect(roomId1, sessionId2, principal);
102102

103103
// RoomService.reconnectSession이 올바른 인자로 호출되었는지 검증
104-
verify(roomService, times(1)).reconnectSession(roomId1, sessionId1, sessionId2,principal);
104+
verify(roomService, times(1)).reconnectSession(roomId1, sessionId1, sessionId2, principal);
105105
verify(roomService, never()).changeConnectedStatus(any(), any(), any()); // 다른 메서드 호출 안됨 확인
106106
}
107107

@@ -114,16 +114,17 @@ void handleUserReconnect_shouldNotReconnectIfNotTarget() {
114114
sessionService, "userIdLatestSession", Map.of(userId1, sessionId1));
115115

116116
// RoomService.isExit -> true 반환하도록 모의 설정
117-
when(roomService.isExit(sessionId1,roomId1)).thenReturn(true);
117+
when(roomService.isExit(sessionId1, roomId1)).thenReturn(true);
118118

119119
User user = new User("provider", "providerId", LocalDateTime.now());
120120
user.setId(userId1);
121121
UserPrincipal principal = new UserPrincipal(user, new HashMap<>());
122122
// 메서드 호출
123-
sessionService.handleUserReconnect(roomId1, sessionId2,principal);
123+
sessionService.handleUserReconnect(roomId1, sessionId2, principal);
124124

125125
// RoomService.reconnectSession이 호출되지 않았는지 검증
126-
verify(roomService, never()).reconnectSession(anyLong(), anyString(), anyString(),any(UserPrincipal.class));
126+
verify(roomService, never())
127+
.reconnectSession(anyLong(), anyString(), anyString(), any(UserPrincipal.class));
127128
}
128129

129130
@Test
@@ -149,7 +150,6 @@ void handleUserDisconnect_shouldExitIfNotPlayingIfDisconnected() throws Interrup
149150
// verify: roomService.exitIfNotPlaying이 호출되었는지 확인
150151
verify(roomService, times(1)).exitIfNotPlaying(eq(roomId1), eq(sessionId1), eq(principal));
151152

152-
153153
Map<Long, String> userIdLatestSession =
154154
(Map<Long, String>)
155155
ReflectionTestUtils.getField(sessionService, "userIdLatestSession");

0 commit comments

Comments
 (0)