|
1 | 1 | package io.f1.backend.domain.game.app; |
2 | 2 |
|
| 3 | +import static io.f1.backend.domain.game.mapper.RoomMapper.ofPlayerEvent; |
3 | 4 | import static io.f1.backend.domain.game.mapper.RoomMapper.toGameResultListResponse; |
4 | 5 | import static io.f1.backend.domain.game.mapper.RoomMapper.toGameSettingResponse; |
5 | | -import static io.f1.backend.domain.game.mapper.RoomMapper.toPlayerListResponse; |
| 6 | +import static io.f1.backend.domain.game.mapper.RoomMapper.toQuestionResultResponse; |
6 | 7 | import static io.f1.backend.domain.game.mapper.RoomMapper.toQuestionStartResponse; |
7 | 8 | import static io.f1.backend.domain.game.mapper.RoomMapper.toRankUpdateResponse; |
8 | 9 | import static io.f1.backend.domain.game.mapper.RoomMapper.toRoomSettingResponse; |
9 | 10 | import static io.f1.backend.domain.game.websocket.WebSocketUtils.getDestination; |
10 | 11 | import static io.f1.backend.domain.quiz.mapper.QuizMapper.toGameStartResponse; |
11 | 12 |
|
| 13 | +import io.f1.backend.domain.game.dto.ChatMessage; |
12 | 14 | import io.f1.backend.domain.game.dto.MessageType; |
| 15 | +import io.f1.backend.domain.game.dto.RoomEventType; |
| 16 | +import io.f1.backend.domain.game.event.GameCorrectAnswerEvent; |
| 17 | +import io.f1.backend.domain.game.event.GameTimeoutEvent; |
13 | 18 | import io.f1.backend.domain.game.event.RoomUpdatedEvent; |
14 | | -import io.f1.backend.domain.game.model.ConnectionState; |
15 | 19 | import io.f1.backend.domain.game.model.Player; |
16 | 20 | import io.f1.backend.domain.game.model.Room; |
17 | 21 | import io.f1.backend.domain.game.model.RoomState; |
|
28 | 32 | import lombok.RequiredArgsConstructor; |
29 | 33 |
|
30 | 34 | import org.springframework.context.ApplicationEventPublisher; |
| 35 | +import org.springframework.context.event.EventListener; |
31 | 36 | import org.springframework.stereotype.Service; |
32 | 37 |
|
33 | | -import java.util.ArrayList; |
34 | 38 | import java.util.List; |
35 | 39 | import java.util.Map; |
36 | 40 | import java.util.Objects; |
|
40 | 44 | public class GameService { |
41 | 45 |
|
42 | 46 | private static final int START_DELAY = 5; |
| 47 | + private static final int CONTINUE_DELAY = 3; |
| 48 | + private static final String NONE_CORRECT_USER = ""; |
43 | 49 |
|
44 | 50 | private final QuizService quizService; |
45 | 51 | private final RoomService roomService; |
@@ -79,42 +85,97 @@ public void gameStart(Long roomId, UserPrincipal principal) { |
79 | 85 | toQuestionStartResponse(room, START_DELAY)); |
80 | 86 | } |
81 | 87 |
|
82 | | - public void gameEnd(Room room) { |
83 | | - room.updateRoomState(RoomState.FINISHED); |
| 88 | + @EventListener |
| 89 | + public void onCorrectAnswer(GameCorrectAnswerEvent event) { |
| 90 | + |
| 91 | + Room room = event.room(); |
| 92 | + String sessionId = event.sessionId(); |
| 93 | + ChatMessage chatMessage = event.chatMessage(); |
| 94 | + String answer = event.answer(); |
| 95 | + |
| 96 | + String destination = getDestination(room.getId()); |
| 97 | + |
| 98 | + room.increasePlayerCorrectCount(sessionId); |
| 99 | + |
| 100 | + messageSender.send( |
| 101 | + destination, |
| 102 | + MessageType.QUESTION_RESULT, |
| 103 | + toQuestionResultResponse(chatMessage.nickname(), answer)); |
| 104 | + messageSender.send(destination, MessageType.RANK_UPDATE, toRankUpdateResponse(room)); |
| 105 | + messageSender.send( |
| 106 | + destination, |
| 107 | + MessageType.SYSTEM_NOTICE, |
| 108 | + ofPlayerEvent(chatMessage.nickname(), RoomEventType.CORRECT_ANSWER)); |
| 109 | + |
| 110 | + timerService.cancelTimer(room); |
| 111 | + |
| 112 | + if (!timerService.validateCurrentRound(room)) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + room.increaseCurrentRound(); |
| 117 | + |
| 118 | + // 타이머 추가하기 |
| 119 | + timerService.startTimer(room, CONTINUE_DELAY); |
| 120 | + messageSender.send( |
| 121 | + destination, |
| 122 | + MessageType.QUESTION_START, |
| 123 | + toQuestionStartResponse(room, CONTINUE_DELAY)); |
| 124 | + } |
| 125 | + |
| 126 | + @EventListener |
| 127 | + public void onTimeout(GameTimeoutEvent event) { |
| 128 | + Room room = event.room(); |
| 129 | + String destination = getDestination(room.getId()); |
84 | 130 |
|
| 131 | + messageSender.send( |
| 132 | + destination, |
| 133 | + MessageType.QUESTION_RESULT, |
| 134 | + toQuestionResultResponse(NONE_CORRECT_USER, room.getCurrentQuestion().getAnswer())); |
| 135 | + messageSender.send( |
| 136 | + destination, |
| 137 | + MessageType.SYSTEM_NOTICE, |
| 138 | + ofPlayerEvent(NONE_CORRECT_USER, RoomEventType.TIMEOUT)); |
| 139 | + |
| 140 | + if (!timerService.validateCurrentRound(room)) { |
| 141 | + gameEnd(room); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + room.increaseCurrentRound(); |
| 146 | + |
| 147 | + timerService.startTimer(room, CONTINUE_DELAY); |
| 148 | + messageSender.send( |
| 149 | + destination, |
| 150 | + MessageType.QUESTION_START, |
| 151 | + toQuestionStartResponse(room, CONTINUE_DELAY)); |
| 152 | + } |
| 153 | + |
| 154 | + public void gameEnd(Room room) { |
85 | 155 | Long roomId = room.getId(); |
86 | 156 | String destination = getDestination(roomId); |
87 | 157 |
|
88 | 158 | Map<String, Player> playerSessionMap = room.getPlayerSessionMap(); |
89 | 159 |
|
| 160 | + // TODO : 랭킹 정보 업데이트 |
90 | 161 | messageSender.send( |
91 | 162 | destination, |
92 | 163 | MessageType.GAME_RESULT, |
93 | 164 | toGameResultListResponse(playerSessionMap, room.getGameSetting().getRound())); |
94 | 165 |
|
95 | | - List<Player> disconnectedPlayers = new ArrayList<>(); |
96 | | - |
97 | 166 | room.initializeRound(); |
98 | | - for (Player player : playerSessionMap.values()) { |
99 | | - if (player.getState().equals(ConnectionState.DISCONNECTED)) { |
100 | | - disconnectedPlayers.add(player); |
101 | | - } |
102 | | - player.initializeCorrectCount(); |
103 | | - player.toggleReady(); |
104 | | - } |
105 | | - |
106 | | - for (Player player : disconnectedPlayers) { |
107 | | - String sessionId = room.getUserIdSessionMap().get(player.id); |
108 | | - roomService.exitRoomForDisconnectedPlayer(roomId, player, sessionId); |
109 | | - } |
| 167 | + room.initializePlayers(); |
110 | 168 |
|
111 | 169 | room.updateRoomState(RoomState.WAITING); |
112 | | - messageSender.send(destination, MessageType.PLAYER_LIST, toPlayerListResponse(room)); |
| 170 | + |
113 | 171 | messageSender.send( |
114 | 172 | destination, |
115 | 173 | MessageType.GAME_SETTING, |
116 | 174 | toGameSettingResponse(room.getGameSetting(), room.getCurrentQuestion().getQuiz())); |
117 | 175 | messageSender.send(destination, MessageType.ROOM_SETTING, toRoomSettingResponse(room)); |
| 176 | + |
| 177 | + List<Player> disconnectedPlayers = room.getDisconnectedPlayers(); |
| 178 | + roomService.handleDisconnectedPlayers(room, disconnectedPlayers); |
118 | 179 | } |
119 | 180 |
|
120 | 181 | private boolean validateReadyStatus(Room room) { |
|
0 commit comments