Merge pull request '[Feature]: Remove Money when Buying things' (!13) from TD-52-geld-ausgeben into trunk
All checks were successful
Quality Check / Validate OAS (push) Successful in 32s
Quality Check / Linting (push) Successful in 1m2s
Quality Check / Testing (push) Successful in 1m1s
Quality Check / Static Analysis (push) Successful in 1m5s

Reviewed-on: #13
Reviewed-by: SZUT-Rajbir <rajbir2@schule.bremen.de>
This commit is contained in:
SZUT-Kevin 2025-03-12 12:11:31 +00:00 committed by Euph Forge
commit 6d536a3ad1
Signed by: Euph Forge
GPG key ID: 85A06461FB6BDBB7
4 changed files with 19 additions and 7 deletions

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,12 +89,20 @@ 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);
}
}
}

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