summaryrefslogtreecommitdiff
path: root/sw-client/src/commonMain/kotlin/org/luxons
diff options
context:
space:
mode:
authorJoffrey Bion <joffrey.bion@gmail.com>2021-03-09 00:06:23 +0100
committerJoffrey Bion <joffrey.bion@gmail.com>2021-03-09 00:06:23 +0100
commit10366ca0240bc6bb5d66cd06a0c18373c2c8e875 (patch)
tree4b491912b1e28d76a41f40d8d46bb3f328596abf /sw-client/src/commonMain/kotlin/org/luxons
parentUpgrade ktlint (diff)
downloadseven-wonders-10366ca0240bc6bb5d66cd06a0c18373c2c8e875.tar.gz
seven-wonders-10366ca0240bc6bb5d66cd06a0c18373c2c8e875.tar.bz2
seven-wonders-10366ca0240bc6bb5d66cd06a0c18373c2c8e875.zip
Unify game events
Diffstat (limited to 'sw-client/src/commonMain/kotlin/org/luxons')
-rw-r--r--sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt93
1 files changed, 30 insertions, 63 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 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<ErrorDTO> = 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<GameListEvent> =
stompSession.subscribe("/topic/games", GameListEventWrapper.serializer()).map { it.event }
+ suspend fun watchGameEvents(): Flow<GameEvent> =
+ 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<LobbyDTO> =
- 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<Long> = 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<LobbyDTO> =
- stompSession.subscribe("/user/queue/lobby/updated", LobbyDTO.serializer())
-
- suspend fun watchGameStarted(): Flow<PlayerTurnInfo> =
- 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<GameEvent> {
- 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 <T> doAndWaitForEvent(send: suspend () -> Unit, subscribe: suspend () -> Flow<T>): T =
coroutineScope {
@@ -192,4 +153,10 @@ private suspend fun <T> doAndWaitForEvent(send: suspend () -> Unit, subscribe: s
}
suspend fun SevenWondersSession.watchTurns() =
- watchPublicGameEvents().filterIsInstance<GameEvent.NewTurnStarted>().map { it.turnInfo }
+ watchGameEvents().filterIsInstance<GameEvent.NewTurnStarted>().map { it.turnInfo }
+
+suspend fun SevenWondersSession.watchLobbyJoined(): Flow<LobbyDTO> =
+ watchGameEvents().filterIsInstance<GameEvent.LobbyJoined>().map { it.lobby }
+
+suspend fun SevenWondersSession.watchNameChoice(): Flow<ConnectedPlayer> =
+ watchGameEvents().filterIsInstance<GameEvent.NameChosen>().map { it.player }
bgstack15