From e48dc809a95a3ac813c4da8798c9e57cbb6a32b6 Mon Sep 17 00:00:00 2001 From: joffrey-bion Date: Wed, 17 Feb 2021 02:34:18 +0100 Subject: Use automatic sayReady call to synchronize STOMP subscriptions before first turn We currently miss the preparedCard event from bots if they are too fast, because the server sends the first turn to everyone without knowing if all clients have properly subscribed to events. We now use sayReady to indicate to the server that all subscriptions have been made. --- .../kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt | 5 ++++- .../sevenwonders/server/controllers/GameController.kt | 9 +++------ .../sevenwonders/server/controllers/LobbyController.kt | 14 +++++--------- .../luxons/sevenwonders/ui/redux/sagas/RouteBasedSagas.kt | 1 + 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt b/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt index 6a4a5bf6..a0688f20 100644 --- a/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt +++ b/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt @@ -82,7 +82,10 @@ class SevenWondersBot( session.watchTurns().filter { it.action == Action.WATCH_SCORE }.first() } session.watchTurns() - .onStart { emit(currentTurn) } + .onStart { + session.sayReady() + emit(currentTurn) + } .takeWhile { it.action != Action.WATCH_SCORE } .catch { e -> logger.error("BOT $player: error in turnInfo flow", e) } .collect { turn -> diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt index cff3aea0..e4c1b39a 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt @@ -40,18 +40,15 @@ class GameController( fun ready(principal: Principal) { val player = principal.player val lobby = player.lobby - if (!lobby.settings.askForReadiness) { - logger.warn("Game {}: player {} is saying ready but readiness concept is disabled", lobby.id, player) - return - } - val game = player.game // This lock doesn't have a clear rationale, but it's cleaner to check the readiness state of everyone within a // lock together with the code that updates the readiness status, to avoid interleaving surprises. synchronized(game) { player.isReady = true - sendPlayerReady(game.id, player) + if (lobby.settings.askForReadiness) { + sendPlayerReady(game.id, player) + } logger.info("Game {}: player {} is ready for the next turn", game.id, player) val players = lobby.getPlayers() 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 70ac50e7..6a696d25 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 @@ -184,12 +184,10 @@ class LobbyController( meterRegistry.counter("games.started").increment() logger.info("Game {} ('{}') successfully started", game.id, lobby.name) - val currentTurnInfo = game.getCurrentTurnInfo().let { - if (lobby.settings.askForReadiness) it.hideHandsAndWaitForReadiness() else it - } - // even if we don't care about ready state for business logic, the UI may use it nonetheless - lobby.initializePlayersReadyState() + // we wait for readiness here to ensure all subscriptions are correctly setup on client side + val currentTurnInfo = game.getCurrentTurnInfo().hideHandsAndWaitForReadiness() + lobby.resetPlayersReadyState() currentTurnInfo.forEach { val player = lobby.getPlayers()[it.playerIndex] @@ -198,10 +196,8 @@ class LobbyController( template.convertAndSend("/topic/games", GameListEvent.CreateOrUpdate(lobby.toDTO()).wrap()) } - private fun Lobby.initializePlayersReadyState() { - val players = getPlayers() - val initialReadyState = !settings.askForReadiness - players.forEach { it.isReady = initialReadyState } + private fun Lobby.resetPlayersReadyState() { + getPlayers().forEach { it.isReady = false } } companion object { diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/RouteBasedSagas.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/RouteBasedSagas.kt index 1970bc08..06b33e13 100644 --- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/RouteBasedSagas.kt +++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/RouteBasedSagas.kt @@ -53,6 +53,7 @@ suspend fun SwSagaContext.gameSaga(session: SevenWondersSession) { session.watchPreparedCards(game.id).map { PreparedCardEvent(it) }.dispatchAllIn(this) session.watchOwnMoves().map { PreparedMoveEvent(it) }.dispatchAllIn(this) session.watchTurns().map { TurnInfoEvent(it) }.dispatchAllIn(this) + session.sayReady() } console.log("End of game saga") } -- cgit