[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.money.PlayerMoneyCallback;
import de.towerdefence.server.server.channels.match.placing.InvalidPlacementReason; import de.towerdefence.server.server.channels.match.placing.InvalidPlacementReason;
import de.towerdefence.server.server.channels.match.placing.Tower; import de.towerdefence.server.server.channels.match.placing.Tower;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import java.util.Optional; import java.util.Optional;
@ -12,6 +11,7 @@ import java.util.Optional;
@Getter @Getter
public class Match { public class Match {
public static final int TOWER_PRICE = 20;
public final static int MAP_SIZE_X = 10; public final static int MAP_SIZE_X = 10;
public final static int MAP_SIZE_Y = 20; public final static int MAP_SIZE_Y = 20;
public final static int START_MONEY = 50; public final static int START_MONEY = 50;
@ -59,11 +59,13 @@ public class Match {
if (player1Map[x][y] != null) { if (player1Map[x][y] != null) {
throw new InvalidPlacementException(InvalidPlacementReason.LOCATION_USED); throw new InvalidPlacementException(InvalidPlacementReason.LOCATION_USED);
} }
removeMoney(player, TOWER_PRICE);
player1Map[x][y] = tower; player1Map[x][y] = tower;
} else { } else {
if (player2Map[x][y] != null) { if (player2Map[x][y] != null) {
throw new InvalidPlacementException(InvalidPlacementReason.LOCATION_USED); throw new InvalidPlacementException(InvalidPlacementReason.LOCATION_USED);
} }
removeMoney(player, TOWER_PRICE);
player2Map[x][y] = tower; player2Map[x][y] = tower;
} }
return getOpponent(player); return getOpponent(player);
@ -87,12 +89,20 @@ public class Match {
player2Money += money; player2Money += money;
} }
public void removeMoney(Player player, int money) { public void removeMoney(Player player, int price) throws InvalidPlacementException {
if (player == player1) { if (player == player1) {
player1Money -= money; if (price < player1Money) {
player1Money -= price;
} else {
throw new InvalidPlacementException(InvalidPlacementReason.NOT_ENOUGH_MONEY);
}
} }
if (player == player2) { if (player == player2) {
player2Money -= money; if (price < player2Money) {
player2Money -= price;
} else {
throw new InvalidPlacementException(InvalidPlacementReason.NOT_ENOUGH_MONEY);
}
} }
} }

View file

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

View file

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