Skip to content

Commit

Permalink
rotate starting player on "play again"
Browse files Browse the repository at this point in the history
  • Loading branch information
edward3h committed Feb 1, 2024
1 parent 94aee96 commit d550fe8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions engine/src/main/java/org/ethelred/games/core/BaseGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public abstract class BaseGame<P extends GamePlayer> implements Game
private String shortCode;
private volatile Status status = Status.PRESTART;

private int playAgainCount = 0;

enum ReadyState {
PRESTART,
READY,
Expand Down Expand Up @@ -86,6 +88,7 @@ public void playerReady(Player player)
if (readyPlayers.values().stream().allMatch(x -> x == ReadyState.READY)
&& players.size() >= minPlayers())
{
currentPlayerIndex = playAgainCount % playerCount();
start();
status = Status.IN_PROGRESS;
}
Expand Down Expand Up @@ -224,4 +227,14 @@ public Set<ActionDefinition> actionsFor(Player player) {
public void log(Player player, Action action) {
log.push(player, action);
}

@Override
public int playAgainCount() {
return playAgainCount;
}

@Override
public void playAgainCount(int i) {
playAgainCount = i;
}
}
4 changes: 4 additions & 0 deletions engine/src/main/java/org/ethelred/games/core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface Game extends Identifiable<Game>

void log(Player player, Action action);


enum Status
{
PRESTART, IN_PROGRESS, ENDED
Expand All @@ -33,4 +34,7 @@ enum Status
Player currentPlayer();
Player nextPlayer();

int playAgainCount();

void playAgainCount(int i);
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ private MessagePlayerView handlePlayAgain(Channel channel, Game game, Collection
return new MessagePlayerView(null, game.playerView(playerManager.get(channel.playerId())));
}
var newGame = createInternal(channel.gameType(), game.id());
newGame.playAgainCount(game.playAgainCount() + 1);
players.forEach(newGame::addPlayer);
players.forEach(newGame::playerReady);
players.forEach(p -> messageSender.accept(new Channel(game.id(), game.type(), p.id()), newGame.playerView(p)));
Expand Down

0 comments on commit d550fe8

Please sign in to comment.