summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoffrey-bion <joffrey.bion@gmail.com>2020-12-12 02:41:33 +0100
committerjoffrey-bion <joffrey.bion@gmail.com>2020-12-12 03:04:22 +0100
commitb4cb5e9ed50d1c94b78cd29485f37c5cd184bc9e (patch)
tree938ce933ff427a66fcf2ad0c1def93b554148596
parentAdd checks for race conditions (diff)
downloadseven-wonders-b4cb5e9ed50d1c94b78cd29485f37c5cd184bc9e.tar.gz
seven-wonders-b4cb5e9ed50d1c94b78cd29485f37c5cd184bc9e.tar.bz2
seven-wonders-b4cb5e9ed50d1c94b78cd29485f37c5cd184bc9e.zip
Delete game at the end of a game
-rw-r--r--sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt5
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt9
2 files changed, 12 insertions, 2 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 01106d21..87ba7983 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
@@ -44,7 +44,10 @@ class SevenWondersBot(
session.watchTurns()
.onStart { emit(firstTurn) }
.takeWhile { it.action != Action.WATCH_SCORE }
- .onCompletion { session.disconnect() }
+ .onCompletion {
+ session.leaveGame()
+ session.disconnect()
+ }
.collect { turn ->
botDelay()
session.playTurn(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 94ebceab..8bd3eb2b 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
@@ -5,6 +5,7 @@ import org.luxons.sevenwonders.model.api.actions.PrepareMoveAction
import org.luxons.sevenwonders.model.cards.PreparedCard
import org.luxons.sevenwonders.model.hideHandsAndWaitForReadiness
import org.luxons.sevenwonders.server.lobby.Player
+import org.luxons.sevenwonders.server.repositories.LobbyRepository
import org.luxons.sevenwonders.server.repositories.PlayerRepository
import org.slf4j.LoggerFactory
import org.springframework.messaging.handler.annotation.MessageMapping
@@ -19,6 +20,7 @@ import java.security.Principal
class GameController(
private val template: SimpMessagingTemplate,
private val playerRepository: PlayerRepository,
+ private val lobbyRepository: LobbyRepository,
) {
private val Principal.player
get() = playerRepository.find(name)
@@ -116,8 +118,13 @@ class GameController(
fun leave(principal: Principal) {
val player = principal.player
val game = player.game
- player.leave()
+ val lobby = player.lobby
+ lobby.removePlayer(player.username)
logger.info("Game {}: player {} left the game", game.id, principal.name)
+ if (lobby.getPlayers().isEmpty()) {
+ lobbyRepository.remove(lobby.id)
+ logger.info("Game {}: game deleted", game.id, principal.name)
+ }
}
companion object {
bgstack15