summaryrefslogtreecommitdiff
path: root/sw-server
diff options
context:
space:
mode:
authorjoffrey-bion <joffrey.bion@gmail.com>2021-02-10 02:02:46 +0100
committerjoffrey-bion <joffrey.bion@gmail.com>2021-02-10 02:31:38 +0100
commitdc62219e281641543cad6ece532f47c79fd19b3f (patch)
tree306d42d39d3aafe7643892bfab36f6072223db33 /sw-server
parentImprove discord notifications (diff)
downloadseven-wonders-dc62219e281641543cad6ece532f47c79fd19b3f.tar.gz
seven-wonders-dc62219e281641543cad6ece532f47c79fd19b3f.tar.bz2
seven-wonders-dc62219e281641543cad6ece532f47c79fd19b3f.zip
Clean games when all humans have left
Resolves: https://github.com/joffrey-bion/seven-wonders/issues/113
Diffstat (limited to 'sw-server')
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt7
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt11
2 files changed, 14 insertions, 4 deletions
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()
+ }
}
}
bgstack15