From dc62219e281641543cad6ece532f47c79fd19b3f Mon Sep 17 00:00:00 2001 From: joffrey-bion Date: Wed, 10 Feb 2021 02:02:46 +0100 Subject: Clean games when all humans have left Resolves: https://github.com/joffrey-bion/seven-wonders/issues/113 --- .../luxons/sevenwonders/server/controllers/GameController.kt | 7 ++++++- .../luxons/sevenwonders/server/controllers/LobbyController.kt | 11 ++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'sw-server/src/main/kotlin/org') 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 4f948ccf..60307962 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 @@ -1,7 +1,9 @@ package org.luxons.sevenwonders.server.controllers import org.luxons.sevenwonders.engine.Game +import org.luxons.sevenwonders.model.api.GameListEvent import org.luxons.sevenwonders.model.api.actions.PrepareMoveAction +import org.luxons.sevenwonders.model.api.wrap import org.luxons.sevenwonders.model.cards.PreparedCard import org.luxons.sevenwonders.model.hideHandsAndWaitForReadiness import org.luxons.sevenwonders.server.lobby.Player @@ -149,8 +151,11 @@ class GameController( synchronized(game) { lobby.removePlayer(player.username) logger.info("Game {}: player {} left the game", game.id, player) - if (lobby.getPlayers().isEmpty()) { + // This could cause problems if the humans are faster than bots to leave a finished game, + // but this case should be quite rare, so it does not matter much + if (lobby.getPlayers().none { it.isHuman }) { lobbyRepository.remove(lobby.id) + template.convertAndSend("/topic/games", GameListEvent.Delete(lobby.id).wrap()) logger.info("Game {}: game deleted", game.id) } } 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 732b9193..f2124009 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,7 +3,7 @@ package org.luxons.sevenwonders.server.controllers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.withTimeout +import kotlinx.coroutines.withTimeoutOrNull import org.luxons.sevenwonders.bot.connectBot import org.luxons.sevenwonders.client.SevenWondersClient import org.luxons.sevenwonders.model.api.GameListEvent @@ -26,7 +26,7 @@ import org.springframework.stereotype.Controller import org.springframework.validation.annotation.Validated import java.security.Principal import kotlin.time.ExperimentalTime -import kotlin.time.hours +import kotlin.time.milliseconds /** * Handles actions in the game's lobby. The lobby is the place where players gather before a game. @@ -158,9 +158,14 @@ class LobbyController( } logger.info("Starting bot {} in game '{}'", action.botDisplayName, lobby.name) GlobalScope.launch { - withTimeout(6.hours) { + val result = withTimeoutOrNull(action.globalBotTimeoutMillis) { bot.joinAndAutoPlay(lobby.id) } + if (result == null) { + val timeoutDuration = action.globalBotTimeoutMillis.milliseconds + logger.error("Bot {} timed out after {}", action.botDisplayName, timeoutDuration) + bot.disconnect() + } } } -- cgit