From 28b77437282eac733a20d5b396de05c06221c65f Mon Sep 17 00:00:00 2001 From: jbion Date: Thu, 8 Dec 2016 19:46:17 +0100 Subject: First draft of client recognition through sessions --- .../org/luxons/sevenwonders/app/LobbyController.java | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/luxons/sevenwonders/app/LobbyController.java b/src/main/java/org/luxons/sevenwonders/app/LobbyController.java index c4e43a3b..be23ad52 100644 --- a/src/main/java/org/luxons/sevenwonders/app/LobbyController.java +++ b/src/main/java/org/luxons/sevenwonders/app/LobbyController.java @@ -10,6 +10,7 @@ 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.messaging.simp.SimpMessageHeaderAccessor; import org.springframework.stereotype.Controller; @Controller @@ -21,7 +22,8 @@ public class LobbyController { @MessageMapping("/lobby/create-game") @SendTo("/broadcast/games") - public String createGame() throws Exception { + public String createGame(SimpMessageHeaderAccessor headerAccessor) throws Exception { + System.out.println("Received message: " + headerAccessor.getSessionId()); Thread.sleep(1000); // simulated delay String id = String.valueOf(lastGameId++); @@ -34,14 +36,22 @@ public class LobbyController { @MessageMapping("/lobby/join-game") @SendTo("/broadcast/players") - public Player joinGame(JoinGameAction joinAction) throws Exception { + public Player joinGame(SimpMessageHeaderAccessor headerAccessor, JoinGameAction joinAction) throws Exception { 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()); + return player; + } 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; + Player newPlayer = new Player(joinAction.getPlayerName()); + game.addPlayer(newPlayer); + + headerAccessor.getSessionAttributes().put("player", newPlayer); + + return newPlayer; } } -- cgit