diff options
-rw-r--r-- | backend/src/main/java/org/luxons/sevenwonders/controllers/GameController.java | 4 | ||||
-rw-r--r-- | backend/src/main/java/org/luxons/sevenwonders/game/Game.java | 29 |
2 files changed, 26 insertions, 7 deletions
diff --git a/backend/src/main/java/org/luxons/sevenwonders/controllers/GameController.java b/backend/src/main/java/org/luxons/sevenwonders/controllers/GameController.java index 0deac4a3..52da8e0c 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/controllers/GameController.java +++ b/backend/src/main/java/org/luxons/sevenwonders/controllers/GameController.java @@ -1,7 +1,6 @@ package org.luxons.sevenwonders.controllers; import java.security.Principal; -import java.util.List; import org.luxons.sevenwonders.actions.PrepareCardAction; import org.luxons.sevenwonders.game.Game; @@ -54,8 +53,7 @@ public class GameController { } private void sendTurnInfo(Game game) { - List<PlayerTurnInfo> turnInfos = game.getTurnInfo(); - for (PlayerTurnInfo turnInfo : turnInfos) { + for (PlayerTurnInfo turnInfo : game.getCurrentTurnInfo()) { String username = turnInfo.getPlayer().getUsername(); template.convertAndSendToUser(username, "/topic/game/" + game.getId() + "/turn", turnInfo); } diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/Game.java b/backend/src/main/java/org/luxons/sevenwonders/game/Game.java index 8aa7d1b9..206b4697 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/game/Game.java +++ b/backend/src/main/java/org/luxons/sevenwonders/game/Game.java @@ -1,9 +1,11 @@ package org.luxons.sevenwonders.game; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; import org.luxons.sevenwonders.game.api.Action; @@ -38,6 +40,8 @@ public class Game { private final Map<Integer, Move> preparedMoves; + private Map<Integer, PlayerTurnInfo> currentTurnInfo; + private Hands hands; public Game(long id, Settings settings, List<Player> players, List<Board> boards, Decks decks) { @@ -47,6 +51,7 @@ public class Game { this.table = new Table(boards); this.decks = decks; this.discardedCards = new ArrayList<>(); + this.currentTurnInfo = new HashMap<>(); this.preparedMoves = new HashMap<>(); startNewAge(); } @@ -66,10 +71,14 @@ public class Game { private void startNewAge() { table.increaseCurrentAge(); hands = decks.deal(table.getCurrentAge(), table.getNbPlayers()); + startNewTurn(); } - public List<PlayerTurnInfo> getTurnInfo() { - return players.stream().map(this::createPlayerTurnInfo).collect(Collectors.toList()); + private void startNewTurn() { + Function<PlayerTurnInfo, Integer> extractPlayerIndex = pti -> pti.getPlayer().getIndex(); + currentTurnInfo = players.stream() + .map(this::createPlayerTurnInfo) + .collect(Collectors.toMap(extractPlayerIndex, pti -> pti)); } private PlayerTurnInfo createPlayerTurnInfo(Player player) { @@ -82,6 +91,10 @@ public class Game { return pti; } + public Collection<PlayerTurnInfo> getCurrentTurnInfo() { + return currentTurnInfo.values(); + } + private Action determineAction(List<HandCard> hand, Board board) { if (endOfGameReached() && board.hasSpecial(SpecialAbility.COPY_GUILD)) { return Action.PICK_NEIGHBOR_GUILD; @@ -121,7 +134,8 @@ public class Game { } public boolean areAllPlayersReady() { - return preparedMoves.size() == players.size(); + long nbExpectedMoves = currentTurnInfo.values().stream().filter(pti -> pti.getAction() != Action.WAIT).count(); + return preparedMoves.size() == nbExpectedMoves; } public void playTurn() { @@ -131,7 +145,14 @@ public class Game { if (!endOfGameReached()) { startNewAge(); } - } else if (!hands.maxOneCardRemains()) { + } else { + rotateHandsIfRelevant(); + startNewTurn(); + } + } + + private void rotateHandsIfRelevant() { + if (!hands.maxOneCardRemains()) { // we don't rotate hands if some player can play his last card (with the special ability) hands.rotate(table.getHandRotationDirection()); } |