summaryrefslogtreecommitdiff
path: root/sw-client/src/commonMain/kotlin/org
diff options
context:
space:
mode:
authorJoffrey Bion <joffrey.bion@gmail.com>2021-02-23 00:01:00 +0100
committerJoffrey Bion <joffrey.bion@gmail.com>2021-02-23 18:38:24 +0100
commite2957be6c0f2dea09d0633cd7ee7549311b9b923 (patch)
tree97b3dcbf09501ac2bd00d9ac8e0a1f1f280ecf0a /sw-client/src/commonMain/kotlin/org
parentFix player re-order animation (diff)
downloadseven-wonders-e2957be6c0f2dea09d0633cd7ee7549311b9b923.tar.gz
seven-wonders-e2957be6c0f2dea09d0633cd7ee7549311b9b923.tar.bz2
seven-wonders-e2957be6c0f2dea09d0633cd7ee7549311b9b923.zip
Funnel game events into a single client subscription
Diffstat (limited to 'sw-client/src/commonMain/kotlin/org')
-rw-r--r--sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt34
1 files changed, 20 insertions, 14 deletions
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<String> =
- stompSession.subscribe("/topic/game/$gameId/playerReady", String.serializer())
+ @OptIn(ExperimentalCoroutinesApi::class)
+ suspend fun watchGameEvents(gameId: Long): Flow<GameEvent> {
+ val private = watchPublicGameEvents()
+ val public = watchPrivateGameEvents(gameId)
+ return merge(private, public)
+ }
- suspend fun watchPreparedCards(gameId: Long): Flow<PreparedCard> =
- 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<PlayerTurnInfo> =
- 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<PlayerMove> =
- 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 <T> doAndWaitForEvent(send: suspend () -> Unit, subscribe: s
send()
deferredFirstEvent.await()
}
+
+suspend fun SevenWondersSession.watchTurns() =
+ watchPublicGameEvents().filterIsInstance<GameEvent.NewTurnStarted>().map { it.turnInfo }
bgstack15