summaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2016-12-12 23:27:49 +0100
committerJoffrey BION <joffrey.bion@gmail.com>2016-12-12 23:27:49 +0100
commite4e0a96379f977a308fe1f0347e63a49b2a2c942 (patch)
tree66df01a20f81f965d54d7b56224ffdb2d39479c1 /src/main/java/org
parentAdd WS experiment test page (diff)
downloadseven-wonders-e4e0a96379f977a308fe1f0347e63a49b2a2c942.tar.gz
seven-wonders-e4e0a96379f977a308fe1f0347e63a49b2a2c942.tar.bz2
seven-wonders-e4e0a96379f977a308fe1f0347e63a49b2a2c942.zip
Lobby vs Game separation
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java19
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/Game.java66
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/GameData.java46
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/Lobby.java69
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java56
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/data/GameDefinitionLoader.java1
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/data/definitions/GameDefinition.java43
7 files changed, 146 insertions, 154 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java b/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java
index f97c5fe2..8e4f93c1 100644
--- a/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java
+++ b/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java
@@ -5,8 +5,8 @@ import java.util.Map;
import org.luxons.sevenwonders.actions.JoinGameAction;
import org.luxons.sevenwonders.game.Game;
+import org.luxons.sevenwonders.game.Lobby;
import org.luxons.sevenwonders.game.Player;
-import org.luxons.sevenwonders.game.Settings;
import org.luxons.sevenwonders.game.data.GameDefinitionLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
@@ -22,6 +22,8 @@ public class LobbyController {
private long lastGameId = 0;
+ private Map<String, Lobby> lobbies = new HashMap<>();
+
private Map<String, Game> games = new HashMap<>();
@Autowired
@@ -38,9 +40,8 @@ public class LobbyController {
String id = String.valueOf(lastGameId++);
System.out.println("Creating game " + id);
- Settings settings = new Settings();
- Game game = new Game(settings, GameDefinitionLoader.load().create(settings));
- games.put(id, game);
+ Lobby lobby = new Lobby(lastGameId, gameDefinitionLoader.getGameDefinition());
+ lobbies.put(id, lobby);
return id;
}
@@ -50,17 +51,19 @@ public class LobbyController {
Thread.sleep(1000); // simulated delay
Player player = (Player)headerAccessor.getSessionAttributes().get("player");
- if (player != null) {
- System.out.println("Client has already joined game under the name " + player.getName());
+ Lobby lobby = (Lobby)headerAccessor.getSessionAttributes().get("lobby");
+ if (player != null && lobby != null) {
+ System.out.println("Client has already joined game " + lobby.getId() + "under the name " + player.getName());
return player;
}
System.out.println("Player " + joinAction.getPlayerName() + " joined game " + joinAction.getGameId());
- Game game = games.get(joinAction.getGameId());
+ lobby = lobbies.get(joinAction.getGameId());
Player newPlayer = new Player(joinAction.getPlayerName());
- game.addPlayer(newPlayer);
+ lobby.addPlayer(newPlayer);
headerAccessor.getSessionAttributes().put("player", newPlayer);
+ headerAccessor.getSessionAttributes().put("lobby", lobby);
return newPlayer;
}
diff --git a/src/main/java/org/luxons/sevenwonders/game/Game.java b/src/main/java/org/luxons/sevenwonders/game/Game.java
index ec2665fe..d07bac93 100644
--- a/src/main/java/org/luxons/sevenwonders/game/Game.java
+++ b/src/main/java/org/luxons/sevenwonders/game/Game.java
@@ -1,75 +1,29 @@
package org.luxons.sevenwonders.game;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
import org.luxons.sevenwonders.game.boards.Board;
-import org.luxons.sevenwonders.game.wonders.Wonder;
-public class Game {
+import java.util.List;
- private final GameData data;
+public class Game {
private final Settings settings;
- private final List<Player> players;
-
private final List<Board> boards;
- private State state = State.LOBBY;
+ private final Decks decks;
- public Game(Settings settings, GameData data) {
+ public Game(Settings settings, List<Board> boards, Decks decks) {
this.settings = settings;
- this.data = data;
- this.players = new ArrayList<>(3);
- this.boards = new ArrayList<>(3);
- }
-
- public synchronized int addPlayer(Player player) {
- if (hasStarted()) {
- throw new GameAlreadyStartedException();
- }
- if (maxPlayersReached()) {
- throw new PlayerOverflowException();
- }
- int playerId = players.size();
- players.add(player);
- return playerId;
- }
-
- public synchronized void startGame() {
- if (!hasEnoughPlayers()) {
- throw new PlayerUnderflowException();
- }
- state = State.PLAYING;
- randomizeBoards();
+ this.boards = boards;
+ this.decks = decks;
}
- private boolean hasStarted() {
- return state == State.PLAYING;
+ public List<Board> getBoards() {
+ return boards;
}
- private boolean maxPlayersReached() {
- return players.size() >= data.getMaxPlayers();
+ public Decks getDecks() {
+ return decks;
}
- private boolean hasEnoughPlayers() {
- return players.size() >= data.getMinPlayers();
- }
-
- private void randomizeBoards() {
- List<Wonder> randomizedWonders = new ArrayList<>(data.getWonders());
- Collections.shuffle(randomizedWonders, settings.getRandom());
- randomizedWonders.stream().map(w -> new Board(w, settings)).forEach(boards::add);
- }
-
- public class GameAlreadyStartedException extends IllegalStateException {
- }
-
- public class PlayerOverflowException extends IllegalStateException {
- }
-
- public class PlayerUnderflowException extends IllegalStateException {
- }
}
diff --git a/src/main/java/org/luxons/sevenwonders/game/GameData.java b/src/main/java/org/luxons/sevenwonders/game/GameData.java
deleted file mode 100644
index 100389be..00000000
--- a/src/main/java/org/luxons/sevenwonders/game/GameData.java
+++ /dev/null
@@ -1,46 +0,0 @@
-package org.luxons.sevenwonders.game;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.luxons.sevenwonders.game.wonders.Wonder;
-
-public class GameData {
-
- private final int minPlayers;
-
- private final int maxPlayers;
-
- private List<Wonder> wonders = new ArrayList<>();
-
- private Decks decks;
-
- public GameData(int minPlayers, int maxPlayers) {
- this.minPlayers = minPlayers;
- this.maxPlayers = maxPlayers;
- }
-
- public int getMinPlayers() {
- return minPlayers;
- }
-
- public int getMaxPlayers() {
- return maxPlayers;
- }
-
- public List<Wonder> getWonders() {
- return wonders;
- }
-
- public void setWonders(List<Wonder> wonders) {
- this.wonders = wonders;
- }
-
- public Decks getDecks() {
- return decks;
- }
-
- public void setDecks(Decks decks) {
- this.decks = decks;
- }
-}
diff --git a/src/main/java/org/luxons/sevenwonders/game/Lobby.java b/src/main/java/org/luxons/sevenwonders/game/Lobby.java
new file mode 100644
index 00000000..a7c42665
--- /dev/null
+++ b/src/main/java/org/luxons/sevenwonders/game/Lobby.java
@@ -0,0 +1,69 @@
+package org.luxons.sevenwonders.game;
+
+import org.luxons.sevenwonders.game.data.GameDefinition;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Lobby {
+
+ private final long id;
+
+ private final List<Player> players;
+
+ private final GameDefinition gameDefinition;
+
+ private State state = State.LOBBY;
+
+ public Lobby(long id, GameDefinition gameDefintion) {
+ this.id = id;
+ this.gameDefinition = gameDefintion;
+ this.players = new ArrayList<>(3);
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public synchronized int addPlayer(Player player) {
+ if (hasStarted()) {
+ throw new GameAlreadyStartedException();
+ }
+ if (maxPlayersReached()) {
+ throw new PlayerOverflowException();
+ }
+ int playerId = players.size();
+ players.add(player);
+ return playerId;
+ }
+
+ private boolean hasStarted() {
+ return state != State.LOBBY;
+ }
+
+ public synchronized Game startGame(Settings settings) {
+ if (!hasEnoughPlayers()) {
+ throw new PlayerUnderflowException();
+ }
+ state = State.PLAYING;
+ settings.setNbPlayers(players.size());
+ return gameDefinition.initGame(settings);
+ }
+
+ private boolean maxPlayersReached() {
+ return players.size() >= gameDefinition.getMaxPlayers();
+ }
+
+ private boolean hasEnoughPlayers() {
+ return players.size() >= gameDefinition.getMinPlayers();
+ }
+
+ public class GameAlreadyStartedException extends IllegalStateException {
+ }
+
+ public class PlayerOverflowException extends IllegalStateException {
+ }
+
+ public class PlayerUnderflowException extends IllegalStateException {
+ }
+}
diff --git a/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java
new file mode 100644
index 00000000..d2932cdf
--- /dev/null
+++ b/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java
@@ -0,0 +1,56 @@
+package org.luxons.sevenwonders.game.data;
+
+import org.luxons.sevenwonders.game.Decks;
+import org.luxons.sevenwonders.game.Game;
+import org.luxons.sevenwonders.game.Settings;
+import org.luxons.sevenwonders.game.boards.Board;
+import org.luxons.sevenwonders.game.data.definitions.DecksDefinition;
+import org.luxons.sevenwonders.game.data.definitions.Definition;
+import org.luxons.sevenwonders.game.data.definitions.WonderDefinition;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class GameDefinition {
+
+ /**
+ * This value is heavily dependent on the JSON data. Any change must be carefully thought through.
+ */
+ private static final int MIN_PLAYERS = 3;
+
+ /**
+ * This value is heavily dependent on the JSON data. Any change must be carefully thought through.
+ */
+ private static final int MAX_PLAYERS = 7;
+
+ private WonderDefinition[] wonders;
+
+ private DecksDefinition decksDefinition;
+
+ public GameDefinition(WonderDefinition[] wonders, DecksDefinition decksDefinition) {
+ this.wonders = wonders;
+ this.decksDefinition = decksDefinition;
+ }
+
+ public int getMinPlayers() {
+ return MIN_PLAYERS;
+ }
+
+ public int getMaxPlayers() {
+ return MAX_PLAYERS;
+ }
+
+ public Game initGame(Settings settings) {
+ List<Board> boards = pickRandomBoards(settings);
+ Decks decks = decksDefinition.create(settings);
+ return new Game(settings, boards, decks);
+ }
+
+ private List<Board> pickRandomBoards(Settings settings) {
+ List<WonderDefinition> randomizedWonders = Arrays.asList(wonders);
+ Collections.shuffle(randomizedWonders, settings.getRandom());
+ return Arrays.stream(wonders).map(def -> def.create(settings)).map(w -> new Board(w, settings)).collect(Collectors.toList());
+ }
+}
diff --git a/src/main/java/org/luxons/sevenwonders/game/data/GameDefinitionLoader.java b/src/main/java/org/luxons/sevenwonders/game/data/GameDefinitionLoader.java
index a2f39ed4..977bb100 100644
--- a/src/main/java/org/luxons/sevenwonders/game/data/GameDefinitionLoader.java
+++ b/src/main/java/org/luxons/sevenwonders/game/data/GameDefinitionLoader.java
@@ -11,7 +11,6 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.luxons.sevenwonders.game.data.definitions.DecksDefinition;
-import org.luxons.sevenwonders.game.data.definitions.GameDefinition;
import org.luxons.sevenwonders.game.data.definitions.WonderDefinition;
import org.luxons.sevenwonders.game.data.serializers.NumericEffectSerializer;
import org.luxons.sevenwonders.game.data.serializers.ProductionIncreaseSerializer;
diff --git a/src/main/java/org/luxons/sevenwonders/game/data/definitions/GameDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/definitions/GameDefinition.java
deleted file mode 100644
index 5947c547..00000000
--- a/src/main/java/org/luxons/sevenwonders/game/data/definitions/GameDefinition.java
+++ /dev/null
@@ -1,43 +0,0 @@
-package org.luxons.sevenwonders.game.data.definitions;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.stream.Collectors;
-
-import org.luxons.sevenwonders.game.Settings;
-import org.luxons.sevenwonders.game.GameData;
-import org.luxons.sevenwonders.game.wonders.Wonder;
-
-public class GameDefinition implements Definition<GameData> {
-
- /**
- * This value is heavily dependent on the JSON data. Any change must be carefully thought through.
- */
- private static final int MIN_PLAYERS = 3;
-
- /**
- * This value is heavily dependent on the JSON data. Any change must be carefully thought through.
- */
- private static final int MAX_PLAYERS = 7;
-
- private WonderDefinition[] wonders;
-
- private DecksDefinition decks;
-
- public GameDefinition(WonderDefinition[] wonders, DecksDefinition decks) {
- this.wonders = wonders;
- this.decks = decks;
- }
-
- @Override
- public GameData create(Settings settings) {
- GameData data = new GameData(MIN_PLAYERS, MAX_PLAYERS);
- data.setWonders(createWonders(settings));
- data.setDecks(decks.create(settings));
- return data;
- }
-
- private List<Wonder> createWonders(Settings settings) {
- return Arrays.stream(wonders).map(def -> def.create(settings)).collect(Collectors.toList());
- }
-}
bgstack15