From d550fe80b232b7cb8cd9f4968202b1614b735fa5 Mon Sep 17 00:00:00 2001 From: Edward Harman Date: Wed, 31 Jan 2024 19:31:27 -0500 Subject: [PATCH] rotate starting player on "play again" --- .../main/java/org/ethelred/games/core/BaseGame.java | 13 +++++++++++++ .../src/main/java/org/ethelred/games/core/Game.java | 4 ++++ .../org/ethelred/games/core/InMemoryEngineImpl.java | 1 + 3 files changed, 18 insertions(+) diff --git a/engine/src/main/java/org/ethelred/games/core/BaseGame.java b/engine/src/main/java/org/ethelred/games/core/BaseGame.java index c2dc7847..61fafee1 100644 --- a/engine/src/main/java/org/ethelred/games/core/BaseGame.java +++ b/engine/src/main/java/org/ethelred/games/core/BaseGame.java @@ -16,6 +16,8 @@ public abstract class BaseGame

implements Game private String shortCode; private volatile Status status = Status.PRESTART; + private int playAgainCount = 0; + enum ReadyState { PRESTART, READY, @@ -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; } @@ -224,4 +227,14 @@ public Set 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; + } } diff --git a/engine/src/main/java/org/ethelred/games/core/Game.java b/engine/src/main/java/org/ethelred/games/core/Game.java index a5265628..91b3df14 100644 --- a/engine/src/main/java/org/ethelred/games/core/Game.java +++ b/engine/src/main/java/org/ethelred/games/core/Game.java @@ -15,6 +15,7 @@ public interface Game extends Identifiable void log(Player player, Action action); + enum Status { PRESTART, IN_PROGRESS, ENDED @@ -33,4 +34,7 @@ enum Status Player currentPlayer(); Player nextPlayer(); + int playAgainCount(); + + void playAgainCount(int i); } diff --git a/engine/src/main/java/org/ethelred/games/core/InMemoryEngineImpl.java b/engine/src/main/java/org/ethelred/games/core/InMemoryEngineImpl.java index 00f33567..ac860e1a 100644 --- a/engine/src/main/java/org/ethelred/games/core/InMemoryEngineImpl.java +++ b/engine/src/main/java/org/ethelred/games/core/InMemoryEngineImpl.java @@ -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)));