Merge branch 'feat/117-send-winner-with-get-gamestate-command' into 'main'
Feat: Winner is now sent to the frontend with get_gamestate Closes #117 See merge request cs108-fs26/Gruppe-13!127
This commit was merged in pull request #283.
This commit is contained in:
+27
-1
@@ -6,6 +6,7 @@ import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.deck.Rank;
|
|||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.deck.Suit;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.deck.Suit;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.Player;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.Player;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.state.GamePhase;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.state.GameState;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.state.GameState;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestContext;
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestContext;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.SuccessResponse;
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.SuccessResponse;
|
||||||
@@ -33,11 +34,13 @@ public class GetGameStateResponse extends SuccessResponse {
|
|||||||
super(
|
super(
|
||||||
context,
|
context,
|
||||||
buildBody(
|
buildBody(
|
||||||
|
game,
|
||||||
Objects.requireNonNull(game, "game must not be null").getState(),
|
Objects.requireNonNull(game, "game must not be null").getState(),
|
||||||
requestingUsername));
|
requestingUsername));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ResponseBody buildBody(GameState state, String requestingUsername) {
|
private static ResponseBody buildBody(
|
||||||
|
GameController game, GameState state, String requestingUsername) {
|
||||||
Objects.requireNonNull(state, "state must not be null");
|
Objects.requireNonNull(state, "state must not be null");
|
||||||
|
|
||||||
ResponseBodyBuilder builder = ResponseBody.builder();
|
ResponseBodyBuilder builder = ResponseBody.builder();
|
||||||
@@ -47,6 +50,7 @@ public class GetGameStateResponse extends SuccessResponse {
|
|||||||
builder.param("CURRENT_BET", computeGlobalCurrentBet(state));
|
builder.param("CURRENT_BET", computeGlobalCurrentBet(state));
|
||||||
builder.param("DEALER", state.getDealerIndex());
|
builder.param("DEALER", state.getDealerIndex());
|
||||||
builder.param("ACTIVE_PLAYER", state.getCurrentPlayerIndex());
|
builder.param("ACTIVE_PLAYER", state.getCurrentPlayerIndex());
|
||||||
|
builder.param("WINNER", computeWinnerIndex(state, game));
|
||||||
|
|
||||||
appendCommunityCards(builder, state);
|
appendCommunityCards(builder, state);
|
||||||
|
|
||||||
@@ -55,6 +59,28 @@ public class GetGameStateResponse extends SuccessResponse {
|
|||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int computeWinnerIndex(GameState state, GameController game) {
|
||||||
|
GamePhase phase = state.getPhase();
|
||||||
|
if (phase != GamePhase.SHOWDOWN && phase != GamePhase.FINISHED) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
PlayerId winnerId = game.determineWinner();
|
||||||
|
if (winnerId == null) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
for (Player p : state.getPlayers()) {
|
||||||
|
if (p != null && winnerId.equals(p.getId())) {
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
private static int computeGlobalCurrentBet(GameState state) {
|
private static int computeGlobalCurrentBet(GameState state) {
|
||||||
int globalCurrentBet = 0;
|
int globalCurrentBet = 0;
|
||||||
for (Player p : state.getPlayers()) {
|
for (Player p : state.getPlayers()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user