2023-07-15 18:44:20 +00:00
|
|
|
import { Region } from '../../constants/regions';
|
2023-07-11 19:54:57 +00:00
|
|
|
import getPeriodNow from '../blitzstars/getPeriodNow';
|
|
|
|
|
import getPeriodStart from '../blitzstars/getPeriodStart';
|
|
|
|
|
import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo';
|
2023-08-04 18:24:20 +00:00
|
|
|
import { PeriodSize, PeriodType } from '../discord/addPeriodSubCommands';
|
2023-06-27 02:02:00 +00:00
|
|
|
import {
|
|
|
|
|
ResolvedPeriod,
|
2023-08-04 18:24:20 +00:00
|
|
|
getPeriodOptionName,
|
2023-07-11 19:54:57 +00:00
|
|
|
} from '../discord/resolvePeriodFromCommand';
|
2023-06-27 02:02:00 +00:00
|
|
|
|
2023-07-10 01:23:49 +00:00
|
|
|
export default function resolvePeriodFromURL(
|
2023-07-15 18:44:20 +00:00
|
|
|
server: Region,
|
2023-07-10 01:23:49 +00:00
|
|
|
urlString: string,
|
|
|
|
|
) {
|
2023-06-27 02:02:00 +00:00
|
|
|
let statsName: string;
|
|
|
|
|
let evolutionName: string;
|
|
|
|
|
let start: number;
|
|
|
|
|
let end: number;
|
|
|
|
|
const url = new URL(urlString);
|
|
|
|
|
const path = url.pathname.split('/').filter(Boolean);
|
2023-08-04 18:24:20 +00:00
|
|
|
const periodSubcommand = path[path.length - 1] as PeriodType;
|
|
|
|
|
const periodOption = url.searchParams.get('period') as PeriodSize;
|
2023-06-27 02:02:00 +00:00
|
|
|
|
2023-08-04 18:24:20 +00:00
|
|
|
if (periodSubcommand === 'custom') {
|
2023-06-27 02:02:00 +00:00
|
|
|
const startRaw = parseInt(url!.searchParams.get('start')!);
|
|
|
|
|
const endRaw = parseInt(url!.searchParams.get('end')!);
|
2023-08-09 17:56:35 +00:00
|
|
|
const startDaysAgoMin = Math.min(startRaw, endRaw);
|
|
|
|
|
const endDaysAgoMax = Math.max(startRaw, endRaw);
|
2023-06-27 02:02:00 +00:00
|
|
|
|
2023-08-09 17:56:35 +00:00
|
|
|
statsName = `${startDaysAgoMin} to ${endDaysAgoMax} days' statistics`;
|
|
|
|
|
evolutionName = `${startDaysAgoMin} to ${endDaysAgoMax} days' evolution`;
|
|
|
|
|
start = getTimeDaysAgo(server, endDaysAgoMax);
|
|
|
|
|
end = getTimeDaysAgo(server, startDaysAgoMin);
|
2023-06-27 02:02:00 +00:00
|
|
|
} else {
|
2023-08-04 18:24:20 +00:00
|
|
|
statsName = `${getPeriodOptionName(periodOption)} Statistics`;
|
|
|
|
|
evolutionName = `${getPeriodOptionName(periodOption)} Evolution`;
|
|
|
|
|
start = getPeriodStart(server, periodOption);
|
2023-06-27 02:02:00 +00:00
|
|
|
end = getPeriodNow();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 17:39:21 +00:00
|
|
|
return { statsName, evolutionName, start, end } satisfies ResolvedPeriod;
|
2023-06-27 02:02:00 +00:00
|
|
|
}
|