import { NotifyContext, TranslateContext } from "#/Modules/context";
import { useContext, useEffect, useState } from "react";
import {
Box,
Chip,
Divider,
Paper,
TextField,
Typography,
} from "@mui/material";
function ScoreRow({
homeTeam,
awayTeam,
homeScore,
awayScore,
label,
emphasized = false,
}: {
homeTeam: string;
awayTeam: string;
homeScore: number | string | null;
awayScore: number | string | null;
label?: string;
emphasized?: boolean;
}) {
return (
{label && (
{label}
)}
{homeTeam}
{homeScore} - {awayScore}
{awayTeam}
);
}
function PointsChip({
points,
predictExact,
predictDifference,
predictWinner,
}: {
points: number;
predictExact: number;
predictDifference: number;
predictWinner: number;
}) {
const t = useContext(TranslateContext);
const color =
points === predictExact
? "success"
: points === predictDifference
? "primary"
: points === predictWinner
? "secondary"
: "default";
return (
0 ? "filled" : "outlined"}
/>
);
}
export interface predictions {
home_team: string;
home_team_name: string | null;
away_team: string;
away_team_name: string | null;
home_score: number | null;
away_score: number | null;
gameStart: number;
gameEnd: number;
home_prediction: number | null;
away_prediction: number | null;
}
export interface GameProps extends predictions {
league: number;
predictWinner: number;
predictDifference: number;
predictExact: number;
readOnly?: boolean;
}
function isValidPredictionInput(value: number | null): boolean {
return value !== null && !isNaN(Number(value));
}
export function Game({
home_team,
home_team_name,
away_team,
away_team_name,
home_score,
away_score,
gameStart,
gameEnd,
home_prediction,
away_prediction,
league,
predictWinner,
predictDifference,
predictExact,
// This is used to mean an outside viewer that should only see the prediction when the game starts
readOnly = false,
}: GameProps) {
const t = useContext(TranslateContext);
const [home, setHome] = useState(home_prediction);
const [away, setAway] = useState(away_prediction);
const notify = useContext(NotifyContext);
const [isPastGameEnd, setIsPastGameEnd] = useState(
() => Date.now() / 1000 > gameEnd,
);
useEffect(() => {
const check = () => setIsPastGameEnd(Date.now() / 1000 > gameEnd);
const id = setInterval(check, 10000);
return () => clearInterval(id);
}, [gameEnd]);
function updateHome(e: React.ChangeEvent) {
setHome(parseInt(e.target.value));
save(parseInt(e.target.value), away);
}
function updateAway(e: React.ChangeEvent) {
setAway(parseInt(e.target.value));
save(home, parseInt(e.target.value));
}
function save(home: number | null, away: number | null) {
notify(t("Saving"));
fetch("/api/predictions", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
home_team,
away_team,
league,
home,
away,
gameStart,
}),
}).then(async (response) => {
notify(t(await response.text()), response.ok ? "success" : "error");
});
}
const [countdown, setCountown] = useState(() =>
Math.ceil((gameStart - Date.now() / 1000) / 60),
);
useEffect(() => {
const check = () =>
setCountown(Math.ceil((gameStart - Date.now() / 1000) / 60));
const id = setInterval(check, 10000);
return () => clearInterval(id);
}, [gameStart]);
const home_team_text = home_team_name || home_team;
const away_team_text = away_team_name || away_team;
const pointsEarned =
home_score === null ||
away_score === null ||
home_prediction === null ||
away_prediction === null
? null
: home_prediction === home_score && away_prediction === away_score
? predictExact
: home_prediction - away_prediction === home_score - away_score
? predictDifference
: home_prediction > away_prediction === home_score > away_score &&
(home_prediction === away_prediction) ===
(home_score === away_score)
? predictWinner
: 0;
return (
{countdown > 0
? t("{home_team} - {away_team} in {day} D {hour} H {minute} M ", {
home_team: home_team_text,
away_team: away_team_text,
day: Math.floor(countdown / 60 / 24),
hour: Math.floor(countdown / 60) % 24,
minute: Math.floor(countdown) % 60,
})
: `${home_team_text} - ${away_team_text}`}
{countdown > 0 && !readOnly && (
-
)}
{countdown > 0 && readOnly && (
)}
{countdown <= 0 && (
<>
{home_prediction !== null && away_prediction !== null && (
<>
>
)}
{pointsEarned !== null && (
)}
>
)}
);
}