Files
blitzkit/packages/website/src/pages/tools/session/_index.tsx
T

446 lines
13 KiB
TypeScript
Raw Normal View History

2024-09-07 21:45:00 -05:00
import {
2024-09-08 10:15:42 -05:00
STAT_KEYS,
STAT_NAMES,
2024-09-08 10:17:40 -05:00
UNLOCALIZED_REGION_NAMES_SHORT,
deltaTankStats,
2024-10-07 17:39:20 -05:00
fetchAverageDefinitions,
2024-10-05 23:20:04 -05:00
fetchTankDefinitions,
2024-09-08 10:15:42 -05:00
generateStats,
getAccountInfo,
2024-09-08 10:17:40 -05:00
getTankStats,
idToRegion,
prettifyStats,
searchPlayersAcrossRegions,
sumBlitzStarsStats,
2024-10-07 17:39:20 -05:00
type AccountListWithServer,
type IndividualAccountInfo,
type IndividualTankStats,
type Stat,
2024-09-08 10:17:40 -05:00
} from '@blitzkit/core';
2024-05-03 09:02:06 -05:00
import {
ArrowDownIcon,
MagnifyingGlassIcon,
PlusIcon,
} from '@radix-ui/react-icons';
2023-10-01 16:37:51 -05:00
import {
AlertDialog,
Button,
2024-05-03 09:02:06 -05:00
DropdownMenu,
2023-10-01 16:37:51 -05:00
Flex,
2024-05-01 17:20:08 -05:00
Heading,
2024-05-03 09:02:06 -05:00
IconButton,
Select,
2024-05-01 17:20:08 -05:00
Table,
2023-10-01 16:37:51 -05:00
Text,
TextField,
} from '@radix-ui/themes';
2024-09-14 19:43:14 -05:00
import { debounce } from 'lodash-es';
2024-10-07 17:39:20 -05:00
import { useEffect, useMemo, useRef, useState } from 'react';
import usePromise from 'react-promise-suspense';
import { PageWrapper } from '../../../components/PageWrapper';
import { TankRowHeaderCell } from '../../../components/TankRowHeaderCell';
import { Session, type SessionTracking } from '../../../stores/session';
2024-10-05 23:20:04 -05:00
2024-10-07 17:39:20 -05:00
const tankDefinitions = await fetchTankDefinitions();
const tankAverages = await fetchAverageDefinitions();
2023-09-18 22:32:21 +00:00
2024-10-07 17:39:20 -05:00
export function Page() {
return (
<Session.Provider>
<Content />
</Session.Provider>
);
}
function Content() {
2024-04-28 18:43:12 -05:00
const [showSearch, setShowSearch] = useState(false);
const [searching, setSearching] = useState(false);
const [searchResults, setSearchResults] = useState<AccountListWithServer>([]);
2024-05-02 18:27:27 -05:00
const [showCCInaccessibilityPrompt, setShowCCInaccessibilityPrompt] =
useState(false);
2023-09-18 23:05:19 +00:00
const input = useRef<HTMLInputElement>(null);
2024-07-09 19:42:57 -05:00
const session = Session.use();
const mutateSession = Session.useMutation();
2024-05-01 17:20:08 -05:00
const [tankStatsB, setTankStatsB] = useState<IndividualTankStats[] | null>(
null,
);
2024-10-07 17:39:20 -05:00
const accountInfo = usePromise(
(id?: number) =>
2024-05-01 17:20:08 -05:00
new Promise<IndividualAccountInfo | null>(async (resolve) => {
2024-10-07 17:39:20 -05:00
if (!id) return resolve(null);
2024-05-01 17:20:08 -05:00
resolve(
2024-10-07 17:39:20 -05:00
session.tracking ? await getAccountInfo(idToRegion(id), id) : null,
2024-05-01 17:20:08 -05:00
);
}),
2024-10-07 17:39:20 -05:00
[session.tracking ? session.player.id : undefined],
2024-05-01 17:20:08 -05:00
);
2024-05-01 20:54:27 -05:00
const delta = useMemo(
() =>
session.tracking && tankStatsB
2024-05-03 09:19:29 -05:00
? deltaTankStats(session.player.stats, tankStatsB)
2024-05-01 22:11:12 -05:00
.sort((a, b) => b.last_battle_time - a.last_battle_time)
.map((entry) => {
2024-10-07 17:39:20 -05:00
const tank = tankDefinitions.tanks[entry.tank_id];
const average = tankAverages.averages[tank.id];
const stats = generateStats(entry.all, average.mu);
2024-05-01 22:11:12 -05:00
const statsPretty = prettifyStats(stats);
return {
tank,
stats,
statsPretty,
};
})
2024-05-01 20:54:27 -05:00
: undefined,
[session.tracking && session.player, tankStatsB],
);
2024-05-01 22:11:12 -05:00
const total = useMemo(
() =>
delta
2024-09-08 10:15:42 -05:00
? prettifyStats(sumBlitzStarsStats(delta.map(({ stats }) => stats)))
2024-05-01 22:11:12 -05:00
: undefined,
[delta],
);
2024-05-23 14:05:25 -05:00
const [showLinkOverridePrompt, setShowLinkOverridePrompt] = useState(true);
2024-04-28 18:43:12 -05:00
const search = debounce(async () => {
if (!input.current) return;
2024-07-22 20:59:38 -05:00
setSearchResults(await searchPlayersAcrossRegions(input.current.value, 15));
2024-04-28 18:43:12 -05:00
setSearching(false);
}, 500);
2023-09-23 21:51:00 +00:00
2024-05-23 14:05:25 -05:00
async function track(id: number) {
const region = idToRegion(id);
const player = await getAccountInfo(region, id);
const stats = await getTankStats(region, id);
if (stats === null) {
setShowCCInaccessibilityPrompt(true);
return;
}
setShowSearch(false);
mutateSession((draft) => {
if (!input.current) return;
draft.tracking = true;
2024-10-07 17:39:20 -05:00
(draft as SessionTracking).player = {
2024-05-23 14:05:25 -05:00
id,
region,
since: Date.now(),
stats,
};
input.current.value = player.nickname;
});
}
2023-10-14 12:04:11 -05:00
useEffect(() => {
2024-05-02 18:27:27 -05:00
async function update() {
if (!session.tracking) return;
2024-04-28 18:43:12 -05:00
2024-05-02 18:27:27 -05:00
const tankStats = await getTankStats(
idToRegion(session.player.id),
2024-04-28 19:16:08 -05:00
session.player.id,
);
2024-05-02 18:27:27 -05:00
setTankStatsB(tankStats);
2024-05-01 17:20:08 -05:00
}
2024-05-02 18:27:27 -05:00
update();
const interval = setInterval(update, 5 * 1000);
return () => clearInterval(interval);
2024-04-28 18:43:12 -05:00
}, [session.tracking && session.player.id]);
2023-10-01 16:37:51 -05:00
2023-09-18 22:32:21 +00:00
return (
2024-06-03 17:45:50 -05:00
<PageWrapper color="blue">
2024-05-02 18:27:27 -05:00
<AlertDialog.Root
open={showCCInaccessibilityPrompt}
onOpenChange={setShowCCInaccessibilityPrompt}
>
<AlertDialog.Content>
<AlertDialog.Title>
You're attempting to track a CC account
</AlertDialog.Title>
<AlertDialog.Description>
CC (community contributor) accounts do not have public tank
2024-05-03 09:10:23 -05:00
statistics. We are in the process of adding embeds for CC accounts,
stay tuned.
2024-05-02 18:27:27 -05:00
</AlertDialog.Description>
2024-04-01 18:36:14 -05:00
2024-05-02 18:27:27 -05:00
<Flex mt="4" justify="end">
<AlertDialog.Action>
<Button variant="solid">Alright</Button>
</AlertDialog.Action>
</Flex>
</AlertDialog.Content>
</AlertDialog.Root>
2024-04-28 18:43:12 -05:00
2024-05-01 17:20:08 -05:00
{!session.tracking && (
<Flex
style={{ flex: 1 }}
align="center"
justify="center"
direction="column"
gap="3"
>
<Flex gap="2" align="center">
<Text color="gray">Look up a player to get started</Text>
<ArrowDownIcon />
</Flex>
2024-04-28 18:43:12 -05:00
2024-05-01 17:20:08 -05:00
<TextField.Root
size="3"
2024-04-28 18:43:12 -05:00
style={{
2024-05-01 17:20:08 -05:00
position: 'relative',
width: '75vw',
maxWidth: 480,
}}
ref={input}
placeholder="Search players..."
onChange={() => {
if (!input.current) return;
const sanitized = input.current.value.trim();
if (sanitized.length > 0) {
setShowSearch(true);
setSearching(true);
search();
} else {
setShowSearch(false);
}
2024-04-28 18:43:12 -05:00
}}
>
2024-05-01 17:20:08 -05:00
<TextField.Slot>
<MagnifyingGlassIcon />
</TextField.Slot>
</TextField.Root>
2024-07-22 20:59:38 -05:00
{showSearch && (
<Flex mt="2">
{searching && (
<Flex justify="center">
<Text color="gray">Searching...</Text>
</Flex>
)}
{!searching && searchResults.length === 0 && (
<Flex justify="center">
<Text color="gray">No players found</Text>
</Flex>
)}
{!searching && searchResults.length > 0 && (
<Flex direction="column" gap="2">
{searchResults.map((player) => (
<Button
key={player.account_id}
variant="ghost"
onClick={() => track(player.account_id)}
>
{player.nickname} (
{UNLOCALIZED_REGION_NAMES_SHORT[player.region]})
</Button>
))}
</Flex>
)}
</Flex>
)}
2024-05-01 17:20:08 -05:00
</Flex>
)}
2024-04-28 18:43:12 -05:00
2024-05-01 17:20:08 -05:00
{session.tracking && (
2024-05-02 18:27:27 -05:00
<Flex
mt="2"
mb="2"
2024-07-09 20:53:00 -05:00
direction={{ initial: 'column', sm: 'row' }}
2024-05-02 18:27:27 -05:00
align="center"
justify="between"
gap="2"
>
<Flex mt="2" mb="2" direction="column" gap="2">
<Heading size="5">Tracking {accountInfo?.nickname}</Heading>
<Text color="gray">
Since {new Date(session.player.since).toLocaleString()}
</Text>
</Flex>
2024-05-01 17:29:40 -05:00
2024-05-02 18:27:27 -05:00
<Flex gap="2" align="center" justify="center">
2024-05-01 17:23:33 -05:00
<Button
color="red"
2024-05-02 18:27:27 -05:00
onClick={async () => {
const stats = await getTankStats(
session.player.region,
session.player.id,
);
if (stats === null) {
setShowCCInaccessibilityPrompt(true);
return;
}
mutateSession((draft) => {
2024-10-07 17:39:20 -05:00
(draft as SessionTracking).player.stats = stats;
2024-05-02 18:27:27 -05:00
});
}}
>
Reset
</Button>
<Button
2024-05-01 17:23:33 -05:00
onClick={() => {
mutateSession((draft) => {
draft.tracking = false;
});
}}
>
2024-05-02 18:27:27 -05:00
Change
2024-05-01 17:29:40 -05:00
</Button>
2024-05-01 17:23:33 -05:00
</Flex>
2024-05-01 17:20:08 -05:00
</Flex>
)}
2024-04-28 18:43:12 -05:00
2024-05-01 20:54:27 -05:00
{session.tracking && delta && delta.length > 0 && (
2024-05-01 17:20:08 -05:00
<Table.Root variant="surface">
<Table.Header>
<Table.Row>
<Table.ColumnHeaderCell>Tank</Table.ColumnHeaderCell>
2024-05-03 09:02:06 -05:00
{session.columns.map((column, index) => (
<Table.ColumnHeaderCell
width="0"
key={column}
style={{
position: 'relative',
}}
>
{index === 0 && session.columns.length < 4 && (
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<IconButton
size="1"
variant="surface"
style={{
position: 'absolute',
left: 0,
top: '50%',
transform: 'translate(-100%, -50%)',
zIndex: 1,
}}
>
<PlusIcon />
</IconButton>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
{STAT_KEYS.filter(
(item) => !session.columns.includes(item),
).map((key) => (
<DropdownMenu.Item
key={key}
onClick={() => {
mutateSession((draft) => {
2024-10-07 17:39:20 -05:00
(draft as SessionTracking).columns.unshift(
key as Stat,
);
2024-05-03 09:02:06 -05:00
});
}}
>
{STAT_NAMES[key]}
</DropdownMenu.Item>
))}
</DropdownMenu.Content>
</DropdownMenu.Root>
)}
<Select.Root
value={column}
onValueChange={(value) => {
mutateSession((draft) => {
if (value === 'remove') {
2024-10-07 17:39:20 -05:00
(draft as SessionTracking).columns.splice(index, 1);
2024-05-03 09:02:06 -05:00
} else {
2024-10-07 17:39:20 -05:00
(draft as SessionTracking).columns[index] =
2024-05-03 09:02:06 -05:00
value as Stat;
}
});
}}
>
<Select.Trigger variant="ghost" />
<Select.Content>
{STAT_KEYS.filter(
(item) =>
!session.columns.includes(item) || item === column,
).map((key) => (
<Select.Item key={key} value={key}>
{STAT_NAMES[key]}
</Select.Item>
))}
<Select.Separator />
<Select.Item value="remove">Remove</Select.Item>
</Select.Content>
</Select.Root>
2024-05-01 17:55:13 -05:00
</Table.ColumnHeaderCell>
))}
2024-05-01 17:20:08 -05:00
</Table.Row>
</Table.Header>
2024-04-28 18:43:12 -05:00
2024-05-01 17:20:08 -05:00
<Table.Body>
2024-05-01 22:11:12 -05:00
<Table.Row
style={{
overflow: 'hidden',
}}
>
<Table.RowHeaderCell
style={{
paddingLeft: 32,
position: 'relative',
overflowY: 'hidden',
}}
>
Total
</Table.RowHeaderCell>
{session.columns.map((column) => (
2024-05-03 09:02:06 -05:00
<Table.Cell key={column}>{total![column]}</Table.Cell>
2024-05-01 22:11:12 -05:00
))}
</Table.Row>
2024-04-28 18:43:12 -05:00
2024-05-01 22:11:12 -05:00
{delta.map(({ statsPretty, tank }) => {
2024-05-01 20:54:27 -05:00
return (
<Table.Row
key={tank.id}
style={{
overflow: 'hidden',
}}
>
2024-06-29 21:22:12 -05:00
<TankRowHeaderCell tank={tank} />
2024-05-01 20:54:27 -05:00
{session.columns.map((column) => (
2024-05-03 09:02:06 -05:00
<Table.Cell key={column}>{statsPretty[column]}</Table.Cell>
2024-05-01 20:54:27 -05:00
))}
</Table.Row>
);
})}
2024-05-01 17:20:08 -05:00
</Table.Body>
</Table.Root>
)}
2024-05-01 20:54:27 -05:00
{session.tracking && delta && delta.length === 0 && (
2024-05-01 17:20:08 -05:00
<Flex
align="center"
justify="center"
direction="column"
style={{
flex: 1,
}}
>
<Heading color="gray">No battles</Heading>
<Text color="gray">Go ahead and play a game</Text>
</Flex>
)}
2023-09-18 22:32:21 +00:00
</PageWrapper>
);
2023-09-18 21:55:40 +00:00
}