feat: artifact section

This commit is contained in:
2026-03-20 21:51:47 +07:00 Unverified
parent 4ca75243d4
commit 9b5e776f40
8 changed files with 219 additions and 84 deletions
+32 -10
View File
@@ -1,5 +1,5 @@
import { readdirSync } from "node:fs";
import { mkdir, readdir } from "node:fs/promises";
import { mkdir, readdir, rm } from "node:fs/promises";
import { join } from "node:path";
import { file, hash, write } from "bun";
import { TTLCache } from "enka.ts";
@@ -14,14 +14,13 @@ export class AssetManager {
private MAX = 300;
private approximateSize = 0;
async getMissingAssets() {
const avatars = await getEnkaAvatarStore();
const weapons = await getEnkaWeaponStore();
const relics = await getEnkaRelicStore();
await mkdir("store").catch(() => {});
const existing = new Set(await readdir("store"));
const missing = new Set<string>();
[
private async getAssetList() {
const [avatars, weapons, relics] = await Promise.all([
getEnkaAvatarStore(),
getEnkaWeaponStore(),
await getEnkaRelicStore(),
]);
return [
...Object.values(avatars).flatMap((a) => [
...a.Consts,
...Object.values(a.Skills),
@@ -30,7 +29,16 @@ export class AssetManager {
]),
...Object.values(weapons).flatMap((w) => [w.Icon, w.AwakenIcon]),
...Object.values(relics.Items).map((r) => r.Icon),
].map((n) => existing.has(hash(n).toString(36)) || missing.add(n));
];
}
async getMissingAssets() {
await mkdir("store").catch(() => {});
const existing = new Set(await readdir("store"));
const missing = new Set<string>();
(await this.getAssetList()).map(
(n) => existing.has(hash(n).toString(36)) || missing.add(n),
);
console.log(
`${missing.size} assets missing, ${existing.size} assets in store`,
@@ -50,6 +58,20 @@ export class AssetManager {
.then((d) => write(join("store", hash(name).toString(36)), d));
console.log("All missing assets fetched and stored");
this.purgeExtraAssets();
}
async purgeExtraAssets() {
const extra = new Set(await readdir("store"));
(await this.getAssetList()).map((n) => extra.delete(hash(n).toString(36)));
if (!extra.size) return;
console.log(
`Clearing extra assets\n${[...extra.values()]
.map((e) => `- ${e}`)
.join("\n")}`,
);
// biome-ignore lint/suspicious/useIterableCallbackReturn: there's no map
extra.forEach((f) => rm(join("store", f)));
}
async load(path: string) {