From 10366ca0240bc6bb5d66cd06a0c18373c2c8e875 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Tue, 9 Mar 2021 00:06:23 +0100 Subject: Unify game events --- .../sevenwonders/client/SevenWondersClient.kt | 93 +++++++--------------- 1 file changed, 30 insertions(+), 63 deletions(-) (limited to 'sw-client') 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 d14b07b7..f11439ef 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 @@ -4,8 +4,10 @@ import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.async import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.flow.* -import kotlinx.serialization.builtins.serializer +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.filterIsInstance +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.map import org.hildan.krossbow.stomp.StompClient import org.hildan.krossbow.stomp.config.HeartBeat import org.hildan.krossbow.stomp.config.HeartBeatTolerance @@ -15,13 +17,11 @@ import org.hildan.krossbow.stomp.conversions.kxserialization.subscribe import org.hildan.krossbow.stomp.conversions.kxserialization.withJsonConversions import org.hildan.krossbow.stomp.sendEmptyMsg import org.luxons.sevenwonders.model.PlayerMove -import org.luxons.sevenwonders.model.PlayerTurnInfo import org.luxons.sevenwonders.model.Settings import org.luxons.sevenwonders.model.api.* import org.luxons.sevenwonders.model.api.actions.* import org.luxons.sevenwonders.model.api.errors.ErrorDTO -import org.luxons.sevenwonders.model.api.events.GameEvent -import org.luxons.sevenwonders.model.api.events.GameEventWrapper +import org.luxons.sevenwonders.model.api.events.* import org.luxons.sevenwonders.model.wonders.AssignedWonder class SevenWondersClient { @@ -43,27 +43,20 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat suspend fun watchErrors(): Flow = stompSession.subscribe("/user/queue/errors", ErrorDTO.serializer()) - suspend fun chooseName(displayName: String, icon: Icon? = null, isHuman: Boolean = true): ConnectedPlayer { - return doAndWaitForEvent( - send = { - stompSession.convertAndSend( - destination = "/app/chooseName", - body = ChooseNameAction(displayName, icon, isHuman), - serializer = ChooseNameAction.serializer(), - ) - }, - subscribe = { - stompSession.subscribe( - destination = "/user/queue/nameChoice", - deserializer = ConnectedPlayer.serializer(), - ) - } - ) - } - suspend fun watchGames(): Flow = stompSession.subscribe("/topic/games", GameListEventWrapper.serializer()).map { it.event } + suspend fun watchGameEvents(): Flow = + stompSession.subscribe("/user/queue/game/events", GameEventWrapper.serializer()).map { it.event } + + suspend fun chooseName(displayName: String, icon: Icon? = null, isHuman: Boolean = true) { + stompSession.convertAndSend( + destination = "/app/chooseName", + body = ChooseNameAction(displayName, icon, isHuman), + serializer = ChooseNameAction.serializer(), + ) + } + suspend fun createGame(gameName: String) { stompSession.convertAndSend("/app/lobby/create", CreateGameAction(gameName), CreateGameAction.serializer()) } @@ -72,9 +65,6 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat stompSession.convertAndSend("/app/lobby/join", JoinGameAction(gameId), JoinGameAction.serializer()) } - suspend fun watchLobbyJoined(): Flow = - stompSession.subscribe("/user/queue/lobby/joined", LobbyDTO.serializer()) - suspend fun leaveLobby() { stompSession.sendEmptyMsg("/app/lobby/leave") } @@ -83,8 +73,6 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat stompSession.sendEmptyMsg("/app/lobby/disband") } - suspend fun watchLobbyLeft(): Flow = stompSession.subscribe("/user/queue/lobby/left", Long.serializer()) - suspend fun addBot(displayName: String) { stompSession.convertAndSend("/app/lobby/addBot", AddBotAction(displayName), AddBotAction.serializer()) } @@ -113,29 +101,10 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat ) } - suspend fun watchLobbyUpdates(): Flow = - stompSession.subscribe("/user/queue/lobby/updated", LobbyDTO.serializer()) - - suspend fun watchGameStarted(): Flow = - stompSession.subscribe("/user/queue/lobby/started", PlayerTurnInfo.serializer()) - suspend fun startGame() { stompSession.sendEmptyMsg("/app/lobby/startGame") } - @OptIn(ExperimentalCoroutinesApi::class) - suspend fun watchGameEvents(gameId: Long): Flow { - val private = watchPublicGameEvents() - val public = watchPrivateGameEvents(gameId) - return merge(private, public) - } - - private suspend fun watchPrivateGameEvents(gameId: Long) = - stompSession.subscribe("/topic/game/$gameId/events", GameEventWrapper.serializer()).map { it.event } - - suspend fun watchPublicGameEvents() = - stompSession.subscribe("/user/queue/game/events", GameEventWrapper.serializer()).map { it.event } - suspend fun sayReady() { stompSession.sendEmptyMsg("/app/game/sayReady") } @@ -157,6 +126,13 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat } } +suspend fun SevenWondersSession.chooseNameAndAwait(displayName: String, icon: Icon? = null, isHuman: Boolean = true): ConnectedPlayer { + return doAndWaitForEvent( + send = { chooseName(displayName, icon, isHuman) }, + subscribe = { watchNameChoice() } + ) +} + suspend fun SevenWondersSession.createGameAndAwaitLobby(gameName: String): LobbyDTO = doAndWaitForEvent( send = { createGame(gameName) }, subscribe = { watchLobbyJoined() }, @@ -167,21 +143,6 @@ suspend fun SevenWondersSession.joinGameAndAwaitLobby(gameId: Long): LobbyDTO = subscribe = { watchLobbyJoined() }, ) -suspend fun SevenWondersSession.startGameAndAwaitFirstTurn(): PlayerTurnInfo = doAndWaitForEvent( - send = { startGame() }, - subscribe = { watchGameStarted() }, -) - -suspend fun SevenWondersSession.joinGameAndAwaitFirstTurn(gameId: Long): PlayerTurnInfo = doAndWaitForEvent( - send = { joinGame(gameId) }, - subscribe = { watchGameStarted() }, -) - -suspend fun SevenWondersSession.leaveGameAndAwaitEnd() = doAndWaitForEvent( - send = { leaveGame() }, - subscribe = { watchLobbyLeft() }, -) - @OptIn(ExperimentalCoroutinesApi::class) private suspend fun doAndWaitForEvent(send: suspend () -> Unit, subscribe: suspend () -> Flow): T = coroutineScope { @@ -192,4 +153,10 @@ private suspend fun doAndWaitForEvent(send: suspend () -> Unit, subscribe: s } suspend fun SevenWondersSession.watchTurns() = - watchPublicGameEvents().filterIsInstance().map { it.turnInfo } + watchGameEvents().filterIsInstance().map { it.turnInfo } + +suspend fun SevenWondersSession.watchLobbyJoined(): Flow = + watchGameEvents().filterIsInstance().map { it.lobby } + +suspend fun SevenWondersSession.watchNameChoice(): Flow = + watchGameEvents().filterIsInstance().map { it.player } -- cgit