Skip to content

Commit 7bc737f

Browse files
[feat] 게임 종료 후 전체 랭킹 업데이트 (#160)
* ✨ feat: 랭킹 업데이트 연결 * chore: Java 스타일 수정 * chore: Java 스타일 수정 * 🔧 chore: GameService 리팩토링 --------- Co-authored-by: github-actions <>
1 parent 8156578 commit 7bc737f

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

backend/src/main/java/io/f1/backend/domain/game/app/GameService.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
import io.f1.backend.domain.game.dto.MessageType;
1616
import io.f1.backend.domain.game.dto.RoomEventType;
1717
import io.f1.backend.domain.game.dto.request.GameSettingChanger;
18+
import io.f1.backend.domain.game.dto.response.GameResultListResponse;
19+
import io.f1.backend.domain.game.dto.response.GameResultResponse;
1820
import io.f1.backend.domain.game.event.GameCorrectAnswerEvent;
1921
import io.f1.backend.domain.game.event.GameTimeoutEvent;
2022
import io.f1.backend.domain.game.event.RoomUpdatedEvent;
23+
import io.f1.backend.domain.game.model.ConnectionState;
2124
import io.f1.backend.domain.game.model.Player;
2225
import io.f1.backend.domain.game.model.Room;
2326
import io.f1.backend.domain.game.model.RoomState;
@@ -26,6 +29,7 @@
2629
import io.f1.backend.domain.question.entity.Question;
2730
import io.f1.backend.domain.quiz.app.QuizService;
2831
import io.f1.backend.domain.quiz.entity.Quiz;
32+
import io.f1.backend.domain.stat.app.StatService;
2933
import io.f1.backend.domain.user.dto.UserPrincipal;
3034
import io.f1.backend.global.exception.CustomException;
3135
import io.f1.backend.global.exception.errorcode.GameErrorCode;
@@ -52,6 +56,7 @@ public class GameService {
5256
private static final int CONTINUE_DELAY = 3;
5357
private static final String NONE_CORRECT_USER = "";
5458

59+
private final StatService statService;
5560
private final QuizService quizService;
5661
private final RoomService roomService;
5762
private final TimerService timerService;
@@ -170,11 +175,12 @@ public void gameEnd(Room room) {
170175

171176
Map<Long, Player> playerMap = room.getPlayerMap();
172177

173-
// TODO : 랭킹 정보 업데이트
174-
messageSender.sendBroadcast(
175-
destination,
176-
MessageType.GAME_RESULT,
177-
toGameResultListResponse(playerMap, room.getGameSetting().getRound()));
178+
GameResultListResponse gameResultListResponse =
179+
toGameResultListResponse(playerMap, room.getGameSetting().getRound());
180+
181+
messageSender.sendBroadcast(destination, MessageType.GAME_RESULT, gameResultListResponse);
182+
183+
updateRank(room, gameResultListResponse);
178184

179185
room.initializeRound();
180186
room.initializePlayers();
@@ -200,6 +206,31 @@ public void gameEnd(Room room) {
200206
destination, MessageType.ROOM_SETTING, toRoomSettingResponse(room));
201207
}
202208

209+
private void updateRank(Room room, GameResultListResponse gameResultListResponse) {
210+
211+
List<GameResultResponse> result = gameResultListResponse.result();
212+
213+
for (GameResultResponse gameResultResponse : result) {
214+
Long playerId = gameResultResponse.id();
215+
int rank = gameResultResponse.rank();
216+
int score = gameResultResponse.score();
217+
218+
Player player = room.getPlayerByUserId(playerId);
219+
220+
if (room.isPlayerInState(playerId, ConnectionState.DISCONNECTED)) {
221+
statService.updateRank(playerId, false, 0);
222+
continue;
223+
}
224+
225+
if (rank == 1) {
226+
statService.updateRank(playerId, true, score);
227+
continue;
228+
}
229+
230+
statService.updateRank(playerId, false, score);
231+
}
232+
}
233+
203234
@DistributedLock(prefix = "room", key = "#roomId")
204235
public void handlePlayerReady(Long roomId, UserPrincipal userPrincipal) {
205236

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package io.f1.backend.domain.game.dto.response;
22

3-
public record GameResultResponse(String nickname, int score, int totalCorrectCount, int rank) {}
3+
public record GameResultResponse(
4+
Long id, String nickname, int score, int totalCorrectCount, int rank) {}

backend/src/main/java/io/f1/backend/domain/game/mapper/RoomMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ public static GameResultResponse toGameResultResponse(
121121
double correctRate = (double) player.getCorrectCount() / round;
122122
int score = (int) (correctRate * 100) + (totalPlayers - rank) * 5;
123123

124-
return new GameResultResponse(player.nickname, score, player.getCorrectCount(), rank);
124+
return new GameResultResponse(
125+
player.id, player.nickname, score, player.getCorrectCount(), rank);
125126
}
126127

127128
public static GameResultListResponse toGameResultListResponse(

backend/src/main/java/io/f1/backend/domain/game/model/Room.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,7 @@ public List<Player> getDisconnectedPlayers() {
118118
}
119119

120120
public void initializePlayers() {
121-
this.playerMap
122-
.values()
123-
.forEach(
124-
player -> {
125-
player.initializeCorrectCount();
126-
});
121+
this.playerMap.values().forEach(Player::initializeCorrectCount);
127122
resetAllPlayerReadyStates();
128123
}
129124

0 commit comments

Comments
 (0)