diff options
author | jbion <joffrey.bion@amadeus.com> | 2016-12-17 23:22:27 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2016-12-17 23:23:22 +0100 |
commit | 0721d2d6be8be3485eaf51622b9fa4671f02f0b6 (patch) | |
tree | eb599f2410101b3acf6c36a26b202071ab61db38 | |
parent | Add default username for unauthenticated users (diff) | |
download | seven-wonders-0721d2d6be8be3485eaf51622b9fa4671f02f0b6.tar.gz seven-wonders-0721d2d6be8be3485eaf51622b9fa4671f02f0b6.tar.bz2 seven-wonders-0721d2d6be8be3485eaf51622b9fa4671f02f0b6.zip |
Add proper logging and validation
-rw-r--r-- | build.gradle | 1 | ||||
-rw-r--r-- | src/main/java/org/luxons/sevenwonders/actions/JoinGameAction.java | 7 | ||||
-rw-r--r-- | src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java | 27 |
3 files changed, 22 insertions, 13 deletions
diff --git a/build.gradle b/build.gradle index bb64b9a0..d72cfc4f 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,7 @@ repositories { dependencies { compile 'com.google.code.gson:gson:2.8.0' + compile 'ch.qos.logback:logback-classic:1.0.13' compile 'org.springframework.boot:spring-boot-starter-websocket' compile 'org.webjars:webjars-locator' diff --git a/src/main/java/org/luxons/sevenwonders/actions/JoinGameAction.java b/src/main/java/org/luxons/sevenwonders/actions/JoinGameAction.java index a9a84d78..fc2e11d2 100644 --- a/src/main/java/org/luxons/sevenwonders/actions/JoinGameAction.java +++ b/src/main/java/org/luxons/sevenwonders/actions/JoinGameAction.java @@ -1,9 +1,16 @@ package org.luxons.sevenwonders.actions; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + public class JoinGameAction { + @NotNull + @Size(min=2, max=30) private String gameId; + @NotNull + @Size(min=2, max=20) private String playerName; public String getGameId() { diff --git a/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java b/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java index 4b1f8ed4..312214d6 100644 --- a/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java +++ b/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java @@ -9,16 +9,21 @@ import org.luxons.sevenwonders.game.Game; import org.luxons.sevenwonders.game.Lobby; import org.luxons.sevenwonders.game.Player; import org.luxons.sevenwonders.game.data.GameDefinitionLoader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; 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.stereotype.Controller; +import org.springframework.validation.annotation.Validated; @Controller @MessageMapping("/lobby") public class LobbyController { + private static final Logger logger = LoggerFactory.getLogger(LobbyController.class); + private final GameDefinitionLoader gameDefinitionLoader; private long lastGameId = 0; @@ -34,41 +39,37 @@ public class LobbyController { @MessageMapping("/create-game") @SendTo("/topic/games") - public String createGame(SimpMessageHeaderAccessor headerAccessor) throws Exception { - System.out.println("Received message: " + headerAccessor.getSessionId()); - Thread.sleep(1000); // simulated delay - + public String createGame(SimpMessageHeaderAccessor headerAccessor, Principal principal) { long newId = lastGameId++; String id = String.valueOf(newId); - System.out.println("Creating game " + id); Lobby lobby = new Lobby(newId, gameDefinitionLoader.getGameDefinition()); lobbies.put(id, lobby); + logger.info("Game {} created by {}", id, principal.getName()); return id; } @MessageMapping("/join-game") @SendTo("/topic/players") - public Player joinGame(SimpMessageHeaderAccessor headerAccessor, JoinGameAction joinAction, Principal principal) - throws Exception { - Thread.sleep(1000); // simulated delay - + public Player joinGame(SimpMessageHeaderAccessor headerAccessor, @Validated JoinGameAction action, + Principal principal) { Player player = (Player)headerAccessor.getSessionAttributes().get("player"); Lobby lobby = (Lobby)headerAccessor.getSessionAttributes().get("lobby"); if (player != null && lobby != null) { - System.out.println("Client has already joined game " + lobby.getId() + "under the name " + player.getName()); + logger.warn("Client has already joined game {} under the name {}", lobby.getId(), player.getName()); return player; } - System.out.println("Player " + joinAction.getPlayerName() + " joined game " + joinAction.getGameId()); - lobby = lobbies.get(joinAction.getGameId()); - Player newPlayer = new Player(joinAction.getPlayerName()); + lobby = lobbies.get(action.getGameId()); + Player newPlayer = new Player(action.getPlayerName()); newPlayer.setUserName(principal.getName()); lobby.addPlayer(newPlayer); headerAccessor.getSessionAttributes().put("player", newPlayer); headerAccessor.getSessionAttributes().put("lobby", lobby); + logger.warn("Player {} joined game {}", action.getPlayerName(), action.getGameId()); + return newPlayer; } } |