Skip to content

Commit

Permalink
feat: 게임 종료 기능 구현 #78
Browse files Browse the repository at this point in the history
  • Loading branch information
yhpark95 committed Nov 7, 2023
1 parent f8b8e9d commit 2e50259
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ private void sendEventResult(GameEventResultRequest gameEventResultRequest) {
gameEventNameResponse));
socketDataSender.send(gameEventResultRequest.getGameId(), new ResponseDTO<>(TypeConstants.STATUS_BOARD,
gameService.proceedEvent(gameEventNameResponse.getName(), gameEventResultRequest.getGameId())));
if (gameService.checkGameOver(gameEventResultRequest.getGameId())) {
socketDataSender.send(
gameEventResultRequest.getGameId(), new ResponseDTO<>(TypeConstants.GAME_OVER,
gameService.createUserRanking(gameEventResultRequest.getGameId())));
}
}

@PostMapping("/api/games")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package codesquad.gaemimarble.game.dto.response;

import lombok.Builder;
import lombok.Getter;

@Getter
public class PlayerAsset {
private final String playerId;
private final Integer totalAsset;

@Builder
private PlayerAsset(String playerId, Integer totalAsset) {
this.playerId = playerId;
this.totalAsset = totalAsset;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package codesquad.gaemimarble.game.dto.response;

import java.util.List;

import lombok.Builder;
import lombok.Getter;

@Getter
public class UserRankingResponse {
List<PlayerAsset> ranking;

@Builder
private UserRankingResponse(List<PlayerAsset> ranking) {
this.ranking = ranking;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ public Player getPlayer(String playerId) {
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("해당하는 플레이어가 없습니다."));
}

public void incrementRoundCount() {
roundCount++;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ public final class TypeConstants {
public static final String GOLD_CARD = "goldCard";
public static final String ERROR = "error";
public static final String ROB = "rob";
public static final String GAME_OVER = "gameOver";
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codesquad.gaemimarble.game.service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -32,6 +33,8 @@
import codesquad.gaemimarble.game.dto.response.GamePrisonDiceResponse;
import codesquad.gaemimarble.game.dto.response.GameReadyResponse;
import codesquad.gaemimarble.game.dto.response.GameRoomCreateResponse;
import codesquad.gaemimarble.game.dto.response.PlayerAsset;
import codesquad.gaemimarble.game.dto.response.UserRankingResponse;
import codesquad.gaemimarble.game.dto.response.generalStatusBoard.GameStatusBoardResponse;
import codesquad.gaemimarble.game.dto.response.userStatusBoard.GameUserBoardResponse;
import codesquad.gaemimarble.game.entity.Board;
Expand Down Expand Up @@ -215,6 +218,7 @@ public GameStatusBoardResponse proceedEvent(String eventName, Long gameId) {
}
}
updatePlayersAsset(gameStatus.getPlayers(), stockList);
gameStatus.incrementRoundCount();

return createGameStatusBoardResponse(gameId);
}
Expand Down Expand Up @@ -323,6 +327,7 @@ public GameEndTurnResponse endTurn(GameEndTurnRequest gameEndTurnRequest) {
currentPlayerInfo.update(player);
}
}

return GameEndTurnResponse.builder().nextPlayerId(null).build();
}

Expand Down Expand Up @@ -408,4 +413,17 @@ public List<Player> rob(GameRobRequest gameRobRequest) {
target.addCashAsset(-10_000_000);
return List.of(taker, target);
}

public boolean checkGameOver(Long gameId) {
GameStatus gameStatus = gameRepository.getGameStatus(gameId);
return gameStatus.getRoundCount() > 15;
}

public UserRankingResponse createUserRanking(Long gameId) {
return UserRankingResponse.builder().ranking(gameRepository.getAllPlayer(gameId)
.stream()
.sorted(Comparator.comparing(Player::getTotalAsset).reversed())
.map(p -> PlayerAsset.builder().playerId(p.getPlayerId()).totalAsset(p.getTotalAsset()).build())
.collect(Collectors.toList())).build();
}
}

0 comments on commit 2e50259

Please sign in to comment.