mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: added the new /today embeds
This commit is contained in:
1 parent
ab56689326
commit
bb2450eb3c
4 files changed
+77
-44
No files matched your search
+26
-4
@@ -24,26 +24,48 @@ export const todayCommand: CommandRegistry = {
|
|||||||
.addStringOption(addUsernameChoices)
|
.addStringOption(addUsernameChoices)
|
||||||
.addIntegerOption((option) =>
|
.addIntegerOption((option) =>
|
||||||
option
|
option
|
||||||
.setName('limit')
|
.setName('cutoff')
|
||||||
.setDescription(
|
.setDescription(
|
||||||
'The maximum number of tanks to display (default: Infinity)',
|
'The maximum number of tanks to display (default: Infinity)',
|
||||||
)
|
)
|
||||||
.setRequired(false)
|
.setRequired(false)
|
||||||
.setMinValue(1),
|
.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) {
|
async handler(interaction) {
|
||||||
const player = await resolvePlayerFromCommand(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 } = (
|
const { nickname } = (
|
||||||
await getWargamingResponse<AccountInfo>(
|
await getWargamingResponse<AccountInfo>(
|
||||||
`https://api.wotblitz.${player.region}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${player.id}`,
|
`https://api.wotblitz.${player.region}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${player.id}`,
|
||||||
)
|
)
|
||||||
)[player.id];
|
)[player.id];
|
||||||
const path = interactionToURL(interaction, player);
|
const path = interactionToURL(interaction, {
|
||||||
|
...player,
|
||||||
|
cutoff,
|
||||||
|
maximized,
|
||||||
|
'show-total': showTotal,
|
||||||
|
});
|
||||||
|
|
||||||
return [
|
return [
|
||||||
await today(player, limit, false),
|
await today(player, cutoff, maximized, showTotal, false),
|
||||||
primaryButton(path, 'Refresh'),
|
primaryButton(path, 'Refresh'),
|
||||||
linkButton(`${CYCLIC_API}/${path}`, 'Embed'),
|
linkButton(`${CYCLIC_API}/${path}`, 'Embed'),
|
||||||
linkButton(
|
linkButton(
|
||||||
|
|||||||
+47
-38
@@ -22,9 +22,13 @@ import { PossiblyPromise } from '../types/possiblyPromise';
|
|||||||
|
|
||||||
export default async function today(
|
export default async function today(
|
||||||
{ region: server, id }: ResolvedPlayer,
|
{ region: server, id }: ResolvedPlayer,
|
||||||
limit = Infinity,
|
cutoff = Infinity,
|
||||||
|
maximized = 4,
|
||||||
|
showTotal = true,
|
||||||
naked?: boolean,
|
naked?: boolean,
|
||||||
) {
|
) {
|
||||||
|
console.log(maximized);
|
||||||
|
|
||||||
const { diffed, order } = await getDiffedTankStats(
|
const { diffed, order } = await getDiffedTankStats(
|
||||||
server,
|
server,
|
||||||
id,
|
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`,
|
`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 careerTankStatsRaw = await getTankStats(server, id);
|
||||||
const careerStats: Record<number, AllStats> = {
|
const careerStats: Record<number, AllStats> = showTotal
|
||||||
0: accountInfo[id].statistics.all,
|
? {
|
||||||
};
|
0: accountInfo[id].statistics.all,
|
||||||
|
}
|
||||||
|
: {};
|
||||||
const allStatsToAccumulate: AllStats[] = [];
|
const allStatsToAccumulate: AllStats[] = [];
|
||||||
|
|
||||||
Object.entries(diffed).forEach(([, tankStats]) => {
|
Object.entries(diffed).forEach(([, tankStats]) => {
|
||||||
@@ -52,7 +58,7 @@ export default async function today(
|
|||||||
|
|
||||||
const accumulatedStats = sumStats(allStatsToAccumulate);
|
const accumulatedStats = sumStats(allStatsToAccumulate);
|
||||||
|
|
||||||
if (Object.keys(diffed).length > 0) {
|
if (showTotal && Object.keys(diffed).length > 0) {
|
||||||
diffed[0] = accumulatedStats;
|
diffed[0] = accumulatedStats;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +68,7 @@ export default async function today(
|
|||||||
>(async (accumulator, [tankIdString, tankStats]) => {
|
>(async (accumulator, [tankIdString, tankStats]) => {
|
||||||
const tankId = parseInt(tankIdString);
|
const tankId = parseInt(tankIdString);
|
||||||
|
|
||||||
return tankId === 0 || !(await tankAverages)[tankId]
|
return (showTotal && tankId === 0) || !(await tankAverages)[tankId]
|
||||||
? accumulator
|
? accumulator
|
||||||
: {
|
: {
|
||||||
...(await accumulator),
|
...(await accumulator),
|
||||||
@@ -73,7 +79,8 @@ export default async function today(
|
|||||||
const careerWN8s = await careerTankStatsRaw.reduce<
|
const careerWN8s = await careerTankStatsRaw.reduce<
|
||||||
PossiblyPromise<Record<number, number>>
|
PossiblyPromise<Record<number, number>>
|
||||||
>(async (accumulator, { tank_id }) => {
|
>(async (accumulator, { tank_id }) => {
|
||||||
return tank_id === 0 || (await tankAverages)[tank_id] === undefined
|
return (showTotal && tank_id === 0) ||
|
||||||
|
(await tankAverages)[tank_id] === undefined
|
||||||
? accumulator
|
? accumulator
|
||||||
: {
|
: {
|
||||||
...(await accumulator),
|
...(await accumulator),
|
||||||
@@ -86,38 +93,40 @@ export default async function today(
|
|||||||
const todayWN8sEntries = Object.entries(todayWN8s);
|
const todayWN8sEntries = Object.entries(todayWN8s);
|
||||||
const careerWN8sEntries = Object.entries(careerWN8s);
|
const careerWN8sEntries = Object.entries(careerWN8s);
|
||||||
|
|
||||||
todayWN8s[0] =
|
if (showTotal) {
|
||||||
todayWN8sEntries.reduce(
|
todayWN8s[0] =
|
||||||
(accumulator, [tankIdString, wn8]) =>
|
todayWN8sEntries.reduce(
|
||||||
accumulator + wn8 * diffed[Number(tankIdString)].battles,
|
(accumulator, [tankIdString, wn8]) =>
|
||||||
0,
|
accumulator + wn8 * diffed[Number(tankIdString)].battles,
|
||||||
) /
|
0,
|
||||||
todayWN8sEntries.reduce(
|
) /
|
||||||
(accumulator, [tankIdString]) =>
|
todayWN8sEntries.reduce(
|
||||||
accumulator + diffed[Number(tankIdString)].battles,
|
(accumulator, [tankIdString]) =>
|
||||||
0,
|
accumulator + diffed[Number(tankIdString)].battles,
|
||||||
);
|
0,
|
||||||
careerWN8s[0] =
|
);
|
||||||
careerWN8sEntries.reduce(
|
careerWN8s[0] =
|
||||||
(accumulator, [tankIdString, WN8]) =>
|
careerWN8sEntries.reduce(
|
||||||
isNaN(WN8)
|
(accumulator, [tankIdString, WN8]) =>
|
||||||
? accumulator
|
isNaN(WN8)
|
||||||
: accumulator + WN8 * careerStats[Number(tankIdString)].battles,
|
? accumulator
|
||||||
0,
|
: accumulator + WN8 * careerStats[Number(tankIdString)].battles,
|
||||||
) /
|
0,
|
||||||
careerWN8sEntries.reduce(
|
) /
|
||||||
(accumulator, [tankIdString, WN8]) =>
|
careerWN8sEntries.reduce(
|
||||||
isNaN(WN8)
|
(accumulator, [tankIdString, WN8]) =>
|
||||||
? accumulator
|
isNaN(WN8)
|
||||||
: accumulator + careerStats[Number(tankIdString)].battles,
|
? accumulator
|
||||||
0,
|
: accumulator + careerStats[Number(tankIdString)].battles,
|
||||||
);
|
0,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const rows = await Promise.all(
|
const rows = await Promise.all(
|
||||||
[
|
[
|
||||||
// this code unreadable on god will forget tomorrow no cap
|
// this code unreadable on god will forget tomorrow no cap
|
||||||
...(order.length === 0 ? [] : [0]),
|
...(order.length === 0 || !showTotal ? [] : [0]),
|
||||||
...(isFinite(limit) ? order.slice(0, limit) : order),
|
...(isFinite(cutoff) ? order.slice(0, cutoff) : order),
|
||||||
].map(async (id, index) => {
|
].map(async (id, index) => {
|
||||||
const tankStats = diffed[id];
|
const tankStats = diffed[id];
|
||||||
const career = careerStats[id];
|
const career = careerStats[id];
|
||||||
@@ -125,12 +134,12 @@ export default async function today(
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Breakdown.Row
|
<Breakdown.Row
|
||||||
isTank={id !== 0}
|
isTank={!showTotal || id !== 0}
|
||||||
tankType={tankopediaEntry?.type}
|
tankType={tankopediaEntry?.type}
|
||||||
treeType={tankopediaEntry ? await getTreeType(id) : undefined}
|
treeType={tankopediaEntry ? await getTreeType(id) : undefined}
|
||||||
minimized={index > 4}
|
minimized={showTotal ? index > maximized : index + 1 > maximized}
|
||||||
key={id}
|
key={id}
|
||||||
name={id === 0 ? 'Total' : await resolveTankName(id)}
|
name={showTotal && id === 0 ? 'Total' : await resolveTankName(id)}
|
||||||
winrate={tankStats.wins / tankStats.battles}
|
winrate={tankStats.wins / tankStats.battles}
|
||||||
careerWinrate={career.wins / career.battles}
|
careerWinrate={career.wins / career.battles}
|
||||||
WN8={isNaN(todayWN8s[id]) ? undefined : todayWN8s[id]}
|
WN8={isNaN(todayWN8s[id]) ? undefined : todayWN8s[id]}
|
||||||
|
|||||||
@@ -12,7 +12,9 @@ export const todayRoute: RouteRegistry = {
|
|||||||
|
|
||||||
return await today(
|
return await today(
|
||||||
player,
|
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,
|
true,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
+1
-1
@@ -31,7 +31,7 @@ const app = express();
|
|||||||
if (registry.handlesInteraction) return;
|
if (registry.handlesInteraction) return;
|
||||||
|
|
||||||
res.send(
|
res.send(
|
||||||
withAutoRefresh(await normalizeRouteReturnable(returnable), 10 * 1000),
|
withAutoRefresh(await normalizeRouteReturnable(returnable), 30 * 1000),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user