summaryrefslogtreecommitdiff
path: root/backend/src/main/java/org
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2017-05-15 20:48:57 +0200
committerJoffrey BION <joffrey.bion@gmail.com>2017-05-15 20:48:57 +0200
commit0640d57928bc0681227c2e513b501e437201fdf8 (patch)
treebcf29785dd8590cf5cb60e2fa7637d6458212345 /backend/src/main/java/org
parentAdd 'leave lobby' action (backend only) (diff)
downloadseven-wonders-0640d57928bc0681227c2e513b501e437201fdf8.tar.gz
seven-wonders-0640d57928bc0681227c2e513b501e437201fdf8.tar.bz2
seven-wonders-0640d57928bc0681227c2e513b501e437201fdf8.zip
Add test for Lobby.removePlayer()
Diffstat (limited to 'backend/src/main/java/org')
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/lobby/Lobby.java23
1 files changed, 15 insertions, 8 deletions
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 0b8ef0b2..b143538a 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/lobby/Lobby.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/lobby/Lobby.java
@@ -62,7 +62,7 @@ public class Lobby {
throw new GameAlreadyStartedException();
}
if (maxPlayersReached()) {
- throw new PlayerOverflowException();
+ throw new PlayerOverflowException(gameDefinition.getMaxPlayers());
}
if (playerNameAlreadyUsed(player.getDisplayName())) {
throw new PlayerNameAlreadyUsedException(player.getDisplayName());
@@ -85,7 +85,7 @@ public class Lobby {
public synchronized Game startGame() throws PlayerUnderflowException {
if (!hasEnoughPlayers()) {
- throw new PlayerUnderflowException();
+ throw new PlayerUnderflowException(gameDefinition.getMinPlayers());
}
state = State.PLAYING;
return gameDefinition.initGame(id, settings, players.size());
@@ -95,7 +95,7 @@ public class Lobby {
return players.size() >= gameDefinition.getMinPlayers();
}
- public void reorderPlayers(List<String> orderedUsernames) {
+ public void reorderPlayers(List<String> orderedUsernames) throws UnknownPlayerException {
List<Player> formerList = new ArrayList<>(players);
players.clear();
for (int i = 0; i < orderedUsernames.size(); i++) {
@@ -105,7 +105,7 @@ public class Lobby {
}
}
- private static Player getPlayer(List<Player> players, String username) {
+ private static Player getPlayer(List<Player> players, String username) throws UnknownPlayerException {
return players.stream()
.filter(p -> p.getUsername().equals(username))
.findAny()
@@ -120,26 +120,33 @@ public class Lobby {
return players.stream().anyMatch(p -> p.getUsername().equals(username));
}
- public void removePlayer(String username) {
- players.removeIf(p -> p.getUsername().equals(username));
+ public void removePlayer(String username) throws UnknownPlayerException {
+ Player player = getPlayer(players, username);
+ players.remove(player);
}
static class GameAlreadyStartedException extends IllegalStateException {
}
static class PlayerOverflowException extends IllegalStateException {
+ PlayerOverflowException(int max) {
+ super(String.format("maximum %d players allowed", max));
+ }
}
static class PlayerUnderflowException extends IllegalStateException {
+ PlayerUnderflowException(int min) {
+ super(String.format("minimum %d players required to start a game", min));
+ }
}
- static class PlayerNameAlreadyUsedException extends RuntimeException {
+ static class PlayerNameAlreadyUsedException extends IllegalStateException {
PlayerNameAlreadyUsedException(String name) {
super(name);
}
}
- static class UnknownPlayerException extends IllegalArgumentException {
+ static class UnknownPlayerException extends IllegalStateException {
UnknownPlayerException(String username) {
super(username);
}
bgstack15