diff options
author | jbion <joffrey.bion@amadeus.com> | 2016-12-28 14:18:18 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2016-12-28 14:18:18 +0100 |
commit | b7eefa589b49115fdada88669601b93370bd42d6 (patch) | |
tree | 94b6b4038ccc72fab74d10869f00155e39237062 /src | |
parent | Remove useless parent exception (diff) | |
download | seven-wonders-b7eefa589b49115fdada88669601b93370bd42d6.tar.gz seven-wonders-b7eefa589b49115fdada88669601b93370bd42d6.tar.bz2 seven-wonders-b7eefa589b49115fdada88669601b93370bd42d6.zip |
Add player reordering support in Lobby
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/luxons/sevenwonders/game/Lobby.java | 26 | ||||
-rw-r--r-- | src/test/java/org/luxons/sevenwonders/game/LobbyTest.java | 28 |
2 files changed, 52 insertions, 2 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/Lobby.java b/src/main/java/org/luxons/sevenwonders/game/Lobby.java index 30b31d4b..b115d32e 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Lobby.java +++ b/src/main/java/org/luxons/sevenwonders/game/Lobby.java @@ -2,6 +2,7 @@ package org.luxons.sevenwonders.game; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import org.luxons.sevenwonders.game.data.GameDefinition; @@ -13,10 +14,10 @@ public class Lobby { private final Player owner; - private final List<Player> players; - private final GameDefinition gameDefinition; + private List<Player> players; + private State state = State.LOBBY; public Lobby(long id, String name, Player owner, GameDefinition gameDefinition) { @@ -36,6 +37,10 @@ public class Lobby { return name; } + public List<Player> getPlayers() { + return players; + } + public synchronized int addPlayer(Player player) throws GameAlreadyStartedException, PlayerOverflowException { if (hasStarted()) { throw new GameAlreadyStartedException(); @@ -76,6 +81,17 @@ public class Lobby { return players.size() >= gameDefinition.getMinPlayers(); } + public void reorderPlayers(List<String> orderedUserNames) { + players = orderedUserNames.stream().map(this::getPlayer).collect(Collectors.toList()); + } + + private Player getPlayer(String userName) { + return players.stream() + .filter(p -> p.getUserName().equals(userName)) + .findAny() + .orElseThrow(() -> new UnknownPlayerException(userName)); + } + public boolean isOwner(String userName) { return owner.getUserName().equals(userName); } @@ -98,4 +114,10 @@ public class Lobby { super(name); } } + + static class UnknownPlayerException extends IllegalArgumentException { + UnknownPlayerException(String userName) { + super(userName); + } + } } diff --git a/src/test/java/org/luxons/sevenwonders/game/LobbyTest.java b/src/test/java/org/luxons/sevenwonders/game/LobbyTest.java index 8e59dea1..65069b88 100644 --- a/src/test/java/org/luxons/sevenwonders/game/LobbyTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/LobbyTest.java @@ -1,5 +1,7 @@ package org.luxons.sevenwonders.game; +import java.util.Arrays; + import org.junit.Before; import org.junit.BeforeClass; import org.junit.Rule; @@ -13,6 +15,7 @@ import org.luxons.sevenwonders.game.Lobby.GameAlreadyStartedException; import org.luxons.sevenwonders.game.Lobby.PlayerNameAlreadyUsedException; import org.luxons.sevenwonders.game.Lobby.PlayerOverflowException; import org.luxons.sevenwonders.game.Lobby.PlayerUnderflowException; +import org.luxons.sevenwonders.game.Lobby.UnknownPlayerException; import org.luxons.sevenwonders.game.data.GameDefinition; import org.luxons.sevenwonders.game.data.GameDefinitionLoader; @@ -110,6 +113,31 @@ public class LobbyTest { } } + @Test + public void reorderPlayers_failsOnSameName() { + Player player1 = new Player("Test User 1", "testuser1"); + Player player2 = new Player("Test User 2", "testuser2"); + Player player3 = new Player("Test User 3", "testuser3"); + lobby.addPlayer(player1); + lobby.addPlayer(player2); + lobby.addPlayer(player3); + lobby.reorderPlayers(Arrays.asList("testuser3", "testuser1", "testuser2")); + assertEquals("testuser3", lobby.getPlayers().get(0).getUserName()); + assertEquals("testuser1", lobby.getPlayers().get(1).getUserName()); + assertEquals("testuser2", lobby.getPlayers().get(2).getUserName()); + } + + @Test(expected = UnknownPlayerException.class) + public void reorderPlayers_failsOnUnknownPlayer() { + Player player1 = new Player("Test User 1", "testuser1"); + Player player2 = new Player("Test User 2", "testuser2"); + Player player3 = new Player("Test User 3", "testuser3"); + lobby.addPlayer(player1); + lobby.addPlayer(player2); + lobby.addPlayer(player3); + lobby.reorderPlayers(Arrays.asList("testuser4", "testuser1", "testuser2")); + } + @Theory public void startGame_failsBelowMinPlayers(int nbPlayers) { assumeTrue(nbPlayers < gameDefinition.getMinPlayers()); |