From 7c2371766b940742f3986d7904d4c20a4127ea70 Mon Sep 17 00:00:00 2001 From: joffrey-bion Date: Wed, 3 Feb 2021 02:37:38 +0100 Subject: Add auto-game with bots only Resolves: https://github.com/joffrey-bion/seven-wonders/issues/82 --- sw-client/build.gradle.kts | 1 + .../sevenwonders/client/SevenWondersClient.kt | 30 +++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) (limited to 'sw-client') diff --git a/sw-client/build.gradle.kts b/sw-client/build.gradle.kts index 421b58dd..68953bc4 100644 --- a/sw-client/build.gradle.kts +++ b/sw-client/build.gradle.kts @@ -13,6 +13,7 @@ kotlin { api(project(":sw-common-model")) api("org.hildan.krossbow:krossbow-stomp-kxserialization:1.1.5") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2") } } val commonTest by getting { diff --git a/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt b/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt index 68026888..fc097d86 100644 --- a/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt +++ b/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt @@ -1,5 +1,7 @@ package org.luxons.sevenwonders.client +import kotlinx.coroutines.async +import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map @@ -148,8 +150,30 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat } } -suspend fun SevenWondersSession.joinGameAndWaitLobby(gameId: Long): LobbyDTO { - val joinedLobbies = watchLobbyJoined() +suspend fun SevenWondersSession.createGameAndWaitLobby(gameName: String): LobbyDTO = coroutineScope { + val lobbies = watchLobbyJoined() + val joinedLobby = async { lobbies.first() } + createGame(gameName) + joinedLobby.await() +} + +suspend fun SevenWondersSession.joinGameAndWaitLobby(gameId: Long): LobbyDTO = coroutineScope { + val lobbies = watchLobbyJoined() + val joinedLobby = async { lobbies.first() } + joinGame(gameId) + joinedLobby.await() +} + +suspend fun SevenWondersSession.startGameAndAwaitFirstTurn(): PlayerTurnInfo = coroutineScope { + val gameStartedEvents = watchGameStarted() + val deferredFirstTurn = async { gameStartedEvents.first() } + startGame() + deferredFirstTurn.await() +} + +suspend fun SevenWondersSession.joinGameAndWaitFirstTurn(gameId: Long): PlayerTurnInfo = coroutineScope { + val gameStartedEvents = watchGameStarted() + val deferredFirstTurn = async { gameStartedEvents.first() } joinGame(gameId) - return joinedLobbies.first() + deferredFirstTurn.await() } -- cgit