diff --git a/src/main/kotlin/de/pheerai/buzzer/alpine/attributes.kt b/src/main/kotlin/de/pheerai/buzzer/alpine/attributes.kt index a580a23..a462b11 100644 --- a/src/main/kotlin/de/pheerai/buzzer/alpine/attributes.kt +++ b/src/main/kotlin/de/pheerai/buzzer/alpine/attributes.kt @@ -43,3 +43,7 @@ fun HTMLTag.xText(text: String) { fun HTMLTag.xBind(type: String, data: String) { attributes["x-bind:$type"] = data } + +fun HTMLTag.xEffect(effect: String) { + attributes["x-effect"] = effect +} diff --git a/src/main/kotlin/de/pheerai/buzzer/clients/gameMasterClient.kt b/src/main/kotlin/de/pheerai/buzzer/clients/gameMasterClient.kt index 042d204..8ff7370 100644 --- a/src/main/kotlin/de/pheerai/buzzer/clients/gameMasterClient.kt +++ b/src/main/kotlin/de/pheerai/buzzer/clients/gameMasterClient.kt @@ -19,20 +19,46 @@ fun HTML.createGameMasterDocument() { script { src = "/assets/buzzerWebSocket.js" } } body { - xData("{receivedNames: [], buzzerWebSocket: null}") + xData("{receivedNames: [], buzzerWebSocket: null, firstPlayer: null }") xInit( //language=JavaScript """ () => { startWebSocket('/socket/master', (ev) => { + if (receivedNames.length === 0) { + firstPlayer = ev.data + } receivedNames.push(ev.data) }); } """.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") { button { - xOn("click", "receivedNames = []") + xOn( + "click", + //language=JavaScript + """ + () => { + receivedNames = [] + firstPlayer = null + } + """.trimIndent() + ) +"Reset" } ol { diff --git a/src/main/kotlin/de/pheerai/buzzer/handlers/handleGameMasterSocket.kt b/src/main/kotlin/de/pheerai/buzzer/handlers/handleGameMasterSocket.kt index 8c07644..8638beb 100644 --- a/src/main/kotlin/de/pheerai/buzzer/handlers/handleGameMasterSocket.kt +++ b/src/main/kotlin/de/pheerai/buzzer/handlers/handleGameMasterSocket.kt @@ -4,13 +4,15 @@ import de.pheerai.buzzer.data.GameMasterSocket import de.pheerai.buzzer.data.SessionStorage import io.ktor.http.cio.websocket.* import kotlinx.coroutines.isActive +import org.slf4j.Logger -suspend fun WebSocketSession.handleGameMasterSocket() { +suspend fun WebSocketSession.handleGameMasterSocket(log: Logger) { SessionStorage.gameMasterSessions.add(GameMasterSocket(this)) for (frame in incoming) { when (frame) { is Frame.Text -> { val text = frame.readText() + log.info("GM Socket received $text") SessionStorage.playerSessions.filter { it.session.isActive } .forEach { it.session.send(text) } } diff --git a/src/main/kotlin/de/pheerai/buzzer/routing/setupRoutes.kt b/src/main/kotlin/de/pheerai/buzzer/routing/setupRoutes.kt index 37fd54b..94ac00d 100644 --- a/src/main/kotlin/de/pheerai/buzzer/routing/setupRoutes.kt +++ b/src/main/kotlin/de/pheerai/buzzer/routing/setupRoutes.kt @@ -21,7 +21,7 @@ fun Application.configureRoutes() { private fun Routing.websocketRoutes(log: Logger) { webSocket("/socket/player") { handlePlayerSocket(log) } - webSocket("/socket/master") { handleGameMasterSocket() } + webSocket("/socket/master") { handleGameMasterSocket(log) } } private fun Routing.clientRoutes() {