feat: gender-separated MVPChart
in mixed teams
This commit is contained in:
@@ -6,12 +6,13 @@ import { useSession } from "./Session";
|
||||
import { useNavigate } from "react-router";
|
||||
|
||||
const MVPChart = () => {
|
||||
let initialData = {} as PlayerRanking[];
|
||||
let initialData = {} as PlayerRanking[][];
|
||||
const [data, setData] = useState(initialData);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState("");
|
||||
const [showStd, setShowStd] = useState(false);
|
||||
const { user, teams } = useSession();
|
||||
const [mixed, setMixed] = useState(false);
|
||||
const navigate = useNavigate();
|
||||
useEffect(() => {
|
||||
user?.scopes.includes(`team:${teams?.activeTeam}`) ||
|
||||
@@ -19,21 +20,30 @@ const MVPChart = () => {
|
||||
navigate("/", { replace: true });
|
||||
}, [user]);
|
||||
|
||||
useEffect(() => {
|
||||
if (teams) {
|
||||
const activeTeam = teams.teams.find(
|
||||
(team) => team.id == teams.activeTeam
|
||||
);
|
||||
activeTeam && setMixed(activeTeam.mixed);
|
||||
}
|
||||
}, [teams]);
|
||||
|
||||
async function loadData() {
|
||||
setLoading(true);
|
||||
if (teams) {
|
||||
await apiAuth(`analysis/mvp/${teams?.activeTeam}`, null)
|
||||
await apiAuth(`analysis/mvp/${teams?.activeTeam}?mixed=${mixed}`, null)
|
||||
.then((data) => {
|
||||
if (data.detail) {
|
||||
setError(data.detail);
|
||||
return initialData;
|
||||
} else {
|
||||
setError("");
|
||||
return data as Promise<PlayerRanking[]>;
|
||||
return data as Promise<PlayerRanking[][]>;
|
||||
}
|
||||
})
|
||||
.then((data) => {
|
||||
setData(data.sort((a, b) => a.rank - b.rank));
|
||||
setData(data.map((_data) => _data.sort((a, b) => a.rank - b.rank)));
|
||||
})
|
||||
.catch(() => setError("no access"));
|
||||
setLoading(false);
|
||||
@@ -46,7 +56,8 @@ const MVPChart = () => {
|
||||
|
||||
if (loading) return <span className="loader" />;
|
||||
else if (error) return <span>{error}</span>;
|
||||
else return <RaceChart std={showStd} players={data} />;
|
||||
else
|
||||
return data.map((_data) => <RaceChart std={showStd} playerRanks={_data} />);
|
||||
};
|
||||
|
||||
export default MVPChart;
|
||||
|
@@ -1,8 +1,9 @@
|
||||
import { FC, useEffect, useState } from "react";
|
||||
import { PlayerRanking } from "./types";
|
||||
import { useSession } from "./Session";
|
||||
|
||||
interface RaceChartProps {
|
||||
players: PlayerRanking[];
|
||||
playerRanks: PlayerRanking[];
|
||||
std: boolean;
|
||||
}
|
||||
|
||||
@@ -13,15 +14,14 @@ const determineNiceWidth = (width: number) => {
|
||||
else return width * 0.96;
|
||||
};
|
||||
|
||||
const RaceChart: FC<RaceChartProps> = ({ players, std }) => {
|
||||
const RaceChart: FC<RaceChartProps> = ({ playerRanks, std }) => {
|
||||
const [width, setWidth] = useState(determineNiceWidth(window.innerWidth));
|
||||
//const [height, setHeight] = useState(window.innerHeight);
|
||||
const height = (players.length + 1) * 40;
|
||||
const height = (playerRanks.length + 1) * 40;
|
||||
const { players } = useSession();
|
||||
|
||||
useEffect(() => {
|
||||
const handleResize = () => {
|
||||
setWidth(determineNiceWidth(window.innerWidth));
|
||||
//setHeight(window.innerHeight);
|
||||
};
|
||||
window.addEventListener("resize", handleResize);
|
||||
return () => {
|
||||
@@ -30,18 +30,18 @@ const RaceChart: FC<RaceChartProps> = ({ players, std }) => {
|
||||
}, []);
|
||||
const padding = 24;
|
||||
const gap = 8;
|
||||
const maxValue = Math.max(...players.map((player) => player.rank)) + 1;
|
||||
const barHeight = (height - 2 * padding) / players.length;
|
||||
const maxValue = Math.max(...playerRanks.map((player) => player.rank)) + 1;
|
||||
const barHeight = (height - 2 * padding) / playerRanks.length;
|
||||
const fontSize = Math.min(barHeight - 1.5 * gap, width / 22);
|
||||
|
||||
return (
|
||||
<svg width={width} height={height} id="RaceChartSVG">
|
||||
{players.map((player, index) => (
|
||||
{playerRanks.map((playerRank, index) => (
|
||||
<rect
|
||||
key={String(index)}
|
||||
x={4}
|
||||
y={index * barHeight + padding}
|
||||
width={(1 - player.rank / maxValue) * width}
|
||||
width={(1 - playerRank.rank / maxValue) * width}
|
||||
height={barHeight - gap} // subtract 2 for some spacing between bars
|
||||
fill="#36c"
|
||||
stroke="aliceblue"
|
||||
@@ -50,49 +50,53 @@ const RaceChart: FC<RaceChartProps> = ({ players, std }) => {
|
||||
/>
|
||||
))}
|
||||
|
||||
{players.map((player, index) => (
|
||||
<g key={"group" + index}>
|
||||
<text
|
||||
key={index + "_name"}
|
||||
x={8}
|
||||
y={index * barHeight + barHeight / 2 + padding + gap / 2}
|
||||
width={(1 - player.rank / maxValue) * width}
|
||||
height={barHeight - 8} // subtract 2 for some spacing between bars
|
||||
fontSize={fontSize}
|
||||
fill="aliceblue"
|
||||
stroke="#36c"
|
||||
strokeWidth={4}
|
||||
fontWeight={"bold"}
|
||||
paintOrder={"stroke fill"}
|
||||
fontFamily="monospace"
|
||||
style={{ whiteSpace: "pre" }}
|
||||
>
|
||||
{`${String(index + 1).padStart(2)}. ${player.name}`}
|
||||
</text>
|
||||
<text
|
||||
key={index + "_value"}
|
||||
x={
|
||||
8 +
|
||||
(4 + Math.max(...players.map((p, _) => p.name.length))) *
|
||||
fontSize *
|
||||
0.66
|
||||
}
|
||||
y={index * barHeight + barHeight / 2 + padding + gap / 2}
|
||||
width={(1 - player.rank / maxValue) * width}
|
||||
height={barHeight - 8} // subtract 2 for some spacing between bars
|
||||
fontSize={0.8 * fontSize}
|
||||
fill="aliceblue"
|
||||
stroke="#36c"
|
||||
fontWeight={"bold"}
|
||||
fontFamily="monospace"
|
||||
strokeWidth={4}
|
||||
paintOrder={"stroke fill"}
|
||||
style={{ whiteSpace: "pre" }}
|
||||
>
|
||||
{`${String(player.rank).padStart(5)} ± ${player.std} N = ${player.n}`}
|
||||
</text>
|
||||
</g>
|
||||
))}
|
||||
{playerRanks.map((playerRank, index) => {
|
||||
const player = players!.find((p) => p.id === playerRank.p_id);
|
||||
return (
|
||||
<g key={"group" + index}>
|
||||
<text
|
||||
key={index + "_name"}
|
||||
x={8}
|
||||
y={index * barHeight + barHeight / 2 + padding + gap / 2}
|
||||
width={(1 - playerRank.rank / maxValue) * width}
|
||||
height={barHeight - 8} // subtract 2 for some spacing between bars
|
||||
fontSize={fontSize}
|
||||
fill="aliceblue"
|
||||
stroke="#36c"
|
||||
strokeWidth={4}
|
||||
fontWeight={"bold"}
|
||||
paintOrder={"stroke fill"}
|
||||
fontFamily="monospace"
|
||||
style={{ whiteSpace: "pre" }}
|
||||
>
|
||||
{`${String(index + 1).padStart(2)}. ${player?.display_name}`}
|
||||
</text>
|
||||
<text
|
||||
key={index + "_value"}
|
||||
x={
|
||||
8 +
|
||||
(4 +
|
||||
Math.max(...players!.map((p, _) => p.display_name.length))) *
|
||||
fontSize *
|
||||
0.66
|
||||
}
|
||||
y={index * barHeight + barHeight / 2 + padding + gap / 2}
|
||||
width={(1 - playerRank.rank / maxValue) * width}
|
||||
height={barHeight - 8} // subtract 2 for some spacing between bars
|
||||
fontSize={0.8 * fontSize}
|
||||
fill="aliceblue"
|
||||
stroke="#36c"
|
||||
fontWeight={"bold"}
|
||||
fontFamily="monospace"
|
||||
strokeWidth={4}
|
||||
paintOrder={"stroke fill"}
|
||||
style={{ whiteSpace: "pre" }}
|
||||
>
|
||||
{`${String(playerRank.rank).padStart(5)} ± ${playerRank.std} N = ${playerRank.n}`}
|
||||
</text>
|
||||
</g>
|
||||
);
|
||||
})}
|
||||
</svg>
|
||||
);
|
||||
};
|
||||
|
@@ -13,7 +13,7 @@ export default interface NetworkData {
|
||||
}
|
||||
|
||||
export interface PlayerRanking {
|
||||
name: string;
|
||||
p_id: number;
|
||||
rank: number;
|
||||
std: number;
|
||||
n: number;
|
||||
|
Reference in New Issue
Block a user