mirror of
https://github.com/samuelthomas2774/nxapi.git
synced 2026-10-24 10:35:14 -05:00
Add monitoring of SplatNet 2 data
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
export * as user from './user.js';
|
||||
export * as dumpResults from './dump-results.js';
|
||||
export * as dumpRecords from './dump-records.js';
|
||||
export * as monitor from './monitor.js';
|
||||
|
||||
198
src/cli/splatnet2/monitor.ts
Normal file
198
src/cli/splatnet2/monitor.ts
Normal file
@@ -0,0 +1,198 @@
|
||||
import createDebug from 'debug';
|
||||
import persist from 'node-persist';
|
||||
import mkdirp from 'mkdirp';
|
||||
import * as fs from 'fs/promises';
|
||||
import * as path from 'path';
|
||||
import SplatNet2Api from '../../api/splatnet2.js';
|
||||
import { getIksmToken, renewIksmToken } from './util.js';
|
||||
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
|
||||
import { Records, Stages, WebServiceError } from '../../api/splatnet2-types.js';
|
||||
import { Arguments as ParentArguments } from '../splatnet2.js';
|
||||
import { dumpCoopResults, dumpResults } from './dump-results.js';
|
||||
import { dumpProfileImage, dumpRecords } from './dump-records.js';
|
||||
|
||||
const debug = createDebug('cli:splatnet2:monitor');
|
||||
|
||||
export const command = 'monitor <directory>';
|
||||
export const desc = 'Monitor SplatNet 2 for new user records/battles/Salmon Run results';
|
||||
|
||||
export function builder(yargs: Argv<ParentArguments>) {
|
||||
return yargs.positional('directory', {
|
||||
describe: 'Directory to write record data to',
|
||||
type: 'string',
|
||||
demandOption: true,
|
||||
}).option('user', {
|
||||
describe: 'Nintendo Account ID',
|
||||
type: 'string',
|
||||
}).option('token', {
|
||||
describe: 'Nintendo Account session token',
|
||||
type: 'string',
|
||||
}).option('profile-image', {
|
||||
describe: 'Include profile image',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
}).option('favourite-stage', {
|
||||
describe: 'Favourite stage to include on profile image',
|
||||
type: 'string',
|
||||
}).option('favourite-colour', {
|
||||
describe: 'Favourite colour to include on profile image',
|
||||
type: 'string',
|
||||
}).option('battles', {
|
||||
describe: 'Include regular/ranked/private/festival battle results',
|
||||
type: 'boolean',
|
||||
default: true,
|
||||
}).option('battle-summary-image', {
|
||||
describe: 'Include regular/ranked/private/festival battle summary image',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
}).option('battle-images', {
|
||||
describe: 'Include regular/ranked/private/festival battle result images',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
}).option('coop', {
|
||||
describe: 'Include coop (Salmon Run) results',
|
||||
type: 'boolean',
|
||||
default: true,
|
||||
}).option('update-interval', {
|
||||
describe: 'Update interval in seconds',
|
||||
type: 'number',
|
||||
// 15 minutes to match splatnet2statink
|
||||
// When used with `nxapi nso notify/presence` this is 3 minutes by default, as the monitor is only
|
||||
// updated when playing Splatoon 2 online
|
||||
default: 15 * 60,
|
||||
});
|
||||
}
|
||||
|
||||
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 getIksmToken(storage, token, argv.zncProxyUrl);
|
||||
|
||||
const records = await splatnet.getRecords();
|
||||
const stages = await splatnet.getStages();
|
||||
|
||||
const i = new SplatNet2RecordsMonitor(storage, token, splatnet, stages, argv.directory, argv.zncProxyUrl);
|
||||
|
||||
i.update_interval = argv.updateInterval;
|
||||
|
||||
i.profile_image = argv.profileImage;
|
||||
i.favourite_stage = argv.favouriteStage;
|
||||
i.favourite_colour = argv.favouriteColour;
|
||||
|
||||
i.results = argv.battles;
|
||||
i.results_summary_image = argv.battleSummaryImage;
|
||||
i.result_images = argv.battleImages;
|
||||
i.coop_results = argv.coop;
|
||||
|
||||
i.cached_records = records;
|
||||
|
||||
console.log('Player %s (Splatoon 2 ID %s, NSA ID %s) level %d',
|
||||
records.records.player.nickname,
|
||||
records.records.unique_id,
|
||||
records.records.player.principal_id,
|
||||
records.records.player.player_rank,
|
||||
records.records.player.player_type);
|
||||
|
||||
await i.init();
|
||||
|
||||
while (true) {
|
||||
await i.loop();
|
||||
}
|
||||
}
|
||||
|
||||
export class SplatNet2RecordsMonitor {
|
||||
update_interval: number = 3 * 60; // 3 minutes in seconds
|
||||
|
||||
profile_image = true;
|
||||
favourite_stage: string | undefined = undefined;
|
||||
favourite_colour: string | undefined = undefined;
|
||||
|
||||
results = true;
|
||||
results_summary_image = true;
|
||||
result_images = true;
|
||||
coop_results = true;
|
||||
|
||||
/** Prevents redownloading user records on the first loop run */
|
||||
cached_records: Records | null = null;
|
||||
|
||||
constructor(
|
||||
public storage: persist.LocalStorage,
|
||||
public token: string,
|
||||
public splatnet: SplatNet2Api,
|
||||
public stages: Stages,
|
||||
public directory: string,
|
||||
public znc_proxy_url?: string
|
||||
) {}
|
||||
|
||||
async init() {
|
||||
await mkdirp(this.directory);
|
||||
}
|
||||
|
||||
async hasChanged(records: Records) {
|
||||
const latest_filename = 'splatnet2-records-' + records.records.unique_id + '-latest.json';
|
||||
const latest_file = path.join(this.directory, latest_filename);
|
||||
|
||||
const updated = new Date(records.records.update_time * 1000);
|
||||
|
||||
try {
|
||||
const {timestamp} = JSON.parse(await fs.readFile(latest_file, 'utf-8'));
|
||||
|
||||
return timestamp <= updated.getTime();
|
||||
} catch (err) {}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async update() {
|
||||
const records = this.cached_records ?? await this.splatnet.getRecords();
|
||||
this.cached_records = null;
|
||||
|
||||
if (!(await this.hasChanged(records))) {
|
||||
debug('No data changed');
|
||||
return;
|
||||
}
|
||||
|
||||
const nickname_and_icons = await this.splatnet.getUserNicknameAndIcon([records.records.player.principal_id]);
|
||||
|
||||
// This will also update the splatnet2-records-...-latest.json file
|
||||
await dumpRecords(this.directory, records.records.unique_id, records,
|
||||
nickname_and_icons.nickname_and_icons[0]);
|
||||
|
||||
if (this.profile_image) {
|
||||
await dumpProfileImage(this.splatnet, this.directory, records.records.unique_id, this.stages,
|
||||
nickname_and_icons.nickname_and_icons[0],
|
||||
this.favourite_stage, this.favourite_colour);
|
||||
}
|
||||
|
||||
if (this.results) {
|
||||
await dumpResults(this.splatnet, this.directory, records.records.unique_id,
|
||||
this.result_images, this.results_summary_image);
|
||||
}
|
||||
if (this.coop_results) {
|
||||
await dumpCoopResults(this.splatnet, this.directory, records.records.unique_id);
|
||||
}
|
||||
}
|
||||
|
||||
async loop() {
|
||||
try {
|
||||
await this.update();
|
||||
|
||||
await new Promise(rs => setTimeout(rs, this.update_interval * 1000));
|
||||
} catch (err) {
|
||||
// @ts-expect-error
|
||||
if ((err?.data as WebServiceError)?.code === 'AUTHENTICATION_ERROR') {
|
||||
// Token expired
|
||||
debug('Renewing iksm_session cookie');
|
||||
|
||||
await renewIksmToken(this.splatnet, this.storage, this.token, this.znc_proxy_url);
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user