Files
blitzkit/src/commands/rating.tsx
T

483 lines
16 KiB
TypeScript
Raw Normal View History

2024-03-20 18:03:00 -05:00
import markdownEscape from 'markdown-escape';
2024-03-25 18:00:07 -05:00
import { Glow } from '../components/AllStatsOverview/components/HeroStat/components/Glow';
2024-03-20 18:03:00 -05:00
import CommandWrapper from '../components/CommandWrapper';
2024-03-24 22:06:48 -05:00
import { DeltaCaret } from '../components/DeltaCaret';
2024-03-20 18:03:00 -05:00
import TitleBar from '../components/TitleBar';
import { getAccountInfo } from '../core/blitz/getAccountInfo';
2024-03-24 22:06:48 -05:00
import { getClanAccountInfo } from '../core/blitz/getClanAccountInfo';
import { getLeagueFromScore } from '../core/blitz/getLeagueFromScore';
import getRatingInfo from '../core/blitz/getRatingInfo';
2024-03-20 18:03:00 -05:00
import { getRatingNeighbors } from '../core/blitz/getRatingNeighbors';
2024-03-24 22:06:48 -05:00
import { normalizeLeagueIcon } from '../core/blitz/normalizeLeagueIcon';
import { emblemURL } from '../core/blitzkrieg/emblemURL';
2024-03-20 18:03:00 -05:00
import { getArchivedRatingLeaderboard } from '../core/blitzkrieg/getArchivedRatingLeaderboard';
2024-03-25 18:00:07 -05:00
import { webpToPng } from '../core/blitzkrieg/iconPng';
2024-03-20 18:03:00 -05:00
import addUsernameChoices from '../core/discord/addUsernameChoices';
import { createLocalizedCommand } from '../core/discord/createLocalizedCommand';
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand';
import { translator } from '../core/localization/translator';
import { deltaBkrlBlitzStats } from '../core/statistics/deltaBkrlBlitzStats';
import { BkrlFormat } from '../core/streams/bkrl';
import { CommandRegistry } from '../events/interactionCreate';
2024-03-24 22:06:48 -05:00
import { theme } from '../stitches.config';
2024-03-20 18:03:00 -05:00
2024-03-25 18:00:07 -05:00
const LEAGUE_ACCENT = [
'_purple',
'_sand',
'_amber',
'_mauve',
'_brown',
] as const;
2024-03-25 18:22:37 -05:00
export const ratingCommand = new Promise<CommandRegistry>((resolve) => {
2024-03-20 18:03:00 -05:00
resolve({
command:
2024-03-25 18:22:37 -05:00
createLocalizedCommand('rating').addStringOption(addUsernameChoices),
2024-03-20 18:03:00 -05:00
inProduction: true,
inPublic: true,
async handler(interaction) {
const { t, translate } = translator(interaction.locale);
const { id, region } = await resolvePlayerFromCommand(interaction);
2024-03-24 22:06:48 -05:00
const clan = (await getClanAccountInfo(region, id, ['clan']))?.clan;
const clanImage = clan ? emblemURL(clan.emblem_set_id) : undefined;
2024-03-20 18:03:00 -05:00
const leaderboard = await getArchivedRatingLeaderboard(region, 53);
2024-03-25 18:43:23 -05:00
if (leaderboard.format === BkrlFormat.Base) {
2024-03-20 18:03:00 -05:00
throw new Error(
'Encountered bkrl minimal format in latest season. Wait till next cron?',
);
}
2024-03-24 22:06:48 -05:00
const ratingInfo = await getRatingInfo(region);
2024-03-20 18:03:00 -05:00
const accountInfo = await getAccountInfo(region, id, [
'statistics.rating',
]);
2024-03-25 18:00:07 -05:00
const statsIndex = leaderboard.entries.findIndex(
(entry) => entry.id === id,
);
const statsA = leaderboard.entries[statsIndex];
const numberA = statsIndex + 1;
2024-03-20 18:03:00 -05:00
const statsB1 = accountInfo.statistics.rating!;
const [statsB2] = (await getRatingNeighbors(region, id, 0)).neighbors;
2024-03-24 22:06:48 -05:00
const league = getLeagueFromScore(statsB2.score);
2024-03-20 18:03:00 -05:00
2024-03-24 22:06:48 -05:00
if (ratingInfo.detail) {
throw new Error(`Rating info not available for ${region}`);
}
2024-03-20 18:03:00 -05:00
if (!statsA) {
2024-03-25 18:22:37 -05:00
return t`bot.commands.rating.errors.unrecorded_stats`;
2024-03-20 18:03:00 -05:00
}
if (!statsB1) {
2024-03-25 18:22:37 -05:00
return translate('bot.commands.rating.errors.no_participation', [
markdownEscape(accountInfo.nickname),
]);
2024-03-20 18:03:00 -05:00
}
2024-03-25 18:00:07 -05:00
const reward = ratingInfo.rewards.find(
(reward) =>
statsB2.number >= reward.from_position &&
statsB2.number <= reward.to_position,
);
const positionDelta = statsB2.number - numberA;
2024-03-24 22:06:48 -05:00
const delta = deltaBkrlBlitzStats(
statsA,
accountInfo.statistics.rating!,
statsB2,
);
2024-03-25 18:00:07 -05:00
const leagueTheme = LEAGUE_ACCENT[league.index];
2024-03-20 18:03:00 -05:00
return (
<CommandWrapper>
<TitleBar
title={accountInfo.nickname}
2024-03-24 22:06:48 -05:00
image={clanImage}
2024-03-25 18:22:37 -05:00
description={`${t`bot.commands.rating.body.subtitle`} • ${new Date().toLocaleDateString(
2024-03-25 18:00:07 -05:00
interaction.locale,
)}`}
2024-03-20 18:03:00 -05:00
/>
2024-03-25 18:00:07 -05:00
<div
style={{
position: 'relative',
display: 'flex',
overflow: 'hidden',
}}
>
2024-03-24 22:06:48 -05:00
<div
style={{
2024-03-25 18:00:07 -05:00
position: 'relative',
2024-03-24 22:06:48 -05:00
padding: 16,
2024-03-25 18:00:07 -05:00
backgroundColor: theme.colors[`appBackground2${leagueTheme}`],
2024-03-24 22:06:48 -05:00
borderRadius: 4,
2024-03-25 18:00:07 -05:00
border: `1px solid ${theme.colors[`componentInteractive${leagueTheme}`]}`,
2024-03-24 22:06:48 -05:00
display: 'flex',
2024-03-25 18:00:07 -05:00
alignItems: 'center',
justifyContent: 'space-between',
width: '100%',
2024-03-20 18:03:00 -05:00
}}
2024-03-24 22:06:48 -05:00
>
2024-03-25 18:00:07 -05:00
{reward === undefined && (
<Glow
endOpacity={3 / 4}
color={theme.colors[`solidBackground${leagueTheme}`]}
style={{
transform: 'translateX(-16px) rotate(90deg)',
}}
/>
)}
2024-03-24 22:06:48 -05:00
<div
style={{
display: 'flex',
gap: 8,
justifyContent: 'center',
alignItems: 'center',
}}
>
<img
style={{
width: 64,
height: 64,
objectFit: 'contain',
}}
src={normalizeLeagueIcon(
ratingInfo.leagues[league.index].big_icon,
)}
/>
<div
style={{
display: 'flex',
flexDirection: 'column',
}}
>
<div
style={{
display: 'flex',
alignItems: 'center',
2024-03-25 18:00:07 -05:00
justifyContent: 'flex-start',
2024-03-24 22:06:48 -05:00
}}
>
<span
style={{
2024-03-25 18:00:07 -05:00
color: theme.colors[`textHighContrast${leagueTheme}`],
2024-03-24 22:06:48 -05:00
fontSize: 24,
fontWeight: 900,
}}
>
{statsB2.score.toLocaleString(interaction.locale)}
</span>
{delta.score !== 0 && (
<>
<DeltaCaret delta={delta.score} />
<span
style={{
color:
delta.score > 0
? theme.colors.textLowContrast_green
: theme.colors.textLowContrast_red,
fontSize: 16,
}}
>
{delta.score.toLocaleString(interaction.locale)}
</span>
</>
)}
</div>
2024-03-25 18:00:07 -05:00
<div
2024-03-24 22:06:48 -05:00
style={{
2024-03-25 18:00:07 -05:00
display: 'flex',
alignItems: 'center',
justifyContent: 'flex-start',
2024-03-24 22:06:48 -05:00
}}
>
2024-03-25 18:00:07 -05:00
<span
style={{
color: theme.colors[`textLowContrast${leagueTheme}`],
fontSize: 16,
}}
>
#{statsB2.number.toLocaleString(interaction.locale)}
</span>
{positionDelta !== 0 && (
<>
<DeltaCaret delta={positionDelta} />
<span
style={{
color:
positionDelta > 0
? theme.colors.textLowContrast_green
: theme.colors.textLowContrast_red,
fontSize: 16,
}}
>
{positionDelta.toLocaleString(interaction.locale)}
</span>
</>
)}
</div>
2024-03-24 22:06:48 -05:00
</div>
</div>
2024-03-25 18:00:07 -05:00
{reward && (
<div
style={{
display: 'flex',
gap: 8,
justifyContent: 'center',
alignItems: 'center',
}}
>
<div
style={{
display: 'flex',
flexDirection: 'column',
}}
>
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'flex-end',
}}
>
<span
style={{
color: theme.colors[`textHighContrast${leagueTheme}`],
fontSize: 16,
maxWidth: 136,
textAlign: 'right',
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
overflow: 'hidden',
}}
>
{reward.type === 'vehicle'
? reward.vehicle.user_string
: reward.stuff.title}
</span>
</div>
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'flex-end',
}}
>
<span
style={{
color: theme.colors[`textLowContrast${leagueTheme}`],
fontSize: 16,
}}
>
Reward
{reward.type === 'stuff' ? ` x${reward.count}` : ''}
</span>
</div>
</div>
<img
style={{
width: 64,
height: 64,
objectFit: 'cover',
}}
src={await webpToPng(
reward.type === 'vehicle'
? reward.vehicle.preview_image_url
: reward.stuff.image_url,
)}
/>
</div>
)}
{reward === undefined && (
<Glow
endOpacity={3 / 4}
color={theme.colors[`solidBackground${leagueTheme}`]}
style={{
transform: 'translateX(16px) rotate(-90deg)',
}}
/>
)}
2024-03-24 22:06:48 -05:00
</div>
2024-03-25 18:00:07 -05:00
{/* separated because fragment middle man invalidates relative parent: https://github.com/vercel/satori/issues/605 */}
{reward && (
<Glow
direction="reverse"
color={theme.colors[`solidBackground${leagueTheme}`]}
style={{
position: 'absolute',
top: -40,
left: '50%',
transform: 'translateX(-50%)',
}}
/>
)}
{reward && (
<Glow
color={theme.colors[`solidBackground${leagueTheme}`]}
style={{
position: 'absolute',
bottom: -40,
left: '50%',
transform: 'translateX(-50%)',
}}
/>
)}
</div>
2024-03-28 10:27:33 -05:00
<div style={{ padding: '8px 0', display: 'flex' }}>
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
gap: 4,
flex: 1,
flexDirection: 'column',
}}
>
<span
2024-03-25 18:12:33 -05:00
style={{
2024-03-28 10:27:33 -05:00
fontSize: 24,
color: theme.colors.textHighContrast,
fontWeight: 'bold',
2024-03-25 18:12:33 -05:00
}}
>
2024-03-28 10:27:33 -05:00
{delta.battles.toLocaleString(interaction.locale)}
</span>
<span
2024-03-25 18:12:33 -05:00
style={{
2024-03-28 10:27:33 -05:00
fontSize: 16,
color: theme.colors.textLowContrast,
2024-03-25 18:12:33 -05:00
}}
>
2024-03-28 10:27:33 -05:00
Battles
</span>
2024-03-25 18:12:33 -05:00
</div>
2024-03-28 10:27:33 -05:00
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
gap: 4,
flex: 1,
flexDirection: 'column',
}}
>
<span
style={{
fontSize: 24,
color: theme.colors.textHighContrast,
fontWeight: 'bold',
}}
>
{(100 * (delta.wins / delta.battles)).toFixed(0)}%
</span>
<span
style={{
fontSize: 16,
color: theme.colors.textLowContrast,
}}
>
Winrate
</span>
</div>
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
gap: 4,
flex: 1,
flexDirection: 'column',
}}
>
<span
style={{
fontSize: 24,
color: theme.colors.textHighContrast,
fontWeight: 'bold',
}}
>
{Math.round(delta.damage / delta.battles).toLocaleString(
interaction.locale,
)}
</span>
<span
style={{
fontSize: 16,
color: theme.colors.textLowContrast,
}}
>
Damage
</span>
</div>
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
gap: 4,
flex: 1,
flexDirection: 'column',
}}
>
<span
style={{
fontSize: 24,
color: theme.colors.textHighContrast,
fontWeight: 'bold',
}}
>
{(100 * (delta.survived / delta.battles)).toFixed(0)}%
</span>
<span
style={{
fontSize: 16,
color: theme.colors.textLowContrast,
}}
>
Survival
</span>
</div>
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
gap: 4,
flex: 1,
flexDirection: 'column',
}}
>
<span
style={{
fontSize: 24,
color: theme.colors.textHighContrast,
fontWeight: 'bold',
}}
>
{(delta.kills / delta.battles).toFixed(2)}
</span>
<span
style={{
fontSize: 16,
color: theme.colors.textLowContrast,
}}
>
Kills
</span>
</div>
</div>
2024-03-20 18:03:00 -05:00
</CommandWrapper>
);
},
});
});