Skip to content

Commit

Permalink
added automatic configuration through JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
Triikk committed Feb 16, 2024
1 parent 7778130 commit 225a4a0
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 26 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ dependencies {
// JGraphT (graph data structures and algorithms)
val jGraphTVersion = "1.5.2"
implementation("org.jgrapht:jgrapht-core:$jGraphTVersion")
val jacksonVersion = "2.16.0"
implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion")

// JavaFX: comment out if you do not need them
val javaFxVersion = 15
Expand Down
117 changes: 97 additions & 20 deletions src/main/java/it/unibo/model/GameManagerImpl.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package it.unibo.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Random;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import it.unibo.common.Recipes;
import it.unibo.common.card.CardType;
Expand All @@ -18,6 +24,7 @@
import it.unibo.model.board.BoardImpl;
import it.unibo.model.developmentcard.DevelopmentCards;
import it.unibo.model.developmentcard.DevelopmentCardsImpl;
import it.unibo.model.mapgenerator.BeginnerGameMapGenerator;
import it.unibo.model.mapgenerator.GameMapGenerator;
import it.unibo.model.mapgenerator.RandomGameMapGenerator;
import it.unibo.model.player.Player;
Expand All @@ -35,9 +42,6 @@
* Implementation of GameManager.
*/
public final class GameManagerImpl implements GameManager {
private static final int DEFAULT_POINTS_TO_WIN = 10;
private static final int DEFAULT_BANK_RESOURCES = 19;

private final DevelopmentCards developmentCards;
private final PropertyManager propertyManager;
private final RoadManager roadManager;
Expand All @@ -49,31 +53,21 @@ public final class GameManagerImpl implements GameManager {
private final int pointsToWin;

/**
* Game manager constructor.
*
* @param generator game map generator
* @param playersNames list of players' names
* @param pointsToWin points to win the game
* @param bankResourcesAmount initial amount for each resource of the bank
* @param playersNames list of players' names
* @see #GameManagerImpl(MapType,List,int)
*/
public GameManagerImpl(final GameMapGenerator generator, final List<String> playersNames, final int pointsToWin,
final int bankResourcesAmount) {
public GameManagerImpl(final List<String> playersNames) {
playersNames.forEach(name -> players.add(new PlayerImpl(name)));
this.pointsToWin = pointsToWin;

this.developmentCards = new DevelopmentCardsImpl();
this.roadManager = new RoadManagerImpl();
this.propertyManager = new PropertyManagerImpl();
this.turnManager = new TurnManagerImpl(players);
this.resourceManager = new ResourceManagerImpl(players, bankResourcesAmount);
this.board = new BoardImpl(generator);
}
this.resourceManager = new ResourceManagerImpl(players);

/**
* @param playersNames list of players' names
*/
public GameManagerImpl(final List<String> playersNames) {
this(new RandomGameMapGenerator(), playersNames, DEFAULT_POINTS_TO_WIN, DEFAULT_BANK_RESOURCES);
final GameSettings settings = new GameSettings("settings/settings.json");
this.pointsToWin = settings.getPoints();
this.board = new BoardImpl(settings.getMapGenerator());
}

@Override
Expand Down Expand Up @@ -339,4 +333,87 @@ private boolean isRoadNearToAnyPlayerProperty(final RoadPosition roadPosition, f
});
}

/**
* Represents
*/
private class GameSettings {

private enum MapType {
/**
* @see BeginnerGameMapGenerator
*/
BEGINNER,
/**
* @see RandomGameMapGenerator
*/
RANDOM
}

private static final String DEFAULT_MAP_FIELD = "beginner";
private static final MapType DEFAULT_MAP_TYPE = MapType.BEGINNER;
private static final Map<String, MapType> fieldToMapType = Map.of(DEFAULT_MAP_FIELD, DEFAULT_MAP_TYPE,
"random", MapType.RANDOM);
private static final int DEFAULT_POINTS = 10;

private int points;
private MapType mapType;

/**
* Class which represents the settings of the game customized by players, which
* are the amount of points necessary to win and the type of map to play with.
*
* @param settingsPath path of settings file in CLASSPATH
*/
private GameSettings(final String settingsPath) {
try {
final ObjectMapper objectMapper = new ObjectMapper();
final JsonNode settings = objectMapper.readTree(ClassLoader.getSystemResourceAsStream(settingsPath));
final String MAP_FIELD_NAME = "map";
final String POINTS_FIELD_NAME = "points";
setMapType(Optional.ofNullable(settings.get(MAP_FIELD_NAME)));
setPoints(Optional.ofNullable(settings.get(POINTS_FIELD_NAME)));
} catch (IOException e) {
setDefaultMapType();
setDefaultPoints();
}
}

private int getPoints() {
return points;
}

private GameMapGenerator getMapGenerator() {
return switch (mapType) {
case RANDOM -> new RandomGameMapGenerator();
default -> new BeginnerGameMapGenerator();
};
}

private void setMapType(Optional<JsonNode> selectedMap) {
if (selectedMap.isPresent()) {
mapType = fieldToMapType.getOrDefault(
selectedMap.get().asText(DEFAULT_MAP_FIELD).toLowerCase(Locale.US),
DEFAULT_MAP_TYPE);
} else {
setDefaultMapType();
}
}

private void setDefaultMapType() {
mapType = DEFAULT_MAP_TYPE;
}

private void setPoints(Optional<JsonNode> selectedPoints) {
if (selectedPoints.isPresent()) {
points = selectedPoints.get().asInt(DEFAULT_POINTS);
} else {
setDefaultPoints();
}
}

private void setDefaultPoints() {
points = DEFAULT_POINTS;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class ResourceManagerImpl implements ResourceManager {
private final Map<ResourceOwner, Map<ResourceType, Integer>> allEntityResources = new HashMap<>();
private final ResourceOwner bank;
static final int DISCARD_THRESHOLD = 7;
static final int DEFAULT_BANK_RESOURCES = 19;

/**
* Create the ResourceManager from the list of the resource owner(such as
Expand All @@ -23,9 +24,9 @@ public final class ResourceManagerImpl implements ResourceManager {
* @param players the list of players
* @param bankResourcesAmount the amount of resources in the bank
*/
public ResourceManagerImpl(final List<Player> players, final int bankResourcesAmount) {
public ResourceManagerImpl(final List<Player> players) {
players.forEach(ro -> allEntityResources.put(ro, ro.getDefaultResources()));
bank = new BankImpl(bankResourcesAmount);
bank = new BankImpl(DEFAULT_BANK_RESOURCES);
allEntityResources.put(bank, bank.getDefaultResources());
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/settings/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"map": "beginner",
"victoryPoints": 10
}
4 changes: 1 addition & 3 deletions src/test/java/it/unibo/model/GameManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ void initializeGame() {
void testProduceResources() {
final Player player1 = new PlayerImpl("first");
final Player player2 = new PlayerImpl("second");
final GameManager gameManager = new GameManagerImpl(new BeginnerGameMapGenerator(),
List.of(player1.getName(), player2.getName()), 10,
19);
final GameManager gameManager = new GameManagerImpl(List.of(player1.getName(), player2.getName()));

final TilePosition tile1 = new TilePositionImpl(1, 0);
final TilePosition tile2 = new TilePositionImpl(3, 2);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/it/unibo/model/ResourceManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ResourceManagerTest {

@BeforeEach
void init() {
this.resourceManager = new ResourceManagerImpl(List.of(PLAYER1, PLAYER2, PLAYER3, PLAYER4), 19);
this.resourceManager = new ResourceManagerImpl(List.of(PLAYER1, PLAYER2, PLAYER3, PLAYER4));
}

/**
Expand Down

0 comments on commit 225a4a0

Please sign in to comment.