fix: league jumping for current season

This commit is contained in:
TrèsAbhi committed 2023-11-15 16:42:52 -06:00
1 parent 20da294b36
commit 8f1017195f
1 file changed
+56 -27
+56 -27
View File
@@ -17,6 +17,7 @@ import { ChangeEvent, useEffect, useRef, useState } from 'react';
import { create } from 'zustand'; import { create } from 'zustand';
import { RatingsInfo, RatingsPlayer } from '../../../commands/ratings'; import { RatingsInfo, RatingsPlayer } from '../../../commands/ratings';
import PageWrapper from '../../../components/PageWrapper'; import PageWrapper from '../../../components/PageWrapper';
import { Skeleton } from '../../../components/Skeleton';
import { LEAGUES } from '../../../constants/leagues'; import { LEAGUES } from '../../../constants/leagues';
import { FIRST_ARCHIVED_RATINGS_SEASON } from '../../../constants/ratings'; import { FIRST_ARCHIVED_RATINGS_SEASON } from '../../../constants/ratings';
import { REGIONS, REGION_NAMES, Region } from '../../../constants/regions'; import { REGIONS, REGION_NAMES, Region } from '../../../constants/regions';
@@ -42,7 +43,7 @@ const SEEDING_SIZE = 2 ** 7;
interface NameCacheEntry { interface NameCacheEntry {
nickname: string; nickname: string;
clan?: string; clan: string | null;
} }
type NameCache = Record<Region, Record<number, NameCacheEntry | null>>; type NameCache = Record<Region, Record<number, NameCacheEntry | null>>;
type LeaderboardCache = Record<Region, Record<number, Record<number, number>>>; type LeaderboardCache = Record<Region, Record<number, Record<number, number>>>;
@@ -99,7 +100,11 @@ export default function Page() {
const positionInput = useRef<HTMLInputElement>(null); const positionInput = useRef<HTMLInputElement>(null);
const scoreInput = useRef<HTMLInputElement>(null); const scoreInput = useRef<HTMLInputElement>(null);
const leaguePositionCache = useRef<Record<number, number>>({}); const leaguePositionCache = useRef<Record<Region, Record<number, number>>>({
asia: {},
com: {},
eu: {},
});
const pages = Math.ceil( const pages = Math.ceil(
(ratingsInfo && ratingsInfo.detail === undefined ? ratingsInfo?.count : 1) / (ratingsInfo && ratingsInfo.detail === undefined ? ratingsInfo?.count : 1) /
@@ -122,7 +127,7 @@ export default function Page() {
); );
})(); })();
}, [season, region]); }, [season, region]);
// initial leagues caching for current season // initial caching
useEffect(() => { useEffect(() => {
if (season in leaderboard[region]) return; if (season in leaderboard[region]) return;
@@ -130,8 +135,12 @@ export default function Page() {
(async () => { (async () => {
const results = ( const results = (
await Promise.all( await Promise.all(
range(0, 5).map((league: number) => { range(0, 5).map(async (league: number) => {
return getRatingsLeague(region, league); const data = await getRatingsLeague(region, league);
leaguePositionCache.current[region][league] =
data.result[0].number - 1;
return data;
}), }),
) )
) )
@@ -242,7 +251,7 @@ export default function Page() {
draft[region][ids[index]] = player draft[region][ids[index]] = player
? { ? {
nickname: player.account_name, nickname: player.account_name,
clan: player.clan?.tag, clan: player.clan?.tag ?? null,
} }
: null; : null;
}), }),
@@ -270,8 +279,6 @@ export default function Page() {
id: number, id: number,
size: number, size: number,
): Promise<RatingsPlayer[]> { ): Promise<RatingsPlayer[]> {
console.log(id, 'working');
const radius = Math.round(size / 2); const radius = Math.round(size / 2);
const targetPosition = Object.entries(leaderboard[region][season]).find( const targetPosition = Object.entries(leaderboard[region][season]).find(
([, player]) => id === player, ([, player]) => id === player,
@@ -291,16 +298,17 @@ export default function Page() {
} }
if (!isMissing) { if (!isMissing) {
console.log(id, 'skipping');
return []; return [];
} }
} }
const seedingPlayerPosition = parseInt( const seedingPlayerNeighborEntry = Object.entries(
Object.entries(leaderboard[region][season]).find( leaderboard[region][season],
([, player]) => id === player, ).find(([, player]) => id === player);
)![0],
); if (seedingPlayerNeighborEntry === undefined) return [];
const seedingPlayerPosition = parseInt(seedingPlayerNeighborEntry[0]);
const { neighbors } = await getRatingsNeighbors(region, id, size, true); const { neighbors } = await getRatingsNeighbors(region, id, size, true);
useLeaderboardCache.setState( useLeaderboardCache.setState(
@@ -316,7 +324,7 @@ export default function Page() {
); );
useScoreCache.setState( useScoreCache.setState(
produce((draft: ScoreCache) => { produce((draft: ScoreCache) => {
neighbors.forEach((neighbor, neighborIndex) => { neighbors.forEach((neighbor) => {
if (!(neighbor.spa_id in draft[region][season])) { if (!(neighbor.spa_id in draft[region][season])) {
draft[region][season][neighbor.spa_id] = neighbor.score; draft[region][season][neighbor.spa_id] = neighbor.score;
} }
@@ -336,8 +344,6 @@ export default function Page() {
}), }),
); );
console.log(id, 'cached');
return neighbors; return neighbors;
} }
async function jumpToPosition(position: number) { async function jumpToPosition(position: number) {
@@ -667,7 +673,7 @@ export default function Page() {
onClick={async () => { onClick={async () => {
if (season === 0) { if (season === 0) {
const position = const position =
leaguePositionCache.current[jumpToLeague]; leaguePositionCache.current[region][jumpToLeague];
const newPage = Math.floor(position / ROWS_PER_PAGE); const newPage = Math.floor(position / ROWS_PER_PAGE);
setPage(newPage); setPage(newPage);
@@ -985,10 +991,28 @@ export default function Page() {
> >
<Table.Cell>{position.toLocaleString()}.</Table.Cell> <Table.Cell>{position.toLocaleString()}.</Table.Cell>
<Table.Cell> <Table.Cell>
{names[region][id] === null <Flex gap="2">
? `Deleted player ${id}` {names[region][id] === null
: names[region][id]?.nickname ?? `Loading player...`} ? `Deleted player ${id}`
<Text color="gray">{clan ? ` [${clan}]` : ''}</Text> : names[region][id]?.nickname ?? (
<Skeleton
style={{
height: 16,
width: Math.random() * 128 + 128,
}}
/>
)}
{clan ? (
<Text color="gray">{`[${clan}]`}</Text>
) : (
clan === undefined &&
names[region][id] !== null && (
<Skeleton
style={{ height: 16, width: Math.random() * 16 + 32 }}
/>
)
)}
</Flex>
</Table.Cell> </Table.Cell>
{season === 0 && ( {season === 0 && (
<Table.Cell align="center" justify="center"> <Table.Cell align="center" justify="center">
@@ -1019,15 +1043,20 @@ export default function Page() {
</Table.Cell> </Table.Cell>
)} )}
<Table.Cell align="right" justify="center"> <Table.Cell align="right" justify="center">
{totalPlayers {totalPlayers ? (
? Math.ceil(((index + 1) / totalPlayers) * 100) `${Math.ceil(((index + 1) / totalPlayers) * 100)}%`
: '--'} ) : (
% <Skeleton style={{ height: 16, width: 24 }} />
)}
</Table.Cell> </Table.Cell>
<Table.Cell align="right"> <Table.Cell align="right">
{(leaderboard[region][season]?.[index] {(leaderboard[region][season]?.[index]
? scores[leaderboard[region][season][index]]?.toLocaleString() ? scores[leaderboard[region][season][index]]?.toLocaleString()
: undefined) ?? '--'} : undefined) ?? (
<Skeleton
style={{ height: 16, width: Math.random() * 4 + 32 }}
/>
)}
</Table.Cell> </Table.Cell>
</Table.Row> </Table.Row>
); );