2024-03-20 18:03:00 -05:00
|
|
|
import markdownEscape from 'markdown-escape';
|
|
|
|
|
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 NoData from '../components/NoData';
|
|
|
|
|
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';
|
|
|
|
|
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
|
|
|
|
|
|
|
|
export const ratingStatsCommand = new Promise<CommandRegistry>((resolve) => {
|
|
|
|
|
resolve({
|
|
|
|
|
command:
|
2024-03-21 22:32:01 -05:00
|
|
|
createLocalizedCommand('rating-session').addStringOption(
|
2024-03-20 18:03:00 -05:00
|
|
|
addUsernameChoices,
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if (leaderboard.format === BkrlFormat.Minimal) {
|
|
|
|
|
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',
|
|
|
|
|
]);
|
|
|
|
|
const statsA = leaderboard.entries.find((entry) => entry.id === id);
|
|
|
|
|
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-21 22:32:01 -05:00
|
|
|
return t`bot.commands.rating_session.errors.unrecorded_stats`;
|
2024-03-20 18:03:00 -05:00
|
|
|
}
|
|
|
|
|
if (!statsB1) {
|
2024-03-21 22:32:01 -05:00
|
|
|
return translate(
|
|
|
|
|
'bot.commands.rating_session.errors.no_participation',
|
|
|
|
|
[markdownEscape(accountInfo.nickname)],
|
|
|
|
|
);
|
2024-03-20 18:03:00 -05:00
|
|
|
}
|
|
|
|
|
|
2024-03-24 22:06:48 -05:00
|
|
|
const delta = deltaBkrlBlitzStats(
|
|
|
|
|
statsA,
|
|
|
|
|
accountInfo.statistics.rating!,
|
|
|
|
|
statsB2,
|
|
|
|
|
);
|
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-21 22:32:01 -05:00
|
|
|
description={t`bot.commands.rating_session.body.subtitle`}
|
2024-03-20 18:03:00 -05:00
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{delta.battles === 0 && (
|
|
|
|
|
<NoData type="battles_in_season" locale={interaction.locale} />
|
|
|
|
|
)}
|
|
|
|
|
{delta.battles > 0 && (
|
2024-03-24 22:06:48 -05:00
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
padding: 16,
|
|
|
|
|
backgroundColor: theme.colors.appBackground2_purple,
|
|
|
|
|
borderRadius: 4,
|
|
|
|
|
border: `1px solid ${theme.colors.componentInteractive_purple}`,
|
|
|
|
|
display: 'flex',
|
2024-03-20 18:03:00 -05:00
|
|
|
}}
|
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',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<span
|
|
|
|
|
style={{
|
|
|
|
|
color: theme.colors.textHighContrast_purple,
|
|
|
|
|
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>
|
|
|
|
|
<span
|
|
|
|
|
style={{
|
|
|
|
|
color: theme.colors.textLowContrast_purple,
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
#{statsB2.number.toLocaleString(interaction.locale)}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2024-03-20 18:03:00 -05:00
|
|
|
)}
|
|
|
|
|
</CommandWrapper>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|