11package io .f1 .backend .domain .game .app ;
22
3+ import static io .f1 .backend .domain .game .mapper .RoomMapper .toQuestionStartResponse ;
4+ import static io .f1 .backend .domain .game .websocket .WebSocketUtils .getDestination ;
35import static io .f1 .backend .domain .quiz .mapper .QuizMapper .toGameStartResponse ;
46
5- import io .f1 .backend .domain .game .dto .request .GameStartRequest ;
6- import io .f1 .backend .domain .game .dto .response .GameStartResponse ;
7+ import io .f1 .backend .domain .game .dto .MessageType ;
78import io .f1 .backend .domain .game .event .RoomUpdatedEvent ;
8- import io .f1 .backend .domain .game .model .GameSetting ;
99import io .f1 .backend .domain .game .model .Player ;
1010import io .f1 .backend .domain .game .model .Room ;
1111import io .f1 .backend .domain .game .model .RoomState ;
1212import io .f1 .backend .domain .game .store .RoomRepository ;
13+ import io .f1 .backend .domain .game .websocket .MessageSender ;
1314import io .f1 .backend .domain .question .entity .Question ;
1415import io .f1 .backend .domain .quiz .app .QuizService ;
1516import io .f1 .backend .domain .quiz .entity .Quiz ;
17+ import io .f1 .backend .domain .user .dto .UserPrincipal ;
1618import io .f1 .backend .global .exception .CustomException ;
1719import io .f1 .backend .global .exception .errorcode .GameErrorCode ;
1820import io .f1 .backend .global .exception .errorcode .RoomErrorCode ;
2426
2527import java .util .List ;
2628import java .util .Map ;
29+ import java .util .Objects ;
2730
2831@ Service
2932@ RequiredArgsConstructor
3033public class GameService {
3134
35+ public static final int START_DELAY = 5 ;
36+
37+ private final MessageSender messageSender ;
38+ private final TimerService timerService ;
3239 private final QuizService quizService ;
3340 private final RoomRepository roomRepository ;
3441 private final ApplicationEventPublisher eventPublisher ;
3542
36- public GameStartResponse gameStart (Long roomId , GameStartRequest gameStartRequest ) {
43+ public void gameStart (Long roomId , UserPrincipal principal ) {
3744
38- Long quizId = gameStartRequest . quizId ( );
45+ String destination = getDestination ( roomId );
3946
4047 Room room =
4148 roomRepository
4249 .findRoom (roomId )
4350 .orElseThrow (() -> new CustomException (RoomErrorCode .ROOM_NOT_FOUND ));
4451
45- if (!validateReadyStatus (room )) {
46- throw new CustomException (RoomErrorCode .PLAYER_NOT_READY );
47- }
48-
49- // ๋ฐฉ์ gameSetting์ ์ค์ ๋ ํด์ฆ๋ ์์ฒญ ํด์ฆ๋ ๊ฐ์์ง ์ฒดํฌ ํ GameSetting์์ ๋ผ์ด๋ ๊ฐ์ ธ์ค๊ธฐ
50- Integer round = checkGameSetting (room , quizId );
52+ validateRoomStart (room , principal );
5153
54+ Long quizId = room .getGameSetting ().getQuizId ();
5255 Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
56+ List <Question > questions = prepareQuestions (room , quiz );
5357
54- // ๋ผ์ด๋ ์๋งํผ ๋๋ค Question ์ถ์ถ
55- List <Question > questions = quizService .getRandomQuestionsWithoutAnswer (quizId , round );
5658 room .updateQuestions (questions );
57-
58- GameStartResponse gameStartResponse = toGameStartResponse (questions );
59-
60- // ๋ฐฉ ์ ๋ณด ๊ฒ์ ์ค์ผ๋ก ๋ณ๊ฒฝ
59+ room .increaseCurrentRound ();
6160 room .updateRoomState (RoomState .PLAYING );
6261
6362 eventPublisher .publishEvent (new RoomUpdatedEvent (room , quiz ));
6463
65- return gameStartResponse ;
66- }
67-
68- private Integer checkGameSetting (Room room , Long quizId ) {
69-
70- GameSetting gameSetting = room .getGameSetting ();
71-
72- if (!gameSetting .validateQuizId (quizId )) {
73- throw new CustomException (GameErrorCode .GAME_SETTING_CONFLICT );
74- }
64+ timerService .startTimer (room , START_DELAY );
7565
76- return gameSetting .getRound ();
66+ messageSender .send (destination , MessageType .GAME_START , toGameStartResponse (questions ));
67+ messageSender .send (
68+ destination ,
69+ MessageType .QUESTION_START ,
70+ toQuestionStartResponse (room , START_DELAY ));
7771 }
7872
7973 private boolean validateReadyStatus (Room room ) {
@@ -82,4 +76,25 @@ private boolean validateReadyStatus(Room room) {
8276
8377 return playerSessionMap .values ().stream ().allMatch (Player ::isReady );
8478 }
79+
80+ private void validateRoomStart (Room room , UserPrincipal principal ) {
81+ if (!Objects .equals (principal .getUserId (), room .getHost ().getId ())) {
82+ throw new CustomException (RoomErrorCode .NOT_ROOM_OWNER );
83+ }
84+
85+ if (!validateReadyStatus (room )) {
86+ throw new CustomException (GameErrorCode .PLAYER_NOT_READY );
87+ }
88+
89+ if (room .getState () == RoomState .PLAYING ) {
90+ throw new CustomException (RoomErrorCode .GAME_ALREADY_PLAYING );
91+ }
92+ }
93+
94+ // ๋ผ์ด๋ ์๋งํผ ๋๋ค Question ์ถ์ถ
95+ private List <Question > prepareQuestions (Room room , Quiz quiz ) {
96+ Long quizId = quiz .getId ();
97+ Integer round = room .getGameSetting ().getRound ();
98+ return quizService .getRandomQuestionsWithoutAnswer (quizId , round );
99+ }
85100}
0 commit comments