summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/luxons/sevenwonders/app/LobbyController.java47
-rw-r--r--src/main/java/org/luxons/sevenwonders/app/WebSocketConfig.java24
-rw-r--r--src/main/java/org/luxons/sevenwonders/app/actions/JoinGameAction.java24
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/Player.java4
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/data/GameData.java9
-rw-r--r--src/main/resources/static/app.js78
-rw-r--r--src/main/resources/static/index.html36
7 files changed, 219 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);
}
}
diff --git a/src/main/resources/static/app.js b/src/main/resources/static/app.js
new file mode 100644
index 00000000..5a3bc38e
--- /dev/null
+++ b/src/main/resources/static/app.js
@@ -0,0 +1,78 @@
+var stompClient = null;
+
+function setConnected(connected) {
+ $("#connect").prop("disabled", connected);
+ $("#disconnect").prop("disabled", !connected);
+ if (connected) {
+ $("#game-list").show();
+ } else {
+ $("#game-list").hide();
+ }
+ $("#greetings").html("");
+}
+
+function connect() {
+ var socket = new SockJS('/seven-wonders-websocket');
+ stompClient = Stomp.over(socket);
+ stompClient.connect({}, function (frame) {
+ setConnected(true);
+ console.log('Connected: ' + frame);
+
+ stompClient.subscribe('/broadcast/games', function (gameId) {
+ console.log("Received new game: " + gameId);
+ addNewGame(gameId.body);
+ });
+
+ stompClient.subscribe('/broadcast/players', function (player) {
+ console.log("Received new player: " + player);
+ addNewPlayer(JSON.parse(player.body));
+ });
+ });
+}
+
+function disconnect() {
+ if (stompClient != null) {
+ stompClient.disconnect();
+ }
+ setConnected(false);
+ console.log("Disconnected");
+}
+
+function sendCreateGame() {
+ stompClient.send("/app/lobby/create-game", {}, "");
+}
+
+function sendJoinGame(gameId) {
+ stompClient.send("/app/lobby/join-game", {},
+ JSON.stringify({'gameId': gameId, 'playerName': $("#player-name-field").val()}));
+}
+
+function addNewGame(gameId) {
+ console.log(gameId);
+ $("#game-list-content").append('<tr><td>' + gameId + '</td><td><button id="join-' + gameId + '" type="submit">Join</button></td></tr>');
+ $("#join-" + gameId).click(function () {
+ sendJoinGame(gameId);
+ });
+}
+
+function addNewPlayer(player) {
+ console.log(player);
+}
+
+$(function () {
+ $("form").on('submit', function (e) {
+ e.preventDefault();
+ });
+ $("#connect").click(function () {
+ connect();
+ });
+ $("#disconnect").click(function () {
+ disconnect();
+ });
+ $("#create-game").click(function () {
+ sendCreateGame();
+ });
+ $("#join-game").click(function () {
+ sendJoinGame();
+ });
+}); \ No newline at end of file
diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html
index e238079b..1b959d56 100644
--- a/src/main/resources/static/index.html
+++ b/src/main/resources/static/index.html
@@ -7,13 +7,49 @@
<script src="/webjars/jquery/jquery.min.js"></script>
<script src="/webjars/sockjs-client/sockjs.min.js"></script>
<script src="/webjars/stomp-websocket/stomp.min.js"></script>
+ <script src="app.js"></script>
</head>
<body>
<noscript>
<h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websocket relies on Javascript being
enabled. Please enable Javascript and reload this page!</h2>
</noscript>
+
<h1>Seven Wonders</h1>
+
<p>This is a stub index page for the project, for the sake of vertical completeness. We will soon get to work on it!</p>
+
+<h2>Connection</h2>
+
+<form class="form-inline">
+ <div class="form-group">
+ <label for="connect">WebSocket connection:</label>
+ <button id="connect" class="btn btn-default" type="submit">Connect</button>
+ <button id="disconnect" class="btn btn-default" type="submit" disabled="disabled">Disconnect</button>
+ </div>
+</form>
+
+<h2>Games</h2>
+
+<form class="form-inline">
+ <div class="form-group">
+ <label for="player-name-field">Player name</label>
+ <input id="player-name-field">
+ </div>
+</form>
+
+<table id="game-list" class="table table-striped">
+ <thead>
+ <tr>
+ <th>Id</th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody id="game-list-content">
+ </tbody>
+</table>
+
+<button id="create-game" class="btn btn-default" type="submit">Create Game</button>
+
</body>
</html> \ No newline at end of file
bgstack15