diff options
Diffstat (limited to 'src')
11 files changed, 149 insertions, 95 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/actions/UpdateSettingsAction.java b/src/main/java/org/luxons/sevenwonders/actions/UpdateSettingsAction.java index deb5e530..822a5a1c 100644 --- a/src/main/java/org/luxons/sevenwonders/actions/UpdateSettingsAction.java +++ b/src/main/java/org/luxons/sevenwonders/actions/UpdateSettingsAction.java @@ -2,18 +2,18 @@ package org.luxons.sevenwonders.actions; import javax.validation.constraints.NotNull; -import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.CustomizableSettings; public class UpdateSettingsAction { @NotNull - private Settings settings; + private CustomizableSettings settings; - public Settings getSettings() { + public CustomizableSettings getSettings() { return settings; } - public void setSettings(Settings settings) { + public void setSettings(CustomizableSettings settings) { this.settings = settings; } } diff --git a/src/main/java/org/luxons/sevenwonders/game/Lobby.java b/src/main/java/org/luxons/sevenwonders/game/Lobby.java index 525321b2..c0201438 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Lobby.java +++ b/src/main/java/org/luxons/sevenwonders/game/Lobby.java @@ -3,6 +3,7 @@ package org.luxons.sevenwonders.game; import java.util.ArrayList; import java.util.List; +import org.luxons.sevenwonders.game.api.CustomizableSettings; import org.luxons.sevenwonders.game.data.GameDefinition; public class Lobby { @@ -17,7 +18,7 @@ public class Lobby { private final List<Player> players; - private Settings settings; + private CustomizableSettings settings; private State state = State.LOBBY; @@ -27,7 +28,7 @@ public class Lobby { this.owner = owner; this.gameDefinition = gameDefinition; this.players = new ArrayList<>(gameDefinition.getMinPlayers()); - this.settings = new Settings(); + this.settings = new CustomizableSettings(); players.add(owner); } @@ -43,11 +44,11 @@ public class Lobby { return players; } - public Settings getSettings() { + public CustomizableSettings getSettings() { return settings; } - public void setSettings(Settings settings) { + public void setSettings(CustomizableSettings settings) { this.settings = settings; } @@ -82,7 +83,6 @@ public class Lobby { throw new PlayerUnderflowException(); } state = State.PLAYING; - settings.setNbPlayers(players.size()); return gameDefinition.initGame(id, settings, players); } diff --git a/src/main/java/org/luxons/sevenwonders/game/Settings.java b/src/main/java/org/luxons/sevenwonders/game/Settings.java index cf7d1ea0..cd847d8a 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Settings.java +++ b/src/main/java/org/luxons/sevenwonders/game/Settings.java @@ -1,86 +1,68 @@ package org.luxons.sevenwonders.game; -import java.util.HashMap; import java.util.Map; import java.util.Random; +import org.luxons.sevenwonders.game.api.CustomizableSettings; import org.luxons.sevenwonders.game.data.definitions.WonderSide; import org.luxons.sevenwonders.game.data.definitions.WonderSidePickMethod; -import com.fasterxml.jackson.annotation.JsonIgnore; - public class Settings { - private int nbPlayers = -1; + private final Random random; - private int initialGold = 3; + private final int nbPlayers; - private int discardedCardGold = 3; + private final int initialGold; - private int defaultTradingCost = 2; + private final int discardedCardGold; - private WonderSidePickMethod wonderSidePickMethod = WonderSidePickMethod.EACH_RANDOM; + private final int defaultTradingCost; - private transient WonderSide lastPickedSide = null; + private final WonderSidePickMethod wonderSidePickMethod; - private long randomSeedForTests = -1; + private WonderSide lastPickedSide = null; - private transient Random random; + private final int lostPointsPerDefeat; - private int lostPointsPerDefeat = 1; + private final Map<Integer, Integer> wonPointsPerVictoryPerAge; - private Map<Integer, Integer> wonPointsPerVictoryPerAge = new HashMap<>(); + public Settings(int nbPlayers) { + this(nbPlayers, new CustomizableSettings()); + } - public Settings() { - wonPointsPerVictoryPerAge.put(1, 1); - wonPointsPerVictoryPerAge.put(2, 3); - wonPointsPerVictoryPerAge.put(3, 5); + public Settings(int nbPlayers, CustomizableSettings customSettings) { + long seed = customSettings.getRandomSeedForTests(); + this.random = seed > 0 ? new Random(seed) : new Random(); + this.nbPlayers = nbPlayers; + this.initialGold = customSettings.getInitialGold(); + this.discardedCardGold = customSettings.getDiscardedCardGold(); + this.defaultTradingCost = customSettings.getDefaultTradingCost(); + this.wonderSidePickMethod = customSettings.getWonderSidePickMethod(); + this.lostPointsPerDefeat = customSettings.getLostPointsPerDefeat(); + this.wonPointsPerVictoryPerAge = customSettings.getWonPointsPerVictoryPerAge(); } - @JsonIgnore - public int getNbPlayers() { - if (nbPlayers < 0) { - throw new IllegalStateException("The number of players has not been initialized"); - } - return nbPlayers; + public Random getRandom() { + return random; } - public void setNbPlayers(int nbPlayers) { - this.nbPlayers = nbPlayers; + public int getNbPlayers() { + return nbPlayers; } public int getInitialGold() { return initialGold; } - public void setInitialGold(int initialGold) { - this.initialGold = initialGold; - } - public int getDiscardedCardGold() { return discardedCardGold; } - public void setDiscardedCardGold(int discardedCardGold) { - this.discardedCardGold = discardedCardGold; - } - public int getDefaultTradingCost() { return defaultTradingCost; } - public void setDefaultTradingCost(int defaultTradingCost) { - this.defaultTradingCost = defaultTradingCost; - } - - public WonderSidePickMethod getWonderSidePickMethod() { - return wonderSidePickMethod; - } - - public void setWonderSidePickMethod(WonderSidePickMethod wonderSidePickMethod) { - this.wonderSidePickMethod = wonderSidePickMethod; - } - public WonderSide pickWonderSide() { return lastPickedSide = wonderSidePickMethod.pickSide(getRandom(), lastPickedSide); } @@ -89,31 +71,7 @@ public class Settings { return lostPointsPerDefeat; } - public void setLostPointsPerDefeat(int lostPointsPerDefeat) { - this.lostPointsPerDefeat = lostPointsPerDefeat; - } - public Map<Integer, Integer> getWonPointsPerVictoryPerAge() { return wonPointsPerVictoryPerAge; } - - public void setWonPointsPerVictoryPerAge(Map<Integer, Integer> wonPointsPerVictoryPerAge) { - this.wonPointsPerVictoryPerAge = wonPointsPerVictoryPerAge; - } - - public long getRandomSeedForTests() { - return randomSeedForTests; - } - - public void setRandomSeedForTests(long randomSeedForTests) { - this.randomSeedForTests = randomSeedForTests; - } - - @JsonIgnore - public Random getRandom() { - if (random == null) { - random = randomSeedForTests > 0 ? new Random(randomSeedForTests) : new Random(); - } - return random; - } } diff --git a/src/main/java/org/luxons/sevenwonders/game/api/CustomizableSettings.java b/src/main/java/org/luxons/sevenwonders/game/api/CustomizableSettings.java new file mode 100644 index 00000000..c303c4b6 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/api/CustomizableSettings.java @@ -0,0 +1,85 @@ +package org.luxons.sevenwonders.game.api; + +import java.util.HashMap; +import java.util.Map; + +import org.luxons.sevenwonders.game.data.definitions.WonderSidePickMethod; + +public class CustomizableSettings { + + private long randomSeedForTests = -1; + + private WonderSidePickMethod wonderSidePickMethod = WonderSidePickMethod.EACH_RANDOM; + + private int initialGold = 3; + + private int discardedCardGold = 3; + + private int defaultTradingCost = 2; + + private int lostPointsPerDefeat = 1; + + private Map<Integer, Integer> wonPointsPerVictoryPerAge = new HashMap<>(); + + public CustomizableSettings() { + wonPointsPerVictoryPerAge.put(1, 1); + wonPointsPerVictoryPerAge.put(2, 3); + wonPointsPerVictoryPerAge.put(3, 5); + } + + public long getRandomSeedForTests() { + return randomSeedForTests; + } + + public void setRandomSeedForTests(long randomSeedForTests) { + this.randomSeedForTests = randomSeedForTests; + } + + public int getInitialGold() { + return initialGold; + } + + public void setInitialGold(int initialGold) { + this.initialGold = initialGold; + } + + public int getDiscardedCardGold() { + return discardedCardGold; + } + + public void setDiscardedCardGold(int discardedCardGold) { + this.discardedCardGold = discardedCardGold; + } + + public int getDefaultTradingCost() { + return defaultTradingCost; + } + + public void setDefaultTradingCost(int defaultTradingCost) { + this.defaultTradingCost = defaultTradingCost; + } + + public WonderSidePickMethod getWonderSidePickMethod() { + return wonderSidePickMethod; + } + + public void setWonderSidePickMethod(WonderSidePickMethod wonderSidePickMethod) { + this.wonderSidePickMethod = wonderSidePickMethod; + } + + public int getLostPointsPerDefeat() { + return lostPointsPerDefeat; + } + + public void setLostPointsPerDefeat(int lostPointsPerDefeat) { + this.lostPointsPerDefeat = lostPointsPerDefeat; + } + + public Map<Integer, Integer> getWonPointsPerVictoryPerAge() { + return wonPointsPerVictoryPerAge; + } + + public void setWonPointsPerVictoryPerAge(Map<Integer, Integer> wonPointsPerVictoryPerAge) { + this.wonPointsPerVictoryPerAge = wonPointsPerVictoryPerAge; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java index 05de6162..4c63718b 100644 --- a/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java +++ b/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java @@ -5,11 +5,12 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import org.luxons.sevenwonders.game.cards.Decks; import org.luxons.sevenwonders.game.Game; import org.luxons.sevenwonders.game.Player; import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.CustomizableSettings; import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.cards.Decks; import org.luxons.sevenwonders.game.data.definitions.DecksDefinition; import org.luxons.sevenwonders.game.data.definitions.WonderDefinition; import org.luxons.sevenwonders.game.wonders.Wonder; @@ -43,7 +44,8 @@ public class GameDefinition { return MAX_PLAYERS; } - public Game initGame(long id, Settings settings, List<Player> orderedPlayers) { + public Game initGame(long id, CustomizableSettings customSettings, List<Player> orderedPlayers) { + Settings settings = new Settings(orderedPlayers.size(), customSettings); List<Board> boards = assignBoards(settings, orderedPlayers); Decks decks = decksDefinition.create(settings); return new Game(id, settings, orderedPlayers, boards, decks); diff --git a/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java b/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java index 675c77de..f9117146 100644 --- a/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java @@ -11,6 +11,7 @@ import org.junit.experimental.theories.Theory; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.CustomizableSettings; import org.luxons.sevenwonders.game.boards.Board.InsufficientFundsException; import org.luxons.sevenwonders.game.cards.Color; import org.luxons.sevenwonders.game.resources.ResourceType; @@ -49,15 +50,16 @@ public class BoardTest { @Theory public void initialGold_respectsSettings(@FromDataPoints("gold") int goldAmountInSettings) { - Settings settings = new Settings(); - settings.setInitialGold(goldAmountInSettings); + CustomizableSettings customSettings = new CustomizableSettings(); + customSettings.setInitialGold(goldAmountInSettings); + Settings settings = new Settings(5, customSettings); Board board = new Board(TestUtils.createWonder(), null, settings); assertEquals(goldAmountInSettings, board.getGold()); } @Theory public void initialProduction_containsInitialResource(ResourceType type) { - Board board = new Board(TestUtils.createWonder(type), null, new Settings()); + Board board = new Board(TestUtils.createWonder(type), null, new Settings(5)); Resources resources = TestUtils.createResources(type); assertTrue(board.getProduction().contains(resources)); } @@ -67,7 +69,7 @@ public class BoardTest { @FromDataPoints("gold") int goldRemoved) { assumeTrue(goldRemoved >= 0); assumeTrue(initialGold >= goldRemoved); - Board board = new Board(TestUtils.createWonder(), null, new Settings()); + Board board = new Board(TestUtils.createWonder(), null, new Settings(5)); board.setGold(initialGold); board.removeGold(goldRemoved); assertEquals(initialGold - goldRemoved, board.getGold()); @@ -79,7 +81,7 @@ public class BoardTest { assumeTrue(goldRemoved >= 0); assumeTrue(initialGold < goldRemoved); thrown.expect(InsufficientFundsException.class); - Board board = new Board(TestUtils.createWonder(), null, new Settings()); + Board board = new Board(TestUtils.createWonder(), null, new Settings(5)); board.setGold(initialGold); board.removeGold(goldRemoved); } @@ -87,7 +89,7 @@ public class BoardTest { @Theory public void getNbCardsOfColor_properCount_singleColor(ResourceType type, @FromDataPoints("nbCards") int nbCards, @FromDataPoints("nbCards") int nbOtherCards, Color color) { - Board board = new Board(TestUtils.createWonder(type), null, new Settings()); + Board board = new Board(TestUtils.createWonder(type), null, new Settings(5)); TestUtils.addCards(board, nbCards, nbOtherCards, color); assertEquals(nbCards, board.getNbCardsOfColor(Collections.singletonList(color))); } @@ -96,7 +98,7 @@ public class BoardTest { public void getNbCardsOfColor_properCount_multiColors(ResourceType type, @FromDataPoints("nbCards") int nbCards1, @FromDataPoints("nbCards") int nbCards2, @FromDataPoints("nbCards") int nbOtherCards, Color color1, Color color2) { - Board board = new Board(TestUtils.createWonder(type), null, new Settings()); + Board board = new Board(TestUtils.createWonder(type), null, new Settings(5)); TestUtils.addCards(board, nbCards1, color1); TestUtils.addCards(board, nbCards2, color2); TestUtils.addCards(board, nbOtherCards, TestUtils.getDifferentColorFrom(color1, color2)); diff --git a/src/test/java/org/luxons/sevenwonders/game/boards/MilitaryTest.java b/src/test/java/org/luxons/sevenwonders/game/boards/MilitaryTest.java index 9a1fcb65..7ef253db 100644 --- a/src/test/java/org/luxons/sevenwonders/game/boards/MilitaryTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/boards/MilitaryTest.java @@ -11,6 +11,7 @@ import org.junit.experimental.theories.Theory; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.CustomizableSettings; import org.luxons.sevenwonders.game.boards.Military.UnknownAgeException; import static org.junit.Assert.*; @@ -35,10 +36,11 @@ public class MilitaryTest { Map<Integer, Integer> wonPointsPerAge = new HashMap<>(); wonPointsPerAge.put(age, nbPointsPerVictory); - Settings settings = new Settings(); - settings.setWonPointsPerVictoryPerAge(wonPointsPerAge); - settings.setLostPointsPerDefeat(nbPointsPerDefeat); + CustomizableSettings customSettings = new CustomizableSettings(); + customSettings.setWonPointsPerVictoryPerAge(wonPointsPerAge); + customSettings.setLostPointsPerDefeat(nbPointsPerDefeat); + Settings settings = new Settings(5, customSettings); return new Military(settings); } diff --git a/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java b/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java index 0748f005..4a481442 100644 --- a/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java @@ -25,7 +25,7 @@ public class CardTest { @Before public void initBoard() { - Settings settings = new Settings(); + Settings settings = new Settings(5); List<Board> boards = new ArrayList<>(3); boards.add(new Board(new Wonder("TestWonder", ResourceType.WOOD), null, settings)); diff --git a/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java b/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java index d610d793..5acc09df 100644 --- a/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java @@ -6,6 +6,7 @@ import org.junit.Test; import org.luxons.sevenwonders.game.Game; import org.luxons.sevenwonders.game.Player; import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.CustomizableSettings; import org.luxons.sevenwonders.game.test.TestUtils; import static org.junit.Assert.*; @@ -17,10 +18,8 @@ public class GameDefinitionTest { GameDefinition gameDefinition = new GameDefinitionLoader().getGameDefinition(); assertNotNull(gameDefinition); - Settings settings = new Settings(); - settings.setNbPlayers(7); List<Player> players = TestUtils.createPlayers(7); - Game game = gameDefinition.initGame(0, settings, players); + Game game = gameDefinition.initGame(0, new CustomizableSettings(), players); assertNotNull(game); } }
\ No newline at end of file diff --git a/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java b/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java index f782118d..6e8c1335 100644 --- a/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java +++ b/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java @@ -6,6 +6,7 @@ import java.util.List; import org.luxons.sevenwonders.game.Player; import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.CustomizableSettings; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.boards.Science; @@ -48,7 +49,7 @@ public class TestUtils { } public static Board createBoard(ResourceType initialResource) { - Settings settings = new Settings(); + Settings settings = new Settings(5); Wonder wonder = createWonder(initialResource); String userName = "testUser" + initialResource.getSymbol(); diff --git a/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java b/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java new file mode 100644 index 00000000..a8a7643c --- /dev/null +++ b/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java @@ -0,0 +1,5 @@ +package org.luxons.sevenwonders.repositories; + +public class PlayerRepositoryTest { + +}
\ No newline at end of file |