Skip to content

Commit fa85e6e

Browse files
committed
make scorelang text editable
1 parent b0ede16 commit fa85e6e

1 file changed

Lines changed: 90 additions & 12 deletions

File tree

packages/web/src/Scoreboard.tsx

Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useMemo } from "react";
1+
import { useState, useMemo, useEffect } from "react";
22
import { Card, CardContent } from "./components/ui/card";
33
import { Button } from "./components/ui/button";
44
import { Plus, Minus, Trophy, FileText, Target } from "lucide-react";
@@ -60,6 +60,88 @@ export default function Scoreboard({ tournamentId }: ScoreboardProps) {
6060
}
6161
}, [tournamentText]);
6262

63+
// Update games array when tournament text changes
64+
useEffect(() => {
65+
if (!tournamentText.trim()) return;
66+
67+
try {
68+
const lexer = new Lexer(tournamentText);
69+
const parser = new Parser(lexer);
70+
const program = parser.parse();
71+
const evaluator = new Evaluator();
72+
const { results } = evaluator.evaluate(program);
73+
74+
// Convert GameResults back to Game format
75+
const parsedGames: Game[] = results.map((result) => {
76+
if (result.type === "RESOLVED") {
77+
// Map scorelang team names back to display names
78+
const winningTeamDisplay =
79+
(Object.entries(TEAM_MAP).find(
80+
([_, scoreTeam]) => scoreTeam === result.winningTeam
81+
)?.[0] as Team) || "🩷 Team A";
82+
const losingTeamDisplay =
83+
(Object.entries(TEAM_MAP).find(
84+
([_, scoreTeam]) => scoreTeam === result.losingTeam
85+
)?.[0] as Team) || "⚪ Team B";
86+
87+
return {
88+
home: winningTeamDisplay,
89+
away: losingTeamDisplay,
90+
homeScore: result.winningTeamScore,
91+
awayScore: result.losingTeamScore,
92+
};
93+
} else {
94+
// DRAW
95+
const leftTeamDisplay =
96+
(Object.entries(TEAM_MAP).find(
97+
([_, scoreTeam]) => scoreTeam === result.leftTeam
98+
)?.[0] as Team) || "🩷 Team A";
99+
const rightTeamDisplay =
100+
(Object.entries(TEAM_MAP).find(
101+
([_, scoreTeam]) => scoreTeam === result.rightTeam
102+
)?.[0] as Team) || "⚪ Team B";
103+
104+
return {
105+
home: leftTeamDisplay,
106+
away: rightTeamDisplay,
107+
homeScore: result.leftTeamScore,
108+
awayScore: result.rightTeamScore,
109+
};
110+
}
111+
});
112+
113+
// Add current live game if games array is not empty
114+
if (parsedGames.length > 0) {
115+
const lastGame = parsedGames[parsedGames.length - 1];
116+
// Identify idle team for next game
117+
const idle = TEAMS.find(
118+
(t) => t !== lastGame.home && t !== lastGame.away
119+
)!;
120+
parsedGames.push({
121+
home: lastGame.away,
122+
away: idle,
123+
homeScore: 0,
124+
awayScore: 0,
125+
});
126+
}
127+
128+
setGames(() =>
129+
parsedGames.length > 0
130+
? parsedGames
131+
: [
132+
{
133+
home: "🩷 Team A",
134+
away: "⚪ Team B",
135+
homeScore: 0,
136+
awayScore: 0,
137+
},
138+
]
139+
);
140+
} catch (error) {
141+
console.error("Error parsing tournament text for games:", error);
142+
}
143+
}, [tournamentText, setGames]);
144+
63145
const updateScore = (side: "home" | "away", delta: 1 | -1) => {
64146
setGames((prev) => {
65147
const next = [...prev];
@@ -272,18 +354,14 @@ export default function Scoreboard({ tournamentId }: ScoreboardProps) {
272354
<Card className="w-full backdrop-blur-md bg-white/10 border-white/20">
273355
<CardContent className="p-4 sm:p-6">
274356
<h3 className="text-xl sm:text-2xl font-bold text-gray-800 mb-4 text-center">
275-
📝 Tournament (ScoreLang)
357+
📝 ScoreLang Code
276358
</h3>
277-
{tournamentText ? (
278-
<pre className="text-white/90 bg-black/20 p-4 rounded-lg text-xs sm:text-sm font-mono overflow-x-auto min-h-[200px]">
279-
{tournamentText}
280-
</pre>
281-
) : (
282-
<div className="text-gray-700 text-center py-8">
283-
No games finalized yet. Complete some games in the Score
284-
Management tab to see ScoreLang output.
285-
</div>
286-
)}
359+
<textarea
360+
className="w-full text-white/90 bg-black/20 p-4 rounded-lg text-xs sm:text-sm font-mono overflow-x-auto min-h-[200px] resize-none border border-white/20 focus:border-white/40 focus:outline-none"
361+
value={tournamentText || ""}
362+
onChange={(e) => setTournamentText(() => e.target.value)}
363+
placeholder="No games finalized yet. Complete some games in the Score Management tab to see ScoreLang output, or write your own ScoreLang code here."
364+
/>
287365
</CardContent>
288366
</Card>
289367
)}

0 commit comments

Comments
 (0)