summaryrefslogtreecommitdiff
path: root/sw-server/src/main/kotlin/org/luxons
diff options
context:
space:
mode:
authorjoffrey-bion <joffrey.bion@gmail.com>2020-12-10 23:06:00 +0100
committerjoffrey-bion <joffrey.bion@gmail.com>2020-12-11 02:16:49 +0100
commiteef32bd9307a3a9f1ee3b532db2fb1f7cf37927a (patch)
treef139487db3866a687e699a01f3ed4baa5e44faf2 /sw-server/src/main/kotlin/org/luxons
parentDecouple some sagas from routes (diff)
downloadseven-wonders-eef32bd9307a3a9f1ee3b532db2fb1f7cf37927a.tar.gz
seven-wonders-eef32bd9307a3a9f1ee3b532db2fb1f7cf37927a.tar.bz2
seven-wonders-eef32bd9307a3a9f1ee3b532db2fb1f7cf37927a.zip
Allow owner to leave/disband the game
Resolves: https://github.com/joffrey-bion/seven-wonders/issues/51
Diffstat (limited to 'sw-server/src/main/kotlin/org/luxons')
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt11
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt41
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt4
3 files changed, 41 insertions, 15 deletions
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt
index 587afc10..d6573f8f 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt
@@ -1,8 +1,11 @@
package org.luxons.sevenwonders.server.controllers
+import org.luxons.sevenwonders.model.api.GameListEvent
+import org.luxons.sevenwonders.model.api.GameListEventWrapper
import org.luxons.sevenwonders.model.api.LobbyDTO
import org.luxons.sevenwonders.model.api.actions.CreateGameAction
import org.luxons.sevenwonders.model.api.actions.JoinGameAction
+import org.luxons.sevenwonders.model.api.wrap
import org.luxons.sevenwonders.server.ApiMisuseException
import org.luxons.sevenwonders.server.api.toDTO
import org.luxons.sevenwonders.server.lobby.Lobby
@@ -36,9 +39,9 @@ class GameBrowserController(
* @return the current list of [Lobby]s
*/
@SubscribeMapping("/games") // prefix /topic not shown
- fun listGames(principal: Principal): List<LobbyDTO> {
+ fun listGames(principal: Principal): GameListEventWrapper {
logger.info("Player '{}' subscribed to /topic/games", principal.name)
- return lobbyRepository.list().map { it.toDTO() }
+ return GameListEvent.ReplaceList(lobbyRepository.list().map { it.toDTO() }).wrap()
}
/**
@@ -61,9 +64,7 @@ class GameBrowserController(
// notify everyone that a new game exists
val lobbyDto = lobby.toDTO()
- // we need to pass a non-generic type (array is fine) so that Spring doesn't break when trying to find a
- // serializer from Kotlinx Serialization
- template.convertAndSend("/topic/games", listOf(lobbyDto).toTypedArray())
+ template.convertAndSend("/topic/games", GameListEvent.CreateOrUpdate(lobbyDto).wrap())
return lobbyDto
}
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt
index 7757c9d9..6cc404e9 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt
@@ -3,10 +3,12 @@ package org.luxons.sevenwonders.server.controllers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.luxons.sevenwonders.bot.SevenWondersBot
+import org.luxons.sevenwonders.model.api.GameListEvent
import org.luxons.sevenwonders.model.api.actions.AddBotAction
import org.luxons.sevenwonders.model.api.actions.ReassignWondersAction
import org.luxons.sevenwonders.model.api.actions.ReorderPlayersAction
import org.luxons.sevenwonders.model.api.actions.UpdateSettingsAction
+import org.luxons.sevenwonders.model.api.wrap
import org.luxons.sevenwonders.model.hideHandsAndWaitForReadiness
import org.luxons.sevenwonders.server.api.toDTO
import org.luxons.sevenwonders.server.lobby.Lobby
@@ -44,12 +46,38 @@ class LobbyController(
val player = principal.player
val lobby = player.lobby
lobby.removePlayer(principal.name)
+ logger.info("Player {} left game '{}'", player, lobby.name)
+ template.convertAndSendToUser(player.username, "/queue/lobby/left", lobby.id)
+
if (lobby.getPlayers().isEmpty()) {
- lobbyRepository.remove(lobby.id)
+ deleteLobby(lobby)
+ } else {
+ sendLobbyUpdateToPlayers(lobby)
}
+ }
- logger.info("Player {} left game '{}'", player, lobby.name)
- sendLobbyUpdateToPlayers(lobby)
+ /**
+ * Disbands the current group, making everyone leave the lobby.
+ *
+ * @param principal the connected user's information
+ */
+ @MessageMapping("/lobby/disband")
+ fun disband(principal: Principal) {
+ val player = principal.player
+ val lobby = player.ownedLobby
+
+ lobby.getPlayers().forEach {
+ it.leave()
+ template.convertAndSendToUser(it.username, "/queue/lobby/left", lobby.id)
+ }
+ logger.info("Player {} disbanded game '{}'", player, lobby.name)
+ deleteLobby(lobby)
+ }
+
+ private fun deleteLobby(lobby: Lobby) {
+ lobbyRepository.remove(lobby.id)
+ template.convertAndSend("/topic/games", GameListEvent.Delete(lobby.id).wrap())
+ logger.info("Game '{}' removed", lobby.name)
}
/**
@@ -110,12 +138,11 @@ class LobbyController(
}
internal fun sendLobbyUpdateToPlayers(lobby: Lobby) {
+ val lobbyDto = lobby.toDTO()
lobby.getPlayers().forEach {
- template.convertAndSendToUser(it.username, "/queue/lobby/updated", lobby.toDTO())
+ template.convertAndSendToUser(it.username, "/queue/lobby/updated", lobbyDto)
}
- // we need to pass a non-generic type (array is fine) so that Spring doesn't break when trying to find a
- // serializer from Kotlinx Serialization
- template.convertAndSend("/topic/games", listOf(lobby.toDTO()).toTypedArray())
+ template.convertAndSend("/topic/games", GameListEvent.CreateOrUpdate(lobbyDto).wrap())
}
/**
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt
index 6ebe39c1..4176c485 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt
@@ -101,7 +101,7 @@ class Lobby(
@Synchronized
fun removePlayer(username: String): Player {
- val playerIndex = find(username)
+ val playerIndex = players.indexOfFirst { it.username == username }
if (playerIndex < 0) {
throw UnknownPlayerException(username)
}
@@ -115,8 +115,6 @@ class Lobby(
return player
}
- private fun find(username: String): Int = players.indexOfFirst { it.username == username }
-
fun setEndOfGame() {
state = State.FINISHED
}
bgstack15