From fcb43d8cccbf0251bccaa64172f1674c5b9ff41b Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Fri, 27 Mar 2020 11:12:58 +0100 Subject: Remove implicit serializers from SW Client --- .../sevenwonders/client/SevenWondersClient.kt | 50 +++++++++++++--------- 1 file changed, 30 insertions(+), 20 deletions(-) (limited to 'sw-client/src/commonMain/kotlin/org/luxons') 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 d85538fd..3cb2f6ee 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,13 +1,13 @@ package org.luxons.sevenwonders.client -import kotlinx.serialization.ImplicitReflectionSerializer +import kotlinx.serialization.DeserializationStrategy +import kotlinx.serialization.SerializationStrategy import kotlinx.serialization.list import kotlinx.serialization.serializer import org.hildan.krossbow.stomp.StompClient import org.hildan.krossbow.stomp.StompSubscription import org.hildan.krossbow.stomp.conversions.kxserialization.StompSessionWithKxSerialization import org.hildan.krossbow.stomp.conversions.kxserialization.convertAndSend -import org.hildan.krossbow.stomp.conversions.kxserialization.subscribe import org.hildan.krossbow.stomp.conversions.kxserialization.withJsonConversions import org.hildan.krossbow.stomp.sendEmptyMsg import org.hildan.krossbow.stomp.subscribeEmptyMsg @@ -37,14 +37,15 @@ class SevenWondersClient { } } -@OptIn(ImplicitReflectionSerializer::class) -suspend inline fun StompSessionWithKxSerialization.request( +private suspend inline fun StompSessionWithKxSerialization.request( sendDestination: String, receiveDestination: String, - payload: T? = null + payload: T? = null, + serializer: SerializationStrategy, + deserializer: DeserializationStrategy ): U { - val sub = subscribe(receiveDestination) - convertAndSend(sendDestination, payload) + val sub = subscribe(receiveDestination, deserializer) + convertAndSend(sendDestination, payload, serializer) val msg = sub.messages.receive() sub.unsubscribe() return msg @@ -57,23 +58,32 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat suspend fun watchErrors(): StompSubscription = stompSession.subscribe("/user/queue/errors", ErrorDTO.serializer()) - suspend fun chooseName(displayName: String): PlayerDTO { - val action = ChooseNameAction(displayName) - return stompSession.request("/app/chooseName", "/user/queue/nameChoice", action) - } + suspend fun chooseName(displayName: String): PlayerDTO = stompSession.request( + sendDestination = "/app/chooseName", + receiveDestination = "/user/queue/nameChoice", + payload = ChooseNameAction(displayName), + serializer = ChooseNameAction.serializer(), + deserializer = PlayerDTO.serializer() + ) suspend fun watchGames(): StompSubscription> = stompSession.subscribe("/topic/games", LobbyDTO.serializer().list) - suspend fun createGame(gameName: String): LobbyDTO { - val action = CreateGameAction(gameName) - return stompSession.request("/app/lobby/create", "/user/queue/lobby/joined", action) - } - - suspend fun joinGame(gameId: Long): LobbyDTO { - val action = JoinGameAction(gameId) - return stompSession.request("/app/lobby/join", "/user/queue/lobby/joined", action) - } + suspend fun createGame(gameName: String): LobbyDTO = stompSession.request( + sendDestination = "/app/lobby/create", + receiveDestination = "/user/queue/lobby/joined", + payload = CreateGameAction(gameName), + serializer = CreateGameAction.serializer(), + deserializer = LobbyDTO.serializer() + ) + + suspend fun joinGame(gameId: Long): LobbyDTO = stompSession.request( + sendDestination = "/app/lobby/join", + receiveDestination = "/user/queue/lobby/joined", + payload = JoinGameAction(gameId), + serializer = JoinGameAction.serializer(), + deserializer = LobbyDTO.serializer() + ) suspend fun leaveGame() { stompSession.sendEmptyMsg("/app/lobby/leave") -- cgit