From 9e6a3b61eabced7e554b3b0d0e60faf86e4322f9 Mon Sep 17 00:00:00 2001 From: TheSaminator Date: Thu, 10 Feb 2022 11:42:04 -0500 Subject: [PATCH] Various changes to game joining --- .../starshipfights/game/client_matchmaking.kt | 20 ++++++++++-- .../kotlin/starshipfights/game/popup.kt | 31 ------------------- .../starshipfights/data/auth/user_sessions.kt | 2 +- .../starshipfights/game/endpoints_game.kt | 16 ++++++---- 4 files changed, 29 insertions(+), 40 deletions(-) diff --git a/src/jsMain/kotlin/starshipfights/game/client_matchmaking.kt b/src/jsMain/kotlin/starshipfights/game/client_matchmaking.kt index 1f4988d..9c30999 100644 --- a/src/jsMain/kotlin/starshipfights/game/client_matchmaking.kt +++ b/src/jsMain/kotlin/starshipfights/game/client_matchmaking.kt @@ -6,8 +6,14 @@ import externals.threejs.WebGLRenderer import io.ktor.client.features.websocket.* import kotlinx.browser.document import kotlinx.browser.window +import kotlinx.coroutines.awaitCancellation import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch +import kotlinx.html.FormEncType +import kotlinx.html.FormMethod +import kotlinx.html.dom.create +import kotlinx.html.hiddenInput +import kotlinx.html.js.form suspend fun setupBackground() { val camera = PerspectiveCamera(69, window.aspectRatio, 0.01, 1_000) @@ -37,6 +43,16 @@ suspend fun setupBackground() { } } +private suspend fun enterGame(connectToken: String): Nothing { + document.create.form(action = "/play", method = FormMethod.post, encType = FormEncType.applicationXWwwFormUrlEncoded) { + hiddenInput { + name = "token" + value = connectToken + } + }.submit() + awaitCancellation() +} + private suspend fun usePlayerLogin(admirals: List) { val playerLogin = Popup.getPlayerLogin(admirals) val admiral = admirals.single { it.id == playerLogin.admiral } @@ -66,7 +82,7 @@ private suspend fun usePlayerLogin(admirals: List) { } while (!joinConnected) val connectToken = receiveObject(GameReady.serializer()) { closeAndReturn { return@webSocket } }.connectToken - Popup.GameReadyScreen(connectToken).display() + enterGame(connectToken) } GlobalSide.GUEST -> { val listOfHosts = receiveObject(JoinListing.serializer()) { closeAndReturn { return@webSocket } }.openGames @@ -86,7 +102,7 @@ private suspend fun usePlayerLogin(admirals: List) { } while (!joinAcceptance) val connectToken = receiveObject(GameReady.serializer()) { closeAndReturn { return@webSocket } }.connectToken - Popup.GameReadyScreen(connectToken).display() + enterGame(connectToken) } } } diff --git a/src/jsMain/kotlin/starshipfights/game/popup.kt b/src/jsMain/kotlin/starshipfights/game/popup.kt index cac0087..a22aea1 100644 --- a/src/jsMain/kotlin/starshipfights/game/popup.kt +++ b/src/jsMain/kotlin/starshipfights/game/popup.kt @@ -14,7 +14,6 @@ import kotlinx.html.* import kotlinx.html.dom.append import kotlinx.html.js.onClickFunction import org.w3c.dom.HTMLDivElement -import org.w3c.dom.HTMLFormElement import kotlin.coroutines.CoroutineContext import kotlin.coroutines.resume @@ -363,36 +362,6 @@ sealed class Popup { } } - class GameReadyScreen(private val connectToken: String) : Popup() { - override fun TagConsumer<*>.render(context: CoroutineContext, callback: (Nothing) -> Unit) { - p { - style = "text-align:center" - - +"Your battle is ready for you to enter!" - } - - form(action = "/play", method = FormMethod.post, encType = FormEncType.applicationXWwwFormUrlEncoded) { - id = "battle-entry" - style = "display:none" - hiddenInput { - name = "token" - value = connectToken - } - } - - div(classes = "button-set row") { - button { - +"Enter Battle" - onClickFunction = { e -> - e.preventDefault() - - document.getElementById("battle-entry").unsafeCast().submit() - } - } - } - } - } - class LoadingScreen(private val loadingText: String, private val loadAction: suspend () -> T) : Popup() { override fun TagConsumer<*>.render(context: CoroutineContext, callback: (T) -> Unit) { p { diff --git a/src/jvmMain/kotlin/starshipfights/data/auth/user_sessions.kt b/src/jvmMain/kotlin/starshipfights/data/auth/user_sessions.kt index bc4d7b9..b561009 100644 --- a/src/jvmMain/kotlin/starshipfights/data/auth/user_sessions.kt +++ b/src/jvmMain/kotlin/starshipfights/data/auth/user_sessions.kt @@ -32,7 +32,7 @@ data class User( } enum class UserStatus { - AVAILABLE, IN_MATCHMAKING, IN_BATTLE + AVAILABLE, IN_MATCHMAKING, READY_FOR_BATTLE, IN_BATTLE } @Serializable diff --git a/src/jvmMain/kotlin/starshipfights/game/endpoints_game.kt b/src/jvmMain/kotlin/starshipfights/game/endpoints_game.kt index 970b898..0af46ef 100644 --- a/src/jvmMain/kotlin/starshipfights/game/endpoints_game.kt +++ b/src/jvmMain/kotlin/starshipfights/game/endpoints_game.kt @@ -29,7 +29,8 @@ fun Routing.installGame() { val clientMode = when (user.status) { UserStatus.AVAILABLE -> ClientMode.Error("You must use the matchmaking interface to enter a game") - UserStatus.IN_MATCHMAKING -> call.getGameClientMode() + UserStatus.IN_MATCHMAKING -> ClientMode.Error("You must start a game in the matchmaking interface") + UserStatus.READY_FOR_BATTLE -> call.getGameClientMode() UserStatus.IN_BATTLE -> ClientMode.Error("You cannot play in multiple battles at the same time") } @@ -42,11 +43,13 @@ fun Routing.installGame() { closeAndReturn("You cannot play in multiple battles at the same time") { return@webSocket } val user = oldUser.copy(status = UserStatus.IN_MATCHMAKING) - launch { - User.put(user) - } + User.put(user) matchmakingEndpoint(user) + + launch { + User.put(user.copy(status = UserStatus.READY_FOR_BATTLE)) + } } webSocket("/game/{token}") { @@ -56,6 +59,8 @@ fun Routing.installGame() { if (oldUser.status == UserStatus.IN_BATTLE) closeAndReturn("You cannot play in multiple battles at the same time") { return@webSocket } + if (oldUser.status == UserStatus.IN_MATCHMAKING) + closeAndReturn("You must start a game in the matchmaking interface") { return@webSocket } if (oldUser.status == UserStatus.AVAILABLE) closeAndReturn("You must use the matchmaking interface to enter a game") { return@webSocket } @@ -65,8 +70,7 @@ fun Routing.installGame() { gameEndpoint(user, token) launch { - val postGameUser = user.copy(status = UserStatus.AVAILABLE) - User.put(postGameUser) + User.put(user.copy(status = UserStatus.AVAILABLE)) } } } -- 2.25.1