summaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
Diffstat (limited to 'backend')
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/controllers/GameBrowserController.java97
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/controllers/HomeController.java37
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java78
3 files changed, 136 insertions, 76 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
new file mode 100644
index 00000000..8d00994c
--- /dev/null
+++ b/backend/src/main/java/org/luxons/sevenwonders/controllers/GameBrowserController.java
@@ -0,0 +1,97 @@
+package org.luxons.sevenwonders.controllers;
+
+import java.security.Principal;
+import java.util.Collection;
+import java.util.Collections;
+
+import org.luxons.sevenwonders.actions.CreateGameAction;
+import org.luxons.sevenwonders.actions.JoinGameAction;
+import org.luxons.sevenwonders.errors.ApiMisuseException;
+import org.luxons.sevenwonders.lobby.Lobby;
+import org.luxons.sevenwonders.lobby.Player;
+import org.luxons.sevenwonders.repositories.LobbyRepository;
+import org.luxons.sevenwonders.repositories.PlayerRepository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.messaging.handler.annotation.MessageMapping;
+import org.springframework.messaging.simp.SimpMessagingTemplate;
+import org.springframework.messaging.simp.annotation.SendToUser;
+import org.springframework.messaging.simp.annotation.SubscribeMapping;
+import org.springframework.stereotype.Controller;
+import org.springframework.validation.annotation.Validated;
+
+@Controller
+public class GameBrowserController {
+
+ private static final Logger logger = LoggerFactory.getLogger(GameBrowserController.class);
+
+ private final LobbyController lobbyController;
+
+ private final LobbyRepository lobbyRepository;
+
+ private final PlayerRepository playerRepository;
+
+ private final SimpMessagingTemplate template;
+
+ @Autowired
+ public GameBrowserController(LobbyController lobbyController, LobbyRepository lobbyRepository, PlayerRepository playerRepository,
+ SimpMessagingTemplate template) {
+ this.lobbyController = lobbyController;
+ this.lobbyRepository = lobbyRepository;
+ this.playerRepository = playerRepository;
+ this.template = template;
+ }
+
+ @SubscribeMapping("/games") // prefix /topic not shown
+ public Collection<Lobby> listGames(Principal principal) {
+ logger.info("Player '{}' subscribed to /topic/games", principal.getName());
+ return lobbyRepository.list();
+ }
+
+ @MessageMapping("/lobby/create")
+ @SendToUser("/queue/lobby/joined")
+ public Lobby createGame(@Validated CreateGameAction action, Principal principal) {
+ checkThatUserIsNotInAGame(principal, "cannot create another game");
+
+ 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());
+
+ // notify everyone that a new game exists
+ template.convertAndSend("/topic/games", Collections.singletonList(lobby));
+ return lobby;
+ }
+
+ @MessageMapping("/lobby/join")
+ @SendToUser("/queue/lobby/joined")
+ public Lobby joinGame(@Validated JoinGameAction action, Principal principal) {
+ checkThatUserIsNotInAGame(principal, "cannot join another game");
+
+ 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());
+ lobbyController.sendLobbyUpdateToPlayers(lobby);
+ return lobby;
+ }
+
+ private void checkThatUserIsNotInAGame(Principal principal, String impossibleActionDescription) {
+ Lobby lobby = playerRepository.find(principal.getName()).getLobby();
+ if (lobby != null) {
+ throw new UserAlreadyInGameException(lobby.getName(), impossibleActionDescription);
+ }
+ }
+
+ private static class UserAlreadyInGameException extends ApiMisuseException {
+ UserAlreadyInGameException(String gameName, String impossibleActionDescription) {
+ super("Client already in game '" + gameName + "', " + impossibleActionDescription);
+ }
+ }
+}
diff --git a/backend/src/main/java/org/luxons/sevenwonders/controllers/HomeController.java b/backend/src/main/java/org/luxons/sevenwonders/controllers/HomeController.java
new file mode 100644
index 00000000..4ac236d3
--- /dev/null
+++ b/backend/src/main/java/org/luxons/sevenwonders/controllers/HomeController.java
@@ -0,0 +1,37 @@
+package org.luxons.sevenwonders.controllers;
+
+import java.security.Principal;
+
+import org.luxons.sevenwonders.actions.ChooseNameAction;
+import org.luxons.sevenwonders.lobby.Player;
+import org.luxons.sevenwonders.repositories.PlayerRepository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.messaging.handler.annotation.MessageMapping;
+import org.springframework.messaging.simp.annotation.SendToUser;
+import org.springframework.stereotype.Controller;
+import org.springframework.validation.annotation.Validated;
+
+@Controller
+public class HomeController {
+
+ private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
+
+ private final PlayerRepository playerRepository;
+
+ @Autowired
+ public HomeController(PlayerRepository playerRepository) {
+ this.playerRepository = playerRepository;
+ }
+
+ @MessageMapping("/chooseName")
+ @SendToUser("/queue/nameChoice")
+ public Player chooseName(@Validated ChooseNameAction action, Principal principal) {
+ String username = principal.getName();
+ Player player = playerRepository.createOrUpdate(username, action.getPlayerName());
+
+ logger.info("Player '{}' chose the name '{}'", username, player.getDisplayName());
+ return player;
+ }
+}
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 b9381cf8..839e27b9 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java
@@ -1,27 +1,19 @@
package org.luxons.sevenwonders.controllers;
import java.security.Principal;
-import java.util.Collection;
import java.util.Collections;
-import org.luxons.sevenwonders.actions.ChooseNameAction;
-import org.luxons.sevenwonders.actions.CreateGameAction;
-import org.luxons.sevenwonders.actions.JoinGameAction;
import org.luxons.sevenwonders.actions.ReorderPlayersAction;
import org.luxons.sevenwonders.actions.UpdateSettingsAction;
import org.luxons.sevenwonders.errors.ApiMisuseException;
import org.luxons.sevenwonders.game.Game;
import org.luxons.sevenwonders.lobby.Lobby;
-import org.luxons.sevenwonders.lobby.Player;
-import org.luxons.sevenwonders.repositories.LobbyRepository;
import org.luxons.sevenwonders.repositories.PlayerRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
-import org.springframework.messaging.simp.annotation.SendToUser;
-import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
@@ -30,76 +22,16 @@ public class LobbyController {
private static final Logger logger = LoggerFactory.getLogger(LobbyController.class);
- private final LobbyRepository lobbyRepository;
-
private final PlayerRepository playerRepository;
private final SimpMessagingTemplate template;
@Autowired
- public LobbyController(LobbyRepository lobbyRepository, PlayerRepository playerRepository,
- SimpMessagingTemplate template) {
- this.lobbyRepository = lobbyRepository;
+ public LobbyController(PlayerRepository playerRepository, SimpMessagingTemplate template) {
this.playerRepository = playerRepository;
this.template = template;
}
- @MessageMapping("/chooseName")
- @SendToUser("/queue/nameChoice")
- public Player chooseName(@Validated ChooseNameAction action, Principal principal) {
- String username = principal.getName();
- Player player = playerRepository.createOrUpdate(username, action.getPlayerName());
-
- logger.info("Player '{}' chose the name '{}'", username, player.getDisplayName());
- return player;
- }
-
- @SubscribeMapping("/games") // prefix /topic not shown
- public Collection<Lobby> listGames(Principal principal) {
- logger.info("Player '{}' subscribed to /topic/games", principal.getName());
- return lobbyRepository.list();
- }
-
- @MessageMapping("/lobby/create")
- @SendToUser("/queue/lobby/joined")
- public Lobby createGame(@Validated CreateGameAction action, Principal principal) {
- checkThatUserIsNotInAGame(principal, "cannot create another game");
-
- 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());
-
- // notify everyone that a new game exists
- template.convertAndSend("/topic/games", Collections.singletonList(lobby));
- return lobby;
- }
-
- @MessageMapping("/lobby/join")
- @SendToUser("/queue/lobby/joined")
- public Lobby joinGame(@Validated JoinGameAction action, Principal principal) {
- checkThatUserIsNotInAGame(principal, "cannot join another game");
-
- 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());
- sendLobbyUpdateToPlayers(lobby);
- return lobby;
- }
-
- private void checkThatUserIsNotInAGame(Principal principal, String impossibleActionDescription) {
- Lobby lobby = playerRepository.find(principal.getName()).getLobby();
- if (lobby != null) {
- throw new UserAlreadyInGameException(lobby.getName(), impossibleActionDescription);
- }
- }
-
@MessageMapping("/lobby/reorderPlayers")
public void reorderPlayers(@Validated ReorderPlayersAction action, Principal principal) {
Lobby lobby = getLobby(principal);
@@ -118,7 +50,7 @@ public class LobbyController {
sendLobbyUpdateToPlayers(lobby);
}
- private void sendLobbyUpdateToPlayers(Lobby lobby) {
+ void sendLobbyUpdateToPlayers(Lobby lobby) {
template.convertAndSend("/topic/lobby/" + lobby.getId() + "/updated", lobby);
template.convertAndSend("/topic/games", Collections.singletonList(lobby));
}
@@ -159,10 +91,4 @@ public class LobbyController {
super("User " + username + " does not own the lobby he's in");
}
}
-
- private static class UserAlreadyInGameException extends ApiMisuseException {
- UserAlreadyInGameException(String gameName, String impossibleActionDescription) {
- super("Client already in game '" + gameName + "', " + impossibleActionDescription);
- }
- }
}
bgstack15