mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-24 03:59:50 +02:00
feat: split resolvePeriod
This commit is contained in:
1 parent
abcb75f06e
commit
c221c06511
8 files changed
+73
-39
No files matched your search
@@ -31,6 +31,7 @@ export default {
|
|||||||
|
|
||||||
return await evolution(commandGroup, period, player, tankId);
|
return await evolution(commandGroup, period, player, tankId);
|
||||||
|
|
||||||
|
// TODO: implement this
|
||||||
// new ButtonBuilder()
|
// new ButtonBuilder()
|
||||||
// .setStyle(ButtonStyle.Link)
|
// .setStyle(ButtonStyle.Link)
|
||||||
// .setLabel('View on BlitzStars')
|
// .setLabel('View on BlitzStars')
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import addStatsSubCommandGroups from '../core/discord/addStatsSubCommandGroups.j
|
|||||||
import interactionToURL from '../core/discord/interactionToURL.js';
|
import interactionToURL from '../core/discord/interactionToURL.js';
|
||||||
import linkButton from '../core/discord/linkButton.js';
|
import linkButton from '../core/discord/linkButton.js';
|
||||||
import primaryButton from '../core/discord/primaryButton.js';
|
import primaryButton from '../core/discord/primaryButton.js';
|
||||||
|
import resolvePeriodFromButton from '../core/discord/resolvePeriodFromButton.js';
|
||||||
import resolvePeriodFromCommand from '../core/discord/resolvePeriodFromCommand.js';
|
import resolvePeriodFromCommand from '../core/discord/resolvePeriodFromCommand.js';
|
||||||
import resolvePlayer from '../core/discord/resolvePlayer.js';
|
import resolvePlayer from '../core/discord/resolvePlayer.js';
|
||||||
import tanksAutocomplete from '../core/discord/tanksAutocomplete.js';
|
import tanksAutocomplete from '../core/discord/tanksAutocomplete.js';
|
||||||
@@ -57,7 +58,7 @@ export default {
|
|||||||
const url = new URL(`${CYCLIC_API}/${interaction.customId}`);
|
const url = new URL(`${CYCLIC_API}/${interaction.customId}`);
|
||||||
const path = url.pathname.split('/').filter(Boolean);
|
const path = url.pathname.split('/').filter(Boolean);
|
||||||
const commandGroup = path[1] as StatType;
|
const commandGroup = path[1] as StatType;
|
||||||
const period = resolvePeriodFromCommand(interaction);
|
const period = resolvePeriodFromButton(interaction);
|
||||||
const player = await resolvePlayer(interaction);
|
const player = await resolvePlayer(interaction);
|
||||||
|
|
||||||
return await stats(
|
return await stats(
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import { ButtonInteraction, CacheType } from 'discord.js';
|
||||||
|
import { CYCLIC_API } from '../../constants/cyclic.js';
|
||||||
|
import resolvePeriodFromURL from '../express/resolvePeriodFromURL.js';
|
||||||
|
|
||||||
|
export default function resolvePeriodFromButton(
|
||||||
|
interaction: ButtonInteraction<CacheType>,
|
||||||
|
) {
|
||||||
|
return resolvePeriodFromURL(`${CYCLIC_API}/${interaction.customId}`);
|
||||||
|
}
|
||||||
@@ -1,10 +1,4 @@
|
|||||||
import {
|
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||||
ButtonInteraction,
|
|
||||||
CacheType,
|
|
||||||
ChatInputCommandInteraction,
|
|
||||||
} from 'discord.js';
|
|
||||||
import { Request } from 'express';
|
|
||||||
import { CYCLIC_API } from '../../constants/cyclic.js';
|
|
||||||
import getPeriodNow from '../blitzstars/getPeriodNow.js';
|
import getPeriodNow from '../blitzstars/getPeriodNow.js';
|
||||||
import getPeriodStart from '../blitzstars/getPeriodStart.js';
|
import getPeriodStart from '../blitzstars/getPeriodStart.js';
|
||||||
import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo.js';
|
import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo.js';
|
||||||
@@ -36,39 +30,17 @@ export interface ResolvedPeriod {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function resolvePeriodFromCommand(
|
export default function resolvePeriodFromCommand(
|
||||||
input:
|
interaction: ChatInputCommandInteraction<CacheType>,
|
||||||
| ChatInputCommandInteraction<CacheType>
|
|
||||||
| ButtonInteraction<CacheType>
|
|
||||||
| Request,
|
|
||||||
) {
|
) {
|
||||||
let statsName: string;
|
let statsName: string;
|
||||||
let evolutionName: string;
|
let evolutionName: string;
|
||||||
let start: number;
|
let start: number;
|
||||||
let end: number;
|
let end: number;
|
||||||
let url: URL;
|
const period = interaction.options.getSubcommand() as Period;
|
||||||
let period: Period;
|
|
||||||
|
|
||||||
if (input instanceof ChatInputCommandInteraction) {
|
|
||||||
period = input.options.getSubcommand() as Period;
|
|
||||||
} else {
|
|
||||||
url = new URL(
|
|
||||||
`${CYCLIC_API}${
|
|
||||||
input instanceof ButtonInteraction ? `/${input.customId}` : input.path
|
|
||||||
}`,
|
|
||||||
);
|
|
||||||
const path = url.pathname.split('/').filter(Boolean);
|
|
||||||
period = path[path.length - 1] as Period;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (period === 'custom') {
|
if (period === 'custom') {
|
||||||
const startRaw =
|
const startRaw = interaction.options.getInteger('start')!;
|
||||||
input instanceof ChatInputCommandInteraction
|
const endRaw = interaction.options.getInteger('end')!;
|
||||||
? input.options.getInteger('start')!
|
|
||||||
: parseInt(url!.searchParams.get('start')!);
|
|
||||||
const endRaw =
|
|
||||||
input instanceof ChatInputCommandInteraction
|
|
||||||
? input.options.getInteger('end')!
|
|
||||||
: parseInt(url!.searchParams.get('end')!);
|
|
||||||
const startMin = Math.min(startRaw, endRaw);
|
const startMin = Math.min(startRaw, endRaw);
|
||||||
const endMax = Math.max(startRaw, endRaw);
|
const endMax = Math.max(startRaw, endRaw);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import { Request } from 'express';
|
||||||
|
import { CYCLIC_API } from '../../constants/cyclic.js';
|
||||||
|
import resolvePeriodFromURL from './resolvePeriodFromURL.js';
|
||||||
|
|
||||||
|
export default function resolvePeriodFromRequest(request: Request) {
|
||||||
|
return resolvePeriodFromURL(`${CYCLIC_API}${request.path}`);
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import getPeriodNow from '../blitzstars/getPeriodNow.js';
|
||||||
|
import getPeriodStart from '../blitzstars/getPeriodStart.js';
|
||||||
|
import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo.js';
|
||||||
|
import { Period } from '../discord/addPeriodSubCommands.js';
|
||||||
|
import {
|
||||||
|
EVOLUTION_PERIOD_NAMES,
|
||||||
|
PERIOD_NAMES,
|
||||||
|
ResolvedPeriod,
|
||||||
|
} from '../discord/resolvePeriodFromCommand.js';
|
||||||
|
|
||||||
|
export default function resolvePeriodFromURL(urlString: string) {
|
||||||
|
let statsName: string;
|
||||||
|
let evolutionName: string;
|
||||||
|
let start: number;
|
||||||
|
let end: number;
|
||||||
|
const url = new URL(urlString);
|
||||||
|
const path = url.pathname.split('/').filter(Boolean);
|
||||||
|
const period = path[path.length - 1] as Period;
|
||||||
|
|
||||||
|
if (period === 'custom') {
|
||||||
|
const startRaw = parseInt(url!.searchParams.get('start')!);
|
||||||
|
const endRaw = parseInt(url!.searchParams.get('end')!);
|
||||||
|
const startMin = Math.min(startRaw, endRaw);
|
||||||
|
const endMax = Math.max(startRaw, endRaw);
|
||||||
|
|
||||||
|
statsName = `${startMin} to ${endMax} days' statistics`;
|
||||||
|
evolutionName = `${startMin} to ${endMax} days' evolution`;
|
||||||
|
start = getTimeDaysAgo(startMin);
|
||||||
|
end = getTimeDaysAgo(endMax);
|
||||||
|
} else {
|
||||||
|
statsName = PERIOD_NAMES[period];
|
||||||
|
evolutionName = EVOLUTION_PERIOD_NAMES[period];
|
||||||
|
start = getPeriodStart(period);
|
||||||
|
end = getPeriodNow();
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
statsName,
|
||||||
|
evolutionName,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
} satisfies ResolvedPeriod;
|
||||||
|
}
|
||||||
@@ -25,7 +25,7 @@ export default async function handleChatInputCommand(
|
|||||||
|
|
||||||
if (registry.handlesInteraction) return;
|
if (registry.handlesInteraction) return;
|
||||||
|
|
||||||
const reply = await normalizeInteractionReturnable(returnable);
|
const reply = await normalizeInteractionReturnable(returnable!);
|
||||||
|
|
||||||
if (psa.data) {
|
if (psa.data) {
|
||||||
if (!reply.embeds) reply.embeds = [];
|
if (!reply.embeds) reply.embeds = [];
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { BlitzServer } from '../constants/servers.js';
|
import { BlitzServer } from '../constants/servers.js';
|
||||||
import resolvePeriodFromCommand from '../core/discord/resolvePeriodFromCommand.js';
|
import resolvePeriodFromRequest from '../core/express/resolvePeriodFromRequest.js';
|
||||||
import stats, { StatType } from '../renderers/stats.js';
|
import stats, { StatType } from '../renderers/stats.js';
|
||||||
import { ResponderRegistry } from '../server.js';
|
import { ResponderRegistry } from '../server.js';
|
||||||
|
|
||||||
@@ -9,11 +9,12 @@ export default {
|
|||||||
inDevelopment: true,
|
inDevelopment: true,
|
||||||
|
|
||||||
async handler(req) {
|
async handler(req) {
|
||||||
console.log('yeah');
|
const type = req.params.type as StatType;
|
||||||
|
const period = resolvePeriodFromRequest(req);
|
||||||
|
|
||||||
return await stats(
|
return await stats(
|
||||||
req.params.type as StatType,
|
type,
|
||||||
resolvePeriodFromCommand(req),
|
period,
|
||||||
{
|
{
|
||||||
server: req.query.server as BlitzServer,
|
server: req.query.server as BlitzServer,
|
||||||
id: parseInt(req.query.id as string),
|
id: parseInt(req.query.id as string),
|
||||||
|
|||||||
Reference in new issue
Block a user