Skip to content

Commit 36f0bdd

Browse files
committed
♻️ pr 리뷰 반영
1 parent ae4589a commit 36f0bdd

File tree

3 files changed

+72
-67
lines changed

3 files changed

+72
-67
lines changed

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

Lines changed: 57 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import io.f1.backend.domain.game.model.Player;
3434
import io.f1.backend.domain.game.model.Room;
3535
import io.f1.backend.domain.game.model.RoomSetting;
36-
import io.f1.backend.domain.game.model.RoomState;
3736
import io.f1.backend.domain.game.store.RoomRepository;
3837
import io.f1.backend.domain.game.store.UserRoomRepository;
3938
import io.f1.backend.domain.game.websocket.DisconnectTaskManager;
@@ -45,18 +44,15 @@
4544
import io.f1.backend.global.exception.CustomException;
4645
import io.f1.backend.global.exception.errorcode.RoomErrorCode;
4746
import 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-
5547
import java.util.List;
5648
import java.util.Map;
5749
import java.util.Optional;
5850
import java.util.concurrent.ConcurrentHashMap;
5951
import 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;
6056

6157
@Slf4j
6258
@Service
@@ -122,7 +118,7 @@ public void enterRoom(RoomValidationRequest request) {
122118
return;
123119
}
124120

125-
if (room.getState().equals(RoomState.PLAYING)) {
121+
if (room.isPlaying()) {
126122
throw new CustomException(RoomErrorCode.ROOM_GAME_IN_PROGRESS);
127123
}
128124

@@ -133,7 +129,7 @@ public void enterRoom(RoomValidationRequest request) {
133129
}
134130

135131
if (room.getRoomSetting().locked()
136-
&& !room.getRoomSetting().password().equals(request.password())) {
132+
&& !room.getRoomSetting().password().equals(request.password())) {
137133
throw new CustomException(RoomErrorCode.WRONG_PASSWORD);
138134
}
139135

@@ -145,8 +141,7 @@ private void exitIfInAnotherRoom(Room room, Long userId) {
145141

146142
Long joinedRoomId = userRoomRepository.getRoomId(userId);
147143

148-
if (joinedRoomId != null && !room.getId().equals(joinedRoomId)) {
149-
144+
if (joinedRoomId != null && !room.isSameRoom(joinedRoomId)) {
150145
if (room.isPlaying()) {
151146
changeConnectedStatus(userId, ConnectionState.DISCONNECTED);
152147
} else {
@@ -165,7 +160,7 @@ public void initializeRoomSocket(Long roomId, UserPrincipal principal) {
165160
}
166161

167162
/* 재연결 */
168-
if (room.getPlayerState(userId).equals(ConnectionState.DISCONNECTED)) {
163+
if (room.isPlayerInState(userId, ConnectionState.DISCONNECTED)) {
169164
changeConnectedStatus(userId, ConnectionState.CONNECTED);
170165
cancelTask(userId);
171166
reconnectSendResponse(roomId, principal);
@@ -180,19 +175,19 @@ public void initializeRoomSocket(Long roomId, UserPrincipal principal) {
180175
Quiz quiz = quizService.getQuizWithQuestionsById(quizId);
181176

182177
GameSettingResponse gameSettingResponse =
183-
toGameSettingResponse(room.getGameSetting(), quiz);
178+
toGameSettingResponse(room.getGameSetting(), quiz);
184179

185180
PlayerListResponse playerListResponse = toPlayerListResponse(room);
186181

187182
SystemNoticeResponse systemNoticeResponse =
188-
ofPlayerEvent(player.getNickname(), RoomEventType.ENTER);
183+
ofPlayerEvent(player.getNickname(), RoomEventType.ENTER);
189184

190185
String destination = getDestination(roomId);
191186

192187
userRoomRepository.addUser(player, room);
193188

194189
messageSender.sendPersonal(
195-
getUserDestination(), MessageType.GAME_SETTING, gameSettingResponse, principal);
190+
getUserDestination(), MessageType.GAME_SETTING, gameSettingResponse, principal);
196191

197192
messageSender.sendBroadcast(destination, MessageType.ROOM_SETTING, roomSettingResponse);
198193
messageSender.sendBroadcast(destination, MessageType.PLAYER_LIST, playerListResponse);
@@ -217,34 +212,34 @@ public void exitRoom(Long roomId, UserPrincipal principal) {
217212
cleanRoom(room, removePlayer);
218213

219214
messageSender.sendPersonal(
220-
getUserDestination(),
221-
MessageType.EXIT_SUCCESS,
222-
new ExitSuccessResponse(true),
223-
principal);
215+
getUserDestination(),
216+
MessageType.EXIT_SUCCESS,
217+
new ExitSuccessResponse(true),
218+
principal);
224219

225220
SystemNoticeResponse systemNoticeResponse =
226-
ofPlayerEvent(removePlayer.nickname, RoomEventType.EXIT);
221+
ofPlayerEvent(removePlayer.nickname, RoomEventType.EXIT);
227222

228223
PlayerListResponse playerListResponse = toPlayerListResponse(room);
229224

230225
messageSender.sendBroadcast(destination, MessageType.PLAYER_LIST, playerListResponse);
231226
messageSender.sendBroadcast(
232-
destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
227+
destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
233228
}
234229
}
235230

236231
public RoomListResponse getAllRooms() {
237232
List<Room> rooms = roomRepository.findAll();
238233
List<RoomResponse> roomResponses =
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();
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();
248243
return new RoomListResponse(roomResponses);
249244
}
250245

@@ -255,27 +250,27 @@ public void reconnectSendResponse(Long roomId, UserPrincipal principal) {
255250
String userDestination = getUserDestination();
256251

257252
messageSender.sendBroadcast(
258-
destination,
259-
MessageType.SYSTEM_NOTICE,
260-
ofPlayerEvent(principal.getUserNickname(), RoomEventType.RECONNECT));
253+
destination,
254+
MessageType.SYSTEM_NOTICE,
255+
ofPlayerEvent(principal.getUserNickname(), RoomEventType.RECONNECT));
261256

262257
if (room.isPlaying()) {
263258
messageSender.sendPersonal(
264-
userDestination,
265-
MessageType.SYSTEM_NOTICE,
266-
ofPlayerEvent(
267-
principal.getUserNickname(), RoomEventType.RECONNECT_PRIVATE_NOTICE),
268-
principal);
259+
userDestination,
260+
MessageType.SYSTEM_NOTICE,
261+
ofPlayerEvent(
262+
principal.getUserNickname(), RoomEventType.RECONNECT_PRIVATE_NOTICE),
263+
principal);
269264
messageSender.sendPersonal(
270-
userDestination,
271-
MessageType.RANK_UPDATE,
272-
toRankUpdateResponse(room),
273-
principal);
265+
userDestination,
266+
MessageType.RANK_UPDATE,
267+
toRankUpdateResponse(room),
268+
principal);
274269
messageSender.sendPersonal(
275-
userDestination,
276-
MessageType.GAME_START,
277-
toGameStartResponse(room.getQuestions()),
278-
principal);
270+
userDestination,
271+
MessageType.GAME_START,
272+
toGameStartResponse(room.getQuestions()),
273+
principal);
279274
} else {
280275
RoomSettingResponse roomSettingResponse = toRoomSettingResponse(room);
281276

@@ -284,16 +279,16 @@ public void reconnectSendResponse(Long roomId, UserPrincipal principal) {
284279
Quiz quiz = quizService.getQuizWithQuestionsById(quizId);
285280

286281
GameSettingResponse gameSettingResponse =
287-
toGameSettingResponse(room.getGameSetting(), quiz);
282+
toGameSettingResponse(room.getGameSetting(), quiz);
288283

289284
PlayerListResponse playerListResponse = toPlayerListResponse(room);
290285

291286
messageSender.sendPersonal(
292-
userDestination, MessageType.ROOM_SETTING, roomSettingResponse, principal);
287+
userDestination, MessageType.ROOM_SETTING, roomSettingResponse, principal);
293288
messageSender.sendPersonal(
294-
userDestination, MessageType.PLAYER_LIST, playerListResponse, principal);
289+
userDestination, MessageType.PLAYER_LIST, playerListResponse, principal);
295290
messageSender.sendPersonal(
296-
userDestination, MessageType.GAME_SETTING, gameSettingResponse, principal);
291+
userDestination, MessageType.GAME_SETTING, gameSettingResponse, principal);
297292
}
298293
}
299294

@@ -329,8 +324,8 @@ private Player createPlayer() {
329324

330325
public Room findRoom(Long roomId) {
331326
return roomRepository
332-
.findRoom(roomId)
333-
.orElseThrow(() -> new CustomException(RoomErrorCode.ROOM_NOT_FOUND));
327+
.findRoom(roomId)
328+
.orElseThrow(() -> new CustomException(RoomErrorCode.ROOM_NOT_FOUND));
334329
}
335330

336331
private void removeRoom(Room room) {
@@ -344,14 +339,14 @@ private void changeHost(Room room, Player host) {
344339
Map<Long, Player> playerMap = room.getPlayerMap();
345340

346341
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();
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();
352347

353348
room.updateHost(
354-
nextHost.orElseThrow(() -> new CustomException(RoomErrorCode.PLAYER_NOT_FOUND)));
349+
nextHost.orElseThrow(() -> new CustomException(RoomErrorCode.PLAYER_NOT_FOUND)));
355350
}
356351

357352
private String getUserDestination() {
@@ -371,12 +366,12 @@ public void exitRoomForDisconnectedPlayer(Long roomId, Player player) {
371366
String destination = getDestination(roomId);
372367

373368
SystemNoticeResponse systemNoticeResponse =
374-
ofPlayerEvent(player.nickname, RoomEventType.EXIT);
369+
ofPlayerEvent(player.nickname, RoomEventType.EXIT);
375370

376371
messageSender.sendBroadcast(
377-
destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
372+
destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
378373
messageSender.sendBroadcast(
379-
destination, MessageType.PLAYER_LIST, toPlayerListResponse(room));
374+
destination, MessageType.PLAYER_LIST, toPlayerListResponse(room));
380375
}
381376
}
382377

backend/src/main/java/io/f1/backend/domain/game/model/Room.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,12 @@ public int getRound() {
187187
public ConnectionState getPlayerState(Long userId) {
188188
return playerMap.get(userId).getState();
189189
}
190+
191+
public boolean isSameRoom(Long otherRoomId){
192+
return Objects.equals(id, otherRoomId);
193+
}
194+
195+
public boolean isPlayerInState(Long userId, ConnectionState state){
196+
return getPlayerState(userId).equals(state);
197+
}
190198
}

backend/src/main/java/io/f1/backend/domain/game/websocket/DisconnectTaskManager.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ public void scheduleDisconnectTask(Long userId, Runnable task) {
2525
ScheduledFuture<?> scheduled = scheduler.schedule(task, 5, TimeUnit.SECONDS);
2626

2727
ScheduledFuture<?> prev = disconnectTasks.put(userId, scheduled);
28-
if (prev != null && !prev.isDone()) {
29-
prev.cancel(false);
30-
}
28+
cancelIfRunning(prev);
3129
}
3230

3331
public void cancelDisconnectTask(Long userId) {
3432
ScheduledFuture<?> task = disconnectTasks.remove(userId);
35-
if (task != null && !task.isDone()) {
36-
task.cancel(false);
33+
cancelIfRunning(task);
34+
}
35+
36+
private static void cancelIfRunning(ScheduledFuture<?> future) {
37+
if (future != null && !future.isDone()) {
38+
future.cancel(false);
3739
}
3840
}
3941
}

0 commit comments

Comments
 (0)