From bff6eb050314b17758f91c4a371a02e61ff729e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Sun, 8 Oct 2023 17:53:24 -0500 Subject: [PATCH] feat: simplified no ongoing ratings season --- src/commands/ratings.tsx | 154 ++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 82 deletions(-) diff --git a/src/commands/ratings.tsx b/src/commands/ratings.tsx index 3f49a15ea..13626733e 100644 --- a/src/commands/ratings.tsx +++ b/src/commands/ratings.tsx @@ -20,6 +20,7 @@ import getMidnightLeaderboard, { import addRegionChoices from '../core/discord/addRegionChoices'; import addUsernameChoices from '../core/discord/addUsernameChoices'; import autocompleteUsername from '../core/discord/autocompleteUsername'; +import embedNegative from '../core/discord/embedNegative'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand'; import { octokit } from '../core/github/octokit'; 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( async (resolve) => { const seasonChoices = await octokit.repos @@ -259,27 +265,28 @@ export const ratingsCommand = new Promise( regionRatingsInfo = await (subcommandGroup === 'current' ? getRatingsInfo(region) : getArchivedRatingsInfo(season!, region)); + + if (regionRatingsInfo.detail !== undefined) return noOngoingSeason; + const regionSubdomain = regionToRegionSubdomain(region); const result = subcommandGroup === 'current' - ? regionRatingsInfo.detail === undefined - ? await fetch( - `https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/league/${leagueIndex}/top/`, + ? await fetch( + `https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/league/${leagueIndex}/top/`, + ) + .then((response) => response.json() as Promise) + .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) - .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 .getContent({ ...DATABASE_REPO, @@ -323,34 +330,25 @@ export const ratingsCommand = new Promise( }) satisfies SimplifiedPlayer, ); }); - const leagueInfo = - regionRatingsInfo.detail === undefined - ? regionRatingsInfo.leagues[leagueIndex] - : undefined; + const leagueInfo = regionRatingsInfo.leagues[leagueIndex]; midnightLeaderboard = subcommandGroup === 'current' - ? regionRatingsInfo.detail === undefined - ? await getMidnightLeaderboard( - region, - regionRatingsInfo.current_season, - ) - : undefined + ? await getMidnightLeaderboard( + region, + regionRatingsInfo.current_season, + ) : await getMidnightLeaderboard(region, season!); players = result; playersBefore = result ? result[0].position - 1 : 0; playersAfter = - regionRatingsInfo.detail === undefined && result !== undefined - ? regionRatingsInfo.count - result[result.length - 1].position - : 0; + regionRatingsInfo.count - result[result.length - 1].position; titleName = `${ leagueInfo ? leagueInfo.title : 'No Ongoing Season' } - ${REGION_NAMES_SHORT[region]}`; - titleImage = leagueInfo - ? leagueInfo.big_icon.startsWith('http') - ? leagueInfo.big_icon - : `https:${leagueInfo.big_icon}` - : undefined; + titleImage = leagueInfo.big_icon.startsWith('http') + ? leagueInfo.big_icon + : `https:${leagueInfo.big_icon}`; titleDescription = `Top ${limit} players`; } else { const { region, id } = await resolvePlayerFromCommand(interaction); @@ -372,6 +370,9 @@ export const ratingsCommand = new Promise( response.json(), )) as RatingsInfo; }); + + if (regionRatingsInfo.detail !== undefined) return noOngoingSeason; + const accountInfo = await getWargamingResponse( `https://api.wotblitz.${region}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, ); @@ -383,29 +384,26 @@ export const ratingsCommand = new Promise( const regionSubdomain = regionToRegionSubdomain(region); const neighbors = subcommandGroup === 'current' - ? regionRatingsInfo.detail === undefined - ? await fetch( - `https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/user/${id}/?neighbors=${Math.round( - limit / 2, - )}`, + ? await fetch( + `https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/user/${id}/?neighbors=${Math.round( + limit / 2, + )}`, + ) + .then( + (response) => response.json() as Promise, + ) + .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, - ) - .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 .getContent({ ...DATABASE_REPO, @@ -453,21 +451,17 @@ export const ratingsCommand = new Promise( }); midnightLeaderboard = subcommandGroup === 'current' - ? regionRatingsInfo.detail === undefined - ? await getMidnightLeaderboard( - region, - regionRatingsInfo.current_season, - ) - : undefined + ? await getMidnightLeaderboard( + region, + regionRatingsInfo.current_season, + ) : await getMidnightLeaderboard(region, season!); if (clan && neighbors) titleNameDiscriminator = `[${clan.tag}]`; players = neighbors; - titleImage = neighbors - ? clan - ? `https://wotblitz-gc.gcdn.co/icons/clanEmblems1x/clan-icon-v2-${clan.emblem_set_id}.png` - : undefined + titleImage = clan + ? `https://wotblitz-gc.gcdn.co/icons/clanEmblems1x/clan-icon-v2-${clan.emblem_set_id}.png` : undefined; titleName = neighbors ? accountInfo[id].nickname @@ -475,24 +469,20 @@ export const ratingsCommand = new Promise( titleDescription = 'Ratings neighbours'; playersBefore = neighbors ? neighbors[0].position - 1 : 0; playersAfter = - regionRatingsInfo.detail === undefined - ? neighbors - ? regionRatingsInfo.count - - neighbors[neighbors.length - 1].position - : 0 - : 0; + regionRatingsInfo.count - neighbors[neighbors.length - 1].position; playerId = id; } - const items = players?.map((player) => { - const reward = - regionRatingsInfo.detail === undefined - ? regionRatingsInfo.rewards.find( - (reward) => - player.position >= reward.from_position && - player.position <= reward.to_position, - ) - : undefined; + const resolvedRegionRatingsInfo: typeof regionRatingsInfo & { + detail: undefined; + } = regionRatingsInfo; + + const items = players.map((player) => { + const reward = resolvedRegionRatingsInfo.rewards.find( + (reward) => + player.position >= reward.from_position && + player.position <= reward.to_position, + ); const midnightIndex = midnightLeaderboard?.findIndex( (item) => item.id === player.id, );