import { render } from "preact";
import { useMemo, useState } from "preact/hooks";
import "./index.css";
const CARD_VALUES = [
{ label: "J", pts: 2 },
{ label: "Q", pts: 3 },
{ label: "K", pts: 4 },
{ label: "3", pts: 10 },
{ label: "A", pts: 11 },
];
function TeamScore({
id,
score,
total,
counts,
opponentCounts,
onAdd,
reverse,
}) {
return (
{CARD_VALUES.map((v, i) => (
onAdd(id, i, v.pts)}
disabled={counts[i] + opponentCounts[i] >= 4}
className="border border-current p-2 disabled:opacity-20 hover:bg-zinc-100 active:bg-black active:text-white transition-colors"
>
{v.label}
L:{4 - (counts[i] + opponentCounts[i])}
))}
);
}
function ScorerView({
s1,
s2,
total1,
total2,
c1,
c2,
onAdd,
onClear,
onSave,
}) {
return (
);
}
function HistoryView({ history, onPurge }) {
return (
#
T1
T2
CUMUL
{history.map((r, i) => {
const h1 = history
.slice(0, i + 1)
.reduce((a, b) => a + b.t1, 0);
const h2 = history
.slice(0, i + 1)
.reduce((a, b) => a + b.t2, 0);
return (
{i + 1}
{r.t1}
{r.t2}
{h1} / {h2}
);
})}
PURGE HISTORY
);
}
// --- Main App ---
export function App() {
const [tab, setTab] = useState("score");
const [s1, setS1] = useState(0);
const [s2, setS2] = useState(0);
const [c1, setC1] = useState([0, 0, 0, 0, 0]);
const [c2, setC2] = useState([0, 0, 0, 0, 0]);
const [history, setHistory] = useState([]);
const histT1 = useMemo(
() => history.reduce((a, b) => a + b.t1, 0),
[history],
);
const histT2 = useMemo(
() => history.reduce((a, b) => a + b.t2, 0),
[history],
);
const add = (team, idx, pts) => {
if (c1[idx] + c2[idx] >= 4) return;
if (team === 1) {
setS1((v) => v + pts);
setC1((v) => {
const n = [...v];
n[idx]++;
return n;
});
} else {
setS2((v) => v + pts);
setC2((v) => {
const n = [...v];
n[idx]++;
return n;
});
}
};
const clearRound = () => {
setS1(0);
setS2(0);
setC1([0, 0, 0, 0, 0]);
setC2([0, 0, 0, 0, 0]);
};
const save = () => {
if (s1 === 0 && s2 === 0) return;
setHistory((prev) => [...prev, { t1: s1, t2: s2 }]);
clearRound();
};
return (
setTab("score")}
className={`flex-1 border-r border-current p-3 font-bold ${tab === "score" ? "bg-black text-white dark:bg-white dark:text-black" : ""}`}
>
[0] SCORER
setTab("hist")}
className={`flex-1 p-3 font-bold ${tab === "hist" ? "bg-black text-white dark:bg-white dark:text-black" : ""}`}
>
[1] HISTORY ({history.length})
{tab === "score" ? (
) : (
confirm("purge?") && setHistory([])}
/>
)}
);
}
render( , document.getElementById("app"));
// todo: central buttons animations