From 0721d2d6be8be3485eaf51622b9fa4671f02f0b6 Mon Sep 17 00:00:00 2001 From: jbion Date: Sat, 17 Dec 2016 23:22:27 +0100 Subject: Add proper logging and validation --- .../sevenwonders/actions/JoinGameAction.java | 7 ++++++ .../sevenwonders/controllers/LobbyController.java | 27 +++++++++++----------- 2 files changed, 21 insertions(+), 13 deletions(-) (limited to 'src') 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; } } -- cgit