From 13a9d6dbf15d3f20f8032c4dd1e8ded22f52bf91 Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Mon, 16 Jan 2017 23:23:46 +0100 Subject: Standardize PlayerRepository's API --- .../sevenwonders/controllers/LobbyController.java | 2 +- .../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 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"); -- cgit