From 543bdd6ac2de06d8c370e518631abae6fbcc5b87 Mon Sep 17 00:00:00 2001 From: TheSaminator Date: Sat, 12 Feb 2022 16:18:01 -0500 Subject: [PATCH] Fix user internal status --- .../starshipfights/game/endpoints_game.kt | 28 ++++++++++++++----- .../starshipfights/game/server_matchmaking.kt | 14 ++++++---- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/jvmMain/kotlin/starshipfights/game/endpoints_game.kt b/src/jvmMain/kotlin/starshipfights/game/endpoints_game.kt index c7b9add..25c9ab7 100644 --- a/src/jvmMain/kotlin/starshipfights/game/endpoints_game.kt +++ b/src/jvmMain/kotlin/starshipfights/game/endpoints_game.kt @@ -5,9 +5,12 @@ import io.ktor.html.* import io.ktor.http.* import io.ktor.routing.* import io.ktor.websocket.* +import kotlinx.coroutines.currentCoroutineContext +import kotlinx.coroutines.job import kotlinx.coroutines.launch import org.litote.kmongo.setValue import starshipfights.auth.getUser +import starshipfights.data.DocumentTable import starshipfights.data.admiralty.getAllInGameAdmirals import starshipfights.data.auth.User import starshipfights.data.auth.UserStatus @@ -46,11 +49,20 @@ fun Routing.installGame() { val user = oldUser.copy(status = UserStatus.IN_MATCHMAKING) User.put(user) - matchmakingEndpoint(user) + currentCoroutineContext().job.invokeOnCompletion { + DocumentTable.launch { + val cancelUser = User.get(user.id)!! + if (cancelUser.status == UserStatus.IN_MATCHMAKING) + User.put( + cancelUser.copy( + status = UserStatus.AVAILABLE + ) + ) + } + } - launch { + if (matchmakingEndpoint(user)) User.set(user.id, setValue(User::status, UserStatus.READY_FOR_BATTLE)) - } } webSocket("/game/{token}") { @@ -68,10 +80,12 @@ fun Routing.installGame() { val user = oldUser.copy(status = UserStatus.IN_BATTLE) User.put(user) - gameEndpoint(user, token) - - launch { - User.set(user.id, setValue(User::status, UserStatus.AVAILABLE)) + currentCoroutineContext().job.invokeOnCompletion { + DocumentTable.launch { + User.set(user.id, setValue(User::status, UserStatus.AVAILABLE)) + } } + + gameEndpoint(user, token) } } diff --git a/src/jvmMain/kotlin/starshipfights/game/server_matchmaking.kt b/src/jvmMain/kotlin/starshipfights/game/server_matchmaking.kt index 87d406f..c3ecff8 100644 --- a/src/jvmMain/kotlin/starshipfights/game/server_matchmaking.kt +++ b/src/jvmMain/kotlin/starshipfights/game/server_matchmaking.kt @@ -23,11 +23,11 @@ class JoinInvitation(val joinRequest: JoinRequest, val responseHandler: Completa val gameIdHandler = CompletableDeferred() } -suspend fun DefaultWebSocketServerSession.matchmakingEndpoint(user: User) { - val playerLogin = receiveObject(PlayerLogin.serializer()) { closeAndReturn { return } } +suspend fun DefaultWebSocketServerSession.matchmakingEndpoint(user: User): Boolean { + val playerLogin = receiveObject(PlayerLogin.serializer()) { closeAndReturn { return false } } val admiralId = playerLogin.admiral - val inGameAdmiral = getInGameAdmiral(admiralId) ?: closeAndReturn("That admiral does not exist") { return } - if (inGameAdmiral.user.id != user.id) closeAndReturn("You do not own that admiral") { return } + val inGameAdmiral = getInGameAdmiral(admiralId) ?: closeAndReturn("That admiral does not exist") { return false } + if (inGameAdmiral.user.id != user.id) closeAndReturn("You do not own that admiral") { return false } when (val loginMode = playerLogin.login) { is LoginMode.Host -> { @@ -52,7 +52,7 @@ suspend fun DefaultWebSocketServerSession.matchmakingEndpoint(user: User) { val joinResponse = receiveObject(JoinResponse.serializer()) { closeAndReturn { joinInvitation.responseHandler.complete(JoinResponse(false)) - return + return false } } @@ -89,7 +89,7 @@ suspend fun DefaultWebSocketServerSession.matchmakingEndpoint(user: User) { val joinListing = JoinListing(openGames.mapValues { (_, invitation) -> invitation.joinable }) sendObject(JoinListing.serializer(), joinListing) - val joinSelection = receiveObject(JoinSelection.serializer()) { closeAndReturn { return } } + val joinSelection = receiveObject(JoinSelection.serializer()) { closeAndReturn { return false } } val hostInvitation = openGames.getValue(joinSelection.selectedId) val joinResponseHandler = CompletableDeferred() @@ -116,4 +116,6 @@ suspend fun DefaultWebSocketServerSession.matchmakingEndpoint(user: User) { } } } + + return true } -- 2.25.1