2025-02-26 13:26:58 +01:00
|
|
|
package de.towerdefence.server.match;
|
|
|
|
|
|
|
|
import de.towerdefence.server.player.Player;
|
2025-03-12 12:15:25 +01:00
|
|
|
import de.towerdefence.server.server.channels.match.money.PlayerMoneyCallback;
|
2025-03-10 13:39:14 +01:00
|
|
|
import de.towerdefence.server.server.channels.match.placing.InvalidPlacementReason;
|
|
|
|
import de.towerdefence.server.server.channels.match.placing.Tower;
|
2025-02-26 13:26:58 +01:00
|
|
|
import lombok.Getter;
|
|
|
|
|
2025-03-12 12:15:25 +01:00
|
|
|
import java.util.Optional;
|
|
|
|
|
2025-02-26 13:26:58 +01:00
|
|
|
@Getter
|
|
|
|
public class Match {
|
2025-03-05 11:39:47 +01:00
|
|
|
|
2025-03-12 13:03:33 +01:00
|
|
|
public static final int TOWER_PRICE = 20;
|
2025-03-10 13:39:14 +01:00
|
|
|
public final static int MAP_SIZE_X = 10;
|
|
|
|
public final static int MAP_SIZE_Y = 20;
|
2025-03-12 12:15:25 +01:00
|
|
|
public final static int START_MONEY = 50;
|
2025-02-26 13:26:58 +01:00
|
|
|
private final String matchId;
|
2025-03-05 11:39:47 +01:00
|
|
|
private final Player player1;
|
|
|
|
private final Player player2;
|
2025-03-12 12:15:25 +01:00
|
|
|
private int player1Money = START_MONEY;
|
|
|
|
private int player2Money = START_MONEY;
|
|
|
|
private PlayerMoneyCallback player1MoneyCallback;
|
|
|
|
private PlayerMoneyCallback player2MoneyCallback;
|
2025-03-10 13:39:14 +01:00
|
|
|
private final Tower[][] player1Map = new Tower[MAP_SIZE_X][MAP_SIZE_Y];
|
|
|
|
private final Tower[][] player2Map = new Tower[MAP_SIZE_X][MAP_SIZE_Y];
|
2025-03-12 12:15:25 +01:00
|
|
|
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;
|
|
|
|
}
|
2025-03-10 13:39:14 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2025-03-12 13:03:33 +01:00
|
|
|
removeMoney(player, TOWER_PRICE);
|
2025-03-10 13:39:14 +01:00
|
|
|
player1Map[x][y] = tower;
|
|
|
|
} else {
|
|
|
|
if (player2Map[x][y] != null) {
|
|
|
|
throw new InvalidPlacementException(InvalidPlacementReason.LOCATION_USED);
|
|
|
|
}
|
2025-03-12 13:03:33 +01:00
|
|
|
removeMoney(player, TOWER_PRICE);
|
2025-03-10 13:39:14 +01:00
|
|
|
player2Map[x][y] = tower;
|
|
|
|
}
|
|
|
|
return getOpponent(player);
|
|
|
|
}
|
2025-03-12 12:15:25 +01:00
|
|
|
|
|
|
|
// INFO: MoneyHandling
|
|
|
|
|
|
|
|
public Optional<Integer> 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;
|
|
|
|
}
|
|
|
|
|
2025-03-12 13:03:33 +01:00
|
|
|
public void removeMoney(Player player, int price) throws InvalidPlacementException {
|
2025-03-12 12:15:25 +01:00
|
|
|
if (player == player1) {
|
2025-03-12 13:03:33 +01:00
|
|
|
if (price < player1Money) {
|
|
|
|
player1Money -= price;
|
|
|
|
} else {
|
|
|
|
throw new InvalidPlacementException(InvalidPlacementReason.NOT_ENOUGH_MONEY);
|
|
|
|
}
|
2025-03-12 12:15:25 +01:00
|
|
|
}
|
|
|
|
if (player == player2) {
|
2025-03-12 13:03:33 +01:00
|
|
|
if (price < player2Money) {
|
|
|
|
player2Money -= price;
|
|
|
|
} else {
|
|
|
|
throw new InvalidPlacementException(InvalidPlacementReason.NOT_ENOUGH_MONEY);
|
|
|
|
}
|
2025-03-12 12:15:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-12 13:03:33 +01:00
|
|
|
public void setPlayerMoneyCallback(Player player, PlayerMoneyCallback playerMoneyCallback) {
|
2025-03-12 12:15:25 +01:00
|
|
|
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;
|
|
|
|
}
|
2025-02-26 13:26:58 +01:00
|
|
|
}
|