From 2d937478ab8a9796c633935c71a99f833a8bea6e Mon Sep 17 00:00:00 2001 From: spacemeowx2 Date: Sat, 26 Nov 2022 23:01:42 +0800 Subject: [PATCH] feat: add summary export to App --- README.md | 1 + s3si.ts | 4 +++- src/app.ts | 39 +++++++++++++++++++++++++++++++++++++-- src/splatnet3.ts | 14 ++++++++++++++ src/types.ts | 8 +++++++- 5 files changed, 62 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2cb0cef..689cf3a 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ Options: --monitor, -m Monitor mode --skip-mode , -s Skip mode (default: null) ("vs", "coop") + --with-summary Include summary in the output --help Show this help message and exit`, ``` diff --git a/s3si.ts b/s3si.ts index f7121ed..f904804 100644 --- a/s3si.ts +++ b/s3si.ts @@ -5,7 +5,7 @@ import { flags } from "./deps.ts"; const parseArgs = (args: string[]) => { const parsed = flags.parse(args, { string: ["profilePath", "exporter", "skipMode"], - boolean: ["help", "noProgress", "monitor"], + boolean: ["help", "noProgress", "monitor", "withSummary"], alias: { "help": "h", "profilePath": ["p", "profile-path"], @@ -13,6 +13,7 @@ const parseArgs = (args: string[]) => { "noProgress": ["n", "no-progress"], "monitor": ["m"], "skipMode": ["s", "skip-mode"], + "withSummary": "with-summary", }, }); return parsed; @@ -32,6 +33,7 @@ Options: --monitor, -m Monitor mode --skip-mode , -s Skip mode (default: null) ("vs", "coop") + --with-summary Include summary in the output --help Show this help message and exit`, ); Deno.exit(0); diff --git a/src/app.ts b/src/app.ts index a123da6..be9e13f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -15,6 +15,7 @@ export type Opts = { exporter: string; noProgress: boolean; monitor: boolean; + withSummary: boolean; skipMode?: string; cache?: Cache; stateBackend?: StateBackend; @@ -26,6 +27,7 @@ export const DEFAULT_OPTS: Opts = { exporter: "stat.ink", noProgress: false, monitor: false, + withSummary: false, env: DEFAULT_ENV, }; @@ -157,7 +159,7 @@ export class App { const skipMode = this.getSkipMode(); const errors: unknown[] = []; - if (skipMode.includes("vs")) { + if (skipMode.includes("vs") || exporters.length === 0) { this.env.logger.log("Skip exporting VS games."); } else { this.env.logger.log("Fetching battle list..."); @@ -211,7 +213,7 @@ export class App { stats = initStats(); - if (skipMode.includes("coop")) { + if (skipMode.includes("coop") || exporters.length === 0) { this.env.logger.log("Skip exporting coop games."); } else { this.env.logger.log("Fetching coop battle list..."); @@ -255,6 +257,39 @@ export class App { throw errors[0]; } } + + const summaryExporters = exporters.filter((e) => e.exportSummary); + if (!this.opts.withSummary || summaryExporters.length === 0) { + this.env.logger.log("Skip exporting summary."); + } else { + this.env.logger.log("Fetching summary..."); + const summary = await splatnet.getSummary(); + + await Promise.all( + summaryExporters.map((e) => + showError( + this.env, + e.exportSummary!(summary), + ).then((result) => { + if (result.status === "success") { + this.env.logger.log(`Exported summary to ${result.url}`); + } else if (result.status === "skip") { + this.env.logger.log(`Skipped exporting summary to ${e.name}`); + } else { + const _never: never = result; + } + }) + .catch((err) => { + errors.push(err); + this.env.logger.error(`\nFailed to export to ${e.name}:`, err); + }) + ), + ); + + if (errors.length > 0) { + throw errors[0]; + } + } } async monitor() { while (true) { diff --git a/src/splatnet3.ts b/src/splatnet3.ts index 9f1df2f..643508d 100644 --- a/src/splatnet3.ts +++ b/src/splatnet3.ts @@ -10,6 +10,7 @@ import { GraphQLResponse, Queries, RespMap, + Summary, VarsMap, } from "./types.ts"; import { DEFAULT_ENV, Env } from "./env.ts"; @@ -223,6 +224,19 @@ export class Splatnet3 { return resp; } + + async getSummary(): Promise { + const ConfigureAnalyticsQuery = await this.request( + Queries.ConfigureAnalyticsQuery, + ); + const HistoryRecordQuery = await this.request(Queries.HistoryRecordQuery); + const CoopHistoryQuery = await this.request(Queries.CoopHistoryQuery); + return { + ConfigureAnalyticsQuery, + HistoryRecordQuery, + CoopHistoryQuery, + }; + } } function getIdsFromGroups( diff --git a/src/types.ts b/src/types.ts index b846513..3d28af3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -310,13 +310,19 @@ export type SummaryFetcher = { ): Promise; }; +export type Summary = { + ConfigureAnalyticsQuery: RespMap[Queries.ConfigureAnalyticsQuery]; + HistoryRecordQuery: RespMap[Queries.HistoryRecordQuery]; + CoopHistoryQuery: RespMap[Queries.CoopHistoryQuery]; +}; + export type GameExporter = { name: string; notExported: ( { type, list }: { type: Game["type"]; list: string[] }, ) => Promise; exportGame: (game: Game) => Promise; - exportSummary?: (fetcher: SummaryFetcher) => Promise; + exportSummary?: (summary: Summary) => Promise; }; export type BankaraBattleHistories = {