From e2957be6c0f2dea09d0633cd7ee7549311b9b923 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Tue, 23 Feb 2021 00:01:00 +0100 Subject: Funnel game events into a single client subscription --- .../sevenwonders/client/SevenWondersClient.kt | 34 +++++++++++++--------- 1 file changed, 20 insertions(+), 14 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 50b9b70c..d14b07b7 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,9 +1,10 @@ package org.luxons.sevenwonders.client -import kotlinx.coroutines.* -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.first -import kotlinx.coroutines.flow.map +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 org.hildan.krossbow.stomp.StompClient import org.hildan.krossbow.stomp.config.HeartBeat @@ -19,7 +20,8 @@ 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.cards.PreparedCard +import org.luxons.sevenwonders.model.api.events.GameEvent +import org.luxons.sevenwonders.model.api.events.GameEventWrapper import org.luxons.sevenwonders.model.wonders.AssignedWonder class SevenWondersClient { @@ -121,22 +123,23 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat stompSession.sendEmptyMsg("/app/lobby/startGame") } - suspend fun watchPlayerReady(gameId: Long): Flow = - stompSession.subscribe("/topic/game/$gameId/playerReady", String.serializer()) + @OptIn(ExperimentalCoroutinesApi::class) + suspend fun watchGameEvents(gameId: Long): Flow { + val private = watchPublicGameEvents() + val public = watchPrivateGameEvents(gameId) + return merge(private, public) + } - suspend fun watchPreparedCards(gameId: Long): Flow = - stompSession.subscribe("/topic/game/$gameId/prepared", PreparedCard.serializer()) + private suspend fun watchPrivateGameEvents(gameId: Long) = + stompSession.subscribe("/topic/game/$gameId/events", GameEventWrapper.serializer()).map { it.event } - suspend fun watchTurns(): Flow = - stompSession.subscribe("/user/queue/game/turn", PlayerTurnInfo.serializer()) + suspend fun watchPublicGameEvents() = + stompSession.subscribe("/user/queue/game/events", GameEventWrapper.serializer()).map { it.event } suspend fun sayReady() { stompSession.sendEmptyMsg("/app/game/sayReady") } - suspend fun watchOwnMoves(): Flow = - stompSession.subscribe("/user/queue/game/preparedMove", PlayerMove.serializer()) - suspend fun prepareMove(move: PlayerMove) { stompSession.convertAndSend( destination = "/app/game/prepareMove", @@ -187,3 +190,6 @@ private suspend fun doAndWaitForEvent(send: suspend () -> Unit, subscribe: s send() deferredFirstEvent.await() } + +suspend fun SevenWondersSession.watchTurns() = + watchPublicGameEvents().filterIsInstance().map { it.turnInfo } -- cgit