feat: added limits to /today

This commit is contained in:
TresAbhi committed 2023-07-10 17:41:48 +00:00
1 parent 438feefa83
commit 297f2f6580
3 files changed
+46 -33

No files matched your search

+12 -2
View File
@@ -21,10 +21,20 @@ export const todayCommand: CommandRegistry = {
command: new SlashCommandBuilder()
.setName('today')
.setDescription('A general daily breakdown of your performance')
.addStringOption(addUsernameChoices),
.addStringOption(addUsernameChoices)
.addIntegerOption((option) =>
option
.setName('limit')
.setDescription(
'The maximum number of tanks to display (default: Infinity)',
)
.setRequired(false)
.setMinValue(1),
),
async handler(interaction) {
const player = await resolvePlayerFromCommand(interaction);
const limit = interaction.options.getInteger('limit') ?? Infinity;
const { nickname } = (
await getWargamingResponse<AccountInfo>(
`https://api.wotblitz.${player.server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${player.id}`,
@@ -33,7 +43,7 @@ export const todayCommand: CommandRegistry = {
const path = interactionToURL(interaction, player);
return [
await today(player),
await today(player, limit, false),
primaryButton(path, 'Refresh'),
linkButton(`${CYCLIC_API}/${path}`, 'Embed'),
linkButton(
+29 -30
View File
@@ -22,6 +22,7 @@ import { PossiblyPromise } from '../types/possiblyPromise.js';
export default async function today(
{ server, id }: ResolvedPlayer,
limit = Infinity,
naked?: boolean,
) {
const { diffed, order } = await getDiffedTankStats(
@@ -113,37 +114,35 @@ export default async function today(
);
const rows = await Promise.all(
[...(order.length === 0 ? [] : [0]), ...order].map(
async (tankId, index) => {
const tankStats = diffed[tankId];
const career = careerStats[tankId];
[
// this code unreadable on god will forget tomorrow no cap
...(order.length === 0 ? [] : [0]),
...(isFinite(limit) ? order.slice(0, limit) : order),
].map(async (tankId, index) => {
const tankStats = diffed[tankId];
const career = careerStats[tankId];
return (
<Breakdown.Row
naked={naked}
isListing={tankId !== 0}
minimized={index > 4}
key={tankId}
name={tankId === 0 ? 'Total' : await resolveTankName(tankId)}
winrate={tankStats.wins / tankStats.battles}
careerWinrate={career.wins / career.battles}
WN8={isNaN(todayWN8s[tankId]) ? undefined : todayWN8s[tankId]}
careerWN8={
isNaN(careerWN8s[tankId]) ? undefined : careerWN8s[tankId]
}
damage={tankStats.damage_dealt / tankStats.battles}
careerDamage={career.damage_dealt / career.battles}
battles={tankStats.battles}
careerBattles={career.battles}
icon={
tankId === 0
? undefined
: (await tankopedia)[tankId]?.images.normal
}
/>
);
},
),
return (
<Breakdown.Row
naked={naked}
isListing={tankId !== 0}
minimized={index > 4}
key={tankId}
name={tankId === 0 ? 'Total' : await resolveTankName(tankId)}
winrate={tankStats.wins / tankStats.battles}
careerWinrate={career.wins / career.battles}
WN8={isNaN(todayWN8s[tankId]) ? undefined : todayWN8s[tankId]}
careerWN8={isNaN(careerWN8s[tankId]) ? undefined : careerWN8s[tankId]}
damage={tankStats.damage_dealt / tankStats.battles}
careerDamage={career.damage_dealt / career.battles}
battles={tankStats.battles}
careerBattles={career.battles}
icon={
tankId === 0 ? undefined : (await tankopedia)[tankId]?.images.normal
}
/>
);
}),
);
return naked ? (
+5 -1
View File
@@ -10,6 +10,10 @@ export const todayRoute: RouteRegistry = {
async handler(req) {
const player = resolvePlayerFromRequest(req);
return await today(player, true);
return await today(
player,
req.query.limit ? parseInt(req.query.limit as string) : undefined,
true,
);
},
};