From 1f16903aa104669ebbdec1c96e694cf6ec18d3cb Mon Sep 17 00:00:00 2001 From: spacemeowx2 Date: Sat, 22 Oct 2022 18:40:20 +0800 Subject: [PATCH] feat: add user prefix to file exporter --- src/exporters/file.ts | 14 ++++---------- src/utils.ts | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/exporters/file.ts b/src/exporters/file.ts index c6f96d9..2af9b87 100644 --- a/src/exporters/file.ts +++ b/src/exporters/file.ts @@ -1,6 +1,7 @@ import { BattleExporter, VsBattle } from "../types.ts"; -import { base64, path } from "../../deps.ts"; +import { path } from "../../deps.ts"; import { NSOAPP_VERSION, S3SI_VERSION } from "../constant.ts"; +import { parseVsHistoryDetailId } from "../utils.ts"; export type FileExporterType = { type: "VS" | "COOP"; @@ -30,16 +31,9 @@ export class FileExporter implements BattleExporter { constructor(private exportPath: string) { } getFilenameById(id: string) { - const fullId = base64.decode(id); - const ts = new TextDecoder().decode( - fullId.slice(fullId.length - 52, fullId.length - 37), - ); + const { uid, timestamp } = parseVsHistoryDetailId(id); - if (!/\d{8}T\d{6}/.test(ts)) { - throw new Error("Invalid battle ID"); - } - - return `${ts}Z.json`; + return `${uid}_${timestamp}Z.json`; } async exportBattle(battle: VsBattle) { await Deno.mkdir(this.exportPath, { recursive: true }); diff --git a/src/utils.ts b/src/utils.ts index c5cfdf2..ae0eff1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -82,6 +82,11 @@ export async function showError(p: Promise) { } } +/** + * @param id id of VeVsHistoryDetail + * @param namespace uuid namespace + * @returns + */ export function battleId( id: string, namespace = S3SI_NAMESPACE, @@ -90,3 +95,21 @@ export function battleId( const tsUuid = fullId.slice(fullId.length - 52, fullId.length); return uuid.v5.generate(namespace, tsUuid); } + +export function parseVsHistoryDetailId(id: string) { + const plainText = new TextDecoder().decode(base64.decode(id)); + + const re = /VsHistoryDetail-([a-z0-9-]+):(\w+):(\d{8}T\d{6})_([0-9a-f-]{36})/; + if (!re.test(plainText)) { + throw new Error(`Invalid battle ID: ${plainText}`); + } + + const [, uid, listType, timestamp, uuid] = plainText.match(re)!; + + return { + uid, + listType, + timestamp, + uuid, + }; +}