From 83b2f2e9922f46fe46fd5330e6b38c66d1483ade Mon Sep 17 00:00:00 2001 From: Ewy Date: Mon, 21 Sep 2026 21:40:05 +0200 Subject: (wip) --- server/api/game.go | 10 ++++++-- server/web/game/game.js | 62 ++++++++++++++++++++++++++++++++-------------- server/web/game/index.html | 7 +++++- 3 files changed, 57 insertions(+), 22 deletions(-) (limited to 'server') diff --git a/server/api/game.go b/server/api/game.go index bccf12a..636ff66 100644 --- a/server/api/game.go +++ b/server/api/game.go @@ -1,11 +1,12 @@ package api import ( - "github.com/gorilla/websocket" "net/http" "sakura/engine" "slices" "uuid" + + "github.com/gorilla/websocket" ) type Conn struct { @@ -73,7 +74,12 @@ func PumpRead(id uuid.UUID, g *engine.Game, conn *websocket.Conn) { if err != nil { break } - g.Actions[interaction].Then(g) + a := g.Actions[interaction] + if a == nil { + continue + } + a.Then(g) + g.ActionToHistory(a) Update(id, g) } } diff --git a/server/web/game/game.js b/server/web/game/game.js index 5e71786..4b0024c 100644 --- a/server/web/game/game.js +++ b/server/web/game/game.js @@ -1,39 +1,63 @@ -"use strict" +"use strict"; const showGame = (g) => { - const tmpl = document.querySelector("template#tmpl") - const disp = document.querySelector("#game") - const clone = document.importNode(tmpl.content, true) - doDistance(clone, g.Distance.Value) - disp.replaceChildren(clone) + const tmpl = document.querySelector("template#tmpl"); + const disp = document.querySelector("#game"); + const clone = document.importNode(tmpl.content, true); + doDistance(clone, g.Distance.Value); + doActions(clone, g.Actions); + disp.replaceChildren(clone); }; const doDistance = (clone, value) => { clone.querySelector(".distance").setAttribute("data-value", value); [...clone.querySelectorAll(".distance .sakura")].map((n, i) => { if (i < value) { - n.checked = true + n.checked = true; } else { - n.checked = false + n.checked = false; } - }) + }); +}; + +const doActions = (g, actions) => { + const actionList = g.querySelector(".actions"); + const actionTemplate = actionList.querySelector(".action").cloneNode(true); + if (actions) { + actionList.replaceChildren( + ...actions.map((a, i) => { + const newAction = actionTemplate.cloneNode(true); + newAction.setAttribute("data-index", i); + newAction.querySelector(".description").innerText = a.Description; + return newAction; + }), + ); + } else { + actionList.replaceChildren() + } +}; +const doAction = (idx) => { + window.socket.send(String(idx)) } const connect = (gameId) => { const connection = new WebSocket(`/api/game/${gameId}/connect`); window.socket = connection; connection.addEventListener("message", (ev) => { - const game = JSON.parse(ev.data); - console.debug(game); - showGame(game); - document.querySelector("#game-json").innerText = - JSON.stringify(ev.data, null, 2); + const game = JSON.parse(ev.data); + console.debug(game); + showGame(game); + document.querySelector("#game-json").innerText = JSON.stringify( + ev.data, + null, + 2, + ); }); -} +}; const init = () => { - const id = window.location.hash.slice(1) - connect(id) -} + const id = window.location.hash.slice(1); + connect(id); +}; -init() +init(); diff --git a/server/web/game/index.html b/server/web/game/index.html index 414ff5c..f4f6593 100644 --- a/server/web/game/index.html +++ b/server/web/game/index.html @@ -15,7 +15,7 @@
Game state -

+                
             
@@ -33,6 +33,11 @@ +