feat: made /ratings data-driven

This commit is contained in:
TresAbhi committed 2023-08-09 00:47:15 +00:00
1 parent d511cfa4cc
commit f7eadb08a9
1 file changed
+41 -53
+41 -53
View File
@@ -1,4 +1,4 @@
import { SlashCommandBuilder, SlashCommandSubcommandBuilder } from 'discord.js'; import { SlashCommandBuilder } from 'discord.js';
import { BlitzkriegRatingsLeaderboard } from '../../scripts/buildRatingsLeaderboard'; import { BlitzkriegRatingsLeaderboard } from '../../scripts/buildRatingsLeaderboard';
import { octokit } from '../bot'; import { octokit } from '../bot';
import * as Leaderboard from '../components/Leaderboard'; import * as Leaderboard from '../components/Leaderboard';
@@ -7,7 +7,7 @@ import Wrapper from '../components/Wrapper';
import { Region } from '../constants/regions'; import { Region } from '../constants/regions';
import regionToRegionSubdomain from '../core/blitz/regionToRegionSubdomain'; import regionToRegionSubdomain from '../core/blitz/regionToRegionSubdomain';
import addRegionChoices from '../core/discord/addRegionChoices'; import addRegionChoices from '../core/discord/addRegionChoices';
import { CommandRegistry } from '../events/interactionCreate'; import { CommandRegistryRaw } from '../events/interactionCreate';
export interface RatingsPlayer { export interface RatingsPlayer {
spa_id: number; spa_id: number;
@@ -102,8 +102,30 @@ const ratingsInfo = fetch(
return info; return info;
}); });
function optionalParameters(option: SlashCommandSubcommandBuilder) { export const DATABASE_REPO = { owner: 'tresabhi', repo: 'blitzkrieg-db' };
const LEAGUE_SUBCOMMAND_TO_INDEX: Record<string, number> = {};
export const ratingsCommand = new Promise<CommandRegistryRaw>(
async (resolve) => {
const awaitedRatingsInfo = await ratingsInfo;
const command = new SlashCommandBuilder()
.setName('ratings')
.setDescription('Ratings battles statistics')
.addSubcommandGroup((option) => {
const subcommandGroup = option
.setName('league')
.setDescription('Top league players');
awaitedRatingsInfo.leagues.forEach((league) => {
subcommandGroup.addSubcommand((option) => {
const subcommandName = league.title.split(' ')[0].toLowerCase();
LEAGUE_SUBCOMMAND_TO_INDEX[subcommandName] = league.index;
return option return option
.setName(subcommandName)
.setDescription(`Top ${league.title} players`)
.addStringOption(addRegionChoices) .addStringOption(addRegionChoices)
.addIntegerOption((option) => .addIntegerOption((option) =>
option option
@@ -113,65 +135,26 @@ function optionalParameters(option: SlashCommandSubcommandBuilder) {
.setMinValue(5) .setMinValue(5)
.setMaxValue(30), .setMaxValue(30),
); );
} });
});
const LEAGUE_INDEXES = ['diamond', 'platinum', 'gold', 'silver', 'bronze']; return subcommandGroup;
});
export const DATABASE_REPO = { owner: 'tresabhi', repo: 'blitzkrieg-db' }; resolve({
export const ratingsCommand: CommandRegistry = {
inProduction: true, inProduction: true,
inDevelopment: true, inDevelopment: true,
inPublic: true, inPublic: true,
command: new SlashCommandBuilder() command,
.setName('ratings')
.setDescription('Ratings battles statistics')
.addSubcommandGroup((option) =>
option
.setName('league')
.setDescription('Top league players')
.addSubcommand((option) =>
optionalParameters(
option
.setName('diamond')
.setDescription('Top Diamond League players'),
),
)
.addSubcommand((option) =>
optionalParameters(
option
.setName('platinum')
.setDescription('Top Platinum League players'),
),
)
.addSubcommand((option) =>
optionalParameters(
option.setName('gold').setDescription('Top Gold League players'),
),
)
.addSubcommand((option) =>
optionalParameters(
option
.setName('silver')
.setDescription('Top Silver League players'),
),
)
.addSubcommand((option) =>
optionalParameters(
option
.setName('bronze')
.setDescription('Top Bronze League players'),
),
),
),
async handler(interaction) { async handler(interaction) {
const subcommandGroup = interaction.options.getSubcommandGroup(true); const subcommandGroup = interaction.options.getSubcommandGroup(true);
if (subcommandGroup === 'league') { if (subcommandGroup === 'league') {
const limit = interaction.options.getInteger('limit') ?? 10; const limit = interaction.options.getInteger('limit') ?? 10;
const subcommand = interaction.options.getSubcommand(true); const subcommand = interaction.options.getSubcommand(true);
const leagueIndex = LEAGUE_INDEXES.indexOf(subcommand); const leagueIndex = LEAGUE_SUBCOMMAND_TO_INDEX[subcommand];
const region = interaction.options.getString('region') as Region; const region = interaction.options.getString('region') as Region;
const regionSubdomain = regionToRegionSubdomain(region); const regionSubdomain = regionToRegionSubdomain(region);
const result = await fetch( const result = await fetch(
@@ -230,7 +213,8 @@ export const ratingsCommand: CommandRegistry = {
); );
const playersBefore = result[0].number - 1; const playersBefore = result[0].number - 1;
const playersAfter = awaitedInfo.count - result[result.length - 1].number; const playersAfter =
awaitedInfo.count - result[result.length - 1].number;
return ( return (
<Wrapper> <Wrapper>
@@ -245,7 +229,9 @@ export const ratingsCommand: CommandRegistry = {
/> />
<Leaderboard.Root> <Leaderboard.Root>
{playersBefore > 0 && <Leaderboard.Gap number={playersBefore} />} {playersBefore > 0 && (
<Leaderboard.Gap number={playersBefore} />
)}
{items} {items}
{playersAfter > 0 && <Leaderboard.Gap number={playersAfter} />} {playersAfter > 0 && <Leaderboard.Gap number={playersAfter} />}
</Leaderboard.Root> </Leaderboard.Root>
@@ -255,4 +241,6 @@ export const ratingsCommand: CommandRegistry = {
return []; return [];
}, },
}; });
},
);