feat: merged stats and tankstats

This commit is contained in:
TresAbhi committed 2023-06-19 17:35:28 +00:00
1 parent 0cf0e63fda
commit d9690460d5
14 files changed
+195 -338

No files matched your search

+1 -5
View File
@@ -1,12 +1,8 @@
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import { go } from 'fuzzysort';
import errorWithCause from '../process/errorWithCause.js';
import { TANKS, tankopedia } from './tankopedia.js';
export default async function resolveTankId(
interaction: ChatInputCommandInteraction<CacheType>,
tank: string,
) {
export default async function resolveTankId(tank: string) {
const number = Number(tank);
if (Number.isNaN(number)) {
+5 -11
View File
@@ -1,18 +1,12 @@
import { StatPeriod } from '../options/addPeriodChoices.js';
import getPeriodStartFromDaysAgo from './getPeriodStartFromDaysAgo.js';
import { Period } from '../options/addPeriodSubCommands.js';
import getTimeDaysAgo from './getTimeDaysAgo.js';
export default function getPeriodicStart(period: StatPeriod) {
export default function getPeriodStart(period: Exclude<Period, 'custom'>) {
if (period === 'career') {
return -Infinity;
} else if (period === 'today') {
const now = new Date();
now.setHours(5, 0, 0, 0);
// If it's after 5:00 AM today, subtract one day
if (now.getTime() > Date.now()) now.setDate(now.getDate() - 1);
return now.getTime() / 1000;
return getTimeDaysAgo(0);
} else {
return getPeriodStartFromDaysAgo(Number(period));
return getTimeDaysAgo(Number(period));
}
}
@@ -1,9 +0,0 @@
export default function getPeriodStartFromDaysAgo(period: number) {
const daysAgo = Number(period) - 1;
const periodStart = new Date();
periodStart.setDate(periodStart.getDate() - daysAgo);
periodStart.setHours(5, 0, 0, 0);
return periodStart.getTime() / 1000;
}
+12
View File
@@ -0,0 +1,12 @@
export default function getTimeDaysAgo(period: number) {
const now = new Date();
now.setHours(5, 0, 0, 0);
// if it's after 5:00 AM today, subtract one day
if (now.getTime() > Date.now()) now.setDate(now.getDate() - 1);
// subtract the period number of days from now
now.setDate(now.getDate() - period);
return now.getTime() / 1000;
}
-21
View File
@@ -1,21 +0,0 @@
import { SlashCommandSubcommandBuilder } from 'discord.js';
export default function addCustomPeriodOption(
option: SlashCommandSubcommandBuilder,
) {
return option
.addIntegerOption((option) =>
option
.setName('start')
.setDescription('Start of period in days ago from today')
.setMinValue(0)
.setRequired(true),
)
.addIntegerOption((option) =>
option
.setName('end')
.setDescription('End of period in days ago from today')
.setMinValue(0)
.setRequired(true),
);
}
-23
View File
@@ -1,23 +0,0 @@
import { SlashCommandStringOption } from 'discord.js';
export type StatPeriod = 'today' | '30' | '60' | '90' | 'career';
export const statPeriodNames: Record<StatPeriod, string> = {
today: "Today's statistics",
30: '30-day statistics',
60: '60-day statistics',
90: '90-day statistics',
career: 'Career statistics',
};
export default function addPeriodChoices(option: SlashCommandStringOption) {
return option
.setName('period')
.setDescription('The period to get stats for')
.setChoices(
{ name: 'Today', value: 'today' satisfies StatPeriod },
{ name: '30 Days', value: '30' satisfies StatPeriod },
{ name: '60 Days', value: '60' satisfies StatPeriod },
{ name: '90 Days', value: '90' satisfies StatPeriod },
{ name: 'Career', value: 'career' satisfies StatPeriod },
)
.setRequired(true);
}
+60
View File
@@ -0,0 +1,60 @@
import {
SlashCommandSubcommandBuilder,
SlashCommandSubcommandGroupBuilder,
} from 'discord.js';
export type Period = 'today' | '30' | '60' | '90' | 'career' | 'custom';
export const PERIOD_NAMES: Record<Period, string> = {
today: "Today's statistics",
'30': "30 days' statistics",
'60': "60 days' statistics",
'90': "90 days' statistics",
career: 'Career statistics',
custom: 'Custom period',
};
export default function addPeriodSubCommands(
option: SlashCommandSubcommandGroupBuilder,
extra: (
option: SlashCommandSubcommandBuilder,
) => SlashCommandSubcommandBuilder = (option) => option,
) {
return option
.addSubcommand((option) =>
extra(option.setName('today').setDescription(PERIOD_NAMES.today)),
)
.addSubcommand((option) =>
extra(option.setName('30').setDescription(PERIOD_NAMES['30'])),
)
.addSubcommand((option) =>
extra(option.setName('60').setDescription(PERIOD_NAMES['60'])),
)
.addSubcommand((option) =>
extra(option.setName('90').setDescription(PERIOD_NAMES['90'])),
)
.addSubcommand((option) =>
extra(option.setName('career').setDescription(PERIOD_NAMES.career)),
)
.addSubcommand((option) =>
extra(
option
.setName('custom')
.setDescription(PERIOD_NAMES.custom)
.addIntegerOption((option) =>
option
.setName('start')
.setDescription('Start of period in days ago from today')
.setMinValue(0)
.setRequired(true),
)
.addIntegerOption((option) =>
option
.setName('end')
.setDescription('End of period in days ago from today')
.setMinValue(0)
.setRequired(true),
),
),
);
}