Add a command and functions to get SplatNet 2 X Rank seasons

This commit is contained in:
Samuel Elliott
2022-06-26 18:00:47 +01:00
parent ed91f2d085
commit 1ddbe10f25
5 changed files with 243 additions and 23 deletions

View File

@@ -9,3 +9,4 @@ export * as schedule from './schedule.js';
export * as dumpResults from './dump-results.js';
export * as dumpRecords from './dump-records.js';
export * as monitor from './monitor.js';
export * as xRankSeasons from './x-rank-seasons.js';

View File

@@ -0,0 +1,62 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../splatnet2.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { getAllSeasons } from '../../api/splatnet2-xrank.js';
const debug = createDebug('cli:splatnet2:x-rank-seasons');
export const command = 'x-rank-seasons';
export const desc = 'Show X Rank seasons';
export function builder(yargs: Argv<ParentArguments>) {
return yargs.option('sort', {
describe: 'Sort',
type: 'string',
choices: ['asc', 'desc'],
default: 'asc',
}).option('json', {
describe: 'Output raw JSON',
type: 'boolean',
}).option('json-pretty-print', {
describe: 'Output pretty-printed JSON',
type: 'boolean',
});
}
type Arguments = YargsArguments<ReturnType<typeof builder>>;
export async function handler(argv: ArgumentsCamelCase<Arguments>) {
const sort_ascending = argv.sort !== 'desc';
if (argv.json || argv.jsonPrettyPrint) {
const result = {
seasons: [...getAllSeasons()],
};
console.log(JSON.stringify(result, null, argv.jsonPrettyPrint ? 4 : 0));
return;
}
const table = new Table({
head: [
'ID',
'Key',
'Start',
'End',
'Status',
],
});
for (const season of getAllSeasons(sort_ascending)) {
table.push([
season.id,
season.key,
season.start.toLocaleString('en-GB'),
season.end.toLocaleString('en-GB'),
season.complete ? 'Complete' : 'Calculating',
]);
}
console.log(table.toString());
}