[Feature]: Remove Money when Buying things #13

Merged
SZUT-Kevin merged 1 commit from TD-52-geld-ausgeben into trunk 2025-03-12 12:11:31 +00:00
4 changed files with 19 additions and 7 deletions
Showing only changes of commit b6bff21a11 - Show all commits

View file

@ -4,7 +4,6 @@ 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.AllArgsConstructor;
import lombok.Getter;
import java.util.Optional;
@ -12,6 +11,7 @@ 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;
@ -59,11 +59,13 @@ public class Match {
if (player1Map[x][y] != null) {
throw new InvalidPlacementException(InvalidPlacementReason.LOCATION_USED);
}
removeMoney(player, TOWER_PRICE);
player1Map[x][y] = tower;
} else {
if (player2Map[x][y] != null) {
throw new InvalidPlacementException(InvalidPlacementReason.LOCATION_USED);
}
removeMoney(player, TOWER_PRICE);
player2Map[x][y] = tower;
}
return getOpponent(player);
@ -87,16 +89,24 @@ public class Match {
player2Money += money;
}
public void removeMoney(Player player, int money) {
public void removeMoney(Player player, int price) throws InvalidPlacementException {
if (player == player1) {
player1Money -= money;
if (price < player1Money) {
player1Money -= price;
} else {
throw new InvalidPlacementException(InvalidPlacementReason.NOT_ENOUGH_MONEY);
}
}
if (player == player2) {
player2Money -= money;
if (price < player2Money) {
player2Money -= price;
} else {
throw new InvalidPlacementException(InvalidPlacementReason.NOT_ENOUGH_MONEY);
}
}
}
public void setPlayerMoneyCallback( Player player, PlayerMoneyCallback playerMoneyCallback) {
public void setPlayerMoneyCallback(Player player, PlayerMoneyCallback playerMoneyCallback) {
if (player == player1) {
this.player1MoneyCallback = playerMoneyCallback;
}

View file

@ -29,7 +29,7 @@ public class MatchService {
/**
* @return opponent
*/
public Player placeTower(Player player, int x, int y) throws InvalidPlacementException {
public Player placeTower(Player player, int x, int y) throws InvalidPlacementException{
return playerMatches.get(player).addTower(player, new Tower(), x, y);
}

View file

@ -7,7 +7,8 @@ import lombok.Getter;
@AllArgsConstructor
public enum InvalidPlacementReason {
OUT_OF_BOUNDS("out-of-bounds"),
LOCATION_USED("location-used");
LOCATION_USED("location-used"),
NOT_ENOUGH_MONEY("not-enough-money");
private final String jsonName;
}

View file

@ -218,6 +218,7 @@ channels:
enum:
- "out-of-bounds"
- "location-used"
- "not-enough-money"
required:
- $id
- x