diff options
author | joffrey-bion <joffrey.bion@gmail.com> | 2021-02-03 02:37:38 +0100 |
---|---|---|
committer | joffrey-bion <joffrey.bion@gmail.com> | 2021-02-03 23:29:16 +0100 |
commit | 7c2371766b940742f3986d7904d4c20a4127ea70 (patch) | |
tree | 294d0c80b2590b69d032a37bcc78b3ce15be012a /sw-server/src/main | |
parent | Cleanup (diff) | |
download | seven-wonders-7c2371766b940742f3986d7904d4c20a4127ea70.tar.gz seven-wonders-7c2371766b940742f3986d7904d4c20a4127ea70.tar.bz2 seven-wonders-7c2371766b940742f3986d7904d4c20a4127ea70.zip |
Add auto-game with bots only
Resolves:
https://github.com/joffrey-bion/seven-wonders/issues/82
Diffstat (limited to 'sw-server/src/main')
3 files changed, 69 insertions, 5 deletions
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/WebSecurityConfig.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/WebSecurityConfig.kt index 9557ba1a..aa080189 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/WebSecurityConfig.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/WebSecurityConfig.kt @@ -8,5 +8,7 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur class WebSecurityConfig : WebSecurityConfigurerAdapter() { // this disables default authentication settings - override fun configure(httpSecurity: HttpSecurity) = Unit + override fun configure(httpSecurity: HttpSecurity) { + http.cors().and().csrf().disable() + } } diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/AutoGameController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/AutoGameController.kt new file mode 100644 index 00000000..4acdc3be --- /dev/null +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/AutoGameController.kt @@ -0,0 +1,52 @@ +package org.luxons.sevenwonders.server.controllers + +import kotlinx.coroutines.withTimeout +import org.luxons.sevenwonders.bot.connectBot +import org.luxons.sevenwonders.bot.connectBots +import org.luxons.sevenwonders.client.SevenWondersClient +import org.luxons.sevenwonders.model.api.AutoGameAction +import org.luxons.sevenwonders.model.api.AutoGameResult +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Value +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RestController +import java.security.Principal +import kotlin.time.ExperimentalTime +import kotlin.time.minutes + +/** + * Handles actions in the game's lobby. The lobby is the place where players gather before a game. + */ +@RestController +class AutoGameController( + @Value("\${server.port}") private val serverPort: String, +) { + @OptIn(ExperimentalTime::class) + @PostMapping("/autoGame") + suspend fun autoGame(@RequestBody action: AutoGameAction, principal: Principal): AutoGameResult { + logger.info("Starting auto-game {}", action.gameName) + val client = SevenWondersClient() + val serverUrl = "ws://localhost:$serverPort" + + val lastTurn = withTimeout(5.minutes) { + val otherBotNames = List(action.nbPlayers - 1) { "JoinerBot${it + 1}" } + val owner = client.connectBot(serverUrl, "OwnerBot", action.config) + val joiners = client.connectBots(serverUrl, otherBotNames, action.config) + + owner.createGameWithBotFriendsAndAutoPlay( + gameName = action.gameName, + otherBots = joiners, + customWonders = action.customWonders, + customSettings = action.customSettings, + ) + } + + val scoreBoard = lastTurn.scoreBoard ?: error("Last turn info doesn't have scoreboard") + return AutoGameResult(scoreBoard, lastTurn.table) + } + + companion object { + private val logger = LoggerFactory.getLogger(AutoGameController::class.java) + } +} 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 aa01a23a..557a1714 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 @@ -2,7 +2,10 @@ package org.luxons.sevenwonders.server.controllers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch -import org.luxons.sevenwonders.bot.SevenWondersBot +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withTimeout +import org.luxons.sevenwonders.bot.connectBot +import org.luxons.sevenwonders.client.SevenWondersClient import org.luxons.sevenwonders.model.api.GameListEvent import org.luxons.sevenwonders.model.api.actions.AddBotAction import org.luxons.sevenwonders.model.api.actions.ReassignWondersAction @@ -22,6 +25,8 @@ import org.springframework.messaging.simp.SimpMessagingTemplate import org.springframework.stereotype.Controller import org.springframework.validation.annotation.Validated import java.security.Principal +import kotlin.time.ExperimentalTime +import kotlin.time.hours /** * Handles actions in the game's lobby. The lobby is the place where players gather before a game. @@ -144,14 +149,19 @@ class LobbyController( template.convertAndSend("/topic/games", GameListEvent.CreateOrUpdate(lobbyDto).wrap()) } + @OptIn(ExperimentalTime::class) @MessageMapping("/lobby/addBot") fun addBot(@Validated action: AddBotAction, principal: Principal) { val lobby = principal.player.ownedLobby - val bot = SevenWondersBot(action.botDisplayName) + val bot = runBlocking { + SevenWondersClient().connectBot("ws://localhost:$serverPort", action.botDisplayName, action.config) + } + logger.info("Starting bot {} in game '{}'", action.botDisplayName, lobby.name) GlobalScope.launch { - bot.play("ws://localhost:$serverPort", lobby.id) + withTimeout(6.hours) { + bot.joinAndAutoPlay(lobby.id) + } } - logger.info("Added bot {} to game '{}'", action.botDisplayName, lobby.name) } /** |