load predefined game state

This commit is contained in:
Johannes Schmelz 2024-12-08 21:36:03 +01:00
parent e0ab09295d
commit 17220c228b

View File

@ -447,16 +447,99 @@ public class ServerGameLogic implements ClientInterpreter {
} else if (msg.getKeyword().equals("PayJail")) {
playerHandler.getPlayerById(from).payBail();
} else if(msg.getKeyword().equals("hack")) {
// for (BuildingProperty bp : boardManager.getPropertyFields( List.of(1,3)).stream().filter(p -> p instanceof BuildingProperty).map(p -> (BuildingProperty) p).collect(Collectors.toList())) {
// bp.build();
// }
for(PropertyField field : boardManager.getBoard().stream().filter(p -> p instanceof PropertyField).map(p -> (PropertyField) p).collect(Collectors.toList())) {
field.setOwner(playerHandler.getPlayerById(0));
playerHandler.getPlayerById(0).addProperty(field.getId());
}
playerHandler.getPlayerById(0).earnMoney(20000);
generatePredefinedGameState();
}
updateAllPlayers();
}
private void generatePredefinedGameState() {
// Ensure at least two players exist
if (playerHandler.getPlayers().size() < 2) {
Player player1 = new Player(0, "Player1", playerHandler);
Player player2 = new Player(1, "Player2", playerHandler);
playerHandler.addPlayer(player1);
playerHandler.addPlayer(player2);
}
Player p1 = playerHandler.getPlayerById(0);
Player p2 = playerHandler.getPlayerById(1);
// Reset properties and balances for a clean state
p1.getProperties().clear();
p2.getProperties().clear();
boardManager.getBoard().forEach(field -> {
if (field instanceof PropertyField) {
((PropertyField) field).setOwner(null);
}
});
// Define properties to assign
Set<Integer> p1Properties = Set.of(1, 3, 6, 8); // Gym, Sportplatz, Studium+, PhysikHörsaal
Set<Integer> p2Properties = Set.of(21, 23, 24, 9); // Red set + Audimax
// Assign properties via AlterProperty
assignProperties(p1, p1Properties);
assignProperties(p2, p2Properties);
// Player 1 builds houses on Gym and Sportplatz
buildHouses(p1, Set.of(1, 3));
// Player 2 builds houses on the Red set
buildHouses(p2, Set.of(21, 23, 24));
// Set player balances
p1.setAccountBalance(12325);
p2.setAccountBalance(26750);
// Add Get Out of Jail cards
p1.addJailCard();
// Set player positions
p1.setPosition(6); // Near Studium+
p2.setPosition(25); // Near Nordtor
LOGGER.log(Level.INFO, "Predefined game state generated.");
updateAllPlayers();
}
/**
* Assigns properties to a player using AlterProperty messages.
*
* @param player the player to assign properties to
* @param properties the set of property IDs to assign
*/
private void assignProperties(Player player, Set<Integer> properties) {
AlterProperty alterProperty = new AlterProperty("AssignProperties");
alterProperty.setProperties(properties);
for (Integer propertyId : properties) {
PropertyField field = (PropertyField) boardManager.getFieldAtIndex(propertyId);
field.setOwner(player);
player.addProperty(propertyId);
}
LOGGER.log(Level.DEBUG, "Properties assigned to player {0}: {1}", player.getName(), properties);
}
/**
* Builds houses for a player on specific properties using AlterProperty messages.
*
* @param player the player building houses
* @param properties the set of property IDs to build houses on
*/
private void buildHouses(Player player, Set<Integer> properties) {
AlterProperty alterProperty = new AlterProperty("BuyHouse");
alterProperty.setProperties(properties);
for (Integer propertyId : properties) {
BuildingProperty field = (BuildingProperty) boardManager.getFieldAtIndex(propertyId);
if (boardManager.canBuild(field) && player.getAccountBalance() >= field.getHousePrice()) {
field.build();
player.pay(field.getHousePrice());
LOGGER.log(Level.DEBUG, "House built on property {0} for player {1}.", field.getName(), player.getName());
}
}
}
}