feat: simplified no ongoing ratings season

This commit is contained in:
TrèsAbhi committed 2023-10-08 17:53:24 -05:00
1 parent dc9ab2067d
commit bff6eb0503
1 file changed
+72 -82
+72 -82
View File
@@ -20,6 +20,7 @@ import getMidnightLeaderboard, {
import addRegionChoices from '../core/discord/addRegionChoices'; import addRegionChoices from '../core/discord/addRegionChoices';
import addUsernameChoices from '../core/discord/addUsernameChoices'; import addUsernameChoices from '../core/discord/addUsernameChoices';
import autocompleteUsername from '../core/discord/autocompleteUsername'; import autocompleteUsername from '../core/discord/autocompleteUsername';
import embedNegative from '../core/discord/embedNegative';
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand';
import { octokit } from '../core/github/octokit'; import { octokit } from '../core/github/octokit';
import throwError from '../core/node/throwError'; import throwError from '../core/node/throwError';
@@ -172,6 +173,11 @@ function addSubcommands(
); );
} }
const noOngoingSeason = embedNegative(
'No ongoing season',
"Wargaming didn't provide any data for this season.",
);
export const ratingsCommand = new Promise<CommandRegistryRaw>( export const ratingsCommand = new Promise<CommandRegistryRaw>(
async (resolve) => { async (resolve) => {
const seasonChoices = await octokit.repos const seasonChoices = await octokit.repos
@@ -259,27 +265,28 @@ export const ratingsCommand = new Promise<CommandRegistryRaw>(
regionRatingsInfo = await (subcommandGroup === 'current' regionRatingsInfo = await (subcommandGroup === 'current'
? getRatingsInfo(region) ? getRatingsInfo(region)
: getArchivedRatingsInfo(season!, region)); : getArchivedRatingsInfo(season!, region));
if (regionRatingsInfo.detail !== undefined) return noOngoingSeason;
const regionSubdomain = regionToRegionSubdomain(region); const regionSubdomain = regionToRegionSubdomain(region);
const result = const result =
subcommandGroup === 'current' subcommandGroup === 'current'
? regionRatingsInfo.detail === undefined ? await fetch(
? await fetch( `https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/league/${leagueIndex}/top/`,
`https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/league/${leagueIndex}/top/`, )
.then((response) => response.json() as Promise<LeagueTop>)
.then(async ({ result }) =>
result.slice(0, limit).map(
(player) =>
({
id: player.spa_id,
score: player.score,
position: player.number,
clan: player.clan_tag,
nickname: player.nickname,
}) satisfies SimplifiedPlayer,
),
) )
.then((response) => response.json() as Promise<LeagueTop>)
.then(async ({ result }) =>
result.slice(0, limit).map(
(player) =>
({
id: player.spa_id,
score: player.score,
position: player.number,
clan: player.clan_tag,
nickname: player.nickname,
}) satisfies SimplifiedPlayer,
),
)
: undefined
: await octokit.repos : await octokit.repos
.getContent({ .getContent({
...DATABASE_REPO, ...DATABASE_REPO,
@@ -323,34 +330,25 @@ export const ratingsCommand = new Promise<CommandRegistryRaw>(
}) satisfies SimplifiedPlayer, }) satisfies SimplifiedPlayer,
); );
}); });
const leagueInfo = const leagueInfo = regionRatingsInfo.leagues[leagueIndex];
regionRatingsInfo.detail === undefined
? regionRatingsInfo.leagues[leagueIndex]
: undefined;
midnightLeaderboard = midnightLeaderboard =
subcommandGroup === 'current' subcommandGroup === 'current'
? regionRatingsInfo.detail === undefined ? await getMidnightLeaderboard(
? await getMidnightLeaderboard( region,
region, regionRatingsInfo.current_season,
regionRatingsInfo.current_season, )
)
: undefined
: await getMidnightLeaderboard(region, season!); : await getMidnightLeaderboard(region, season!);
players = result; players = result;
playersBefore = result ? result[0].position - 1 : 0; playersBefore = result ? result[0].position - 1 : 0;
playersAfter = playersAfter =
regionRatingsInfo.detail === undefined && result !== undefined regionRatingsInfo.count - result[result.length - 1].position;
? regionRatingsInfo.count - result[result.length - 1].position
: 0;
titleName = `${ titleName = `${
leagueInfo ? leagueInfo.title : 'No Ongoing Season' leagueInfo ? leagueInfo.title : 'No Ongoing Season'
} - ${REGION_NAMES_SHORT[region]}`; } - ${REGION_NAMES_SHORT[region]}`;
titleImage = leagueInfo titleImage = leagueInfo.big_icon.startsWith('http')
? leagueInfo.big_icon.startsWith('http') ? leagueInfo.big_icon
? leagueInfo.big_icon : `https:${leagueInfo.big_icon}`;
: `https:${leagueInfo.big_icon}`
: undefined;
titleDescription = `Top ${limit} players`; titleDescription = `Top ${limit} players`;
} else { } else {
const { region, id } = await resolvePlayerFromCommand(interaction); const { region, id } = await resolvePlayerFromCommand(interaction);
@@ -372,6 +370,9 @@ export const ratingsCommand = new Promise<CommandRegistryRaw>(
response.json(), response.json(),
)) as RatingsInfo; )) as RatingsInfo;
}); });
if (regionRatingsInfo.detail !== undefined) return noOngoingSeason;
const accountInfo = await getWargamingResponse<AccountInfo>( const accountInfo = await getWargamingResponse<AccountInfo>(
`https://api.wotblitz.${region}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, `https://api.wotblitz.${region}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
); );
@@ -383,29 +384,26 @@ export const ratingsCommand = new Promise<CommandRegistryRaw>(
const regionSubdomain = regionToRegionSubdomain(region); const regionSubdomain = regionToRegionSubdomain(region);
const neighbors = const neighbors =
subcommandGroup === 'current' subcommandGroup === 'current'
? regionRatingsInfo.detail === undefined ? await fetch(
? await fetch( `https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/user/${id}/?neighbors=${Math.round(
`https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/user/${id}/?neighbors=${Math.round( limit / 2,
limit / 2, )}`,
)}`, )
.then(
(response) => response.json() as Promise<RatingsNeighbors>,
)
.then((players) =>
players.neighbors.map(
(player) =>
({
id: player.spa_id,
position: player.number,
score: player.score,
clan: player.clan_tag,
nickname: player.nickname,
}) satisfies SimplifiedPlayer,
),
) )
.then(
(response) =>
response.json() as Promise<RatingsNeighbors>,
)
.then((players) =>
players.neighbors.map(
(player) =>
({
id: player.spa_id,
position: player.number,
score: player.score,
clan: player.clan_tag,
nickname: player.nickname,
}) satisfies SimplifiedPlayer,
),
)
: undefined
: await octokit.repos : await octokit.repos
.getContent({ .getContent({
...DATABASE_REPO, ...DATABASE_REPO,
@@ -453,21 +451,17 @@ export const ratingsCommand = new Promise<CommandRegistryRaw>(
}); });
midnightLeaderboard = midnightLeaderboard =
subcommandGroup === 'current' subcommandGroup === 'current'
? regionRatingsInfo.detail === undefined ? await getMidnightLeaderboard(
? await getMidnightLeaderboard( region,
region, regionRatingsInfo.current_season,
regionRatingsInfo.current_season, )
)
: undefined
: await getMidnightLeaderboard(region, season!); : await getMidnightLeaderboard(region, season!);
if (clan && neighbors) titleNameDiscriminator = `[${clan.tag}]`; if (clan && neighbors) titleNameDiscriminator = `[${clan.tag}]`;
players = neighbors; players = neighbors;
titleImage = neighbors titleImage = clan
? clan ? `https://wotblitz-gc.gcdn.co/icons/clanEmblems1x/clan-icon-v2-${clan.emblem_set_id}.png`
? `https://wotblitz-gc.gcdn.co/icons/clanEmblems1x/clan-icon-v2-${clan.emblem_set_id}.png`
: undefined
: undefined; : undefined;
titleName = neighbors titleName = neighbors
? accountInfo[id].nickname ? accountInfo[id].nickname
@@ -475,24 +469,20 @@ export const ratingsCommand = new Promise<CommandRegistryRaw>(
titleDescription = 'Ratings neighbours'; titleDescription = 'Ratings neighbours';
playersBefore = neighbors ? neighbors[0].position - 1 : 0; playersBefore = neighbors ? neighbors[0].position - 1 : 0;
playersAfter = playersAfter =
regionRatingsInfo.detail === undefined regionRatingsInfo.count - neighbors[neighbors.length - 1].position;
? neighbors
? regionRatingsInfo.count -
neighbors[neighbors.length - 1].position
: 0
: 0;
playerId = id; playerId = id;
} }
const items = players?.map((player) => { const resolvedRegionRatingsInfo: typeof regionRatingsInfo & {
const reward = detail: undefined;
regionRatingsInfo.detail === undefined } = regionRatingsInfo;
? regionRatingsInfo.rewards.find(
(reward) => const items = players.map((player) => {
player.position >= reward.from_position && const reward = resolvedRegionRatingsInfo.rewards.find(
player.position <= reward.to_position, (reward) =>
) player.position >= reward.from_position &&
: undefined; player.position <= reward.to_position,
);
const midnightIndex = midnightLeaderboard?.findIndex( const midnightIndex = midnightLeaderboard?.findIndex(
(item) => item.id === player.id, (item) => item.id === player.id,
); );