Files
blitzkit/packages/bot/src/commands/rating.tsx
T

535 lines
17 KiB
TypeScript
Raw Normal View History

2024-09-08 10:11:24 -05:00
import {
deltaBkrlBlitzStats,
2024-09-08 10:42:34 -05:00
emblemURL,
2024-09-08 10:17:40 -05:00
getAccountInfo,
2024-11-20 10:50:09 -06:00
getArchivedRatingLeaderboard,
2024-09-08 10:17:40 -05:00
getClanAccountInfo,
2024-09-08 10:11:24 -05:00
getLeagueFromScore,
getRatingInfo,
2024-09-08 10:17:40 -05:00
getRatingNeighbors,
2024-12-14 15:46:47 -06:00
imgur,
2025-10-01 22:45:21 -05:00
} from "@blitzkit/core";
import { literals } from "@blitzkit/i18n";
import { SlashCommandSubcommandBuilder } from "discord.js";
import markdownEscape from "markdown-escape";
import { Glow } from "../components/AllStatsOverview/components/HeroStat/components/Glow";
import { CommandWrapper } from "../components/CommandWrapper";
import { DeltaCaret } from "../components/DeltaCaret";
import { TitleBar } from "../components/TitleBar";
import { normalizeLeagueIcon } from "../core/blitz/normalizeLeagueIcon";
import { iconPng } from "../core/blitzkit/iconPng";
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 { CommandRegistry } from "../events/interactionCreate";
import { theme } from "../stitches.config";
2024-03-20 18:03:00 -05:00
2024-03-25 18:00:07 -05:00
const LEAGUE_ACCENT = [
2025-10-01 22:45:21 -05:00
"_purple",
"_sand",
"_amber",
"_mauve",
"_brown",
2024-03-25 18:00:07 -05:00
] 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({
2025-10-01 22:45:21 -05:00
command: createLocalizedCommand("rating", [
2024-06-06 19:31:53 -05:00
{
2025-10-01 22:45:21 -05:00
subcommand: "today",
2024-06-06 19:31:53 -05:00
modify(option: SlashCommandSubcommandBuilder) {
option.addStringOption(addUsernameChoices);
},
},
{
2025-10-01 22:45:21 -05:00
subcommand: "season",
2024-06-06 19:31:53 -05:00
modify(option: SlashCommandSubcommandBuilder) {
option.addStringOption(addUsernameChoices);
},
},
]),
2024-03-20 18:03:00 -05:00
async handler(interaction) {
2024-06-06 19:31:53 -05:00
const subcommand = interaction.options.getSubcommand(true) as
2025-10-01 22:45:21 -05:00
| "season"
| "today";
2025-01-16 13:48:13 -06:00
const { strings } = translator(interaction.locale);
2024-03-20 18:03:00 -05:00
const { id, region } = await resolvePlayerFromCommand(interaction);
2025-10-01 22:45:21 -05:00
const clan = (await getClanAccountInfo(region, id, ["clan"]))?.clan;
2024-03-24 22:06:48 -05:00
const clanImage = clan ? emblemURL(clan.emblem_set_id) : undefined;
2024-06-06 19:00:37 -05:00
const ratingInfo = await getRatingInfo(region);
if (ratingInfo.detail) {
throw new Error(`Rating info not available for ${region}`);
}
2024-11-20 10:50:09 -06:00
const leaderboard = await getArchivedRatingLeaderboard(
2024-03-30 12:32:48 -05:00
region,
2025-10-01 22:45:21 -05:00
ratingInfo.current_season - (subcommand === "today" ? 0 : 1)
2024-03-30 12:32:48 -05:00
);
2024-03-20 18:03:00 -05:00
2025-10-01 22:45:21 -05:00
if (leaderboard.version!.$case === "v1") {
2024-03-20 18:03:00 -05:00
throw new Error(
2025-10-01 22:45:21 -05:00
"Encountered bkrl minimal format in latest season. Wait till next cron?"
2024-03-20 18:03:00 -05:00
);
}
const accountInfo = await getAccountInfo(region, id, [
2025-10-01 22:45:21 -05:00
"statistics.rating",
2024-03-20 18:03:00 -05:00
]);
2024-11-20 10:50:09 -06:00
const statsIndex = leaderboard.version!.value.entries.findIndex(
2025-10-01 22:45:21 -05:00
(entry) => entry.id === id
2024-03-25 18:00:07 -05:00
);
2024-11-20 10:50:09 -06:00
const statsA = leaderboard
? leaderboard.version!.value.entries[statsIndex!]
: null;
2024-03-30 12:32:48 -05:00
const positionA = leaderboard ? statsIndex! + 1 : undefined;
2024-03-20 18:03:00 -05:00
const statsB1 = accountInfo.statistics.rating!;
2024-06-06 19:40:37 -05:00
const statsB2Array = (await getRatingNeighbors(region, id, 0)).neighbors;
if (statsB2Array === undefined) {
2025-10-01 22:45:21 -05:00
return literals(strings.bot.commands.rating.errors.no_participation, {
name: markdownEscape(accountInfo.nickname),
});
2024-06-06 19:40:37 -05:00
}
const [statsB2] = statsB2Array;
2024-03-24 22:06:48 -05:00
const league = getLeagueFromScore(statsB2.score);
2024-03-20 18:03:00 -05:00
if (!statsA) {
2025-01-16 13:48:13 -06:00
return strings.bot.commands.rating.errors.unrecorded_stats;
2024-03-20 18:03:00 -05:00
}
if (!statsB1) {
2025-10-01 22:45:21 -05:00
return literals(strings.bot.commands.rating.errors.no_participation, {
name: 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 &&
2025-10-01 22:45:21 -05:00
statsB2.number <= reward.to_position
2024-03-25 18:00:07 -05:00
);
2024-03-30 12:32:48 -05:00
const positionDelta = leaderboard
? statsB2.number - positionA!
: undefined;
2024-03-24 22:06:48 -05:00
const delta = deltaBkrlBlitzStats(
statsA,
accountInfo.statistics.rating!,
2025-10-01 22:45:21 -05:00
statsB2
2024-03-24 22:06:48 -05:00
);
2024-03-25 18:00:07 -05:00
const leagueTheme = LEAGUE_ACCENT[league.index];
2024-12-14 15:46:47 -06:00
let rewardImage = reward
2025-10-01 22:45:21 -05:00
? reward.type === "vehicle"
2024-12-14 15:46:47 -06:00
? reward.vehicle.preview_image_url
: reward.stuff.image_url
: undefined;
if (rewardImage) {
const { ok } = await fetch(rewardImage);
if (!ok) {
2025-10-01 22:45:21 -05:00
rewardImage = imgur("kP88vqr");
2024-12-14 15:46:47 -06:00
}
}
2024-03-20 18:03:00 -05:00
return (
<CommandWrapper>
<TitleBar
title={accountInfo.nickname}
2024-03-24 22:06:48 -05:00
image={clanImage}
2025-01-16 13:48:13 -06:00
description={`${
strings.bot.commands.rating.body.subtitle[subcommand]
} • ${new Date().toLocaleDateString(interaction.locale)}`}
2024-03-20 18:03:00 -05:00
/>
2024-03-25 18:00:07 -05:00
<div
style={{
2025-10-01 22:45:21 -05:00
position: "relative",
display: "flex",
overflow: "hidden",
2024-03-25 18:00:07 -05:00
}}
>
2024-03-24 22:06:48 -05:00
<div
style={{
2025-10-01 22:45:21 -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-09-07 21:45:00 -05:00
border: `1px solid ${
theme.colors[`componentInteractive${leagueTheme}`]
}`,
2025-10-01 22:45:21 -05:00
display: "flex",
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={{
2025-10-01 22:45:21 -05:00
transform: "translateX(-16px) rotate(90deg)",
2024-03-25 18:00:07 -05:00
}}
/>
)}
2024-03-24 22:06:48 -05:00
<div
style={{
2025-10-01 22:45:21 -05:00
display: "flex",
2024-03-24 22:06:48 -05:00
gap: 8,
2025-10-01 22:45:21 -05:00
justifyContent: "center",
alignItems: "center",
2024-03-24 22:06:48 -05:00
}}
>
<img
2024-05-04 16:28:12 -05:00
alt={`League ${league.name}`}
2024-03-24 22:06:48 -05:00
style={{
width: 64,
height: 64,
2025-10-01 22:45:21 -05:00
objectFit: "contain",
2024-03-24 22:06:48 -05:00
}}
src={normalizeLeagueIcon(
2025-10-01 22:45:21 -05:00
ratingInfo.leagues[league.index].big_icon
2024-03-24 22:06:48 -05:00
)}
/>
<div
style={{
2025-10-01 22:45:21 -05:00
display: "flex",
flexDirection: "column",
2024-03-24 22:06:48 -05:00
}}
>
<div
style={{
2025-10-01 22:45:21 -05:00
display: "flex",
alignItems: "center",
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,
}}
>
2024-03-28 10:33:05 -05:00
{Math.abs(delta.score).toLocaleString(
2025-10-01 22:45:21 -05:00
interaction.locale
2024-03-28 10:33:05 -05:00
)}
2024-03-24 22:06:48 -05:00
</span>
</>
)}
</div>
2024-03-25 18:00:07 -05:00
<div
2024-03-24 22:06:48 -05:00
style={{
2025-10-01 22:45:21 -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>
2024-03-30 12:32:48 -05:00
{positionDelta !== undefined && positionDelta !== 0 && (
2024-03-25 18:00:07 -05:00
<>
2024-06-06 19:23:21 -05:00
<DeltaCaret delta={-positionDelta} />
2024-03-25 18:00:07 -05:00
<span
style={{
color:
positionDelta > 0
2024-06-06 19:23:21 -05:00
? theme.colors.textLowContrast_red
: theme.colors.textLowContrast_green,
2024-03-25 18:00:07 -05:00
fontSize: 16,
}}
>
2024-03-28 10:33:05 -05:00
{Math.abs(positionDelta).toLocaleString(
2025-10-01 22:45:21 -05:00
interaction.locale
2024-03-28 10:33:05 -05:00
)}
2024-03-25 18:00:07 -05:00
</span>
</>
)}
</div>
2024-03-24 22:06:48 -05:00
</div>
</div>
2024-03-25 18:00:07 -05:00
{reward && (
<div
style={{
2025-10-01 22:45:21 -05:00
display: "flex",
2024-03-25 18:00:07 -05:00
gap: 8,
2025-10-01 22:45:21 -05:00
justifyContent: "center",
alignItems: "center",
2024-03-25 18:00:07 -05:00
}}
>
<div
style={{
2025-10-01 22:45:21 -05:00
display: "flex",
flexDirection: "column",
2024-03-25 18:00:07 -05:00
}}
>
<div
style={{
2025-10-01 22:45:21 -05:00
display: "flex",
alignItems: "center",
justifyContent: "flex-end",
2024-03-25 18:00:07 -05:00
}}
>
<span
style={{
color: theme.colors[`textHighContrast${leagueTheme}`],
fontSize: 16,
maxWidth: 136,
2025-10-01 22:45:21 -05:00
textAlign: "right",
whiteSpace: "nowrap",
textOverflow: "ellipsis",
overflow: "hidden",
2024-03-25 18:00:07 -05:00
}}
>
2025-10-01 22:45:21 -05:00
{reward.type === "vehicle"
2024-03-25 18:00:07 -05:00
? reward.vehicle.user_string
: reward.stuff.title}
</span>
</div>
<div
style={{
2025-10-01 22:45:21 -05:00
display: "flex",
alignItems: "center",
justifyContent: "flex-end",
2024-03-25 18:00:07 -05:00
}}
>
<span
style={{
color: theme.colors[`textLowContrast${leagueTheme}`],
fontSize: 16,
}}
>
Reward
2025-10-01 22:45:21 -05:00
{reward.type === "stuff" ? ` x${reward.count}` : ""}
2024-03-25 18:00:07 -05:00
</span>
</div>
</div>
<img
style={{
width: 64,
height: 64,
2025-10-01 22:45:21 -05:00
objectFit: "cover",
2024-03-25 18:00:07 -05:00
}}
2024-12-14 15:46:47 -06:00
src={await iconPng(rewardImage!)}
2024-03-25 18:00:07 -05:00
/>
</div>
)}
{reward === undefined && (
<Glow
endOpacity={3 / 4}
color={theme.colors[`solidBackground${leagueTheme}`]}
style={{
2025-10-01 22:45:21 -05:00
transform: "translateX(16px) rotate(-90deg)",
2024-03-25 18:00:07 -05:00
}}
/>
)}
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={{
2025-10-01 22:45:21 -05:00
position: "absolute",
2024-03-25 18:00:07 -05:00
top: -40,
2025-10-01 22:45:21 -05:00
left: "50%",
transform: "translateX(-50%)",
2024-03-25 18:00:07 -05:00
}}
/>
)}
{reward && (
<Glow
color={theme.colors[`solidBackground${leagueTheme}`]}
style={{
2025-10-01 22:45:21 -05:00
position: "absolute",
2024-03-25 18:00:07 -05:00
bottom: -40,
2025-10-01 22:45:21 -05:00
left: "50%",
transform: "translateX(-50%)",
2024-03-25 18:00:07 -05:00
}}
/>
)}
</div>
2024-03-30 12:33:17 -05:00
{delta.battles > 0 && (
2025-10-01 22:45:21 -05:00
<div style={{ padding: "8px 0", display: "flex" }}>
2024-03-30 12:33:17 -05:00
<div
2024-03-25 18:12:33 -05:00
style={{
2025-10-01 22:45:21 -05:00
display: "flex",
alignItems: "center",
justifyContent: "center",
2024-03-30 12:33:17 -05:00
gap: 4,
flex: 1,
2025-10-01 22:45:21 -05:00
flexDirection: "column",
2024-03-25 18:12:33 -05:00
}}
>
2024-03-30 12:33:17 -05:00
<span
style={{
fontSize: 24,
color: theme.colors.textHighContrast,
2025-10-01 22:45:21 -05:00
fontWeight: "bold",
2024-03-30 12:33:17 -05:00
}}
>
{delta.battles.toLocaleString(interaction.locale)}
</span>
<span
style={{
fontSize: 16,
color: theme.colors.textLowContrast,
}}
>
Battles
</span>
</div>
<div
2024-03-25 18:12:33 -05:00
style={{
2025-10-01 22:45:21 -05:00
display: "flex",
alignItems: "center",
justifyContent: "center",
2024-03-30 12:33:17 -05:00
gap: 4,
flex: 1,
2025-10-01 22:45:21 -05:00
flexDirection: "column",
2024-03-25 18:12:33 -05:00
}}
>
2024-03-30 12:33:17 -05:00
<span
style={{
fontSize: 24,
color: theme.colors.textHighContrast,
2025-10-01 22:45:21 -05:00
fontWeight: "bold",
2024-03-30 12:33:17 -05:00
}}
>
{(100 * (delta.wins / delta.battles)).toFixed(0)}%
</span>
<span
style={{
fontSize: 16,
color: theme.colors.textLowContrast,
}}
>
Winrate
</span>
</div>
<div
style={{
2025-10-01 22:45:21 -05:00
display: "flex",
alignItems: "center",
justifyContent: "center",
2024-03-30 12:33:17 -05:00
gap: 4,
flex: 1,
2025-10-01 22:45:21 -05:00
flexDirection: "column",
2024-03-30 12:33:17 -05:00
}}
>
<span
style={{
fontSize: 24,
color: theme.colors.textHighContrast,
2025-10-01 22:45:21 -05:00
fontWeight: "bold",
2024-03-30 12:33:17 -05:00
}}
>
{Math.round(delta.damage / delta.battles).toLocaleString(
2025-10-01 22:45:21 -05:00
interaction.locale
2024-03-30 12:33:17 -05:00
)}
</span>
<span
style={{
fontSize: 16,
color: theme.colors.textLowContrast,
}}
>
Damage
</span>
</div>
<div
style={{
2025-10-01 22:45:21 -05:00
display: "flex",
alignItems: "center",
justifyContent: "center",
2024-03-30 12:33:17 -05:00
gap: 4,
flex: 1,
2025-10-01 22:45:21 -05:00
flexDirection: "column",
2024-03-30 12:33:17 -05:00
}}
>
<span
style={{
fontSize: 24,
color: theme.colors.textHighContrast,
2025-10-01 22:45:21 -05:00
fontWeight: "bold",
2024-03-30 12:33:17 -05:00
}}
>
{(100 * (delta.survived / delta.battles)).toFixed(0)}%
</span>
<span
style={{
fontSize: 16,
color: theme.colors.textLowContrast,
}}
>
Survival
</span>
</div>
<div
style={{
2025-10-01 22:45:21 -05:00
display: "flex",
alignItems: "center",
justifyContent: "center",
2024-03-30 12:33:17 -05:00
gap: 4,
flex: 1,
2025-10-01 22:45:21 -05:00
flexDirection: "column",
2024-03-30 12:33:17 -05:00
}}
>
<span
style={{
fontSize: 24,
color: theme.colors.textHighContrast,
2025-10-01 22:45:21 -05:00
fontWeight: "bold",
2024-03-30 12:33:17 -05:00
}}
>
{(delta.kills / delta.battles).toFixed(2)}
</span>
<span
style={{
fontSize: 16,
color: theme.colors.textLowContrast,
}}
>
Kills
</span>
</div>
2024-03-25 18:12:33 -05:00
</div>
2024-03-30 12:33:17 -05:00
)}
2024-03-20 18:03:00 -05:00
</CommandWrapper>
);
},
});
});