s3si.ts/src/exporters/file.ts

78 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-10-24 14:45:36 -04:00
import { CoopInfo, GameExporter, VsInfo } from "../types.ts";
2022-10-22 06:40:20 -04:00
import { path } from "../../deps.ts";
2022-10-20 09:45:59 -04:00
import { NSOAPP_VERSION, S3SI_VERSION } from "../constant.ts";
2022-10-24 14:45:36 -04:00
import { parseHistoryDetailId } from "../utils.ts";
2022-10-20 09:45:59 -04:00
2022-10-21 07:37:11 -04:00
export type FileExporterType = {
2022-10-20 09:45:59 -04:00
type: "VS" | "COOP";
nsoVersion: string;
s3siVersion: string;
exportTime: string;
2022-10-24 14:45:36 -04:00
data: VsInfo | CoopInfo;
2022-10-20 09:45:59 -04:00
};
2022-10-21 03:56:43 -04:00
/**
* Don't save url in exported file
*/
function replacer(key: string, value: unknown): unknown {
return ["url", "maskImageUrl", "overlayImageUrl"].includes(key)
? undefined
: value;
}
2022-10-20 09:45:59 -04:00
/**
* Exporter to file.
*
* This is useful for debugging. It will write each battle detail to a file.
2022-10-20 23:14:29 -04:00
* Timestamp is used as filename. Example: 20210101T000000Z.json
2022-10-20 09:45:59 -04:00
*/
2022-10-24 14:45:36 -04:00
export class FileExporter implements GameExporter {
2022-10-20 09:45:59 -04:00
name = "file";
constructor(private exportPath: string) {
}
2022-10-20 23:14:29 -04:00
getFilenameById(id: string) {
2022-10-24 14:45:36 -04:00
const { uid, timestamp } = parseHistoryDetailId(id);
2022-10-21 03:56:43 -04:00
2022-10-22 06:40:20 -04:00
return `${uid}_${timestamp}Z.json`;
2022-10-20 23:14:29 -04:00
}
2022-10-24 14:45:36 -04:00
async exportGame(info: VsInfo | CoopInfo) {
2022-10-20 09:45:59 -04:00
await Deno.mkdir(this.exportPath, { recursive: true });
2022-10-24 14:45:36 -04:00
const filename = this.getFilenameById(info.detail.id);
2022-10-20 09:45:59 -04:00
const filepath = path.join(this.exportPath, filename);
const body: FileExporterType = {
2022-10-24 14:45:36 -04:00
type: info.type === "VsInfo" ? "VS" : "COOP",
2022-10-20 09:45:59 -04:00
nsoVersion: NSOAPP_VERSION,
s3siVersion: S3SI_VERSION,
exportTime: new Date().toISOString(),
2022-10-24 14:45:36 -04:00
data: info,
2022-10-20 09:45:59 -04:00
};
2022-10-21 03:56:43 -04:00
await Deno.writeTextFile(
filepath,
JSON.stringify(body, replacer),
);
2022-10-31 00:33:00 -04:00
return {
url: filepath,
};
2022-10-20 09:45:59 -04:00
}
2022-10-24 14:45:36 -04:00
async notExported({ list }: { list: string[] }): Promise<string[]> {
const out: string[] = [];
for (const id of list) {
2022-10-20 23:14:29 -04:00
const filename = this.getFilenameById(id);
const filepath = path.join(this.exportPath, filename);
const isFile = await Deno.stat(filepath).then((f) => f.isFile).catch(() =>
false
);
2022-10-20 22:57:08 -04:00
if (!isFile) {
out.push(id);
}
}
return out;
2022-10-20 09:45:59 -04:00
}
}