Step 1: Send "first" Player or clear to server.

This commit is contained in:
Oliver Rümpelein 2021-09-26 16:11:03 +02:00
parent 847d09e734
commit 67b5bf5fe0
4 changed files with 36 additions and 4 deletions

View file

@ -43,3 +43,7 @@ fun HTMLTag.xText(text: String) {
fun HTMLTag.xBind(type: String, data: String) { fun HTMLTag.xBind(type: String, data: String) {
attributes["x-bind:$type"] = data attributes["x-bind:$type"] = data
} }
fun HTMLTag.xEffect(effect: String) {
attributes["x-effect"] = effect
}

View file

@ -19,20 +19,46 @@ fun HTML.createGameMasterDocument() {
script { src = "/assets/buzzerWebSocket.js" } script { src = "/assets/buzzerWebSocket.js" }
} }
body { body {
xData("{receivedNames: [], buzzerWebSocket: null}") xData("{receivedNames: [], buzzerWebSocket: null, firstPlayer: null }")
xInit( xInit(
//language=JavaScript //language=JavaScript
""" """
() => { () => {
startWebSocket('/socket/master', (ev) => { startWebSocket('/socket/master', (ev) => {
if (receivedNames.length === 0) {
firstPlayer = ev.data
}
receivedNames.push(ev.data) receivedNames.push(ev.data)
}); });
} }
""".trimIndent() """.trimIndent()
) )
xEffect(
//language=JavaScript
"""
if (Alpine.data.buzzerWebSocket.readyState === 1) {
if (firstPlayer !== null) {
console.log("Player")
Alpine.data.buzzerWebSocket.send(`PLAYER:${"$"}{firstPlayer}`);
} else {
console.log("Clear")
Alpine.data.buzzerWebSocket.send("CLEAR:");
}
}
""".trimIndent()
)
div(classes = "parent") { div(classes = "parent") {
button { button {
xOn("click", "receivedNames = []") xOn(
"click",
//language=JavaScript
"""
() => {
receivedNames = []
firstPlayer = null
}
""".trimIndent()
)
+"Reset" +"Reset"
} }
ol { ol {

View file

@ -4,13 +4,15 @@ import de.pheerai.buzzer.data.GameMasterSocket
import de.pheerai.buzzer.data.SessionStorage import de.pheerai.buzzer.data.SessionStorage
import io.ktor.http.cio.websocket.* import io.ktor.http.cio.websocket.*
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import org.slf4j.Logger
suspend fun WebSocketSession.handleGameMasterSocket() { suspend fun WebSocketSession.handleGameMasterSocket(log: Logger) {
SessionStorage.gameMasterSessions.add(GameMasterSocket(this)) SessionStorage.gameMasterSessions.add(GameMasterSocket(this))
for (frame in incoming) { for (frame in incoming) {
when (frame) { when (frame) {
is Frame.Text -> { is Frame.Text -> {
val text = frame.readText() val text = frame.readText()
log.info("GM Socket received $text")
SessionStorage.playerSessions.filter { it.session.isActive } SessionStorage.playerSessions.filter { it.session.isActive }
.forEach { it.session.send(text) } .forEach { it.session.send(text) }
} }

View file

@ -21,7 +21,7 @@ fun Application.configureRoutes() {
private fun Routing.websocketRoutes(log: Logger) { private fun Routing.websocketRoutes(log: Logger) {
webSocket("/socket/player") { handlePlayerSocket(log) } webSocket("/socket/player") { handlePlayerSocket(log) }
webSocket("/socket/master") { handleGameMasterSocket() } webSocket("/socket/master") { handleGameMasterSocket(log) }
} }
private fun Routing.clientRoutes() { private fun Routing.clientRoutes() {