diff options
author | jbion <joffrey.bion@amadeus.com> | 2016-12-26 17:04:51 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2016-12-26 17:04:51 +0100 |
commit | 2c908c1c13e0f2da3e327ccb0163f48af9a3fbed (patch) | |
tree | 92e3829cf04300ac8d7d44cf0520a545902f8746 | |
parent | Replace switches on MoveType by multiple Move implementations (diff) | |
download | seven-wonders-2c908c1c13e0f2da3e327ccb0163f48af9a3fbed.tar.gz seven-wonders-2c908c1c13e0f2da3e327ccb0163f48af9a3fbed.tar.bz2 seven-wonders-2c908c1c13e0f2da3e327ccb0163f48af9a3fbed.zip |
Create GameController
7 files changed, 142 insertions, 19 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/actions/PrepareCardAction.java b/src/main/java/org/luxons/sevenwonders/actions/PrepareCardAction.java index a59f08ee..b333d6c1 100644 --- a/src/main/java/org/luxons/sevenwonders/actions/PrepareCardAction.java +++ b/src/main/java/org/luxons/sevenwonders/actions/PrepareCardAction.java @@ -1,16 +1,16 @@ package org.luxons.sevenwonders.actions; -import org.luxons.sevenwonders.game.moves.Move; +import org.luxons.sevenwonders.game.api.PlayerMove; public class PrepareCardAction { - private Move move; + private PlayerMove move; - public Move getMove() { + public PlayerMove getMove() { return move; } - public void setMove(Move move) { + public void setMove(PlayerMove move) { this.move = move; } } diff --git a/src/main/java/org/luxons/sevenwonders/controllers/GameController.java b/src/main/java/org/luxons/sevenwonders/controllers/GameController.java new file mode 100644 index 00000000..0bb3eb23 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/controllers/GameController.java @@ -0,0 +1,63 @@ +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; +import org.luxons.sevenwonders.game.Player; +import org.luxons.sevenwonders.game.api.PlayerTurnInfo; +import org.luxons.sevenwonders.game.api.PreparedCard; +import org.luxons.sevenwonders.repositories.GameRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.messaging.handler.annotation.DestinationVariable; +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.simp.SimpMessagingTemplate; +import org.springframework.stereotype.Controller; + +@Controller +public class GameController { + + private static final Logger logger = LoggerFactory.getLogger(GameController.class); + + private final SimpMessagingTemplate template; + + private final GameRepository gameRepository; + + @Autowired + public GameController(SimpMessagingTemplate template, GameRepository gameRepository) { + this.template = template; + this.gameRepository = gameRepository; + } + + @MessageMapping("/game/{gameId}/prepare") + public void prepareCard(@DestinationVariable long gameId, PrepareCardAction action, Principal principal) { + Game game = gameRepository.find(gameId); + PreparedCard preparedCard = game.prepareCard(principal.getName(), action.getMove()); + logger.info("Game '{}': player {} prepared move {}", gameId, principal.getName(), action.getMove()); + + if (game.areAllPlayersReady()) { + game.playTurn(); + sendTurnInfo(game); + } else { + sendPreparedCard(preparedCard, game); + } + } + + private void sendPreparedCard(PreparedCard preparedCard, Game game) { + for (Player player : game.getPlayers()) { + String userName = player.getUserName(); + template.convertAndSendToUser(userName, "/topic/game/" + game.getId() + "/prepared", preparedCard); + } + } + + private void sendTurnInfo(Game game) { + List<PlayerTurnInfo> turnInfos = game.getTurnInfo(); + for (PlayerTurnInfo turnInfo : turnInfos) { + String userName = turnInfo.getPlayer().getUserName(); + template.convertAndSendToUser(userName, "/topic/game/" + game.getId() + "/turn", turnInfo); + } + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/Game.java b/src/main/java/org/luxons/sevenwonders/game/Game.java index 7b79f716..71fea9e5 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Game.java +++ b/src/main/java/org/luxons/sevenwonders/game/Game.java @@ -5,10 +5,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import java.util.stream.IntStream; import org.luxons.sevenwonders.game.api.PlayerMove; import org.luxons.sevenwonders.game.api.PlayerTurnInfo; +import org.luxons.sevenwonders.game.api.PreparedCard; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.cards.Card; @@ -55,27 +55,40 @@ public class Game { return players.stream().anyMatch(p -> p.getUserName().equals(userName)); } + public List<Player> getPlayers() { + return players; + } + private void startNewAge() { currentAge++; hands = decks.deal(currentAge, table.getNbPlayers()); } public List<PlayerTurnInfo> getTurnInfo() { - return IntStream.range(0, players.size()).mapToObj(this::createPlayerTurnInfo).collect(Collectors.toList()); + return players.stream().map(this::createPlayerTurnInfo).collect(Collectors.toList()); } - private PlayerTurnInfo createPlayerTurnInfo(int playerIndex) { - PlayerTurnInfo pti = new PlayerTurnInfo(playerIndex, table); - pti.setHand(hands.createHand(table, playerIndex)); + private PlayerTurnInfo createPlayerTurnInfo(Player player) { + PlayerTurnInfo pti = new PlayerTurnInfo(player, table); + pti.setHand(hands.createHand(table, player.getIndex())); pti.setCurrentAge(currentAge); return pti; } - public void prepareCard(int playerIndex, PlayerMove playerMove) throws InvalidMoveException { + public PreparedCard prepareCard(String userName, PlayerMove playerMove) throws InvalidMoveException { + Player player = getPlayer(userName); Card card = decks.getCard(playerMove.getCardName()); - Move move = playerMove.getType().resolve(playerIndex, card, playerMove); + Move move = playerMove.getType().resolve(player.getIndex(), card, playerMove); validate(move); - preparedMoves.put(playerIndex, move); + preparedMoves.put(player.getIndex(), move); + return new PreparedCard(player, card.getBack()); + } + + private Player getPlayer(String userName) { + return players.stream() + .filter(p -> p.getUserName().equals(userName)) + .findAny() + .orElseThrow(() -> new UnknownPlayerException(userName)); } private void validate(Move move) throws InvalidMoveException { @@ -170,13 +183,19 @@ public class Game { return currentAge % 2 == 0 ? -1 : 1; } - private static class MissingPreparedMoveException extends RuntimeException { + private static class MissingPreparedMoveException extends IllegalStateException { MissingPreparedMoveException(int playerIndex) { super("Player " + playerIndex + " is not ready to play"); } } - private static class InvalidMoveException extends RuntimeException { + private static class UnknownPlayerException extends IllegalArgumentException { + UnknownPlayerException(String userName) { + super(userName); + } + } + + private static class InvalidMoveException extends IllegalArgumentException { InvalidMoveException(String message) { super(message); } diff --git a/src/main/java/org/luxons/sevenwonders/game/Player.java b/src/main/java/org/luxons/sevenwonders/game/Player.java index 9df51d5f..d550cf60 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Player.java +++ b/src/main/java/org/luxons/sevenwonders/game/Player.java @@ -6,6 +6,8 @@ public class Player { private final String userName; + private int index; + public Player(String displayName, String userName) { this.displayName = displayName; this.userName = userName; @@ -18,4 +20,12 @@ public class Player { public String getUserName() { return userName; } + + public int getIndex() { + return index; + } + + public void setIndex(int index) { + this.index = index; + } } diff --git a/src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java b/src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java index 44e32c7f..6d2889e0 100644 --- a/src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java +++ b/src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java @@ -37,4 +37,9 @@ public class PlayerMove { public void setBoughtResources(List<BoughtResources> boughtResources) { this.boughtResources = boughtResources; } + + @Override + public String toString() { + return type + " '" + cardName + '\''; + } } diff --git a/src/main/java/org/luxons/sevenwonders/game/api/PlayerTurnInfo.java b/src/main/java/org/luxons/sevenwonders/game/api/PlayerTurnInfo.java index b7090ff9..1566b862 100644 --- a/src/main/java/org/luxons/sevenwonders/game/api/PlayerTurnInfo.java +++ b/src/main/java/org/luxons/sevenwonders/game/api/PlayerTurnInfo.java @@ -2,9 +2,11 @@ package org.luxons.sevenwonders.game.api; import java.util.List; +import org.luxons.sevenwonders.game.Player; + public class PlayerTurnInfo { - private final int playerIndex; + private final Player player; private final Table table; @@ -14,13 +16,13 @@ public class PlayerTurnInfo { private String message; - public PlayerTurnInfo(int playerIndex, Table table) { - this.playerIndex = playerIndex; + public PlayerTurnInfo(Player player, Table table) { + this.player = player; this.table = table; } - public int getPlayerIndex() { - return playerIndex; + public Player getPlayer() { + return player; } public Table getTable() { diff --git a/src/main/java/org/luxons/sevenwonders/game/api/PreparedCard.java b/src/main/java/org/luxons/sevenwonders/game/api/PreparedCard.java new file mode 100644 index 00000000..85cac1de --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/api/PreparedCard.java @@ -0,0 +1,24 @@ +package org.luxons.sevenwonders.game.api; + +import org.luxons.sevenwonders.game.Player; +import org.luxons.sevenwonders.game.cards.CardBack; + +public class PreparedCard { + + private final Player player; + + private final CardBack cardBack; + + public PreparedCard(Player player, CardBack cardBack) { + this.player = player; + this.cardBack = cardBack; + } + + public Player getPlayer() { + return player; + } + + public CardBack getCardBack() { + return cardBack; + } +} |