diff options
author | jbion <joffrey.bion@amadeus.com> | 2016-12-25 16:36:19 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2016-12-25 16:36:19 +0100 |
commit | 4acc7de32840aaac2f00fa73629b8e791ee00e79 (patch) | |
tree | 18209422554a22a7226126e40846dc83d9e60bd3 | |
parent | Add proper test for military victory/defeat points (diff) | |
download | seven-wonders-4acc7de32840aaac2f00fa73629b8e791ee00e79.tar.gz seven-wonders-4acc7de32840aaac2f00fa73629b8e791ee00e79.tar.bz2 seven-wonders-4acc7de32840aaac2f00fa73629b8e791ee00e79.zip |
Make LobbyController always send a list of lobbies on /topic/games
-rw-r--r-- | src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java | 20 | ||||
-rw-r--r-- | src/main/resources/static/app.js | 15 |
2 files changed, 13 insertions, 22 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java b/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java index 3b98d72f..285fd778 100644 --- a/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java +++ b/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java @@ -2,6 +2,7 @@ package org.luxons.sevenwonders.controllers; import java.security.Principal; import java.util.Collection; +import java.util.Collections; import org.luxons.sevenwonders.actions.JoinOrCreateGameAction; import org.luxons.sevenwonders.actions.StartGameAction; @@ -18,7 +19,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.handler.annotation.MessageMapping; 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; @@ -29,16 +29,12 @@ public class LobbyController { private static final Logger logger = LoggerFactory.getLogger(LobbyController.class); - private final SimpMessagingTemplate template; - private final LobbyRepository lobbyRepository; private final GameRepository gameRepository; @Autowired - public LobbyController(SimpMessagingTemplate template, LobbyRepository lobbyRepository, - GameRepository gameRepository) { - this.template = template; + public LobbyController(LobbyRepository lobbyRepository, GameRepository gameRepository) { this.lobbyRepository = lobbyRepository; this.gameRepository = gameRepository; } @@ -51,8 +47,8 @@ public class LobbyController { @MessageMapping("/lobby/create-game") @SendTo("/topic/games") - public Lobby createGame(SimpMessageHeaderAccessor headerAccessor, @Validated JoinOrCreateGameAction action, - Principal principal) { + public Collection<Lobby> createGame(SimpMessageHeaderAccessor headerAccessor, + @Validated JoinOrCreateGameAction action, Principal principal) { checkThatUserIsNotInAGame(headerAccessor, "cannot create another game"); Player gameOwner = new Player(action.getPlayerName(), principal.getName()); @@ -61,13 +57,13 @@ public class LobbyController { logger.info("Game '{}' (id={}) created by {} ({})", lobby.getName(), lobby.getId(), gameOwner.getDisplayName(), gameOwner.getUserName()); - return lobby; + return Collections.singletonList(lobby); } @MessageMapping("/lobby/join-game") @SendToUser("/queue/join-game") - public Lobby joinGame(SimpMessageHeaderAccessor headerAccessor, @Validated JoinOrCreateGameAction action, - Principal principal) { + public Collection<Lobby> joinGame(SimpMessageHeaderAccessor headerAccessor, + @Validated JoinOrCreateGameAction action, Principal principal) { checkThatUserIsNotInAGame(headerAccessor, "cannot join another game"); Lobby lobby = lobbyRepository.find(action.getGameName()); @@ -76,7 +72,7 @@ public class LobbyController { headerAccessor.getSessionAttributes().put(SessionAttributes.ATTR_LOBBY, lobby); logger.info("Player {} joined game {}", action.getPlayerName(), action.getGameName()); - return lobby; + return Collections.singletonList(lobby); } private void checkThatUserIsNotInAGame(SimpMessageHeaderAccessor headerAccessor, diff --git a/src/main/resources/static/app.js b/src/main/resources/static/app.js index 2b6b160b..0d68d2ef 100644 --- a/src/main/resources/static/app.js +++ b/src/main/resources/static/app.js @@ -24,15 +24,10 @@ function connect() { }); stompClient.subscribe('/topic/games', function (msg) { - var game = JSON.parse(msg.body); - 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); + var games = JSON.parse(msg.body); + console.log("Received new games: " + games); + for (var i = 0; i < games.length; i++) { + addNewGame(games[i]); } }); @@ -45,7 +40,7 @@ function connect() { } function disconnect() { - if (stompClient != null) { + if (stompClient !== null) { stompClient.disconnect(); } setConnected(false); |