package de.towerdefence.server.match; import de.towerdefence.server.player.Player; import de.towerdefence.server.server.channels.match.money.PlayerMoneyCallback; import de.towerdefence.server.server.channels.match.placing.InvalidPlacementReason; import de.towerdefence.server.server.channels.match.placing.Tower; import lombok.Getter; import java.util.Optional; @Getter public class Match { public static final int TOWER_PRICE = 20; public final static int MAP_SIZE_X = 10; public final static int MAP_SIZE_Y = 20; public final static int START_MONEY = 50; private final String matchId; private final Player player1; private final Player player2; private int player1Money = START_MONEY; private int player2Money = START_MONEY; private PlayerMoneyCallback player1MoneyCallback; private PlayerMoneyCallback player2MoneyCallback; private final Tower[][] player1Map = new Tower[MAP_SIZE_X][MAP_SIZE_Y]; private final Tower[][] player2Map = new Tower[MAP_SIZE_X][MAP_SIZE_Y]; private boolean isPlayer1Connected = false; private boolean isPlayer2Connected = false; public Match(String matchId, Player player1, Player player2) { this.matchId = matchId; this.player1 = player1; this.player2 = player2; } public Player getOpponent(Player player) { boolean isPlayer1 = player1.equals(player); boolean isPlayer2 = player2.equals(player); if (!isPlayer1 && !isPlayer2) { return null; } if (isPlayer1) { return player2; } return player1; } /** * @return opponent */ public Player addTower(Player player, Tower tower, int x, int y) throws InvalidPlacementException { if (player != player1 && player != player2) { return null; } if (x < 0 || y < 0 || x + 1 > Match.MAP_SIZE_X || y + 1 > Match.MAP_SIZE_Y) { throw new InvalidPlacementException(InvalidPlacementReason.OUT_OF_BOUNDS); } if (player == player1) { if (player1Map[x][y] != null) { throw new InvalidPlacementException(InvalidPlacementReason.LOCATION_USED); } removeMoney(player, TOWER_PRICE); player1MoneyCallback.call(player1, player1Money); player1Map[x][y] = tower; } else { if (player2Map[x][y] != null) { throw new InvalidPlacementException(InvalidPlacementReason.LOCATION_USED); } removeMoney(player, TOWER_PRICE); player2MoneyCallback.call(player2, player2Money); player2Map[x][y] = tower; } return getOpponent(player); } // INFO: MoneyHandling public Optional getPlayerMoney(Player player) { if (player != player1 && player != player2) { return Optional.empty(); } if (player == player1) { return Optional.of(player1Money); } else { return Optional.of(player2Money); } } public void addMoney(int money) { player1Money += money; player2Money += money; } public void removeMoney(Player player, int price) throws InvalidPlacementException { if (player == player1) { if (price < player1Money) { player1Money -= price; } else { throw new InvalidPlacementException(InvalidPlacementReason.NOT_ENOUGH_MONEY); } } if (player == player2) { if (price < player2Money) { player2Money -= price; } else { throw new InvalidPlacementException(InvalidPlacementReason.NOT_ENOUGH_MONEY); } } } public void setPlayerMoneyCallback(Player player, PlayerMoneyCallback playerMoneyCallback) { if (player == player1) { this.player1MoneyCallback = playerMoneyCallback; } if (player == player2) { this.player2MoneyCallback = playerMoneyCallback; } } /** * @return true if both players are connected */ public boolean setPlayerConnected(Player player) { if (player == player1) { isPlayer1Connected = true; } if (player == player2) { isPlayer2Connected = true; } return isPlayer1Connected && isPlayer2Connected; } }