diff options
Diffstat (limited to 'backend/src')
3 files changed, 29 insertions, 7 deletions
diff --git a/backend/src/main/java/org/luxons/sevenwonders/controllers/GameBrowserController.java b/backend/src/main/java/org/luxons/sevenwonders/controllers/GameBrowserController.java index 23b03127..dca77465 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/controllers/GameBrowserController.java +++ b/backend/src/main/java/org/luxons/sevenwonders/controllers/GameBrowserController.java @@ -61,7 +61,6 @@ public class GameBrowserController { Player gameOwner = playerRepository.find(principal.getName()); Lobby lobby = lobbyRepository.create(action.getGameName(), gameOwner); - gameOwner.setLobby(lobby); logger.info("Game '{}' ({}) created by {} ({})", lobby.getName(), lobby.getId(), gameOwner.getDisplayName(), gameOwner.getUsername()); @@ -80,7 +79,6 @@ public class GameBrowserController { Lobby lobby = lobbyRepository.find(action.getGameId()); Player newPlayer = playerRepository.find(principal.getName()); lobby.addPlayer(newPlayer); - newPlayer.setLobby(lobby); logger.info("Player '{}' ({}) joined game {}", newPlayer.getDisplayName(), newPlayer.getUsername(), lobby.getName()); 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 2081d531..1cd5b3ae 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/lobby/Lobby.java +++ b/backend/src/main/java/org/luxons/sevenwonders/lobby/Lobby.java @@ -68,6 +68,7 @@ public class Lobby { throw new PlayerNameAlreadyUsedException(player.getDisplayName()); } player.setIndex(players.size()); + player.setLobby(this); players.add(player); } @@ -88,7 +89,9 @@ public class Lobby { throw new PlayerUnderflowException(gameDefinition.getMinPlayers()); } state = State.PLAYING; - return gameDefinition.initGame(id, settings, players.size()); + Game game = gameDefinition.initGame(id, settings, players.size()); + players.forEach(p -> p.setGame(game)); + return game; } private boolean hasEnoughPlayers() { @@ -123,6 +126,9 @@ public class Lobby { public void removePlayer(String username) throws UnknownPlayerException { Player player = getPlayer(players, username); players.remove(player); + player.setIndex(-1); + player.setLobby(null); + player.setGame(null); } static class GameAlreadyStartedException extends IllegalStateException { diff --git a/backend/src/test/java/org/luxons/sevenwonders/lobby/LobbyTest.java b/backend/src/test/java/org/luxons/sevenwonders/lobby/LobbyTest.java index 8a428aef..2e40d3cf 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/lobby/LobbyTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/lobby/LobbyTest.java @@ -11,6 +11,8 @@ import org.junit.experimental.theories.Theories; import org.junit.experimental.theories.Theory; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.Game; +import org.luxons.sevenwonders.game.api.CustomizableSettings; import org.luxons.sevenwonders.game.data.GameDefinition; import org.luxons.sevenwonders.game.data.GameDefinitionLoader; import org.luxons.sevenwonders.lobby.Lobby.GameAlreadyStartedException; @@ -21,6 +23,8 @@ import org.luxons.sevenwonders.lobby.Lobby.UnknownPlayerException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; @@ -70,6 +74,7 @@ public class LobbyTest { gameOwner.setIndex(42); Lobby l = new Lobby(5, "Test Game", gameOwner, gameDefinition); assertSame(gameOwner, l.getPlayers().get(0)); + assertSame(l, gameOwner.getLobby()); assertEquals(0, gameOwner.getIndex()); } @@ -105,6 +110,8 @@ public class LobbyTest { Player player = new Player("testuser", "Test User"); lobby.addPlayer(player); assertTrue(lobby.containsUser("testuser")); + assertSame(lobby, player.getLobby()); + assertSame(1, player.getIndex()); // the owner is 0 } @Test(expected = PlayerNameAlreadyUsedException.class) @@ -136,9 +143,8 @@ public class LobbyTest { } } - @Test + @Test(expected = UnknownPlayerException.class) public void removePlayer_failsWhenNotPresent() { - thrown.expect(UnknownPlayerException.class); lobby.removePlayer("anyname"); } @@ -148,6 +154,9 @@ public class LobbyTest { lobby.addPlayer(player); lobby.removePlayer("testuser"); assertFalse(lobby.containsUser("testuser")); + assertNull(player.getLobby()); + assertNull(player.getGame()); + assertEquals(-1, player.getIndex()); } @Test @@ -175,7 +184,7 @@ public class LobbyTest { lobby.addPlayer(player1); lobby.addPlayer(player2); lobby.addPlayer(player3); - lobby.reorderPlayers(Arrays.asList("testuser4", "testuser1", "testuser2")); + lobby.reorderPlayers(Arrays.asList("unknown", "testuser1", "testuser2")); } @Theory @@ -193,7 +202,9 @@ public class LobbyTest { assumeTrue(nbPlayers <= gameDefinition.getMaxPlayers()); // there is already the owner addPlayers(nbPlayers - 1); - lobby.startGame(); + Game game = lobby.startGame(); + assertNotNull(game); + lobby.getPlayers().forEach(p -> assertSame(game, p.getGame())); } @Test @@ -204,4 +215,11 @@ public class LobbyTest { lobby.startGame(); assertTrue(lobby.getState() == State.PLAYING); } + + @Test + public void setSettings() throws Exception { + CustomizableSettings settings = new CustomizableSettings(); + lobby.setSettings(settings); + assertSame(settings, lobby.getSettings()); + } } |