Files
blitzkit/src/renderers/evolution.tsx
T

128 lines
4.1 KiB
TypeScript
Raw Normal View History

2023-06-23 01:45:44 +00:00
import * as Graph from '../components/Graph/index.js';
import NoData, { NoDataType } from '../components/NoData.js';
2023-06-28 18:07:49 +00:00
import PoweredBy, { PoweredByType } from '../components/PoweredBy.js';
2023-06-23 01:45:44 +00:00
import TitleBar from '../components/TitleBar.js';
import Wrapper from '../components/Wrapper.js';
import { BLITZ_SERVERS } from '../constants/servers.js';
import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
import resolveTankName from '../core/blitz/resolveTankName.js';
import { tankopedia } from '../core/blitz/tankopedia.js';
import getPlayerHistories from '../core/blitzstars/getPlayerHistories.js';
import getTankHistories from '../core/blitzstars/getTankHistories.js';
2023-06-27 01:46:08 +00:00
import { ResolvedPeriod } from '../core/discord/resolvePeriodFromCommand.js';
2023-06-27 02:07:14 +00:00
import { ResolvedPlayer } from '../core/discord/resolvePlayerFromCommand.js';
2023-06-27 17:22:27 +00:00
import { WARGAMING_APPLICATION_ID } from '../core/node/arguments.js';
2023-06-23 01:45:44 +00:00
import { AccountInfo } from '../types/accountInfo.js';
import { Histories } from '../types/histories.js';
import { PlayerClanData } from '../types/playerClanData.js';
2023-06-27 19:10:12 +00:00
import { StatType } from './statsfull.js';
2023-06-23 01:45:44 +00:00
export default async function evolution<Type extends StatType>(
type: Type,
2023-06-25 02:40:54 +00:00
{ start, end, evolutionName }: ResolvedPeriod,
2023-06-23 01:45:44 +00:00
{ server, id }: ResolvedPlayer,
2023-06-28 18:45:14 +00:00
tankId: Type extends 'tank' ? number : null,
2023-06-23 01:45:44 +00:00
) {
let nameDiscriminator: string | undefined;
let image: string | undefined;
if (type === 'player') {
const clan = (
await getWargamingResponse<PlayerClanData>(
`https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
)
)[id]?.clan;
if (clan) nameDiscriminator = `[${clan.tag}]`;
image = clan
? `https://wotblitz-gc.gcdn.co/icons/clanEmblems1x/clan-icon-v2-${clan.emblem_set_id}.png`
: undefined;
} else {
nameDiscriminator = `(${await resolveTankName(tankId!)})`;
image = (await tankopedia)[tankId!]?.images.normal;
2023-06-23 01:45:44 +00:00
}
let histories: Histories;
if (type === 'player') {
histories = await getPlayerHistories(server, id, {
start,
end,
includeLatestHistories: true,
includePreviousHistories: true,
});
} else {
histories = await getTankHistories(server, id, {
2023-06-28 18:45:14 +00:00
tankId: tankId!,
2023-06-23 01:45:44 +00:00
start,
end,
includeLatestHistories: true,
includePreviousHistories: true,
});
}
2023-06-29 18:39:59 +00:00
const plot = histories.map(
2023-06-23 01:45:44 +00:00
(history) =>
[
2023-06-29 18:39:59 +00:00
history.all.battles,
2023-06-23 01:45:44 +00:00
(history.all.wins / history.all.battles) * 100,
] as Graph.PlotItem,
);
2023-06-29 18:39:59 +00:00
const times = histories.map((history) => history.last_battle_time);
const minTime = Math.min(...times);
const maxTime = Math.max(...times);
const ys = plot.map(([, y]) => y);
const xs = plot.map(([x]) => x);
const maxY = Math.max(...ys);
const minY = Math.min(...ys);
const maxX = Math.max(...xs);
const minX = Math.min(...xs);
2023-06-23 01:45:44 +00:00
const accountInfo = await getWargamingResponse<AccountInfo>(
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
);
return (
<Wrapper>
<TitleBar
name={accountInfo[id].nickname}
nameDiscriminator={nameDiscriminator}
image={image}
description={`${evolutionName} • ${new Date().toDateString()} • ${
BLITZ_SERVERS[server]
}`}
/>
{/* goofy ahh bug forces me to call them as functions */}
2023-06-29 18:39:59 +00:00
{plot.length > 0 && (
2023-06-23 01:45:44 +00:00
<Graph.Root
2023-06-29 18:39:59 +00:00
verticalMargin={{
min: minY,
max: maxY,
2023-06-23 01:45:44 +00:00
suffix: '%',
2023-06-29 18:39:59 +00:00
precision: 1,
2023-06-23 01:45:44 +00:00
}}
2023-06-29 18:39:59 +00:00
horizontalMargin={{
min: minX,
max: maxX,
precision: 0,
2023-06-23 01:45:44 +00:00
}}
2023-06-29 18:39:59 +00:00
xMinLabel={new Date(minTime * 1000).toDateString()}
xMaxLabel={new Date(maxTime * 1000).toDateString()}
2023-06-23 01:45:44 +00:00
>
{Graph.Line({
// career winrate
2023-06-29 18:39:59 +00:00
color: Graph.LineColor.Red,
plot: plot,
minY,
maxY,
2023-06-23 01:45:44 +00:00
})}
</Graph.Root>
)}
2023-06-29 18:39:59 +00:00
{plot.length === 0 && <NoData type={NoDataType.BattlesInPeriod} />}
2023-06-23 01:45:44 +00:00
2023-06-28 18:07:49 +00:00
<PoweredBy type={PoweredByType.BlitzStars} />
2023-06-23 01:45:44 +00:00
</Wrapper>
);
}