diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2016-12-08 04:17:42 +0100 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2016-12-08 04:27:05 +0100 |
commit | 14b390aa5acca85b126bb16192bb40a0e7fde757 (patch) | |
tree | 4dacc24981afdd9df29e9dc5b1d7318e174889af /src/main/java/org | |
parent | Setup gradle wrapper to make Travis use a recent version (diff) | |
download | seven-wonders-14b390aa5acca85b126bb16192bb40a0e7fde757.tar.gz seven-wonders-14b390aa5acca85b126bb16192bb40a0e7fde757.tar.bz2 seven-wonders-14b390aa5acca85b126bb16192bb40a0e7fde757.zip |
POC for the lobby
Diffstat (limited to 'src/main/java/org')
5 files changed, 105 insertions, 3 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/app/LobbyController.java b/src/main/java/org/luxons/sevenwonders/app/LobbyController.java new file mode 100644 index 00000000..c4e43a3b --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/app/LobbyController.java @@ -0,0 +1,47 @@ +package org.luxons.sevenwonders.app; + +import java.util.HashMap; +import java.util.Map; + +import org.luxons.sevenwonders.app.actions.JoinGameAction; +import org.luxons.sevenwonders.game.Game; +import org.luxons.sevenwonders.game.Player; +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.data.GameDataLoader; +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.handler.annotation.SendTo; +import org.springframework.stereotype.Controller; + +@Controller +public class LobbyController { + + private long lastGameId = 0; + + private Map<String, Game> games = new HashMap<>(); + + @MessageMapping("/lobby/create-game") + @SendTo("/broadcast/games") + public String createGame() throws Exception { + Thread.sleep(1000); // simulated delay + + String id = String.valueOf(lastGameId++); + System.out.println("Creating game " + id); + + Game game = new Game(new Settings(), GameDataLoader.load()); + games.put(id, game); + return id; + } + + @MessageMapping("/lobby/join-game") + @SendTo("/broadcast/players") + public Player joinGame(JoinGameAction joinAction) throws Exception { + Thread.sleep(1000); // simulated delay + + System.out.println("Player " + joinAction.getPlayerName() + " joined game " + joinAction.getGameId()); + + Game game = games.get(joinAction.getGameId()); + Player player = new Player(joinAction.getPlayerName()); + game.addPlayer(player); + return player; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/app/WebSocketConfig.java b/src/main/java/org/luxons/sevenwonders/app/WebSocketConfig.java new file mode 100644 index 00000000..e969c44e --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/app/WebSocketConfig.java @@ -0,0 +1,24 @@ +package org.luxons.sevenwonders.app; + +import org.springframework.context.annotation.Configuration; +import org.springframework.messaging.simp.config.MessageBrokerRegistry; +import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; +import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; +import org.springframework.web.socket.config.annotation.StompEndpointRegistry; + +@Configuration +@EnableWebSocketMessageBroker +public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { + + @Override + public void configureMessageBroker(MessageBrokerRegistry config) { + config.enableSimpleBroker("/broadcast"); + config.setApplicationDestinationPrefixes("/app"); + } + + @Override + public void registerStompEndpoints(StompEndpointRegistry registry) { + registry.addEndpoint("/seven-wonders-websocket").withSockJS(); + } + +}
\ No newline at end of file diff --git a/src/main/java/org/luxons/sevenwonders/app/actions/JoinGameAction.java b/src/main/java/org/luxons/sevenwonders/app/actions/JoinGameAction.java new file mode 100644 index 00000000..66476427 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/app/actions/JoinGameAction.java @@ -0,0 +1,24 @@ +package org.luxons.sevenwonders.app.actions; + +public class JoinGameAction { + + private String gameId; + + private String playerName; + + public String getGameId() { + return gameId; + } + + public void setGameId(String gameId) { + this.gameId = gameId; + } + + public String getPlayerName() { + return playerName; + } + + public void setPlayerName(String playerName) { + this.playerName = playerName; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/Player.java b/src/main/java/org/luxons/sevenwonders/game/Player.java index 2d35e120..ac7067b4 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Player.java +++ b/src/main/java/org/luxons/sevenwonders/game/Player.java @@ -4,6 +4,10 @@ public class Player { private String name; + public Player(String name) { + this.name = name; + } + public String getName() { return name; } 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 70a64eef..9d4e9a03 100644 --- a/src/main/java/org/luxons/sevenwonders/game/data/GameData.java +++ b/src/main/java/org/luxons/sevenwonders/game/data/GameData.java @@ -1,7 +1,10 @@ package org.luxons.sevenwonders.game.data; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.luxons.sevenwonders.game.cards.Card; import org.luxons.sevenwonders.game.wonders.Wonder; @@ -16,7 +19,7 @@ public class GameData { private List<Wonder> wonders = new ArrayList<>(); - private List<List<Card>> cardsPerAge = new ArrayList<>(); + private Map<Integer, List<Card>> cardsPerAge = new HashMap<>(); public int getNbAges() { return nbAges; @@ -51,10 +54,10 @@ public class GameData { } public List<Card> getCards(int age) { - return cardsPerAge.get(age - 1); // 0-based + return cardsPerAge.getOrDefault(age, Collections.emptyList()); } public void setCards(int age, List<Card> cards) { - cardsPerAge.set(age - 1, cards); // 0-based + cardsPerAge.put(age, cards); } } |