11package io .f1 .backend .domain .game .app ;
22
3- import static io .f1 .backend .domain .game .mapper .RoomMapper .*;
4-
3+ import static io .f1 .backend .domain .game .mapper .RoomMapper .ofPlayerEvent ;
4+ import static io .f1 .backend .domain .game .mapper .RoomMapper .toGameSetting ;
5+ import static io .f1 .backend .domain .game .mapper .RoomMapper .toGameSettingResponse ;
6+ import static io .f1 .backend .domain .game .mapper .RoomMapper .toPlayerListResponse ;
7+ import static io .f1 .backend .domain .game .mapper .RoomMapper .toRoomResponse ;
8+ import static io .f1 .backend .domain .game .mapper .RoomMapper .toRoomSetting ;
9+ import static io .f1 .backend .domain .game .mapper .RoomMapper .toRoomSettingResponse ;
10+ import static io .f1 .backend .global .util .SecurityUtils .getCurrentUserId ;
11+ import static io .f1 .backend .global .util .SecurityUtils .getCurrentUserNickname ;
12+
13+ import io .f1 .backend .domain .game .dto .RoomEventType ;
14+ import io .f1 .backend .domain .game .dto .RoomExitData ;
515import io .f1 .backend .domain .game .dto .RoomInitialData ;
616import io .f1 .backend .domain .game .dto .request .RoomCreateRequest ;
717import io .f1 .backend .domain .game .dto .request .RoomValidationRequest ;
818import io .f1 .backend .domain .game .dto .response .GameSettingResponse ;
919import io .f1 .backend .domain .game .dto .response .PlayerListResponse ;
10- import io .f1 .backend .domain .game .dto .response .QuizResponse ;
1120import io .f1 .backend .domain .game .dto .response .RoomCreateResponse ;
1221import io .f1 .backend .domain .game .dto .response .RoomListResponse ;
1322import io .f1 .backend .domain .game .dto .response .RoomResponse ;
1423import io .f1 .backend .domain .game .dto .response .RoomSettingResponse ;
24+ import io .f1 .backend .domain .game .dto .response .SystemNoticeResponse ;
1525import io .f1 .backend .domain .game .event .RoomCreatedEvent ;
1626import io .f1 .backend .domain .game .model .GameSetting ;
1727import io .f1 .backend .domain .game .model .Player ;
2939
3040import java .util .List ;
3141import java .util .Map ;
42+ import java .util .Optional ;
3243import java .util .concurrent .atomic .AtomicLong ;
3344
3445@ Service
@@ -40,12 +51,15 @@ public class RoomService {
4051 private final AtomicLong roomIdGenerator = new AtomicLong (0 );
4152 private final ApplicationEventPublisher eventPublisher ;
4253
43- public RoomCreateResponse saveRoom (RoomCreateRequest request , Map <String , Object > loginUser ) {
54+ public RoomCreateResponse saveRoom (RoomCreateRequest request ) {
55+
56+ Long quizMinId = quizService .getQuizMinId ();
57+ Quiz quiz = quizService .getQuizById (quizMinId );
58+
59+ GameSetting gameSetting = toGameSetting (quiz );
60+
61+ Player host = createPlayer ();
4462
45- // todo 제일 작은 index quizId 가져와서 gameSetting(round 설정)
46- GameSetting gameSetting = new GameSetting (1L , 10 , 60 );
47- // todo security에서 가져오는걸로 변경
48- Player host = new Player ((Long ) loginUser .get ("id" ), loginUser .get ("nickname" ).toString ());
4963 RoomSetting roomSetting = toRoomSetting (request );
5064
5165 Long newId = roomIdGenerator .incrementAndGet ();
@@ -54,9 +68,6 @@ public RoomCreateResponse saveRoom(RoomCreateRequest request, Map<String, Object
5468
5569 roomRepository .saveRoom (room );
5670
57- Long quizId = room .getGameSetting ().getQuizId ();
58- Quiz quiz = quizService .getQuizById (quizId );
59-
6071 eventPublisher .publishEvent (new RoomCreatedEvent (room , quiz ));
6172
6273 return new RoomCreateResponse (newId );
@@ -67,7 +78,7 @@ public void validateRoom(RoomValidationRequest request) {
6778 Room room =
6879 roomRepository
6980 .findRoom (request .roomId ())
70- .orElseThrow (() -> new IllegalArgumentException ("404 존재하지 않는 방입니다." ));
81+ .orElseThrow (() -> new IllegalArgumentException ("404 존재하지 않는 방입니다.-1 " ));
7182
7283 if (room .getState ().equals (RoomState .PLAYING )) {
7384 throw new IllegalArgumentException ("403 게임이 진행중입니다." );
@@ -92,26 +103,69 @@ public RoomInitialData enterRoom(Long roomId, String sessionId) {
92103 .findRoom (roomId )
93104 .orElseThrow (() -> new IllegalArgumentException ("404 존재하지 않는 방입니다." ));
94105
95- // todo security
96- Player player = new Player (1L , "빵야빵야" );
106+ Player player = createPlayer ();
97107
98108 Map <String , Player > playerSessionMap = room .getPlayerSessionMap ();
99109
100110 playerSessionMap .put (sessionId , player );
101111
102- String destination = "/sub/room/" + roomId ;
103-
104112 RoomSettingResponse roomSettingResponse = toRoomSettingResponse (room );
105- // todo quiz 생성 api 완성 후 수정
106- QuizResponse quiz =
107- new QuizResponse (room .getGameSetting ().getQuizId (), "title" , "설명" , "url" , 10 );
113+
114+ Long quizId = room .getGameSetting ().getQuizId ();
115+ Quiz quiz = quizService .getQuizById (quizId );
116+
108117 GameSettingResponse gameSettingResponse =
109118 toGameSettingResponse (room .getGameSetting (), quiz );
110119
111120 PlayerListResponse playerListResponse = toPlayerListResponse (room );
112121
122+ SystemNoticeResponse systemNoticeResponse = ofPlayerEvent (player , RoomEventType .ENTER );
123+
113124 return new RoomInitialData (
114- destination , roomSettingResponse , gameSettingResponse , playerListResponse );
125+ getDestination (roomId ),
126+ roomSettingResponse ,
127+ gameSettingResponse ,
128+ playerListResponse ,
129+ systemNoticeResponse );
130+ }
131+
132+ public RoomExitData exitRoom (Long roomId , String sessionId ) {
133+ Room room =
134+ roomRepository
135+ .findRoom (roomId )
136+ .orElseThrow (() -> new IllegalArgumentException ("404 존재하지 않는 방입니다." ));
137+
138+ Map <String , Player > playerSessionMap = room .getPlayerSessionMap ();
139+
140+ String destination = getDestination (roomId );
141+
142+ if (playerSessionMap .size () == 1 && playerSessionMap .get (sessionId ) != null ) {
143+ roomRepository .removeRoom (roomId );
144+ return RoomExitData .builder ().destination (destination ).removedRoom (true ).build ();
145+ }
146+
147+ Player removedPlayer = playerSessionMap .remove (sessionId );
148+ if (removedPlayer == null ) {
149+ throw new IllegalArgumentException ("퇴장 처리 불가 - 404 해당 세션 플레이어는 존재하지않습니다." );
150+ }
151+
152+ if (room .getHost ().getId ().equals (removedPlayer .getId ())) {
153+ Optional <String > nextHostSessionId = playerSessionMap .keySet ().stream ().findFirst ();
154+ Player nextHost =
155+ playerSessionMap .get (
156+ nextHostSessionId .orElseThrow (
157+ () ->
158+ new IllegalArgumentException (
159+ "방장 교체 불가 - 404 해당 세션 플레이어는 존재하지않습니다." )));
160+ room .updateHost (nextHost );
161+ }
162+
163+ SystemNoticeResponse systemNoticeResponse =
164+ ofPlayerEvent (removedPlayer , RoomEventType .EXIT );
165+
166+ PlayerListResponse playerListResponse = toPlayerListResponse (room );
167+
168+ return new RoomExitData (destination , playerListResponse , systemNoticeResponse , false );
115169 }
116170
117171 public RoomListResponse getAllRooms () {
@@ -128,4 +182,12 @@ public RoomListResponse getAllRooms() {
128182 .toList ();
129183 return new RoomListResponse (roomResponses );
130184 }
185+
186+ private static String getDestination (Long roomId ) {
187+ return "/sub/room/" + roomId ;
188+ }
189+
190+ private static Player createPlayer () {
191+ return new Player (getCurrentUserId (), getCurrentUserNickname ());
192+ }
131193}
0 commit comments