From b8cff38a1cdddad1baad754f6a813bc26cafe0d9 Mon Sep 17 00:00:00 2001 From: jbion Date: Mon, 19 Dec 2016 22:13:42 +0100 Subject: Add response with list of games at subscription to /topic/games --- .../sevenwonders/config/WebSocketConfig.java | 2 +- .../sevenwonders/controllers/LobbyController.java | 24 ++++++++++------------ .../sevenwonders/repositories/LobbyRepository.java | 5 +++++ src/main/resources/static/app.js | 11 ++++++++-- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/luxons/sevenwonders/config/WebSocketConfig.java b/src/main/java/org/luxons/sevenwonders/config/WebSocketConfig.java index 83629d6a..3b588894 100644 --- a/src/main/java/org/luxons/sevenwonders/config/WebSocketConfig.java +++ b/src/main/java/org/luxons/sevenwonders/config/WebSocketConfig.java @@ -19,7 +19,7 @@ public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { config.setUserDestinationPrefix("/user"); // prefix for all calls from clients - config.setApplicationDestinationPrefixes("/app"); + config.setApplicationDestinationPrefixes("/app", "/topic"); } @Override diff --git a/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java b/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java index 04735040..3b98d72f 100644 --- a/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java +++ b/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java @@ -1,7 +1,7 @@ package org.luxons.sevenwonders.controllers; import java.security.Principal; -import java.util.List; +import java.util.Collection; import org.luxons.sevenwonders.actions.JoinOrCreateGameAction; import org.luxons.sevenwonders.actions.StartGameAction; @@ -9,7 +9,6 @@ import org.luxons.sevenwonders.errors.ApiMisuseException; import org.luxons.sevenwonders.game.Game; import org.luxons.sevenwonders.game.Lobby; import org.luxons.sevenwonders.game.Player; -import org.luxons.sevenwonders.game.api.PlayerTurnInfo; import org.luxons.sevenwonders.repositories.GameRepository; import org.luxons.sevenwonders.repositories.LobbyRepository; import org.luxons.sevenwonders.session.SessionAttributes; @@ -21,11 +20,11 @@ import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.messaging.simp.SimpMessageHeaderAccessor; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.messaging.simp.annotation.SendToUser; +import org.springframework.messaging.simp.annotation.SubscribeMapping; import org.springframework.stereotype.Controller; import org.springframework.validation.annotation.Validated; @Controller -@MessageMapping("/lobby") public class LobbyController { private static final Logger logger = LoggerFactory.getLogger(LobbyController.class); @@ -44,7 +43,13 @@ public class LobbyController { this.gameRepository = gameRepository; } - @MessageMapping("/create-game") + @SubscribeMapping("/games") // prefix /topic not shown + public Collection listGames() { + logger.info("Subscribed to /games"); + return lobbyRepository.list(); + } + + @MessageMapping("/lobby/create-game") @SendTo("/topic/games") public Lobby createGame(SimpMessageHeaderAccessor headerAccessor, @Validated JoinOrCreateGameAction action, Principal principal) { @@ -59,7 +64,7 @@ public class LobbyController { return lobby; } - @MessageMapping("/join-game") + @MessageMapping("/lobby/join-game") @SendToUser("/queue/join-game") public Lobby joinGame(SimpMessageHeaderAccessor headerAccessor, @Validated JoinOrCreateGameAction action, Principal principal) { @@ -82,7 +87,7 @@ public class LobbyController { } } - @MessageMapping("/start-game") + @MessageMapping("/lobby/start-game") public void startGame(SimpMessageHeaderAccessor headerAccessor, @Validated StartGameAction action, Principal principal) { Lobby lobby = getOwnedLobby(headerAccessor, principal); @@ -90,13 +95,6 @@ public class LobbyController { gameRepository.add(game); logger.info("Game {} successfully started", game.getId()); - - List playerTurnInfos = game.startTurn(); - for (PlayerTurnInfo playerTurnInfo : playerTurnInfos) { - Player player = playerTurnInfo.getTable().getPlayers().get(playerTurnInfo.getPlayerIndex()); - String userName = player.getUserName(); - template.convertAndSendToUser(userName, "/queue/game/turn", playerTurnInfo); - } } private Lobby getOwnedLobby(SimpMessageHeaderAccessor headerAccessor, Principal principal) { diff --git a/src/main/java/org/luxons/sevenwonders/repositories/LobbyRepository.java b/src/main/java/org/luxons/sevenwonders/repositories/LobbyRepository.java index 2a4db2f3..bede34af 100644 --- a/src/main/java/org/luxons/sevenwonders/repositories/LobbyRepository.java +++ b/src/main/java/org/luxons/sevenwonders/repositories/LobbyRepository.java @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.repositories; +import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -24,6 +25,10 @@ public class LobbyRepository { this.gameDefinitionLoader = gameDefinitionLoader; } + public Collection list() { + return lobbies.values(); + } + public Lobby create(String gameName, Player owner) { if (lobbies.containsKey(gameName)) { throw new GameNameAlreadyUsedException(gameName); diff --git a/src/main/resources/static/app.js b/src/main/resources/static/app.js index ace23fdb..2b6b160b 100644 --- a/src/main/resources/static/app.js +++ b/src/main/resources/static/app.js @@ -25,8 +25,15 @@ function connect() { stompClient.subscribe('/topic/games', function (msg) { var game = JSON.parse(msg.body); - console.log("Received new game: " + game); - addNewGame(game); + if (Array.isArray(game)) { + console.log("Received new games: " + game); + for (var i = 0; i < game.length; i++) { + addNewGame(game[i]); + } + } else { + console.log("Received new game: " + game); + addNewGame(game); + } }); stompClient.subscribe('/user/queue/join-game', function (msg) { -- cgit