Merge pull request '[Feature]: Remove Money when Buying things' (!13) from TD-52-geld-ausgeben into trunk
Reviewed-on: #13 Reviewed-by: SZUT-Rajbir <rajbir2@schule.bremen.de>
This commit is contained in:
commit
6d536a3ad1
4 changed files with 19 additions and 7 deletions
|
@ -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,16 +89,24 @@ 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPlayerMoneyCallback( Player player, PlayerMoneyCallback playerMoneyCallback) {
|
public void setPlayerMoneyCallback(Player player, PlayerMoneyCallback playerMoneyCallback) {
|
||||||
if (player == player1) {
|
if (player == player1) {
|
||||||
this.player1MoneyCallback = playerMoneyCallback;
|
this.player1MoneyCallback = playerMoneyCallback;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ public class MatchService {
|
||||||
/**
|
/**
|
||||||
* @return opponent
|
* @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);
|
return playerMatches.get(player).addTower(player, new Tower(), x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Add table
Reference in a new issue