Fix user internal status
authorTheSaminator <TheSaminator@users.noreply.github.com>
Sat, 12 Feb 2022 21:18:01 +0000 (16:18 -0500)
committerTheSaminator <TheSaminator@users.noreply.github.com>
Sat, 12 Feb 2022 21:18:01 +0000 (16:18 -0500)
src/jvmMain/kotlin/starshipfights/game/endpoints_game.kt
src/jvmMain/kotlin/starshipfights/game/server_matchmaking.kt

index c7b9addd63988a010e6aa82dc6112ff8575fba26..25c9ab7c6ce43441e17cda9a0d51d7bfe0adb3ba 100644 (file)
@@ -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)
        }
 }
index 87d406f047a5ef9d30840cf328d29d87ce931d1e..c3ecff87229a3bf8f3ac993926c7dbf6668c239a 100644 (file)
@@ -23,11 +23,11 @@ class JoinInvitation(val joinRequest: JoinRequest, val responseHandler: Completa
        val gameIdHandler = CompletableDeferred<String>()
 }
 
-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<JoinResponse>()
@@ -116,4 +116,6 @@ suspend fun DefaultWebSocketServerSession.matchmakingEndpoint(user: User) {
                        }
                }
        }
+       
+       return true
 }