1313import io .f1 .backend .domain .question .entity .Question ;
1414import io .f1 .backend .domain .quiz .app .QuizService ;
1515import io .f1 .backend .domain .quiz .entity .Quiz ;
16+ import io .f1 .backend .domain .user .dto .UserPrincipal ;
1617import io .f1 .backend .global .exception .CustomException ;
17- import io .f1 .backend .global .exception .errorcode .GameErrorCode ;
1818import io .f1 .backend .global .exception .errorcode .RoomErrorCode ;
1919
20+ import java .util .Objects ;
2021import lombok .RequiredArgsConstructor ;
2122
2223import org .springframework .context .ApplicationEventPublisher ;
@@ -33,53 +34,54 @@ public class GameService {
3334 private final RoomRepository roomRepository ;
3435 private final ApplicationEventPublisher eventPublisher ;
3536
36- public GameStartResponse gameStart (Long roomId , GameStartRequest gameStartRequest ) {
37-
38- Long quizId = gameStartRequest .quizId ();
37+ public GameStartResponse gameStart (Long roomId , UserPrincipal principal ) {
3938
4039 Room room =
4140 roomRepository
4241 .findRoom (roomId )
4342 .orElseThrow (() -> new CustomException (RoomErrorCode .ROOM_NOT_FOUND ));
4443
45- if (!validateReadyStatus (room )) {
46- throw new CustomException (RoomErrorCode .PLAYER_NOT_READY );
47- }
48-
49- // 방의 gameSetting에 설정된 퀴즈랑 요청 퀴즈랑 같은지 체크 후 GameSetting에서 라운드 가져오기
50- Integer round = checkGameSetting (room , quizId );
44+ validateRoomStart (room , principal );
5145
46+ Long quizId = room .getGameSetting ().getQuizId ();
5247 Quiz quiz = quizService .getQuizWithQuestionsById (quizId );
48+ List <Question > questions = prepareQuestions (room , quiz );
5349
54- // 라운드 수만큼 랜덤 Question 추출
55- List <Question > questions = quizService .getRandomQuestionsWithoutAnswer (quizId , round );
5650 room .updateQuestions (questions );
5751
58- GameStartResponse gameStartResponse = toGameStartResponse (questions );
59-
6052 // 방 정보 게임 중으로 변경
6153 room .updateRoomState (RoomState .PLAYING );
6254
6355 eventPublisher .publishEvent (new RoomUpdatedEvent (room , quiz ));
6456
65- return gameStartResponse ;
57+ return toGameStartResponse ( questions ) ;
6658 }
6759
68- private Integer checkGameSetting (Room room , Long quizId ) {
69-
70- GameSetting gameSetting = room .getGameSetting ();
60+ private boolean validateReadyStatus (Room room ) {
7161
72- if (!gameSetting .validateQuizId (quizId )) {
73- throw new CustomException (GameErrorCode .GAME_SETTING_CONFLICT );
74- }
62+ Map <String , Player > playerSessionMap = room .getPlayerSessionMap ();
7563
76- return gameSetting . getRound ( );
64+ return playerSessionMap . values (). stream (). allMatch ( Player :: isReady );
7765 }
7866
79- private boolean validateReadyStatus (Room room ) {
67+ private void validateRoomStart (Room room , UserPrincipal principal ) {
68+ if (!Objects .equals (principal .getUserId (), room .getHost ().getId ())) {
69+ throw new CustomException (RoomErrorCode .NOT_ROOM_OWNER );
70+ }
8071
81- Map <String , Player > playerSessionMap = room .getPlayerSessionMap ();
72+ if (!validateReadyStatus (room )) {
73+ throw new CustomException (RoomErrorCode .PLAYER_NOT_READY );
74+ }
8275
83- return playerSessionMap .values ().stream ().allMatch (Player ::isReady );
76+ if (room .getState () == RoomState .PLAYING ) {
77+ throw new CustomException (RoomErrorCode .GAME_ALREADY_PLAYING );
78+ }
79+ }
80+
81+ // 라운드 수만큼 랜덤 Question 추출
82+ public List <Question > prepareQuestions (Room room , Quiz quiz ) {
83+ Long quizId = quiz .getId ();
84+ Integer round = room .getGameSetting ().getRound ();
85+ return quizService .getRandomQuestionsWithoutAnswer (quizId , round );
8486 }
8587}
0 commit comments