diff options
Diffstat (limited to 'src/main/java')
11 files changed, 253 insertions, 35 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/app/LobbyController.java b/src/main/java/org/luxons/sevenwonders/app/LobbyController.java index be23ad52..a1358a58 100644 --- a/src/main/java/org/luxons/sevenwonders/app/LobbyController.java +++ b/src/main/java/org/luxons/sevenwonders/app/LobbyController.java @@ -29,7 +29,8 @@ public class LobbyController { String id = String.valueOf(lastGameId++); System.out.println("Creating game " + id); - Game game = new Game(new Settings(), GameDataLoader.load()); + Settings settings = new Settings(); + Game game = new Game(settings, GameDataLoader.load(settings)); games.put(id, game); return id; } diff --git a/src/main/java/org/luxons/sevenwonders/game/Settings.java b/src/main/java/org/luxons/sevenwonders/game/Settings.java index e4065991..4d06c3e8 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Settings.java +++ b/src/main/java/org/luxons/sevenwonders/game/Settings.java @@ -4,7 +4,7 @@ import org.luxons.sevenwonders.game.wonders.WonderSide; public class Settings { - private int nbPlayers = 3; + private int nbPlayers = 4; private int initialGold = 3; diff --git a/src/main/java/org/luxons/sevenwonders/game/cards/Card.java b/src/main/java/org/luxons/sevenwonders/game/cards/Card.java index 27763b2d..111275be 100644 --- a/src/main/java/org/luxons/sevenwonders/game/cards/Card.java +++ b/src/main/java/org/luxons/sevenwonders/game/cards/Card.java @@ -41,4 +41,10 @@ public class Card { requirements.pay(board); effect.apply(board, leftNeighbourBoard, rightNeighbourBoard); } + + @Override + public String toString() { + return "Card{" + "name='" + name + '\'' + ", color=" + color + ", requirements=" + requirements + ", effect=" + + effect + '}'; + } } diff --git a/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java b/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java index e6335a58..99b8da69 100644 --- a/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java +++ b/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java @@ -5,16 +5,16 @@ import org.luxons.sevenwonders.game.resources.Resources; public class Requirements { - private int goldCost; + private int gold; private Resources resources = new Resources(); - public int getGoldCost() { - return goldCost; + public int getGold() { + return gold; } - public void setGoldCost(int goldCost) { - this.goldCost = goldCost; + public void setGold(int gold) { + this.gold = gold; } public Resources getResources() { @@ -26,10 +26,10 @@ public class Requirements { } public boolean isAffordedBy(Board board) { - return board.getGold() >= goldCost && board.getProduction().contains(resources); + return board.getGold() >= gold && board.getProduction().contains(resources); } public void pay(Board board) { - board.setGold(board.getGold() - goldCost); + board.setGold(board.getGold() - gold); } } diff --git a/src/main/java/org/luxons/sevenwonders/game/data/CardDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/CardDefinition.java new file mode 100644 index 00000000..1254bb55 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/CardDefinition.java @@ -0,0 +1,63 @@ +package org.luxons.sevenwonders.game.data; + +import java.util.List; +import java.util.Map; + +import org.luxons.sevenwonders.game.cards.Card; +import org.luxons.sevenwonders.game.cards.Color; +import org.luxons.sevenwonders.game.cards.Requirements; + +public class CardDefinition { + + private String name; + + private Color color; + + private String effect; + + private Requirements requirements; + + private String chainParent; + + private List<String> chainChildren; + + private Map<Integer, Integer> countPerNbPlayer; + + public Card createCard() { + return new Card(name, color, requirements, EffectParser.parse(effect)); + } + + private Requirements createRequirements() { + Requirements req = new Requirements(); + + return req; + } + + public String getName() { + return name; + } + + public Color getColor() { + return color; + } + + public String getEffect() { + return effect; + } + + public Requirements getRequirements() { + return requirements; + } + + public String getChainParent() { + return chainParent; + } + + public List<String> getChainChildren() { + return chainChildren; + } + + public Map<Integer, Integer> getCountPerNbPlayer() { + return countPerNbPlayer; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/DecksDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/DecksDefinition.java new file mode 100644 index 00000000..914217a8 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/DecksDefinition.java @@ -0,0 +1,30 @@ +package org.luxons.sevenwonders.game.data; + +import java.util.List; + +public class DecksDefinition { + + private List<CardDefinition> age1; + + private List<CardDefinition> age2; + + private List<CardDefinition> age3; + + private List<CardDefinition> guild; + + public List<CardDefinition> getAge1() { + return age1; + } + + public List<CardDefinition> getAge2() { + return age2; + } + + public List<CardDefinition> getAge3() { + return age3; + } + + public List<CardDefinition> getGuild() { + return guild; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/EffectParser.java b/src/main/java/org/luxons/sevenwonders/game/data/EffectParser.java new file mode 100644 index 00000000..62fe3d13 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/EffectParser.java @@ -0,0 +1,11 @@ +package org.luxons.sevenwonders.game.data; + +import org.luxons.sevenwonders.game.effects.Effect; + +public class EffectParser { + + static Effect parse(String effectStr) { + // TODO + return null; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/GameData.java b/src/main/java/org/luxons/sevenwonders/game/data/GameData.java index 9d4e9a03..20a353cd 100644 --- a/src/main/java/org/luxons/sevenwonders/game/data/GameData.java +++ b/src/main/java/org/luxons/sevenwonders/game/data/GameData.java @@ -60,4 +60,10 @@ public class GameData { public void setCards(int age, List<Card> cards) { cardsPerAge.put(age, cards); } + + @Override + public String toString() { + return "GameData{" + "nbAges=" + nbAges + ", minPlayers=" + minPlayers + ", maxPlayers=" + maxPlayers + + ", wonders=" + wonders + ", cardsPerAge=" + cardsPerAge + '}'; + } } diff --git a/src/main/java/org/luxons/sevenwonders/game/data/GameDataLoader.java b/src/main/java/org/luxons/sevenwonders/game/data/GameDataLoader.java index 3047b900..7702a9f6 100644 --- a/src/main/java/org/luxons/sevenwonders/game/data/GameDataLoader.java +++ b/src/main/java/org/luxons/sevenwonders/game/data/GameDataLoader.java @@ -1,27 +1,44 @@ package org.luxons.sevenwonders.game.data; +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.luxons.sevenwonders.game.Settings; import org.luxons.sevenwonders.game.cards.Card; -import org.luxons.sevenwonders.game.cards.Color; -import org.luxons.sevenwonders.game.cards.Requirements; -import org.luxons.sevenwonders.game.effects.GoldIncrease; -import org.luxons.sevenwonders.game.effects.RawPointsIncrease; -import org.luxons.sevenwonders.game.effects.ScienceProgress; import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; import org.luxons.sevenwonders.game.wonders.Wonder; public class GameDataLoader { + private static final String BASE_PACKAGE = GameDataLoader.class.getPackage().getName(); + + private static final String BASE_PACKAGE_PATH = '/' + BASE_PACKAGE.replace('.', '/'); + private static final String wondersFile = "wonders.json"; - public static GameData load() { + public static GameData load(Settings settings) { GameData data = new GameData(); data.setWonders(loadWonders()); - for (int age = 1; age <= data.getNbAges(); age++) { - data.setCards(age, loadCards(age)); - } + + int nbPlayers = settings.getNbPlayers(); + + DecksDefinition defs = loadDecks(); + data.setCards(1, createCards(defs.getAge1(), nbPlayers)); + data.setCards(2, createCards(defs.getAge2(), nbPlayers)); + + List<Card> age3 = createCards(defs.getAge3(), nbPlayers); + List<Card> guild = createGuildCards(defs.getGuild(), nbPlayers); + age3.addAll(guild); + data.setCards(3, age3); return data; } @@ -38,12 +55,38 @@ public class GameDataLoader { return wonders; } - private static List<Card> loadCards(int age) { - List<Card> wonders = new ArrayList<>(); - // TODO load actual file - wonders.add(new Card("TestCard Yellow", Color.YELLOW, new Requirements(), new GoldIncrease())); - wonders.add(new Card("TestCard Blue", Color.BLUE, new Requirements(), new RawPointsIncrease())); - wonders.add(new Card("TestCard Green", Color.GREEN, new Requirements(), new ScienceProgress())); - return wonders; + private static List<Card> createCards(List<CardDefinition> defs, int nbPlayers) { + List<Card> cards = new ArrayList<>(); + for (CardDefinition def : defs) { + for (int i = 0; i < def.getCountPerNbPlayer().get(nbPlayers); i++) { + cards.add(def.createCard()); + } + } + return cards; + } + + private static List<Card> createGuildCards(List<CardDefinition> defs, int nbPlayers) { + List<Card> guild = defs.stream().map(CardDefinition::createCard).collect(Collectors.toList()); + Collections.shuffle(guild); + return guild.subList(0, nbPlayers + 2); + } + + private static DecksDefinition loadDecks() { + return readJsonFile("cards.json", DecksDefinition.class); + } + + private static <T> T readJsonFile(String filename, Class<T> clazz) { + InputStream in = GameDataLoader.class.getResourceAsStream(BASE_PACKAGE_PATH + '/' + filename); + Reader reader = new BufferedReader(new InputStreamReader(in)); + Gson gson = createGson(); + return gson.fromJson(reader, clazz); + } + + private static Gson createGson() { + return new GsonBuilder().disableHtmlEscaping() + .serializeNulls() + .registerTypeAdapter(Resources.class, new ResourcesSerializer()) + .setPrettyPrinting() + .create(); } } diff --git a/src/main/java/org/luxons/sevenwonders/game/data/ResourcesSerializer.java b/src/main/java/org/luxons/sevenwonders/game/data/ResourcesSerializer.java new file mode 100644 index 00000000..ff4a8ee3 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/ResourcesSerializer.java @@ -0,0 +1,40 @@ +package org.luxons.sevenwonders.game.data; + +import java.lang.reflect.Type; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; + +public class ResourcesSerializer implements JsonSerializer<Resources>, JsonDeserializer<Resources> { + + @Override + public JsonElement serialize(Resources resources, Type typeOfSrc, JsonSerializationContext context) { + String s = resources.getQuantities() + .entrySet() + .stream() + .flatMap(e -> Stream.generate(() -> e.getKey().getSymbol()).limit(e.getValue())) + .map(Object::toString) + .collect(Collectors.joining()); + return new JsonPrimitive(s); + } + + @Override + public Resources deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws + JsonParseException { + String s = json.getAsString(); + Resources resources = new Resources(); + for (char c : s.toCharArray()) { + resources.add(ResourceType.fromSymbol(c), 1); + } + return resources; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/resources/ResourceType.java b/src/main/java/org/luxons/sevenwonders/game/resources/ResourceType.java index 5e74254d..46d60123 100644 --- a/src/main/java/org/luxons/sevenwonders/game/resources/ResourceType.java +++ b/src/main/java/org/luxons/sevenwonders/game/resources/ResourceType.java @@ -1,21 +1,39 @@ package org.luxons.sevenwonders.game.resources; +import java.util.HashMap; +import java.util.Map; + public enum ResourceType { - WOOD("W"), - STONE("S"), - ORE("O"), - CLAY("C"), - GLASS("G"), - PAPYRUS("P"), - LOOM("L"); + WOOD('W'), + STONE('S'), + ORE('O'), + CLAY('C'), + GLASS('G'), + PAPYRUS('P'), + LOOM('L'); + + private static final Map<Character, ResourceType> typesPerSymbol = new HashMap<>(7); + static { + for (ResourceType type : values()) { + typesPerSymbol.put(type.symbol, type); + } + } - private final String symbol; + private final Character symbol; - ResourceType(String symbol) { + ResourceType(Character symbol) { this.symbol = symbol; } - public String getSymbol() { + public static ResourceType fromSymbol(Character symbol) { + ResourceType type = typesPerSymbol.get(symbol); + if (type == null) { + throw new IllegalArgumentException(String.format("Unknown resource type symbol '%s'", symbol)); + } + return type; + } + + public Character getSymbol() { return symbol; } } |