1313import static io .f1 .backend .global .util .SecurityUtils .getCurrentUserNickname ;
1414
1515import io .f1 .backend .domain .game .dto .ChatMessage ;
16+ import io .f1 .backend .domain .game .dto .MessageType ;
1617import io .f1 .backend .domain .game .dto .RoomEventType ;
17- import io .f1 .backend .domain .game .dto .RoomExitData ;
18- import io .f1 .backend .domain .game .dto .RoomInitialData ;
19- import io .f1 .backend .domain .game .dto .RoundResult ;
2018import io .f1 .backend .domain .game .dto .request .RoomCreateRequest ;
2119import io .f1 .backend .domain .game .dto .request .RoomValidationRequest ;
2220import io .f1 .backend .domain .game .dto .response .GameSettingResponse ;
3230import io .f1 .backend .domain .game .model .RoomSetting ;
3331import io .f1 .backend .domain .game .model .RoomState ;
3432import io .f1 .backend .domain .game .store .RoomRepository ;
33+ import io .f1 .backend .domain .game .websocket .MessageSender ;
3534import io .f1 .backend .domain .question .entity .Question ;
3635import io .f1 .backend .domain .quiz .app .QuizService ;
3736import io .f1 .backend .domain .quiz .dto .QuizMinData ;
@@ -62,6 +61,7 @@ public class RoomService {
6261 private final AtomicLong roomIdGenerator = new AtomicLong (0 );
6362 private final ApplicationEventPublisher eventPublisher ;
6463 private final Map <Long , Object > roomLocks = new ConcurrentHashMap <>();
64+ private final MessageSender messageSender ;
6565 private static final String PENDING_SESSION_ID = "PENDING_SESSION_ID" ;
6666
6767 public RoomCreateResponse saveRoom (RoomCreateRequest request ) {
@@ -116,8 +116,7 @@ public void enterRoom(RoomValidationRequest request) {
116116 }
117117 }
118118
119- public RoomInitialData initializeRoomSocket (
120- Long roomId , String sessionId , UserPrincipal principal ) {
119+ public void initializeRoomSocket (Long roomId , String sessionId , UserPrincipal principal ) {
121120
122121 Room room = findRoom (roomId );
123122
@@ -150,11 +149,15 @@ public RoomInitialData initializeRoomSocket(
150149 SystemNoticeResponse systemNoticeResponse =
151150 ofPlayerEvent (player .getNickname (), RoomEventType .ENTER );
152151
153- return new RoomInitialData (
154- roomSettingResponse , gameSettingResponse , playerListResponse , systemNoticeResponse );
152+ String destination = getDestination (roomId );
153+
154+ messageSender .send (destination , MessageType .ROOM_SETTING , roomSettingResponse );
155+ messageSender .send (destination , MessageType .GAME_SETTING , gameSettingResponse );
156+ messageSender .send (destination , MessageType .PLAYER_LIST , playerListResponse );
157+ messageSender .send (destination , MessageType .SYSTEM_NOTICE , systemNoticeResponse );
155158 }
156159
157- public RoomExitData exitRoom (Long roomId , String sessionId , UserPrincipal principal ) {
160+ public void exitRoom (Long roomId , String sessionId , UserPrincipal principal ) {
158161
159162 Object lock = roomLocks .computeIfAbsent (roomId , k -> new Object ());
160163
@@ -165,7 +168,8 @@ public RoomExitData exitRoom(Long roomId, String sessionId, UserPrincipal princi
165168
166169 /* 방 삭제 */
167170 if (isLastPlayer (room , sessionId )) {
168- return removeRoom (room );
171+ removeRoom (room );
172+ return ;
169173 }
170174
171175 /* 방장 변경 */
@@ -181,11 +185,14 @@ public RoomExitData exitRoom(Long roomId, String sessionId, UserPrincipal princi
181185
182186 PlayerListResponse playerListResponse = toPlayerListResponse (room );
183187
184- return new RoomExitData (playerListResponse , systemNoticeResponse , false );
188+ String destination = getDestination (roomId );
189+
190+ messageSender .send (destination , MessageType .PLAYER_LIST , playerListResponse );
191+ messageSender .send (destination , MessageType .SYSTEM_NOTICE , systemNoticeResponse );
185192 }
186193 }
187194
188- public PlayerListResponse handlePlayerReady (Long roomId , String sessionId ) {
195+ public void handlePlayerReady (Long roomId , String sessionId ) {
189196 Player player =
190197 roomRepository
191198 .findPlayerInRoomBySessionId (roomId , sessionId )
@@ -195,7 +202,9 @@ public PlayerListResponse handlePlayerReady(Long roomId, String sessionId) {
195202
196203 Room room = findRoom (roomId );
197204
198- return toPlayerListResponse (room );
205+ String destination = getDestination (roomId );
206+
207+ messageSender .send (destination , MessageType .PLAYER_LIST , toPlayerListResponse (room ));
199208 }
200209
201210 public RoomListResponse getAllRooms () {
@@ -214,30 +223,34 @@ public RoomListResponse getAllRooms() {
214223 }
215224
216225 // todo 동시성적용
217- public RoundResult chat (Long roomId , String sessionId , ChatMessage chatMessage ) {
226+ public void chat (Long roomId , String sessionId , ChatMessage chatMessage ) {
218227 Room room = findRoom (roomId );
219228
229+ String destination = getDestination (roomId );
230+
231+ messageSender .send (destination , MessageType .CHAT , chatMessage );
232+
220233 if (!room .isPlaying ()) {
221- return buildResultOnlyChat ( chatMessage ) ;
234+ return ;
222235 }
223236
224237 Question currentQuestion = room .getCurrentQuestion ();
225238
226239 String answer = currentQuestion .getAnswer ();
227240
228- if (!answer .equals (chatMessage .message ())) {
229- return buildResultOnlyChat (chatMessage );
241+ if (answer .equals (chatMessage .message ())) {
242+ room .increasePlayerCorrectCount (sessionId );
243+
244+ messageSender .send (
245+ destination ,
246+ MessageType .QUESTION_RESULT ,
247+ toQuestionResultResponse (currentQuestion .getId (), chatMessage , answer ));
248+ messageSender .send (destination , MessageType .RANK_UPDATE , toRankUpdateResponse (room ));
249+ messageSender .send (
250+ destination ,
251+ MessageType .SYSTEM_NOTICE ,
252+ ofPlayerEvent (chatMessage .nickname (), RoomEventType .ENTER ));
230253 }
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 ();
241254 }
242255
243256 private Player getRemovePlayer (Room room , String sessionId , UserPrincipal principal ) {
@@ -268,12 +281,11 @@ private boolean isLastPlayer(Room room, String sessionId) {
268281 return playerSessionMap .size () == 1 && playerSessionMap .containsKey (sessionId );
269282 }
270283
271- private RoomExitData removeRoom (Room room ) {
284+ private void removeRoom (Room room ) {
272285 Long roomId = room .getId ();
273286 roomRepository .removeRoom (roomId );
274287 roomLocks .remove (roomId );
275288 log .info ("{}번 방 삭제" , roomId );
276- return RoomExitData .builder ().removedRoom (true ).build ();
277289 }
278290
279291 private void changeHost (Room room , String hostSessionId ) {
@@ -298,7 +310,7 @@ private void removePlayer(Room room, String sessionId, Player removePlayer) {
298310 room .removeSessionId (sessionId );
299311 }
300312
301- private RoundResult buildResultOnlyChat ( ChatMessage chatMessage ) {
302- return RoundResult . builder (). chat ( chatMessage ). build () ;
313+ private String getDestination ( Long roomId ) {
314+ return "/sub/room/" + roomId ;
303315 }
304316}
0 commit comments