diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2017-01-16 23:23:46 +0100 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2017-01-16 23:23:46 +0100 |
commit | 13a9d6dbf15d3f20f8032c4dd1e8ded22f52bf91 (patch) | |
tree | 27a94d18ec568ffa2d8d74124bb25218321126f4 /src/main/java | |
parent | Remove unicity constraint on game names (diff) | |
download | seven-wonders-13a9d6dbf15d3f20f8032c4dd1e8ded22f52bf91.tar.gz seven-wonders-13a9d6dbf15d3f20f8032c4dd1e8ded22f52bf91.tar.bz2 seven-wonders-13a9d6dbf15d3f20f8032c4dd1e8ded22f52bf91.zip |
Standardize PlayerRepository's API
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java | 2 | ||||
-rw-r--r-- | src/main/java/org/luxons/sevenwonders/repositories/PlayerRepository.java | 34 |
2 files changed, 22 insertions, 14 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java b/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java index 020576bc..996ea361 100644 --- a/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java +++ b/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java @@ -53,7 +53,7 @@ public class LobbyController { @SendToUser("/queue/nameChoice") public Player chooseName(@Validated ChooseNameAction action, Principal principal) { String username = principal.getName(); - Player player = playerRepository.updateOrCreatePlayer(username, action.getPlayerName()); + Player player = playerRepository.createOrUpdate(username, action.getPlayerName()); logger.info("Player '{}' chose the name '{}'", username, player.getDisplayName()); return player; diff --git a/src/main/java/org/luxons/sevenwonders/repositories/PlayerRepository.java b/src/main/java/org/luxons/sevenwonders/repositories/PlayerRepository.java index f6c29d83..67f01808 100644 --- a/src/main/java/org/luxons/sevenwonders/repositories/PlayerRepository.java +++ b/src/main/java/org/luxons/sevenwonders/repositories/PlayerRepository.java @@ -12,28 +12,28 @@ public class PlayerRepository { private Map<String, Player> players = new HashMap<>(); - public Player updateOrCreatePlayer(String username, String displayName) { + public boolean contains(String username) { + return players.containsKey(username); + } + + public Player createOrUpdate(String username, String displayName) { if (players.containsKey(username)) { - return updatePlayerName(username, displayName); + return update(username, displayName); } else { - return createPlayer(username, displayName); + return create(username, displayName); } } - private Player createPlayer(String username, String displayName) throws PlayerAlreadyExistsException { + private Player create(String username, String displayName) throws PlayerAlreadyExistsException { + if (players.containsKey(username)) { + throw new PlayerAlreadyExistsException(username); + } Player player = new Player(username, displayName); - add(player); + players.put(username, player); return player; } - private void add(Player player) throws PlayerAlreadyExistsException { - if (players.containsKey(player.getUsername())) { - throw new PlayerAlreadyExistsException(player.getUsername()); - } - players.put(player.getUsername(), player); - } - - private Player updatePlayerName(String username, String displayName) throws PlayerNotFoundException { + private Player update(String username, String displayName) throws PlayerNotFoundException { Player player = find(username); player.setDisplayName(displayName); return player; @@ -47,6 +47,14 @@ public class PlayerRepository { return player; } + public Player remove(String username) { + Player player = players.remove(username); + if (player == null) { + throw new PlayerNotFoundException(username); + } + return player; + } + public static class PlayerNotFoundException extends ApiMisuseException { PlayerNotFoundException(String username) { super("Player '" + username + "' doesn't exist"); |