Finished the scorecard
All checks were successful
/ publish (push) Successful in 50s

This commit is contained in:
Leon Mika 2025-12-21 10:56:49 +11:00
parent 1f8394f23b
commit c5420c97eb
6 changed files with 154 additions and 40 deletions

View file

@ -2,13 +2,10 @@ import { Application, Controller } from "https://unpkg.com/@hotwired/stimulus/di
import { GameState, getGameState, setGameState, calculateScore } from "./models.js";
function emitGameStateEvent(details) {
// window.setTimeout(() => {
let ev = new CustomEvent("gamestatechanged", { detail: details });
window.dispatchEvent(ev);
// }, 1);
let ev = new CustomEvent("gamestatechanged", { detail: details });
window.dispatchEvent(ev);
}
window.Stimulus = Application.start();
Stimulus.register("scorecard", class extends Controller {
@ -18,8 +15,6 @@ Stimulus.register("scorecard", class extends Controller {
}
connect() {
// this._rebuildTable();
// this.element.classList.remove("hidden");
}
handleGameState(ev) {
@ -75,8 +70,8 @@ Stimulus.register("scorecard", class extends Controller {
let td = document.createElement("td");
td.textContent = c.s;
if (c.w) {
td.textContent += " 🀄";
td.classList.add("winner");
td.textContent += " 🏆";
}
tr.append(td);
}
@ -94,7 +89,7 @@ Stimulus.register("scorecard", class extends Controller {
this.element.querySelector("table.scorecard").replaceWith(tbl);
this.prevailingTarget.textContent = `Prevailing: ${gameState.prevaling}`;
this.prevailingTarget.textContent = `Prevailing: ${gameState.prevailing}`;
}
});
@ -102,7 +97,6 @@ Stimulus.register("newgame", class extends Controller {
static targets = ["playerName"];
connect() {
this.element.classList.remove("hidden");
}
handleGameState(ev) {
@ -133,10 +127,9 @@ Stimulus.register("newgame", class extends Controller {
});
Stimulus.register("endround", class extends Controller {
static targets = ["form", "input", "wind"];
static targets = ["form", "input", "wind", "prevailing"];
connect() {
// this.element.classList.remove("hidden");
}
handleGameState(ev) {
@ -148,6 +141,7 @@ Stimulus.register("endround", class extends Controller {
break;
case "endround":
this._prepForms();
this._updatePreview();
this.element.classList.remove("hidden");
break;
}
@ -155,7 +149,10 @@ Stimulus.register("endround", class extends Controller {
updatePreview(ev) {
ev.preventDefault();
this._updatePreview();
}
_updatePreview() {
let scoreExprs = this._readScoreExprs();
let nextRound = getGameState().determineNextRound(scoreExprs);
@ -163,15 +160,23 @@ Stimulus.register("endround", class extends Controller {
let previewElem = this.element.querySelector(`.preview[data-player="${i}"]`);
previewElem.textContent = nextRound.roundScores[i].score;
if (parseInt(i) === nextRound.roundWinner) {
previewElem.textContent += " 🏆";
previewElem.classList.add("winner");
previewElem.textContent += " 🀄";
} else {
previewElem.classList.remove("winner");
}
}
if (nextRound.windsBump) {
this.windTarget.textContent = `${getGameState().players[nextRound.nextRoundEast].name} will be East next round`;
this.windTarget.textContent = ` will be East next round`;
} else {
this.windTarget.textContent = `${getGameState().players[nextRound.nextRoundEast].name} will remain East next round`;
this.windTarget.textContent = ` will remain East next round`;
}
let nextWindNameElem = document.createElement("strong");
nextWindNameElem.textContent = getGameState().players[nextRound.nextRoundEast].name;
this.windTarget.prepend(nextWindNameElem);
this.prevailingTarget.textContent = `Next prevailing: ${nextRound.nextPrevailing}`;
}
goBack(ev) {
@ -224,4 +229,13 @@ Stimulus.register("endround", class extends Controller {
}
return scoreExprs;
}
});
});
document.addEventListener("DOMContentLoaded", () => {
let game = getGameState();
if (game !== null) {
emitGameStateEvent({mode: "startgame"});
} else {
emitGameStateEvent({mode: "newgame"});
}
})