refactor: add uid to file summary export

main
spacemeowx2 2022-11-26 23:15:40 +08:00 committed by imspace
parent 2d937478ab
commit 8e46df1868
4 changed files with 77 additions and 9 deletions

View File

@ -39,8 +39,8 @@ async function exportType(
const detail = await getContent(); const detail = await getContent();
let resultUrl: string | undefined; let resultUrl: string | undefined;
try { try {
const { url } = await statInkExporter.exportGame(detail); const result = await statInkExporter.exportGame(detail);
resultUrl = url; resultUrl = result.status === "success" ? result.url : undefined;
} catch (e) { } catch (e) {
console.log("Failed to export game", e); console.log("Failed to export game", e);
// try to re-export using cached data // try to re-export using cached data

View File

@ -3,20 +3,32 @@ import {
ExportResult, ExportResult,
Game, Game,
GameExporter, GameExporter,
Summary,
VsInfo, VsInfo,
} from "../types.ts"; } from "../types.ts";
import { path } from "../../deps.ts"; import { path } from "../../deps.ts";
import { NSOAPP_VERSION, S3SI_VERSION } from "../constant.ts"; import { NSOAPP_VERSION, S3SI_VERSION } from "../constant.ts";
import { parseHistoryDetailId, urlSimplify } from "../utils.ts"; import { parseHistoryDetailId, urlSimplify } from "../utils.ts";
export type FileExporterType = { export type FileExporterTypeCommon = {
type: "VS" | "COOP";
nsoVersion: string; nsoVersion: string;
s3siVersion: string; s3siVersion: string;
exportTime: string; exportTime: string;
data: VsInfo | CoopInfo;
}; };
export type FileExporterType =
& ({
type: "VS";
data: VsInfo;
} | {
type: "COOP";
data: CoopInfo;
} | {
type: "SUMMARY";
data: Summary;
})
& FileExporterTypeCommon;
/** /**
* Don't save url in exported file * Don't save url in exported file
*/ */
@ -62,6 +74,9 @@ export class FileExporter implements GameExporter {
const content = await Deno.readTextFile(filepath); const content = await Deno.readTextFile(filepath);
const body = JSON.parse(content) as FileExporterType; const body = JSON.parse(content) as FileExporterType;
if (body.type === "SUMMARY") {
continue;
}
if (body.type === "VS" && type === "VsInfo") { if (body.type === "VS" && type === "VsInfo") {
out.push({ out.push({
id: body.data.detail.id, id: body.data.detail.id,
@ -85,22 +100,58 @@ export class FileExporter implements GameExporter {
const content = await Deno.readTextFile(filepath); const content = await Deno.readTextFile(filepath);
const body = JSON.parse(content) as FileExporterType; const body = JSON.parse(content) as FileExporterType;
return body.data; // summary is excluded
return body.data as Game;
}, },
})); }));
} }
async exportSummary(summary: Summary): Promise<ExportResult> {
const filename = `${summary.uid}_summary.json`;
const filepath = path.join(this.exportPath, filename);
const body: FileExporterType = {
type: "SUMMARY",
nsoVersion: NSOAPP_VERSION,
s3siVersion: S3SI_VERSION,
exportTime: new Date().toISOString(),
data: summary,
};
await Deno.writeTextFile(
filepath,
JSON.stringify({
body,
}),
);
return {
status: "success",
url: filepath,
};
}
async exportGame(info: Game): Promise<ExportResult> { async exportGame(info: Game): Promise<ExportResult> {
await Deno.mkdir(this.exportPath, { recursive: true }); await Deno.mkdir(this.exportPath, { recursive: true });
const filename = this.getFilenameById(info.detail.id); const filename = this.getFilenameById(info.detail.id);
const filepath = path.join(this.exportPath, filename); const filepath = path.join(this.exportPath, filename);
const body: FileExporterType = { const common: FileExporterTypeCommon = {
type: info.type === "VsInfo" ? "VS" : "COOP",
nsoVersion: NSOAPP_VERSION, nsoVersion: NSOAPP_VERSION,
s3siVersion: S3SI_VERSION, s3siVersion: S3SI_VERSION,
exportTime: new Date().toISOString(), exportTime: new Date().toISOString(),
};
const dataType = info.type === "VsInfo"
? {
type: "VS" as const,
data: info, data: info,
}
: {
type: "COOP" as const,
data: info,
};
const body: FileExporterType = {
...common,
...dataType,
}; };
await Deno.writeTextFile( await Deno.writeTextFile(

View File

@ -15,6 +15,7 @@ import {
} from "./types.ts"; } from "./types.ts";
import { DEFAULT_ENV, Env } from "./env.ts"; import { DEFAULT_ENV, Env } from "./env.ts";
import { getBulletToken, getGToken } from "./iksm.ts"; import { getBulletToken, getGToken } from "./iksm.ts";
import { parseHistoryDetailId } from "./utils.ts";
export class Splatnet3 { export class Splatnet3 {
protected profile: Profile; protected profile: Profile;
@ -231,7 +232,22 @@ export class Splatnet3 {
); );
const HistoryRecordQuery = await this.request(Queries.HistoryRecordQuery); const HistoryRecordQuery = await this.request(Queries.HistoryRecordQuery);
const CoopHistoryQuery = await this.request(Queries.CoopHistoryQuery); const CoopHistoryQuery = await this.request(Queries.CoopHistoryQuery);
const getFirstBattleId = async () => {
const latest = await this.request(Queries.LatestBattleHistoriesQuery);
const id = latest?.latestBattleHistories?.historyGroups?.nodes?.[0]
?.historyDetails?.nodes?.[0]?.id;
return id;
};
const id = CoopHistoryQuery?.coopResult?.historyGroups?.nodes?.[0]
?.historyDetails?.nodes?.[0]?.id ?? await getFirstBattleId();
if (!id) {
throw new Error("No battle id found");
}
const { uid } = parseHistoryDetailId(id);
return { return {
uid,
ConfigureAnalyticsQuery, ConfigureAnalyticsQuery,
HistoryRecordQuery, HistoryRecordQuery,
CoopHistoryQuery, CoopHistoryQuery,

View File

@ -311,6 +311,7 @@ export type SummaryFetcher = {
}; };
export type Summary = { export type Summary = {
uid: string;
ConfigureAnalyticsQuery: RespMap[Queries.ConfigureAnalyticsQuery]; ConfigureAnalyticsQuery: RespMap[Queries.ConfigureAnalyticsQuery];
HistoryRecordQuery: RespMap[Queries.HistoryRecordQuery]; HistoryRecordQuery: RespMap[Queries.HistoryRecordQuery];
CoopHistoryQuery: RespMap[Queries.CoopHistoryQuery]; CoopHistoryQuery: RespMap[Queries.CoopHistoryQuery];