2023-09-18 22:32:21 +00:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { CopyIcon, ListBulletIcon, ReloadIcon } from '@radix-ui/react-icons';
|
2023-09-18 23:05:19 +00:00
|
|
|
import { debounce } from 'lodash';
|
2023-09-23 21:51:00 +00:00
|
|
|
import { ChangeEvent, useEffect, useRef, useState } from 'react';
|
|
|
|
|
import * as Breakdown from '../../../components/Breakdown';
|
2023-09-18 22:32:21 +00:00
|
|
|
import { Button } from '../../../components/Button';
|
|
|
|
|
import PageWrapper from '../../../components/PageWrapper';
|
|
|
|
|
import { SearchBar } from '../../../components/SearchBar';
|
2023-09-24 15:58:42 -05:00
|
|
|
import { TreeTypeEnum } from '../../../components/Tanks';
|
2023-09-18 23:25:24 +00:00
|
|
|
import { REGION_NAMES } from '../../../constants/regions';
|
2023-09-23 19:20:29 -05:00
|
|
|
import { WARGAMING_APPLICATION_ID } from '../../../constants/wargamingApplicationID';
|
2023-09-24 15:58:42 -05:00
|
|
|
import calculateWN8 from '../../../core/blitz/calculateWN8';
|
2023-09-23 21:51:00 +00:00
|
|
|
import { diffNormalizedTankStats } from '../../../core/blitz/diffNormalizedTankStats';
|
2023-09-24 15:58:42 -05:00
|
|
|
import getWN8Percentile from '../../../core/blitz/getWN8Percentile';
|
2023-09-23 19:20:29 -05:00
|
|
|
import getWargamingResponse from '../../../core/blitz/getWargamingResponse';
|
|
|
|
|
import listPlayers, {
|
|
|
|
|
AccountListWithServer,
|
|
|
|
|
} from '../../../core/blitz/listPlayers';
|
2023-09-24 15:58:42 -05:00
|
|
|
import { TankopediaEntry, tankopedia } from '../../../core/blitz/tankopedia';
|
|
|
|
|
import { tankAverages } from '../../../core/blitzstars/tankAverages';
|
2023-09-18 23:25:24 +00:00
|
|
|
import { theme } from '../../../stitches.config';
|
2023-09-19 15:52:39 +00:00
|
|
|
import { useSession } from '../../../stores/session';
|
2023-09-23 21:51:00 +00:00
|
|
|
import {
|
|
|
|
|
IndividualTankStats,
|
|
|
|
|
NormalizedTankStats,
|
2023-09-23 19:20:29 -05:00
|
|
|
TanksStats,
|
2023-09-23 21:51:00 +00:00
|
|
|
} from '../../../types/tanksStats';
|
2023-09-18 22:32:21 +00:00
|
|
|
import * as styles from './page.css';
|
|
|
|
|
|
2023-09-18 21:55:40 +00:00
|
|
|
export default function Page() {
|
2023-09-18 23:05:19 +00:00
|
|
|
const input = useRef<HTMLInputElement>(null);
|
2023-09-18 23:25:24 +00:00
|
|
|
const [searchResults, setSearchResults] = useState<
|
|
|
|
|
AccountListWithServer | undefined
|
|
|
|
|
>(undefined);
|
|
|
|
|
const [showSearchResults, setShowSearchResults] = useState(false);
|
2023-09-18 23:05:19 +00:00
|
|
|
const handleChange = debounce(
|
|
|
|
|
async (event: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
if (event.target.value) {
|
2023-09-23 19:20:29 -05:00
|
|
|
setSearchResults(await listPlayers(event.target.value));
|
2023-09-18 23:05:19 +00:00
|
|
|
} else {
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
500,
|
|
|
|
|
);
|
2023-09-23 21:51:00 +00:00
|
|
|
const session = useSession();
|
2023-09-24 15:58:42 -05:00
|
|
|
const [diff, setDiff] = useState<
|
|
|
|
|
| {
|
|
|
|
|
stats: IndividualTankStats;
|
|
|
|
|
tankopedia?: TankopediaEntry;
|
|
|
|
|
career: IndividualTankStats;
|
|
|
|
|
currentWN8?: number;
|
|
|
|
|
careerWN8?: number;
|
|
|
|
|
}[]
|
|
|
|
|
| undefined
|
|
|
|
|
>(undefined);
|
2023-09-23 21:51:00 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
(async () => {
|
|
|
|
|
if (session.isTracking) {
|
|
|
|
|
const { id, region } = session;
|
2023-09-24 15:58:42 -05:00
|
|
|
const careerRaw = await getWargamingResponse<TanksStats>(
|
2023-09-23 19:20:29 -05:00
|
|
|
`https://api.wotblitz.${region}/wotb/tanks/stats/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
|
|
|
|
|
);
|
2023-09-24 15:58:42 -05:00
|
|
|
const career = careerRaw[id].reduce<
|
2023-09-23 19:20:29 -05:00
|
|
|
Record<number, IndividualTankStats>
|
|
|
|
|
>(
|
|
|
|
|
(accumulator, curr) => ({
|
|
|
|
|
...accumulator,
|
|
|
|
|
[curr.tank_id]: curr,
|
|
|
|
|
}),
|
|
|
|
|
{},
|
|
|
|
|
);
|
2023-09-24 15:58:42 -05:00
|
|
|
const awaitedTankopedia = await tankopedia;
|
|
|
|
|
const awaitedTankAverages = await tankAverages;
|
2023-09-23 21:51:00 +00:00
|
|
|
|
2023-09-24 15:58:42 -05:00
|
|
|
setDiff(
|
|
|
|
|
(
|
|
|
|
|
Object.values(
|
|
|
|
|
diffNormalizedTankStats(session.tankStats, career),
|
|
|
|
|
) as IndividualTankStats[]
|
|
|
|
|
).map((stats) => {
|
|
|
|
|
return {
|
|
|
|
|
stats,
|
|
|
|
|
tankopedia: awaitedTankopedia[stats.tank_id],
|
|
|
|
|
career: career[stats.tank_id],
|
|
|
|
|
currentWN8: awaitedTankAverages[stats.tank_id]
|
|
|
|
|
? calculateWN8(
|
|
|
|
|
awaitedTankAverages[stats.tank_id].all,
|
|
|
|
|
stats.all,
|
|
|
|
|
)
|
|
|
|
|
: undefined,
|
|
|
|
|
careerWN8: awaitedTankAverages[stats.tank_id]
|
|
|
|
|
? calculateWN8(
|
|
|
|
|
awaitedTankAverages[stats.tank_id].all,
|
|
|
|
|
career[stats.tank_id].all,
|
|
|
|
|
)
|
|
|
|
|
: undefined,
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
);
|
2023-09-23 21:51:00 +00:00
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
}, []);
|
2023-09-18 23:05:19 +00:00
|
|
|
|
2023-09-18 22:32:21 +00:00
|
|
|
return (
|
|
|
|
|
<PageWrapper>
|
|
|
|
|
<div className={styles.toolBar}>
|
2023-09-18 23:25:24 +00:00
|
|
|
<div style={{ flex: 1, boxSizing: 'border-box', position: 'relative' }}>
|
|
|
|
|
<SearchBar
|
|
|
|
|
ref={input}
|
|
|
|
|
style={{ width: '100%', boxSizing: 'border-box' }}
|
2023-09-23 21:51:00 +00:00
|
|
|
defaultValue={session.isTracking ? session.nickname : undefined}
|
2023-09-18 23:25:24 +00:00
|
|
|
onChange={(event) => {
|
|
|
|
|
if (event.target.value) {
|
|
|
|
|
setShowSearchResults(true);
|
|
|
|
|
setSearchResults(undefined);
|
|
|
|
|
} else {
|
|
|
|
|
setShowSearchResults(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleChange(event);
|
|
|
|
|
}}
|
|
|
|
|
placeholder="Search for a player..."
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{showSearchResults && (
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
width: '100%',
|
|
|
|
|
top: '100%',
|
|
|
|
|
marginTop: 8,
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
backgroundColor: theme.colors.componentNonInteractive,
|
|
|
|
|
border: theme.borderStyles.nonInteractive,
|
|
|
|
|
borderRadius: 4,
|
|
|
|
|
padding: 8,
|
|
|
|
|
boxSizing: 'border-box',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{searchResults === undefined ? (
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
alignContent: 'center',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
color: theme.colors.textLowContrast,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Searching...
|
|
|
|
|
</div>
|
|
|
|
|
) : searchResults.length === 0 ? (
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
alignContent: 'center',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
color: theme.colors.textLowContrast,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
No results
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
2023-09-19 15:52:39 +00:00
|
|
|
searchResults?.map(({ account_id: id, nickname, region }) => (
|
2023-09-18 23:25:24 +00:00
|
|
|
<button
|
2023-09-19 15:52:39 +00:00
|
|
|
key={id}
|
2023-09-18 23:25:24 +00:00
|
|
|
className={styles.searchButton}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setShowSearchResults(false);
|
2023-09-19 15:52:39 +00:00
|
|
|
input.current!.value = nickname;
|
|
|
|
|
|
|
|
|
|
fetch(
|
|
|
|
|
`/api/stats/normalized/tanks?id=${id}®ion=${region}`,
|
|
|
|
|
)
|
|
|
|
|
.then(
|
|
|
|
|
(response) =>
|
|
|
|
|
response.json() as Promise<NormalizedTankStats>,
|
|
|
|
|
)
|
|
|
|
|
.then((tankStats) =>
|
|
|
|
|
useSession.setState({
|
|
|
|
|
isTracking: true,
|
|
|
|
|
id,
|
|
|
|
|
region,
|
|
|
|
|
nickname,
|
|
|
|
|
tankStats,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2023-09-18 23:25:24 +00:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<span
|
|
|
|
|
style={{
|
|
|
|
|
color: theme.colors.textHighContrast,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{nickname}
|
|
|
|
|
</span>
|
|
|
|
|
<span
|
|
|
|
|
style={{
|
|
|
|
|
color: theme.colors.textLowContrast,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{REGION_NAMES[region]}
|
|
|
|
|
</span>
|
|
|
|
|
</button>
|
|
|
|
|
))
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2023-09-18 22:32:21 +00:00
|
|
|
|
|
|
|
|
<div style={{ display: 'flex', gap: 8 }}>
|
2023-09-19 15:52:39 +00:00
|
|
|
<Button
|
|
|
|
|
className={styles.toolbarButton}
|
|
|
|
|
color="dangerous"
|
|
|
|
|
onClick={async () => {
|
|
|
|
|
const session = useSession.getState();
|
|
|
|
|
|
|
|
|
|
if (!session.isTracking) return;
|
|
|
|
|
|
|
|
|
|
const { id, region } = session;
|
|
|
|
|
const tankStats = await fetch(
|
|
|
|
|
`/api/stats/normalized/tanks?id=${id}®ion=${region}`,
|
|
|
|
|
).then(
|
|
|
|
|
(response) => response.json() as Promise<NormalizedTankStats>,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useSession.setState({ tankStats });
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-09-18 22:32:21 +00:00
|
|
|
<ReloadIcon /> Reset
|
|
|
|
|
</Button>
|
2023-09-18 23:25:24 +00:00
|
|
|
<Button className={styles.toolbarButton}>
|
2023-09-18 22:32:21 +00:00
|
|
|
<CopyIcon /> Embed
|
|
|
|
|
</Button>
|
2023-09-18 23:25:24 +00:00
|
|
|
<Button className={styles.toolbarButton}>
|
2023-09-18 22:32:21 +00:00
|
|
|
<ListBulletIcon /> Options
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2023-09-23 21:51:00 +00:00
|
|
|
|
|
|
|
|
<Breakdown.Root>
|
|
|
|
|
{diff &&
|
2023-09-24 15:58:42 -05:00
|
|
|
diff
|
|
|
|
|
.sort((a, b) => b.stats.last_battle_time - a.stats.last_battle_time)
|
|
|
|
|
.map(({ stats, tankopedia, career, careerWN8, currentWN8 }) => (
|
|
|
|
|
<Breakdown.Row
|
|
|
|
|
key={stats.tank_id}
|
|
|
|
|
title={tankopedia?.name ?? `Unknown tank ${stats.tank_id}`}
|
|
|
|
|
type="tank"
|
|
|
|
|
tankType={tankopedia?.type}
|
|
|
|
|
treeType={(() => {
|
|
|
|
|
if (tankopedia?.is_collectible) return TreeTypeEnum.Collector;
|
|
|
|
|
if (tankopedia?.is_premium) return TreeTypeEnum.Premium;
|
|
|
|
|
})()}
|
|
|
|
|
rows={[
|
|
|
|
|
{
|
|
|
|
|
title: 'Battles',
|
|
|
|
|
current: stats.all.battles.toLocaleString(),
|
|
|
|
|
career: career.all.battles.toLocaleString(),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Winrate',
|
|
|
|
|
current: `${(
|
|
|
|
|
100 *
|
|
|
|
|
(stats.all.wins / stats.all.battles)
|
|
|
|
|
).toFixed(2)}%`,
|
|
|
|
|
career: `${(
|
|
|
|
|
100 *
|
|
|
|
|
(career.all.wins / career.all.battles)
|
|
|
|
|
).toFixed(2)}%`,
|
|
|
|
|
delta:
|
|
|
|
|
stats.all.wins / stats.all.battles -
|
|
|
|
|
career.all.wins / career.all.battles,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'WN8',
|
|
|
|
|
current:
|
|
|
|
|
currentWN8 === undefined
|
|
|
|
|
? undefined
|
|
|
|
|
: Math.round(currentWN8).toLocaleString(),
|
|
|
|
|
career:
|
|
|
|
|
careerWN8 === undefined
|
|
|
|
|
? undefined
|
|
|
|
|
: Math.round(careerWN8).toLocaleString(),
|
|
|
|
|
percentile:
|
|
|
|
|
currentWN8 === undefined
|
|
|
|
|
? undefined
|
|
|
|
|
: getWN8Percentile(currentWN8),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Damage',
|
|
|
|
|
current: Math.round(
|
|
|
|
|
stats.all.damage_dealt / stats.all.battles,
|
|
|
|
|
).toLocaleString(),
|
|
|
|
|
career: Math.round(
|
|
|
|
|
career.all.damage_dealt / career.all.battles,
|
|
|
|
|
).toLocaleString(),
|
|
|
|
|
delta:
|
|
|
|
|
stats.all.damage_dealt / stats.all.battles -
|
|
|
|
|
career.all.damage_dealt / career.all.battles,
|
|
|
|
|
},
|
|
|
|
|
]}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
2023-09-23 21:51:00 +00:00
|
|
|
</Breakdown.Root>
|
2023-09-18 22:32:21 +00:00
|
|
|
</PageWrapper>
|
|
|
|
|
);
|
2023-09-18 21:55:40 +00:00
|
|
|
}
|