Skip to content

Commit 655929e

Browse files
committed
♻️ refactor: 코드 리뷰 반영
1 parent fdaaee7 commit 655929e

File tree

7 files changed

+63
-31
lines changed

7 files changed

+63
-31
lines changed

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import static io.f1.backend.domain.game.mapper.RoomMapper.toQuestionStartResponse;
66
import static io.f1.backend.domain.quiz.mapper.QuizMapper.toGameStartResponse;
77

8-
import io.f1.backend.domain.game.dto.MessageType;
98
import io.f1.backend.domain.game.dto.request.GameSettingChanger;
9+
import io.f1.backend.domain.game.dto.MessageType;
10+
import io.f1.backend.domain.game.dto.response.PlayerListResponse;
1011
import io.f1.backend.domain.game.event.RoomUpdatedEvent;
1112
import io.f1.backend.domain.game.model.Player;
1213
import io.f1.backend.domain.game.model.Room;
@@ -23,12 +24,14 @@
2324

2425
import lombok.RequiredArgsConstructor;
2526

27+
import lombok.extern.slf4j.Slf4j;
2628
import org.springframework.context.ApplicationEventPublisher;
2729
import org.springframework.stereotype.Service;
2830

2931
import java.util.List;
3032
import java.util.Objects;
3133

34+
@Slf4j
3235
@Service
3336
@RequiredArgsConstructor
3437
public class GameService {
@@ -72,18 +75,18 @@ public void gameStart(Long roomId, UserPrincipal principal) {
7275
}
7376

7477
public void handlePlayerReady(Long roomId, String sessionId) {
75-
Player player =
76-
roomRepository
77-
.findPlayerInRoomBySessionId(roomId, sessionId)
78-
.orElseThrow(() -> new CustomException(RoomErrorCode.PLAYER_NOT_FOUND));
7978

8079
Room room = findRoom(roomId);
8180

81+
Player player = room.getPlayerSessionMap().get(sessionId);
82+
8283
toggleReadyIfPossible(room, player);
8384

8485
String destination = getDestination(roomId);
8586

86-
messageSender.send(destination, MessageType.PLAYER_LIST, toPlayerListResponse(room));
87+
PlayerListResponse playerListResponse = toPlayerListResponse(room);
88+
log.info(playerListResponse.toString());
89+
messageSender.send(destination, MessageType.PLAYER_LIST, playerListResponse);
8790
}
8891

8992
public void changeGameSetting(
@@ -98,10 +101,8 @@ public void changeGameSetting(
98101

99102
broadcastGameSetting(room);
100103

101-
RoomUpdatedEvent roomUpdatedEvent =
102-
new RoomUpdatedEvent(
103-
room,
104-
quizService.getQuizWithQuestionsById(room.getGameSetting().getQuizId()));
104+
RoomUpdatedEvent roomUpdatedEvent = new RoomUpdatedEvent(room,
105+
quizService.getQuizWithQuestionsById(room.getGameSetting().getQuizId()));
105106

106107
eventPublisher.publishEvent(roomUpdatedEvent);
107108
}
@@ -138,7 +139,7 @@ private String getDestination(Long roomId) {
138139
}
139140

140141
private void validateHostAndState(Room room, UserPrincipal principal) {
141-
if (!Objects.equals(principal.getUserId(), room.getHost().getId())) {
142+
if (!room.isHost(principal.getUserId())) {
142143
throw new CustomException(RoomErrorCode.NOT_ROOM_OWNER);
143144
}
144145
if (room.isPlaying()) {
@@ -150,7 +151,7 @@ private void toggleReadyIfPossible(Room room, Player player) {
150151
if (room.isPlaying()) {
151152
throw new CustomException(RoomErrorCode.GAME_ALREADY_PLAYING);
152153
}
153-
if (!Objects.equals(player.getId(), room.getHost().getId())) {
154+
if (!room.isHost(player.getId())) {
154155
player.toggleReady();
155156
}
156157
}
@@ -159,8 +160,8 @@ private void broadcastGameSetting(Room room) {
159160
String destination = getDestination(room.getId());
160161
Quiz quiz = quizService.getQuizWithQuestionsById(room.getGameSetting().getQuizId());
161162
messageSender.send(
162-
destination,
163-
MessageType.GAME_SETTING,
164-
toGameSettingResponse(room.getGameSetting(), quiz));
163+
destination,
164+
MessageType.GAME_SETTING,
165+
toGameSettingResponse(room.getGameSetting(), quiz));
165166
}
166167
}
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.f1.backend.domain.game.dto.request;
22

3-
import static io.f1.backend.domain.game.mapper.RoomMapper.toPlayerListResponse;
4-
53
import io.f1.backend.domain.game.dto.MessageType;
64
import io.f1.backend.domain.game.dto.response.PlayerListResponse;
75
import io.f1.backend.domain.game.model.Room;
@@ -10,29 +8,35 @@
108
import io.f1.backend.domain.quiz.entity.Quiz;
119

1210
import java.util.Objects;
11+
import lombok.extern.slf4j.Slf4j;
12+
13+
import static io.f1.backend.domain.game.mapper.RoomMapper.toPlayerListResponse;
14+
import static io.f1.backend.domain.game.websocket.WebSocketUtils.getDestination;
1315

16+
@Slf4j
1417
public record QuizChangeRequest(Long quizId) implements GameSettingChanger {
1518

1619
@Override
1720
public boolean change(Room room, QuizService quizService) {
18-
if (Objects.equals(room.getGameSetting().getQuizId(), quizId)) {
21+
if (Objects.equals(room.getQuizId(), quizId)) {
1922
return false; // 동일하면 무시
2023
}
2124
Quiz quiz = quizService.getQuizWithQuestionsById(quizId);
2225
int questionSize = quiz.getQuestions().size();
23-
room.getGameSetting().changeQuiz(quiz);
26+
room.changeQuiz(quiz);
2427
// 퀴즈의 문제 갯수로 변경
25-
room.getGameSetting().changeRound(questionSize, questionSize);
28+
room.changeRound(questionSize, questionSize);
2629
return true;
2730
}
2831

2932
@Override
3033
public void afterChange(Room room, MessageSender messageSender) {
3134
room.resetAllPlayerReadyStates();
3235

33-
String destination = "/sub/room/" + room.getId();
36+
String destination = getDestination(room.getId());
3437
PlayerListResponse response = toPlayerListResponse(room);
3538

39+
log.info(response.toString());
3640
messageSender.send(destination, MessageType.PLAYER_LIST, response);
3741
}
3842
}

backend/src/main/java/io/f1/backend/domain/game/dto/request/RoundChangeRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ public record RoundChangeRequest(Integer round) implements GameSettingChanger {
1111

1212
@Override
1313
public boolean change(Room room, QuizService quizService) {
14-
if (Objects.equals(room.getGameSetting().getRound(), round)) {
14+
if (Objects.equals(room.getRound(), round)) {
1515
return false; // 동일하면 무시
1616
}
1717

18-
Quiz quiz = quizService.findQuizById(room.getGameSetting().getQuizId());
18+
Quiz quiz = quizService.findQuizById(room.getQuizId());
1919
int questionSize = quiz.getQuestions().size();
2020

21-
room.getGameSetting().changeRound(round, questionSize);
21+
room.changeRound(round, questionSize);
2222
return true;
2323
}
2424

backend/src/main/java/io/f1/backend/domain/game/dto/request/TimeLimitChangeRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ public record TimeLimitChangeRequest(int timeLimit) implements GameSettingChange
88

99
@Override
1010
public boolean change(Room room, QuizService quizService) {
11-
if (room.getGameSetting().getTimeLimit() == timeLimit) {
11+
if (room.getTimeLimit() == timeLimit) {
1212
return false; // 동일하면 무시
1313
}
14-
room.getGameSetting().changeTimeLimit(TimeLimit.from(timeLimit));
14+
room.changeTimeLimit(TimeLimit.from(timeLimit));
1515
return true;
1616
}
1717

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.f1.backend.domain.game.model;
22

3+
import io.f1.backend.domain.game.dto.request.TimeLimit;
34
import io.f1.backend.domain.question.entity.Question;
45

6+
import io.f1.backend.domain.quiz.entity.Quiz;
57
import lombok.Getter;
68

79
import java.time.LocalDateTime;
@@ -49,7 +51,7 @@ public Room(Long id, RoomSetting roomSetting, GameSetting gameSetting, Player ho
4951
}
5052

5153
public boolean isHost(Long id) {
52-
return this.host.getId().equals(id);
54+
return Objects.equals(host.id, id);
5355
}
5456

5557
public void updateQuestions(List<Question> questions) {
@@ -103,4 +105,29 @@ public void resetAllPlayerReadyStates() {
103105
player.setReadyFalse();
104106
}
105107
}
108+
109+
public void changeQuiz(Quiz quiz) {
110+
gameSetting.changeQuiz(quiz);
111+
}
112+
113+
public void changeTimeLimit(TimeLimit timeLimit) {
114+
gameSetting.changeTimeLimit(timeLimit);
115+
}
116+
117+
public void changeRound(int round, int questionCount) {
118+
gameSetting.changeRound(round, questionCount);
119+
}
120+
121+
public Long getQuizId() {
122+
return gameSetting.getQuizId();
123+
}
124+
125+
public int getTimeLimit() {
126+
return gameSetting.getTimeLimit();
127+
}
128+
129+
public int getRound() {
130+
return gameSetting.getRound();
131+
}
132+
106133
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import io.f1.backend.domain.game.app.GameService;
77
import io.f1.backend.domain.game.app.RoomService;
88
import io.f1.backend.domain.game.dto.ChatMessage;
9-
import io.f1.backend.domain.game.dto.QuizChangeRequest;
10-
import io.f1.backend.domain.game.dto.RoundChangeRequest;
11-
import io.f1.backend.domain.game.dto.TimeLimitChangeRequest;
9+
import io.f1.backend.domain.game.dto.request.QuizChangeRequest;
10+
import io.f1.backend.domain.game.dto.request.RoundChangeRequest;
11+
import io.f1.backend.domain.game.dto.request.TimeLimitChangeRequest;
1212
import io.f1.backend.domain.game.dto.request.DefaultWebSocketRequest;
1313
import io.f1.backend.domain.user.dto.UserPrincipal;
1414

backend/src/main/java/io/f1/backend/global/exception/errorcode/RoomErrorCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public enum RoomErrorCode implements ErrorCode {
1515
PLAYER_NOT_FOUND("E404007", HttpStatus.NOT_FOUND, "존재하지 않는 플레이어입니다."),
1616
SOCKET_SESSION_NOT_FOUND("E404006", HttpStatus.NOT_FOUND, "존재하지 않는 소켓 세션입니다."),
1717
GAME_ALREADY_PLAYING("E400015", HttpStatus.BAD_REQUEST, "이미 게임이 진행 중 입니다."),
18-
NOT_ROOM_OWNER("E403005", HttpStatus.FORBIDDEN, "방장만 게임 시작이 가능합니다.");
18+
NOT_ROOM_OWNER("E403005", HttpStatus.FORBIDDEN, "방장의 권한입니다.");
1919

2020
private final String code;
2121

0 commit comments

Comments
 (0)