44import static io .f1 .backend .domain .game .mapper .RoomMapper .toGameSetting ;
55import static io .f1 .backend .domain .game .mapper .RoomMapper .toGameSettingResponse ;
66import static io .f1 .backend .domain .game .mapper .RoomMapper .toPlayerListResponse ;
7+ import static io .f1 .backend .domain .game .mapper .RoomMapper .toQuestionResultResponse ;
8+ import static io .f1 .backend .domain .game .mapper .RoomMapper .toRankUpdateResponse ;
79import static io .f1 .backend .domain .game .mapper .RoomMapper .toRoomResponse ;
810import static io .f1 .backend .domain .game .mapper .RoomMapper .toRoomSetting ;
911import static io .f1 .backend .domain .game .mapper .RoomMapper .toRoomSettingResponse ;
1012import static io .f1 .backend .global .util .SecurityUtils .getCurrentUserId ;
1113import static io .f1 .backend .global .util .SecurityUtils .getCurrentUserNickname ;
1214
15+ import io .f1 .backend .domain .game .dto .ChatMessage ;
1316import io .f1 .backend .domain .game .dto .RoomEventType ;
1417import io .f1 .backend .domain .game .dto .RoomExitData ;
1518import io .f1 .backend .domain .game .dto .RoomInitialData ;
19+ import io .f1 .backend .domain .game .dto .RoundResult ;
1620import io .f1 .backend .domain .game .dto .request .RoomCreateRequest ;
1721import io .f1 .backend .domain .game .dto .request .RoomValidationRequest ;
1822import io .f1 .backend .domain .game .dto .response .GameSettingResponse ;
2226import io .f1 .backend .domain .game .dto .response .RoomResponse ;
2327import io .f1 .backend .domain .game .dto .response .RoomSettingResponse ;
2428import io .f1 .backend .domain .game .dto .response .SystemNoticeResponse ;
25- import io .f1 .backend .domain .game .event .RoomCreatedEvent ;
2629import io .f1 .backend .domain .game .model .GameSetting ;
2730import io .f1 .backend .domain .game .model .Player ;
2831import io .f1 .backend .domain .game .model .Room ;
2932import io .f1 .backend .domain .game .model .RoomSetting ;
3033import io .f1 .backend .domain .game .model .RoomState ;
3134import io .f1 .backend .domain .game .store .RoomRepository ;
35+ import io .f1 .backend .domain .question .entity .Question ;
3236import io .f1 .backend .domain .quiz .app .QuizService ;
37+ import io .f1 .backend .domain .quiz .dto .QuizMinData ;
3338import io .f1 .backend .domain .quiz .entity .Quiz ;
39+ import io .f1 .backend .domain .user .dto .UserPrincipal ;
3440import io .f1 .backend .global .exception .CustomException ;
3541import io .f1 .backend .global .exception .errorcode .RoomErrorCode ;
3642
@@ -60,10 +66,10 @@ public class RoomService {
6066
6167 public RoomCreateResponse saveRoom (RoomCreateRequest request ) {
6268
63- Long quizMinId = quizService .getQuizMinId ();
64- Quiz quiz = quizService .getQuizWithQuestionsById (quizMinId );
69+ QuizMinData quizMinData = quizService .getQuizMinData ();
70+ // Quiz quiz = quizService.getQuizWithQuestionsById(quizMinId);
6571
66- GameSetting gameSetting = toGameSetting (quiz );
72+ GameSetting gameSetting = toGameSetting (quizMinData );
6773
6874 Player host = createPlayer ();
6975
@@ -77,7 +83,7 @@ public RoomCreateResponse saveRoom(RoomCreateRequest request) {
7783
7884 roomRepository .saveRoom (room );
7985
80- eventPublisher .publishEvent (new RoomCreatedEvent (room , quiz ));
86+ // eventPublisher.publishEvent(new RoomCreatedEvent(room, quiz));
8187
8288 return new RoomCreateResponse (newId );
8389 }
@@ -110,11 +116,12 @@ public void enterRoom(RoomValidationRequest request) {
110116 }
111117 }
112118
113- public RoomInitialData initializeRoomSocket (Long roomId , String sessionId ) {
119+ public RoomInitialData initializeRoomSocket (
120+ Long roomId , String sessionId , UserPrincipal principal ) {
114121
115122 Room room = findRoom (roomId );
116123
117- Player player = createPlayer ();
124+ Player player = createPlayer (principal );
118125
119126 Map <String , Player > playerSessionMap = room .getPlayerSessionMap ();
120127 Map <Long , String > userIdSessionMap = room .getUserIdSessionMap ();
@@ -140,30 +147,25 @@ public RoomInitialData initializeRoomSocket(Long roomId, String sessionId) {
140147
141148 PlayerListResponse playerListResponse = toPlayerListResponse (room );
142149
143- SystemNoticeResponse systemNoticeResponse = ofPlayerEvent (player , RoomEventType .ENTER );
150+ SystemNoticeResponse systemNoticeResponse =
151+ ofPlayerEvent (player .getNickname (), RoomEventType .ENTER );
144152
145153 return new RoomInitialData (
146- getDestination (roomId ),
147- roomSettingResponse ,
148- gameSettingResponse ,
149- playerListResponse ,
150- systemNoticeResponse );
154+ roomSettingResponse , gameSettingResponse , playerListResponse , systemNoticeResponse );
151155 }
152156
153- public RoomExitData exitRoom (Long roomId , String sessionId ) {
157+ public RoomExitData exitRoom (Long roomId , String sessionId , UserPrincipal principal ) {
154158
155159 Object lock = roomLocks .computeIfAbsent (roomId , k -> new Object ());
156160
157161 synchronized (lock ) {
158162 Room room = findRoom (roomId );
159163
160- String destination = getDestination (roomId );
161-
162- Player removePlayer = getRemovePlayer (room , sessionId );
164+ Player removePlayer = getRemovePlayer (room , sessionId , principal );
163165
164166 /* ๋ฐฉ ์ญ์ */
165167 if (isLastPlayer (room , sessionId )) {
166- return removeRoom (room , destination );
168+ return removeRoom (room );
167169 }
168170
169171 /* ๋ฐฉ์ฅ ๋ณ๊ฒฝ */
@@ -175,14 +177,27 @@ public RoomExitData exitRoom(Long roomId, String sessionId) {
175177 removePlayer (room , sessionId , removePlayer );
176178
177179 SystemNoticeResponse systemNoticeResponse =
178- ofPlayerEvent (removePlayer , RoomEventType .EXIT );
180+ ofPlayerEvent (removePlayer . nickname , RoomEventType .EXIT );
179181
180182 PlayerListResponse playerListResponse = toPlayerListResponse (room );
181183
182- return new RoomExitData (destination , playerListResponse , systemNoticeResponse , false );
184+ return new RoomExitData (playerListResponse , systemNoticeResponse , false );
183185 }
184186 }
185187
188+ public PlayerListResponse handlePlayerReady (Long roomId , String sessionId ) {
189+ Player player =
190+ roomRepository
191+ .findPlayerInRoomBySessionId (roomId , sessionId )
192+ .orElseThrow (() -> new CustomException (RoomErrorCode .PLAYER_NOT_FOUND ));
193+
194+ player .toggleReady ();
195+
196+ Room room = findRoom (roomId );
197+
198+ return toPlayerListResponse (room );
199+ }
200+
186201 public RoomListResponse getAllRooms () {
187202 List <Room > rooms = roomRepository .findAll ();
188203 List <RoomResponse > roomResponses =
@@ -198,17 +213,44 @@ public RoomListResponse getAllRooms() {
198213 return new RoomListResponse (roomResponses );
199214 }
200215
201- private Player getRemovePlayer (Room room , String sessionId ) {
216+ // todo ๋์์ฑ์ ์ฉ
217+ public RoundResult chat (Long roomId , String sessionId , ChatMessage chatMessage ) {
218+ Room room = findRoom (roomId );
219+
220+ if (!room .isPlaying ()) {
221+ return buildResultOnlyChat (chatMessage );
222+ }
223+
224+ Question currentQuestion = room .getCurrentQuestion ();
225+
226+ String answer = currentQuestion .getAnswer ();
227+
228+ if (!answer .equals (chatMessage .message ())) {
229+ return buildResultOnlyChat (chatMessage );
230+ }
231+
232+ room .increasePlayerCorrectCount (sessionId );
233+
234+ return RoundResult .builder ()
235+ .questionResult (
236+ toQuestionResultResponse (currentQuestion .getId (), chatMessage , answer ))
237+ .rankUpdate (toRankUpdateResponse (room ))
238+ .systemNotice (ofPlayerEvent (chatMessage .nickname (), RoomEventType .ENTER ))
239+ .chat (chatMessage )
240+ .build ();
241+ }
242+
243+ private Player getRemovePlayer (Room room , String sessionId , UserPrincipal principal ) {
202244 Player removePlayer = room .getPlayerSessionMap ().get (sessionId );
203245 if (removePlayer == null ) {
204- room .removeUserId (getCurrentUserId ());
246+ room .removeUserId (principal . getUserId ());
205247 throw new CustomException (RoomErrorCode .SOCKET_SESSION_NOT_FOUND );
206248 }
207249 return removePlayer ;
208250 }
209251
210- private static String getDestination ( Long roomId ) {
211- return "/sub/room/" + roomId ;
252+ private Player createPlayer ( UserPrincipal principal ) {
253+ return new Player ( principal . getUserId (), principal . getUserNickname ()) ;
212254 }
213255
214256 private Player createPlayer () {
@@ -226,12 +268,12 @@ private boolean isLastPlayer(Room room, String sessionId) {
226268 return playerSessionMap .size () == 1 && playerSessionMap .containsKey (sessionId );
227269 }
228270
229- private RoomExitData removeRoom (Room room , String destination ) {
271+ private RoomExitData removeRoom (Room room ) {
230272 Long roomId = room .getId ();
231273 roomRepository .removeRoom (roomId );
232274 roomLocks .remove (roomId );
233275 log .info ("{}๋ฒ ๋ฐฉ ์ญ์ " , roomId );
234- return RoomExitData .builder ().destination ( destination ). removedRoom (true ).build ();
276+ return RoomExitData .builder ().removedRoom (true ).build ();
235277 }
236278
237279 private void changeHost (Room room , String hostSessionId ) {
@@ -255,4 +297,8 @@ private void removePlayer(Room room, String sessionId, Player removePlayer) {
255297 room .removeUserId (removePlayer .getId ());
256298 room .removeSessionId (sessionId );
257299 }
300+
301+ private RoundResult buildResultOnlyChat (ChatMessage chatMessage ) {
302+ return RoundResult .builder ().chat (chatMessage ).build ();
303+ }
258304}
0 commit comments