feat: add summary export to App

main
spacemeowx2 2022-11-26 23:01:42 +08:00 committed by imspace
parent aabc9cf733
commit 2d937478ab
5 changed files with 62 additions and 4 deletions

View File

@ -24,6 +24,7 @@ Options:
--monitor, -m Monitor mode
--skip-mode <mode>, -s Skip mode (default: null)
("vs", "coop")
--with-summary Include summary in the output
--help Show this help message and exit`,
```

View File

@ -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 <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);

View File

@ -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) {

View File

@ -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<Summary> {
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<T extends { id: string }>(

View File

@ -310,13 +310,19 @@ export type SummaryFetcher = {
): Promise<RespMap[T]>;
};
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<string[]>;
exportGame: (game: Game) => Promise<ExportResult>;
exportSummary?: (fetcher: SummaryFetcher) => Promise<ExportResult>;
exportSummary?: (summary: Summary) => Promise<ExportResult>;
};
export type BankaraBattleHistories = {