Initial Splatfest types and commands

This commit is contained in:
Samuel Elliott
2022-09-19 19:12:22 +01:00
parent c60442fa5e
commit 8012dba821
7 changed files with 394 additions and 5 deletions

View File

@@ -0,0 +1,92 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../splatnet3.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getBulletToken } from '../../common/auth/splatnet3.js';
const debug = createDebug('cli:splatnet3:festival');
export const command = 'festival <id>';
export const desc = 'Show details about a specific Splatfest in your region';
export function builder(yargs: Argv<ParentArguments>) {
return yargs.positional('id', {
describe: 'Splatfest ID',
type: 'string',
demandOption: true,
}).option('user', {
describe: 'Nintendo Account ID',
type: 'string',
}).option('token', {
describe: 'Nintendo Account session token',
type: 'string',
}).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 storage = await initStorage(argv.dataPath);
const usernsid = argv.user ?? await storage.getItem('SelectedUser');
const token: string = argv.token ||
await storage.getItem('NintendoAccountToken.' + usernsid);
const {splatnet, data} = await getBulletToken(storage, token, argv.zncProxyUrl, argv.autoUpdateSession);
const fest_records = await splatnet.getFestRecords();
const req_id = argv.id;
const encoded_req_id = Buffer.from(req_id).toString('base64');
const encoded_part_req_id = Buffer.from('Fest-' + req_id).toString('base64');
const fest = fest_records.data.festRecords.nodes.find(f => f.id === req_id ||
f.id === encoded_req_id || f.id === encoded_part_req_id);
if (!fest) {
throw new Error('Invalid Splatfest ID');
}
const detail = await splatnet.getFestDetail(fest.id);
const votes = await splatnet.getFestVotingStatus(fest.id);
if (argv.jsonPrettyPrint) {
console.log(JSON.stringify({fest: detail.data.fest, votes: votes.data.fest}, null, 4));
return;
}
if (argv.json) {
console.log(JSON.stringify({fest: detail.data.fest, votes: votes.data.fest}));
return;
}
console.log('Details', detail.data.fest);
const table = new Table({
head: [
'Name',
'State',
'Team',
],
});
for (const team of votes.data.fest.teams) {
for (const vote of team.votes.nodes) {
table.push([vote.playerName, 'Voted', team.teamName]);
}
for (const vote of team.preVotes.nodes) {
table.push([vote.playerName, 'Planning to vote', team.teamName]);
}
}
for (const vote of votes.data.fest.undecidedVotes.nodes) {
table.push([vote.playerName, 'Undecided', '-']);
}
console.log('Friends votes');
console.log(table.toString());
}

View File

@@ -0,0 +1,77 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../splatnet3.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getBulletToken } from '../../common/auth/splatnet3.js';
const debug = createDebug('cli:splatnet3:festivals');
export const command = 'festivals';
export const desc = 'List all Splatfests in your region';
export function builder(yargs: Argv<ParentArguments>) {
return yargs.option('user', {
describe: 'Nintendo Account ID',
type: 'string',
}).option('token', {
describe: 'Nintendo Account session token',
type: 'string',
}).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 storage = await initStorage(argv.dataPath);
const usernsid = argv.user ?? await storage.getItem('SelectedUser');
const token: string = argv.token ||
await storage.getItem('NintendoAccountToken.' + usernsid);
const {splatnet} = await getBulletToken(storage, token, argv.zncProxyUrl, argv.autoUpdateSession);
const fest_records = await splatnet.getFestRecords();
if (argv.jsonPrettyPrint) {
console.log(JSON.stringify({festRecords: fest_records.data.festRecords.nodes}, null, 4));
return;
}
if (argv.json) {
console.log(JSON.stringify({festRecords: fest_records.data.festRecords.nodes}));
return;
}
const table = new Table({
head: [
'ID',
'State',
'Start',
// 'L',
'Title',
'A',
'B',
'C',
],
});
for (const fest of fest_records.data.festRecords.nodes) {
const id_str = Buffer.from(fest.id, 'base64').toString() || fest.id;
table.push([
id_str,
fest.state,
fest.startTime,
// fest.lang,
fest.title,
...fest.teams.map(t => t.teamName),
]);
}
console.log(table.toString());
}

View File

@@ -2,5 +2,7 @@ export * as user from './user.js';
export * as token from './token.js';
export * as friends from './friends.js';
export * as schedule from './schedule.js';
export * as festivals from './festivals.js';
export * as festival from './festival.js';
export * as battles from './battles.js';
export * as dumpResults from './dump-results.js';

View File

@@ -34,5 +34,6 @@ export async function handler(argv: ArgumentsCamelCase<Arguments>) {
console.log('Player %s#%s (title %s, first played %s)',
history.data.currentPlayer.name,
history.data.currentPlayer.nameId,
history.data.playHistory.gameStartTime);
history.data.currentPlayer.byname,
new Date(history.data.playHistory.gameStartTime).toLocaleString());
}