feat: added the new /today embeds

This commit is contained in:
TresAbhi committed 2023-08-10 01:31:05 +00:00
1 parent ab56689326
commit bb2450eb3c
4 files changed
+77 -44

No files matched your search

+26 -4
View File
@@ -24,26 +24,48 @@ export const todayCommand: CommandRegistry = {
.addStringOption(addUsernameChoices)
.addIntegerOption((option) =>
option
.setName('limit')
.setName('cutoff')
.setDescription(
'The maximum number of tanks to display (default: Infinity)',
)
.setRequired(false)
.setMinValue(1),
)
.addIntegerOption((option) =>
option
.setName('maximized')
.setDescription(
'The number of rows with full stats after which all are collapsed (default: 4)',
)
.setMinValue(0)
.setRequired(false),
)
.addBooleanOption((option) =>
option
.setName('show-total')
.setDescription('Show the total stats at the top (default: true)')
.setRequired(false),
),
async handler(interaction) {
const player = await resolvePlayerFromCommand(interaction);
const limit = interaction.options.getInteger('limit') ?? Infinity;
const cutoff = interaction.options.getInteger('cutoff') ?? undefined;
const maximized = interaction.options.getInteger('maximized') ?? undefined;
const showTotal = interaction.options.getBoolean('show-total') ?? undefined;
const { nickname } = (
await getWargamingResponse<AccountInfo>(
`https://api.wotblitz.${player.region}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${player.id}`,
)
)[player.id];
const path = interactionToURL(interaction, player);
const path = interactionToURL(interaction, {
...player,
cutoff,
maximized,
'show-total': showTotal,
});
return [
await today(player, limit, false),
await today(player, cutoff, maximized, showTotal, false),
primaryButton(path, 'Refresh'),
linkButton(`${CYCLIC_API}/${path}`, 'Embed'),
linkButton(
+47 -38
View File
@@ -22,9 +22,13 @@ import { PossiblyPromise } from '../types/possiblyPromise';
export default async function today(
{ region: server, id }: ResolvedPlayer,
limit = Infinity,
cutoff = Infinity,
maximized = 4,
showTotal = true,
naked?: boolean,
) {
console.log(maximized);
const { diffed, order } = await getDiffedTankStats(
server,
id,
@@ -38,9 +42,11 @@ export default async function today(
`https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
);
const careerTankStatsRaw = await getTankStats(server, id);
const careerStats: Record<number, AllStats> = {
0: accountInfo[id].statistics.all,
};
const careerStats: Record<number, AllStats> = showTotal
? {
0: accountInfo[id].statistics.all,
}
: {};
const allStatsToAccumulate: AllStats[] = [];
Object.entries(diffed).forEach(([, tankStats]) => {
@@ -52,7 +58,7 @@ export default async function today(
const accumulatedStats = sumStats(allStatsToAccumulate);
if (Object.keys(diffed).length > 0) {
if (showTotal && Object.keys(diffed).length > 0) {
diffed[0] = accumulatedStats;
}
@@ -62,7 +68,7 @@ export default async function today(
>(async (accumulator, [tankIdString, tankStats]) => {
const tankId = parseInt(tankIdString);
return tankId === 0 || !(await tankAverages)[tankId]
return (showTotal && tankId === 0) || !(await tankAverages)[tankId]
? accumulator
: {
...(await accumulator),
@@ -73,7 +79,8 @@ export default async function today(
const careerWN8s = await careerTankStatsRaw.reduce<
PossiblyPromise<Record<number, number>>
>(async (accumulator, { tank_id }) => {
return tank_id === 0 || (await tankAverages)[tank_id] === undefined
return (showTotal && tank_id === 0) ||
(await tankAverages)[tank_id] === undefined
? accumulator
: {
...(await accumulator),
@@ -86,38 +93,40 @@ export default async function today(
const todayWN8sEntries = Object.entries(todayWN8s);
const careerWN8sEntries = Object.entries(careerWN8s);
todayWN8s[0] =
todayWN8sEntries.reduce(
(accumulator, [tankIdString, wn8]) =>
accumulator + wn8 * diffed[Number(tankIdString)].battles,
0,
) /
todayWN8sEntries.reduce(
(accumulator, [tankIdString]) =>
accumulator + diffed[Number(tankIdString)].battles,
0,
);
careerWN8s[0] =
careerWN8sEntries.reduce(
(accumulator, [tankIdString, WN8]) =>
isNaN(WN8)
? accumulator
: accumulator + WN8 * careerStats[Number(tankIdString)].battles,
0,
) /
careerWN8sEntries.reduce(
(accumulator, [tankIdString, WN8]) =>
isNaN(WN8)
? accumulator
: accumulator + careerStats[Number(tankIdString)].battles,
0,
);
if (showTotal) {
todayWN8s[0] =
todayWN8sEntries.reduce(
(accumulator, [tankIdString, wn8]) =>
accumulator + wn8 * diffed[Number(tankIdString)].battles,
0,
) /
todayWN8sEntries.reduce(
(accumulator, [tankIdString]) =>
accumulator + diffed[Number(tankIdString)].battles,
0,
);
careerWN8s[0] =
careerWN8sEntries.reduce(
(accumulator, [tankIdString, WN8]) =>
isNaN(WN8)
? accumulator
: accumulator + WN8 * careerStats[Number(tankIdString)].battles,
0,
) /
careerWN8sEntries.reduce(
(accumulator, [tankIdString, WN8]) =>
isNaN(WN8)
? accumulator
: accumulator + careerStats[Number(tankIdString)].battles,
0,
);
}
const rows = await Promise.all(
[
// this code unreadable on god will forget tomorrow no cap
...(order.length === 0 ? [] : [0]),
...(isFinite(limit) ? order.slice(0, limit) : order),
...(order.length === 0 || !showTotal ? [] : [0]),
...(isFinite(cutoff) ? order.slice(0, cutoff) : order),
].map(async (id, index) => {
const tankStats = diffed[id];
const career = careerStats[id];
@@ -125,12 +134,12 @@ export default async function today(
return (
<Breakdown.Row
isTank={id !== 0}
isTank={!showTotal || id !== 0}
tankType={tankopediaEntry?.type}
treeType={tankopediaEntry ? await getTreeType(id) : undefined}
minimized={index > 4}
minimized={showTotal ? index > maximized : index + 1 > maximized}
key={id}
name={id === 0 ? 'Total' : await resolveTankName(id)}
name={showTotal && id === 0 ? 'Total' : await resolveTankName(id)}
winrate={tankStats.wins / tankStats.battles}
careerWinrate={career.wins / career.battles}
WN8={isNaN(todayWN8s[id]) ? undefined : todayWN8s[id]}
+3 -1
View File
@@ -12,7 +12,9 @@ export const todayRoute: RouteRegistry = {
return await today(
player,
req.query.limit ? parseInt(req.query.limit as string) : undefined,
req.query.cutoff ? parseInt(req.query.cutoff as string) : undefined,
req.query.maximized ? parseInt(req.query.maximized as string) : undefined,
req.query['show-total'] ? req.query['show-total'] === 'true' : undefined,
true,
);
},
+1 -1
View File
@@ -31,7 +31,7 @@ const app = express();
if (registry.handlesInteraction) return;
res.send(
withAutoRefresh(await normalizeRouteReturnable(returnable), 10 * 1000),
withAutoRefresh(await normalizeRouteReturnable(returnable), 30 * 1000),
);
});
});