From 07295f3e96a16efc517e5a5e6ae0af4e3d5c5035 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Fri, 15 May 2020 03:05:09 +0200 Subject: Add dumb bots feature --- sw-server/build.gradle.kts | 3 ++- .../sevenwonders/server/controllers/LobbyController.kt | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'sw-server') diff --git a/sw-server/build.gradle.kts b/sw-server/build.gradle.kts index 7d1ee0ae..66e70dbb 100644 --- a/sw-server/build.gradle.kts +++ b/sw-server/build.gradle.kts @@ -10,8 +10,10 @@ apply(plugin = "io.spring.dependency-management") dependencies { implementation(project(":sw-common-model")) implementation(project(":sw-engine")) + implementation(project(":sw-bot")) implementation(kotlin("stdlib-jdk8")) implementation(kotlin("reflect")) // required by Spring 5 + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.6") implementation("org.springframework.boot:spring-boot-starter-websocket") implementation("org.springframework.boot:spring-boot-starter-security") @@ -32,7 +34,6 @@ dependencies { testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("org.hildan.jackstomp:jackstomp:2.0.0") testImplementation("com.fasterxml.jackson.module:jackson-module-kotlin") - testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5") } // packages the frontend app within the jar 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 403a4696..cdb636fb 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 @@ -1,6 +1,8 @@ package org.luxons.sevenwonders.server.controllers import org.hildan.livedoc.core.annotations.Api +import org.luxons.sevenwonders.bot.SevenWondersBot +import org.luxons.sevenwonders.model.api.actions.AddBotAction import org.luxons.sevenwonders.model.api.actions.ReorderPlayersAction import org.luxons.sevenwonders.model.api.actions.UpdateSettingsAction import org.luxons.sevenwonders.model.hideHandsAndWaitForReadiness @@ -16,6 +18,8 @@ import org.springframework.messaging.simp.SimpMessagingTemplate import org.springframework.stereotype.Controller import org.springframework.validation.annotation.Validated import java.security.Principal +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch /** * Handles actions in the game's lobby. The lobby is the place where players gather before a game. @@ -78,6 +82,18 @@ class LobbyController @Autowired constructor( sendLobbyUpdateToPlayers(lobby) } + @MessageMapping("/lobby/addBot") + fun addBot(@Validated action: AddBotAction, principal: Principal) { + val lobby = principal.player.ownedLobby + val bot = SevenWondersBot(action.botDisplayName) + GlobalScope.launch { + bot.play("localhost:8000", lobby.id) + } + + logger.info("Added bot {} to game '{}'", action.botDisplayName, lobby.name) + sendLobbyUpdateToPlayers(lobby) + } + internal fun sendLobbyUpdateToPlayers(lobby: Lobby) { lobby.getPlayers().forEach { template.convertAndSendToUser(it.username, "/queue/lobby/updated", lobby.toDTO()) -- cgit