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();
let resultUrl: string | undefined;
try {
const { url } = await statInkExporter.exportGame(detail);
resultUrl = url;
const result = await statInkExporter.exportGame(detail);
resultUrl = result.status === "success" ? result.url : undefined;
} catch (e) {
console.log("Failed to export game", e);
// try to re-export using cached data

View File

@ -3,20 +3,32 @@ import {
ExportResult,
Game,
GameExporter,
Summary,
VsInfo,
} from "../types.ts";
import { path } from "../../deps.ts";
import { NSOAPP_VERSION, S3SI_VERSION } from "../constant.ts";
import { parseHistoryDetailId, urlSimplify } from "../utils.ts";
export type FileExporterType = {
type: "VS" | "COOP";
export type FileExporterTypeCommon = {
nsoVersion: string;
s3siVersion: 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
*/
@ -62,6 +74,9 @@ export class FileExporter implements GameExporter {
const content = await Deno.readTextFile(filepath);
const body = JSON.parse(content) as FileExporterType;
if (body.type === "SUMMARY") {
continue;
}
if (body.type === "VS" && type === "VsInfo") {
out.push({
id: body.data.detail.id,
@ -85,22 +100,58 @@ export class FileExporter implements GameExporter {
const content = await Deno.readTextFile(filepath);
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> {
await Deno.mkdir(this.exportPath, { recursive: true });
const filename = this.getFilenameById(info.detail.id);
const filepath = path.join(this.exportPath, filename);
const body: FileExporterType = {
type: info.type === "VsInfo" ? "VS" : "COOP",
const common: FileExporterTypeCommon = {
nsoVersion: NSOAPP_VERSION,
s3siVersion: S3SI_VERSION,
exportTime: new Date().toISOString(),
};
const dataType = info.type === "VsInfo"
? {
type: "VS" as const,
data: info,
}
: {
type: "COOP" as const,
data: info,
};
const body: FileExporterType = {
...common,
...dataType,
};
await Deno.writeTextFile(

View File

@ -15,6 +15,7 @@ import {
} from "./types.ts";
import { DEFAULT_ENV, Env } from "./env.ts";
import { getBulletToken, getGToken } from "./iksm.ts";
import { parseHistoryDetailId } from "./utils.ts";
export class Splatnet3 {
protected profile: Profile;
@ -231,7 +232,22 @@ export class Splatnet3 {
);
const HistoryRecordQuery = await this.request(Queries.HistoryRecordQuery);
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 {
uid,
ConfigureAnalyticsQuery,
HistoryRecordQuery,
CoopHistoryQuery,

View File

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