11package io .f1 .backend .domain .game .app ;
22
3+ import static io .f1 .backend .domain .game .mapper .RoomMapper .ofPlayerEvent ;
4+ import static io .f1 .backend .domain .game .mapper .RoomMapper .toGameResultListResponse ;
35import static io .f1 .backend .domain .game .mapper .RoomMapper .toGameSettingResponse ;
46import static io .f1 .backend .domain .game .mapper .RoomMapper .toPlayerListResponse ;
7+ import static io .f1 .backend .domain .game .mapper .RoomMapper .toQuestionResultResponse ;
58import static io .f1 .backend .domain .game .mapper .RoomMapper .toQuestionStartResponse ;
9+ import static io .f1 .backend .domain .game .mapper .RoomMapper .toRankUpdateResponse ;
10+ import static io .f1 .backend .domain .game .mapper .RoomMapper .toRoomSettingResponse ;
11+ import static io .f1 .backend .domain .game .websocket .WebSocketUtils .getDestination ;
612import static io .f1 .backend .domain .quiz .mapper .QuizMapper .toGameStartResponse ;
713
14+ import io .f1 .backend .domain .game .dto .ChatMessage ;
815import io .f1 .backend .domain .game .dto .MessageType ;
16+ import io .f1 .backend .domain .game .dto .RoomEventType ;
917import io .f1 .backend .domain .game .dto .request .GameSettingChanger ;
1018import io .f1 .backend .domain .game .dto .response .PlayerListResponse ;
19+ import io .f1 .backend .domain .game .event .GameCorrectAnswerEvent ;
20+ import io .f1 .backend .domain .game .event .GameTimeoutEvent ;
1121import io .f1 .backend .domain .game .event .RoomUpdatedEvent ;
1222import io .f1 .backend .domain .game .model .Player ;
1323import io .f1 .backend .domain .game .model .Room ;
2636import lombok .extern .slf4j .Slf4j ;
2737
2838import org .springframework .context .ApplicationEventPublisher ;
39+ import org .springframework .context .event .EventListener ;
2940import org .springframework .stereotype .Service ;
3041
3142import java .util .List ;
43+ import java .util .Map ;
3244import java .util .Objects ;
3345
3446@ Slf4j
3547@ Service
3648@ RequiredArgsConstructor
3749public class GameService {
3850
39- public static final int START_DELAY = 5 ;
51+ private static final int START_DELAY = 5 ;
52+ private static final int CONTINUE_DELAY = 3 ;
53+ private static final String NONE_CORRECT_USER = "" ;
4054
41- private final MessageSender messageSender ;
42- private final TimerService timerService ;
4355 private final QuizService quizService ;
56+ private final RoomService roomService ;
57+ private final TimerService timerService ;
58+ private final MessageSender messageSender ;
4459 private final RoomRepository roomRepository ;
4560 private final ApplicationEventPublisher eventPublisher ;
4661
@@ -67,13 +82,114 @@ public void gameStart(Long roomId, UserPrincipal principal) {
6782
6883 timerService .startTimer (room , START_DELAY );
6984
70- messageSender .send (destination , MessageType .GAME_START , toGameStartResponse (questions ));
71- messageSender .send (
85+ messageSender .sendBroadcast (
86+ destination , MessageType .GAME_START , toGameStartResponse (questions ));
87+ messageSender .sendBroadcast (
88+ destination , MessageType .RANK_UPDATE , toRankUpdateResponse (room ));
89+ messageSender .sendBroadcast (
7290 destination ,
7391 MessageType .QUESTION_START ,
7492 toQuestionStartResponse (room , START_DELAY ));
7593 }
7694
95+ @ EventListener
96+ public void onCorrectAnswer (GameCorrectAnswerEvent event ) {
97+
98+ Room room = event .room ();
99+ String sessionId = event .sessionId ();
100+ ChatMessage chatMessage = event .chatMessage ();
101+ String answer = event .answer ();
102+
103+ String destination = getDestination (room .getId ());
104+
105+ room .increasePlayerCorrectCount (sessionId );
106+
107+ messageSender .sendBroadcast (
108+ destination ,
109+ MessageType .QUESTION_RESULT ,
110+ toQuestionResultResponse (chatMessage .nickname (), answer ));
111+ messageSender .sendBroadcast (
112+ destination , MessageType .RANK_UPDATE , toRankUpdateResponse (room ));
113+ messageSender .sendBroadcast (
114+ destination ,
115+ MessageType .SYSTEM_NOTICE ,
116+ ofPlayerEvent (chatMessage .nickname (), RoomEventType .CORRECT_ANSWER ));
117+
118+ timerService .cancelTimer (room );
119+
120+ if (!timerService .validateCurrentRound (room )) {
121+ gameEnd (room );
122+ return ;
123+ }
124+
125+ room .increaseCurrentRound ();
126+
127+ // ํ์ด๋จธ ์ถ๊ฐํ๊ธฐ
128+ timerService .startTimer (room , CONTINUE_DELAY );
129+ messageSender .sendBroadcast (
130+ destination ,
131+ MessageType .QUESTION_START ,
132+ toQuestionStartResponse (room , CONTINUE_DELAY ));
133+ }
134+
135+ @ EventListener
136+ public void onTimeout (GameTimeoutEvent event ) {
137+ Room room = event .room ();
138+ String destination = getDestination (room .getId ());
139+
140+ messageSender .sendBroadcast (
141+ destination ,
142+ MessageType .QUESTION_RESULT ,
143+ toQuestionResultResponse (NONE_CORRECT_USER , room .getCurrentQuestion ().getAnswer ()));
144+ messageSender .sendBroadcast (
145+ destination ,
146+ MessageType .SYSTEM_NOTICE ,
147+ ofPlayerEvent (NONE_CORRECT_USER , RoomEventType .TIMEOUT ));
148+
149+ if (!timerService .validateCurrentRound (room )) {
150+ gameEnd (room );
151+ return ;
152+ }
153+
154+ room .increaseCurrentRound ();
155+
156+ timerService .startTimer (room , CONTINUE_DELAY );
157+ messageSender .sendBroadcast (
158+ destination ,
159+ MessageType .QUESTION_START ,
160+ toQuestionStartResponse (room , CONTINUE_DELAY ));
161+ }
162+
163+ public void gameEnd (Room room ) {
164+ Long roomId = room .getId ();
165+ String destination = getDestination (roomId );
166+
167+ Map <String , Player > playerSessionMap = room .getPlayerSessionMap ();
168+
169+ // TODO : ๋ญํน ์ ๋ณด ์
๋ฐ์ดํธ
170+ messageSender .sendBroadcast (
171+ destination ,
172+ MessageType .GAME_RESULT ,
173+ toGameResultListResponse (playerSessionMap , room .getGameSetting ().getRound ()));
174+
175+ room .initializeRound ();
176+ room .initializePlayers ();
177+
178+ List <Player > disconnectedPlayers = room .getDisconnectedPlayers ();
179+ roomService .handleDisconnectedPlayers (room , disconnectedPlayers );
180+
181+ room .updateRoomState (RoomState .WAITING );
182+
183+ messageSender .sendBroadcast (
184+ destination ,
185+ MessageType .GAME_SETTING ,
186+ toGameSettingResponse (
187+ room .getGameSetting (),
188+ quizService .getQuizWithQuestionsById (room .getGameSetting ().getQuizId ())));
189+ messageSender .sendBroadcast (
190+ destination , MessageType .ROOM_SETTING , toRoomSettingResponse (room ));
191+ }
192+
77193 public void handlePlayerReady (Long roomId , String sessionId ) {
78194
79195 Room room = findRoom (roomId );
@@ -86,7 +202,7 @@ public void handlePlayerReady(Long roomId, String sessionId) {
86202
87203 PlayerListResponse playerListResponse = toPlayerListResponse (room );
88204 log .info (playerListResponse .toString ());
89- messageSender .send (destination , MessageType .PLAYER_LIST , playerListResponse );
205+ messageSender .sendBroadcast (destination , MessageType .PLAYER_LIST , playerListResponse );
90206 }
91207
92208 public void changeGameSetting (
@@ -136,10 +252,6 @@ private Room findRoom(Long roomId) {
136252 .orElseThrow (() -> new CustomException (RoomErrorCode .ROOM_NOT_FOUND ));
137253 }
138254
139- private String getDestination (Long roomId ) {
140- return "/sub/room/" + roomId ;
141- }
142-
143255 private void validateHostAndState (Room room , UserPrincipal principal ) {
144256 if (!room .isHost (principal .getUserId ())) {
145257 throw new CustomException (RoomErrorCode .NOT_ROOM_OWNER );
@@ -161,7 +273,7 @@ private void toggleReadyIfPossible(Room room, Player player) {
161273 private void broadcastGameSetting (Room room ) {
162274 String destination = getDestination (room .getId ());
163275 Quiz quiz = quizService .getQuizWithQuestionsById (room .getGameSetting ().getQuizId ());
164- messageSender .send (
276+ messageSender .sendBroadcast (
165277 destination ,
166278 MessageType .GAME_SETTING ,
167279 toGameSettingResponse (room .getGameSetting (), quiz ));
0 commit comments