summaryrefslogtreecommitdiff
path: root/backend/src
diff options
context:
space:
mode:
Diffstat (limited to 'backend/src')
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java18
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/game/api/CustomizableSettings.java23
-rw-r--r--backend/src/test/java/org/luxons/sevenwonders/controllers/LobbyControllerTest.java212
3 files changed, 244 insertions, 9 deletions
diff --git a/backend/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java b/backend/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java
index 2bace487..c3378149 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java
@@ -49,7 +49,7 @@ public class LobbyController {
@ApiMethod
@MessageMapping("/lobby/reorderPlayers")
public void reorderPlayers(@Validated ReorderPlayersAction action, Principal principal) {
- Lobby lobby = getLobby(principal);
+ Lobby lobby = getOwnedLobby(principal);
lobby.reorderPlayers(action.getOrderedPlayers());
logger.info("Players in game '{}' reordered to {}", lobby.getName(), action.getOrderedPlayers());
@@ -59,7 +59,7 @@ public class LobbyController {
@ApiMethod
@MessageMapping("/lobby/updateSettings")
public void updateSettings(@Validated UpdateSettingsAction action, Principal principal) {
- Lobby lobby = getLobby(principal);
+ Lobby lobby = getOwnedLobby(principal);
lobby.setSettings(action.getSettings());
logger.info("Updated settings of game '{}'", lobby.getName());
@@ -78,13 +78,13 @@ public class LobbyController {
Game game = lobby.startGame();
logger.info("Game '{}' successfully started", game.getId());
- template.convertAndSend("/topic/lobby/" + lobby.getId() + "/started", (Object) null);
+ template.convertAndSend("/topic/lobby/" + lobby.getId() + "/started", "");
}
private Lobby getOwnedLobby(Principal principal) {
Lobby lobby = getLobby(principal);
if (!lobby.isOwner(principal.getName())) {
- throw new UserIsNotOwnerException(principal.getName());
+ throw new PlayerIsNotOwnerException(principal.getName());
}
return lobby;
}
@@ -92,7 +92,7 @@ public class LobbyController {
private Lobby getLobby(Principal principal) {
Lobby lobby = getPlayer(principal).getLobby();
if (lobby == null) {
- throw new UserNotInLobbyException(principal.getName());
+ throw new PlayerNotInLobbyException(principal.getName());
}
return lobby;
}
@@ -101,14 +101,14 @@ public class LobbyController {
return playerRepository.find(principal.getName());
}
- private static class UserNotInLobbyException extends ApiMisuseException {
- UserNotInLobbyException(String username) {
+ static class PlayerNotInLobbyException extends ApiMisuseException {
+ PlayerNotInLobbyException(String username) {
super("User " + username + " is not in a lobby, create or join a game first");
}
}
- private static class UserIsNotOwnerException extends ApiMisuseException {
- UserIsNotOwnerException(String username) {
+ static class PlayerIsNotOwnerException extends ApiMisuseException {
+ PlayerIsNotOwnerException(String username) {
super("User " + username + " does not own the lobby he's in");
}
}
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/api/CustomizableSettings.java b/backend/src/main/java/org/luxons/sevenwonders/game/api/CustomizableSettings.java
index ee6a7714..2cbf8727 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/game/api/CustomizableSettings.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/game/api/CustomizableSettings.java
@@ -2,6 +2,7 @@ package org.luxons.sevenwonders.game.api;
import java.util.HashMap;
import java.util.Map;
+import java.util.Objects;
import org.luxons.sevenwonders.game.data.definitions.WonderSidePickMethod;
@@ -102,4 +103,26 @@ public class CustomizableSettings {
public void setWonPointsPerVictoryPerAge(Map<Integer, Integer> wonPointsPerVictoryPerAge) {
this.wonPointsPerVictoryPerAge = wonPointsPerVictoryPerAge;
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ CustomizableSettings that = (CustomizableSettings) o;
+ return randomSeedForTests == that.randomSeedForTests && timeLimitInSeconds == that.timeLimitInSeconds
+ && initialGold == that.initialGold && discardedCardGold == that.discardedCardGold
+ && defaultTradingCost == that.defaultTradingCost && pointsPer3Gold == that.pointsPer3Gold
+ && lostPointsPerDefeat == that.lostPointsPerDefeat && wonderSidePickMethod == that.wonderSidePickMethod
+ && Objects.equals(wonPointsPerVictoryPerAge, that.wonPointsPerVictoryPerAge);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(randomSeedForTests, timeLimitInSeconds, wonderSidePickMethod, initialGold,
+ discardedCardGold, defaultTradingCost, pointsPer3Gold, lostPointsPerDefeat, wonPointsPerVictoryPerAge);
+ }
}
diff --git a/backend/src/test/java/org/luxons/sevenwonders/controllers/LobbyControllerTest.java b/backend/src/test/java/org/luxons/sevenwonders/controllers/LobbyControllerTest.java
new file mode 100644
index 00000000..9377f885
--- /dev/null
+++ b/backend/src/test/java/org/luxons/sevenwonders/controllers/LobbyControllerTest.java
@@ -0,0 +1,212 @@
+package org.luxons.sevenwonders.controllers;
+
+import java.security.Principal;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.luxons.sevenwonders.actions.ReorderPlayersAction;
+import org.luxons.sevenwonders.actions.UpdateSettingsAction;
+import org.luxons.sevenwonders.controllers.LobbyController.PlayerIsNotOwnerException;
+import org.luxons.sevenwonders.controllers.LobbyController.PlayerNotInLobbyException;
+import org.luxons.sevenwonders.game.api.CustomizableSettings;
+import org.luxons.sevenwonders.game.data.GameDefinitionLoader;
+import org.luxons.sevenwonders.game.data.definitions.WonderSidePickMethod;
+import org.luxons.sevenwonders.lobby.Lobby;
+import org.luxons.sevenwonders.lobby.Player;
+import org.luxons.sevenwonders.lobby.State;
+import org.luxons.sevenwonders.repositories.LobbyRepository;
+import org.luxons.sevenwonders.repositories.PlayerRepository;
+import org.luxons.sevenwonders.repositories.PlayerRepository.PlayerNotFoundException;
+import org.luxons.sevenwonders.test.TestUtils;
+import org.springframework.messaging.simp.SimpMessagingTemplate;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+public class LobbyControllerTest {
+
+ private PlayerRepository playerRepository;
+
+ private LobbyRepository lobbyRepository;
+
+ private LobbyController lobbyController;
+
+ @Before
+ public void setUp() {
+ playerRepository = new PlayerRepository();
+ lobbyRepository = new LobbyRepository(new GameDefinitionLoader());
+ SimpMessagingTemplate template = TestUtils.createSimpMessagingTemplate();
+ lobbyController = new LobbyController(playerRepository, template);
+ }
+
+ @Test(expected = PlayerNotFoundException.class)
+ public void leave_failsWhenPlayerDoesNotExist() {
+ Principal principal = TestUtils.createPrincipal("testuser");
+ lobbyController.leave(principal);
+ }
+
+ @Test(expected = PlayerNotInLobbyException.class)
+ public void leave_failsWhenNotInLobby() {
+ playerRepository.createOrUpdate("testuser", "Test User");
+ Principal principal = TestUtils.createPrincipal("testuser");
+ lobbyController.leave(principal);
+ }
+
+ @Test
+ public void leave_succeedsWhenInALobby_asOwner() {
+ Player player = playerRepository.createOrUpdate("testuser", "Test User");
+ Lobby lobby = lobbyRepository.create("Test Game", player);
+
+ assertTrue(lobby.getPlayers().contains(player));
+ assertSame(lobby, player.getLobby());
+
+ Principal principal = TestUtils.createPrincipal("testuser");
+ lobbyController.leave(principal);
+
+ assertFalse(lobby.getPlayers().contains(player));
+ assertNull(player.getLobby());
+ }
+
+ @Test
+ public void leave_succeedsWhenInALobby_asPeasant() {
+ Player player = playerRepository.createOrUpdate("testuser", "Test User");
+ Lobby lobby = lobbyRepository.create("Test Game", player);
+ Player player2 = addPlayer(lobby, "testuser2");
+
+ assertTrue(lobby.getPlayers().contains(player2));
+ assertSame(lobby, player2.getLobby());
+
+ Principal principal = TestUtils.createPrincipal("testuser2");
+ lobbyController.leave(principal);
+
+ assertFalse(lobby.getPlayers().contains(player2));
+ assertNull(player2.getLobby());
+ }
+
+ @Test
+ public void reorderPlayers_succeedsForOwner() {
+ Player player = playerRepository.createOrUpdate("testuser", "Test User");
+ Lobby lobby = lobbyRepository.create("Test Game", player);
+
+ Player player2 = addPlayer(lobby, "testuser2");
+ Player player3 = addPlayer(lobby, "testuser3");
+ Player player4 = addPlayer(lobby, "testuser4");
+
+ List<Player> players = Arrays.asList(player, player2, player3, player4);
+ assertEquals(players, lobby.getPlayers());
+
+ ReorderPlayersAction reorderPlayersAction = new ReorderPlayersAction();
+ List<Player> reorderedPlayers = Arrays.asList(player3, player, player2, player4);
+ List<String> playerNames = reorderedPlayers.stream().map(Player::getUsername).collect(Collectors.toList());
+ reorderPlayersAction.setOrderedPlayers(playerNames);
+
+ Principal principal = TestUtils.createPrincipal("testuser");
+ lobbyController.reorderPlayers(reorderPlayersAction, principal);
+
+ assertEquals(reorderedPlayers, lobby.getPlayers());
+ }
+
+ @Test(expected = PlayerIsNotOwnerException.class)
+ public void reorderPlayers_failsForPeasant() {
+ Player player = playerRepository.createOrUpdate("testuser", "Test User");
+ Lobby lobby = lobbyRepository.create("Test Game", player);
+
+ Player player2 = addPlayer(lobby, "testuser2");
+ Player player3 = addPlayer(lobby, "testuser3");
+
+ ReorderPlayersAction reorderPlayersAction = new ReorderPlayersAction();
+ List<Player> reorderedPlayers = Arrays.asList(player3, player, player2);
+ List<String> playerNames = reorderedPlayers.stream().map(Player::getUsername).collect(Collectors.toList());
+ reorderPlayersAction.setOrderedPlayers(playerNames);
+
+ Principal principal = TestUtils.createPrincipal("testuser2");
+ lobbyController.reorderPlayers(reorderPlayersAction, principal);
+ }
+
+ @Test
+ public void updateSettings_succeedsForOwner() {
+ Player player = playerRepository.createOrUpdate("testuser", "Test User");
+ Lobby lobby = lobbyRepository.create("Test Game", player);
+
+ addPlayer(lobby, "testuser2");
+ addPlayer(lobby, "testuser3");
+ addPlayer(lobby, "testuser4");
+
+ assertEquals(new CustomizableSettings(), lobby.getSettings());
+
+ UpdateSettingsAction updateSettingsAction = new UpdateSettingsAction();
+ CustomizableSettings newSettings = new CustomizableSettings();
+ newSettings.setInitialGold(12);
+ newSettings.setDefaultTradingCost(5);
+ newSettings.setWonderSidePickMethod(WonderSidePickMethod.ALL_A);
+ newSettings.setTimeLimitInSeconds(5);
+ newSettings.setPointsPer3Gold(5);
+ newSettings.setDiscardedCardGold(4);
+ newSettings.setLostPointsPerDefeat(10);
+ newSettings.setWonPointsPerVictoryPerAge(new HashMap<>());
+ updateSettingsAction.setSettings(newSettings);
+
+ Principal principal = TestUtils.createPrincipal("testuser");
+ lobbyController.updateSettings(updateSettingsAction, principal);
+
+ assertEquals(newSettings, lobby.getSettings());
+ }
+
+ @Test(expected = PlayerIsNotOwnerException.class)
+ public void updateSettings_failsForPeasant() {
+ Player player = playerRepository.createOrUpdate("testuser", "Test User");
+ Lobby lobby = lobbyRepository.create("Test Game", player);
+
+ addPlayer(lobby, "testuser2");
+ addPlayer(lobby, "testuser3");
+
+ UpdateSettingsAction updateSettingsAction = new UpdateSettingsAction();
+ updateSettingsAction.setSettings(new CustomizableSettings());
+
+ Principal principal = TestUtils.createPrincipal("testuser2");
+ lobbyController.updateSettings(updateSettingsAction, principal);
+ }
+
+ @Test
+ public void startGame_succeedsForOwner() {
+ Player player = playerRepository.createOrUpdate("testuser", "Test User");
+ Lobby lobby = lobbyRepository.create("Test Game", player);
+
+ addPlayer(lobby, "testuser2");
+ addPlayer(lobby, "testuser3");
+ addPlayer(lobby, "testuser4");
+
+ Principal principal = TestUtils.createPrincipal("testuser");
+ lobbyController.startGame(principal);
+
+ assertSame(State.PLAYING, lobby.getState());
+ }
+
+ @Test(expected = PlayerIsNotOwnerException.class)
+ public void startGame_failsForPeasant() {
+ Player player = playerRepository.createOrUpdate("testuser", "Test User");
+ Lobby lobby = lobbyRepository.create("Test Game", player);
+
+ addPlayer(lobby, "testuser2");
+ addPlayer(lobby, "testuser3");
+
+ Principal principal = TestUtils.createPrincipal("testuser2");
+ lobbyController.startGame(principal);
+ }
+
+ private Player addPlayer(Lobby lobby, String username) {
+ Player player = playerRepository.createOrUpdate(username, username);
+ lobby.addPlayer(player);
+
+ assertTrue(lobby.getPlayers().contains(player));
+ assertSame(lobby, player.getLobby());
+ return player;
+ }
+}
bgstack15