diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2017-05-15 08:44:24 +0200 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2017-05-15 08:44:24 +0200 |
commit | 97c8a4805ce0686a2ffa67e62e2f5dc1cd47c30b (patch) | |
tree | 3be22975bf671d09a6aadbebca3af1d8332480c3 /backend | |
parent | Add lobby saga (diff) | |
download | seven-wonders-97c8a4805ce0686a2ffa67e62e2f5dc1cd47c30b.tar.gz seven-wonders-97c8a4805ce0686a2ffa67e62e2f5dc1cd47c30b.tar.bz2 seven-wonders-97c8a4805ce0686a2ffa67e62e2f5dc1cd47c30b.zip |
Add 'leave lobby' action (backend only)
Diffstat (limited to 'backend')
-rw-r--r-- | backend/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java | 23 | ||||
-rw-r--r-- | backend/src/main/java/org/luxons/sevenwonders/lobby/Lobby.java | 4 |
2 files changed, 23 insertions, 4 deletions
diff --git a/backend/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java b/backend/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java index 8d6623c6..2bace487 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java +++ b/backend/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java @@ -10,6 +10,7 @@ import org.luxons.sevenwonders.actions.UpdateSettingsAction; import org.luxons.sevenwonders.errors.ApiMisuseException; import org.luxons.sevenwonders.game.Game; import org.luxons.sevenwonders.lobby.Lobby; +import org.luxons.sevenwonders.lobby.Player; import org.luxons.sevenwonders.repositories.PlayerRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,12 +37,22 @@ public class LobbyController { } @ApiMethod + @MessageMapping("/lobby/leave") + public void leave(Principal principal) { + Lobby lobby = getLobby(principal); + lobby.removePlayer(principal.getName()); + + logger.info("Player '{}' left game '{}'", principal.getName(), lobby.getName()); + sendLobbyUpdateToPlayers(lobby); + } + + @ApiMethod @MessageMapping("/lobby/reorderPlayers") public void reorderPlayers(@Validated ReorderPlayersAction action, Principal principal) { Lobby lobby = getLobby(principal); lobby.reorderPlayers(action.getOrderedPlayers()); - logger.info("Players in game {} reordered to {}", lobby.getName(), action.getOrderedPlayers()); + logger.info("Players in game '{}' reordered to {}", lobby.getName(), action.getOrderedPlayers()); sendLobbyUpdateToPlayers(lobby); } @@ -51,7 +62,7 @@ public class LobbyController { Lobby lobby = getLobby(principal); lobby.setSettings(action.getSettings()); - logger.info("Updated settings of game {}", lobby.getName()); + logger.info("Updated settings of game '{}'", lobby.getName()); sendLobbyUpdateToPlayers(lobby); } @@ -66,7 +77,7 @@ public class LobbyController { Lobby lobby = getOwnedLobby(principal); Game game = lobby.startGame(); - logger.info("Game {} successfully started", game.getId()); + logger.info("Game '{}' successfully started", game.getId()); template.convertAndSend("/topic/lobby/" + lobby.getId() + "/started", (Object) null); } @@ -79,13 +90,17 @@ public class LobbyController { } private Lobby getLobby(Principal principal) { - Lobby lobby = playerRepository.find(principal.getName()).getLobby(); + Lobby lobby = getPlayer(principal).getLobby(); if (lobby == null) { throw new UserNotInLobbyException(principal.getName()); } return lobby; } + private Player getPlayer(Principal principal) { + return playerRepository.find(principal.getName()); + } + private static class UserNotInLobbyException extends ApiMisuseException { UserNotInLobbyException(String username) { super("User " + username + " is not in a lobby, create or join a game first"); diff --git a/backend/src/main/java/org/luxons/sevenwonders/lobby/Lobby.java b/backend/src/main/java/org/luxons/sevenwonders/lobby/Lobby.java index 05b39569..0b8ef0b2 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/lobby/Lobby.java +++ b/backend/src/main/java/org/luxons/sevenwonders/lobby/Lobby.java @@ -120,6 +120,10 @@ public class Lobby { return players.stream().anyMatch(p -> p.getUsername().equals(username)); } + public void removePlayer(String username) { + players.removeIf(p -> p.getUsername().equals(username)); + } + static class GameAlreadyStartedException extends IllegalStateException { } |