|
1 | | -import { useState, useMemo } from "react"; |
| 1 | +import { useState, useMemo, useEffect } from "react"; |
2 | 2 | import { Card, CardContent } from "./components/ui/card"; |
3 | 3 | import { Button } from "./components/ui/button"; |
4 | 4 | import { Plus, Minus, Trophy, FileText, Target } from "lucide-react"; |
@@ -60,6 +60,88 @@ export default function Scoreboard({ tournamentId }: ScoreboardProps) { |
60 | 60 | } |
61 | 61 | }, [tournamentText]); |
62 | 62 |
|
| 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 | + |
63 | 145 | const updateScore = (side: "home" | "away", delta: 1 | -1) => { |
64 | 146 | setGames((prev) => { |
65 | 147 | const next = [...prev]; |
@@ -272,18 +354,14 @@ export default function Scoreboard({ tournamentId }: ScoreboardProps) { |
272 | 354 | <Card className="w-full backdrop-blur-md bg-white/10 border-white/20"> |
273 | 355 | <CardContent className="p-4 sm:p-6"> |
274 | 356 | <h3 className="text-xl sm:text-2xl font-bold text-gray-800 mb-4 text-center"> |
275 | | - 📝 Tournament (ScoreLang) |
| 357 | + 📝 ScoreLang Code |
276 | 358 | </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 | + /> |
287 | 365 | </CardContent> |
288 | 366 | </Card> |
289 | 367 | )} |
|
0 commit comments