import playerStyles from "../styles/Player.module.css";
import { useContext, useEffect, useState } from "react";
import { NotifyContext, TranslateContext } from "../Modules/context";
import Image from "next/image";
import { useSession } from "next-auth/react";
import fallbackImg from "../public/playerFallback.png";
import Link from "./Link";
import {
Box,
Button,
CircularProgress,
InputAdornment,
Paper,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
TextField,
alpha,
useTheme,
} from "@mui/material";
import Dialog from "./Dialog";
import { UserChip } from "./Username";
// Used to create the layout for a player card that shows some simple details on a player just requires the data of the player to be passed into it and you can pass a custom button as a child of the component
// extraText is shown in parenthesis next to the player name
// condensed is which type of condensed view should be shown (transfer, squad, or historical)
function InternalPlayer({ data, children, starred, extraText, condensed }) {
const t = useContext(TranslateContext);
const [countdown, setCountown] = useState(0);
useEffect(() => {
const check = () =>
setCountown(
data.game
? Math.ceil((data.game.gameStart - Date.now() / 1000) / 60)
: 0,
);
queueMicrotask(check);
const id = setInterval(check, 10000);
return () => clearInterval(id);
}, [data]);
const theme = useTheme();
const dark = theme.palette.mode === "dark";
let background = "main";
if (Object.keys(data).length > 0) {
// Changes the background to the correct color if the player is missing or not known if they are coming
if (data.forecast == "u") {
background = "warning";
} else if (data.forecast == "m") {
background = "error";
}
// Checks if the player exists
if (data.exists === 0) {
background = "secondary";
}
const price_component =
data.value === data.sale_price ? (
);
// Checks if the game has started less than 120 minutes ago and that this is the squad view
const gameRunning =
countdown < 0 &&
countdown > (data.game.gameStart - data.game.gameEnd) / 60;
const border =
gameRunning && condensed === "squad"
? {
border: `5px solid ${
dark ? "rgba(255, 255, 255, 0.12)" : "rgba(0, 0, 0, 0.5)"
}`,
}
: {};
return (
{data.club}
{t(data.position)}
{data.name}
{starred ? (
) : (
""
)}
{extraText && {extraText}}
{data.updateRunning === false && (
{t("Player Data not updating click for details")}
)}
{countdown >= 0 && condensed == "squad"
? t("{team} in {day} D {hour} H {minute} M ", {
team: data.game.opponent,
day: Math.floor(countdown / 60 / 24),
hour: Math.floor(countdown / 60) % 24,
minute: Math.floor(countdown) % 60,
})
: data.game.opponent}
)}
{children}
);
} else {
return (
);
}
}
// Used for the transfer screen
export function TransferPlayer({
uid,
ownership,
money,
league,
transferData,
transferLeft,
open,
duplicatePlayers,
allOwnership,
leagueType,
showHidden,
}) {
const notify = useContext(NotifyContext);
const session = useSession();
const user = session.data ? session.data.user.id : -1;
const [data, setData] = useState({});
const [focused, setFocused] = useState(false);
const [amount, setAmount] = useState(money);
const t = useContext(TranslateContext);
if (
user !== -1 &&
Object.values(allOwnership).filter(
(e) => e.filter((e2) => e2.owner === user).length > 0,
).length == 0
)
transferLeft = true;
// Used to get the data for the player
useEffect(() => {
async function getData() {
const info = await (
await fetch(`/api/player/${leagueType}/${uid}`)
).json();
setData(info);
setAmount(info.sale_price / 1000000);
}
getData();
}, [uid, leagueType]);
// Used to purchase a player
function buySell(amount) {
amount = amount * 1000000;
setFocused(false);
notify("Buying/Selling");
fetch(`/api/transfer/${league}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
playeruid: uid,
amount,
}),
}).then(async (response) => {
notify(
t(await response.text(), { amount: amount / 1000000 }),
response.ok ? "success" : "error",
);
transferData();
});
}
let ButtonText = t("Error Getting Player info");
let ButtonColor = "primary";
const BuyText = (
<>
M}
onChange={(val) => {
// Used to change the invite link
setAmount(val.target.value);
}}
value={amount}
/>
>
);
const SellText = (
<>
M}
onChange={(val) => {
// Used to change the invite link
setAmount(val.target.value);
}}
value={amount}
/>
>
);
// This will contain the input for everything you can do
let Actions = <>>;
// Checks if the ownership info exists
if (ownership !== undefined) {
// Checks if the transfer market is still open
if (!open) {
ButtonText = t("Transfer Market is closed");
// Checks if the user is already purchasing the player
} else if (ownership.filter((e) => e.buyer === user).length > 0) {
ButtonText = t("Edit purchase");
ButtonColor = "success";
Actions = (
<>
{BuyText}
>
);
// Checks if the user is already selling the player
} else if (ownership.filter((e) => e.seller === user).length > 0) {
ButtonText = t("Edit sale");
ButtonColor = "error";
Actions = (
<>
{SellText}
>
);
// Checks if ther user is out of transfers
} else if (!transferLeft) {
ButtonText = t("View transfers");
// Checks if the user owns the player and can sell the player
} else if (ownership.filter((e) => e.owner === user).length > 0) {
ButtonText = t("Sell");
ButtonColor = "error";
Actions = <>{SellText}>;
// Checks if the player is still purchasable
} else if (
duplicatePlayers <= ownership.filter((e) => !e.transfer).length
) {
ButtonText = t("View transfers");
// Checks if the player can be bought from the market
} else if (duplicatePlayers > ownership.length) {
if (data.value > money) {
ButtonText = t("View transfers");
} else {
ButtonText = t("Buy");
ButtonColor = "success";
Actions = BuyText;
}
// Finds the cheapest market price and sets the purchase for that
} else {
let bestDeal = ownership
.filter((e) => e.transfer)
.sort((a, b) => a.amount - b.amount)[0];
let cheapestPrice = bestDeal.amount + 100000;
if (cheapestPrice > money) {
ButtonText = t("View transfers");
} else {
ButtonText = t("Buy");
ButtonColor = "success";
Actions = BuyText;
}
}
} else {
// If no ownership data exists the player must not be owned by anyone
if (!open) {
ButtonText = t("Transfer Market is closed");
} else if (!transferLeft) {
ButtonText = t("View transfers");
// Checks if the user owns the player and can sell the player
} else if (data.sale_price > money) {
ButtonText = t("View transfers");
} else {
ButtonText = t("Buy");
ButtonColor = "success";
Actions = BuyText;
}
}
// Checks if the player should even be shown
if (
Object.keys(data).length != 0 &&
!showHidden &&
!data.exists &&
(!ownership ||
ownership.filter((x) =>
x.transfer ? x.buyer == user || x.seller == user : x.owner == user,
).length == 0)
) {
return <>>;
}
return (
);
}
/**
* Renders a player component with the buttons for squad.
*
* @param {Object} props - The properties object for the component.
* @param {string} props.uid - The unique ID of the player.
* @param {function} props.update - The function to run whenever the squad is updated.
* @param {undefined | {att: boolean, mid: boolean, def: boolean, gk: boolean}} props.field - The field property of the player.
* @param {number} props.league - The league id of the user who has that player.
* @param {undefined | boolean} props.starred - If the player is starred.
* @param {string} props.status - Special status of the player like buy/sell.
* @param {string} props.leagueType - The league type property of the player.
* @param {boolean} props.transferOpen - If the transfer market is open
* @param {boolean} [props.hideButton] - If the buttons should be shown
*/
export function SquadPlayer({
uid,
update,
field,
league,
starred,
status,
leagueType,
transferOpen,
hideButton,
}) {
const t = useContext(TranslateContext);
const notify = useContext(NotifyContext);
const [data, setData] = useState({});
// Used to get the data for the player
useEffect(() => {
async function getData() {
setData(await (await fetch(`/api/player/${leagueType}/${uid}`)).json());
}
getData();
}, [uid, leagueType]);
// Checks if the player is on the bench or not
let MoveButton = "";
let disabled = false;
let extraText;
let Buttons = <>>;
if (!hideButton) {
if (field === undefined) {
MoveButton = t("Move to bench");
} else {
disabled = !field[data.position];
MoveButton = !disabled ? t("Move to field") : t("Position is full");
if (data.locked) {
disabled = true;
MoveButton = t("Player has already played");
}
}
Buttons = (
<>
{(starred === false || starred === 0) && !data.locked && (
)}
>
);
}
if (status == "buy") {
extraText = transferOpen ? t("Buying") : t("Buying next transfer period");
} else if (status == "sell") {
extraText = transferOpen ? t("Selling") : t("Selling next transfer period");
}
return (
{Buttons}
);
}
// Used to show the player without any buttons
export function Player({ uid, children, starred, leagueType }) {
const [data, setData] = useState({});
// Used to get the data for the player
useEffect(() => {
async function getData() {
setData(await (await fetch(`/api/player/${leagueType}/${uid}`)).json());
}
getData();
}, [uid, leagueType]);
return (
{" "}
{children}
);
}
// Used to show the player with historical data
export function HistoricalPlayer({ uid, time, children, starred, leagueType }) {
const [data, setData] = useState({});
// Used to get the data for the player
useEffect(() => {
async function getData() {
setData(
await (
await fetch(`/api/player/${leagueType}/${uid}?time=${time}`)
).json(),
);
}
getData();
}, [uid, time, leagueType]);
return (
{children}
);
}