feat(catalog) : sync Lunaris updates from Discord
This commit is contained in:
+37
-3
@@ -21,15 +21,21 @@ import type {
|
||||
CatalogTalent,
|
||||
} from "@/lib/catalog/lunaris";
|
||||
import { customTravelers, isUpstreamTraveler } from "@/lib/catalog/travelers";
|
||||
import { withCatalogSyncLock } from "@/lib/catalog/sync-lock";
|
||||
import {
|
||||
compareLunarisVersions,
|
||||
lunarisVersionSchema,
|
||||
} from "@/lib/catalog/version";
|
||||
import { getMediaStorage } from "@/lib/media/storage";
|
||||
|
||||
const VERSION_URL = "https://api.lunaris.moe/data/version.json";
|
||||
const API_ROOT = "https://api.lunaris.moe/data";
|
||||
|
||||
const versionSchema = z.object({
|
||||
version: z.string().regex(/^\d+(?:\.\d+)+$/u),
|
||||
version: lunarisVersionSchema,
|
||||
timestamp: z.number().int().optional(),
|
||||
});
|
||||
const currentVersionSchema = z.object({ version: lunarisVersionSchema });
|
||||
const characterSchema = z.record(
|
||||
z.string(),
|
||||
z.object({
|
||||
@@ -179,8 +185,7 @@ async function uploadAssets(
|
||||
return missing;
|
||||
}
|
||||
|
||||
export async function processCatalogSync(): Promise<void> {
|
||||
const version = await getLatestLunarisVersion();
|
||||
async function syncCatalogVersion(version: string): Promise<void> {
|
||||
const [charactersRaw, weaponsRaw, artifactsRaw] = await Promise.all([
|
||||
getJson(`${API_ROOT}/${version}/charlist.json`),
|
||||
getJson(`${API_ROOT}/${version}/weaponlist.json`),
|
||||
@@ -491,3 +496,32 @@ export async function processCatalogSync(): Promise<void> {
|
||||
acl: "public-read",
|
||||
});
|
||||
}
|
||||
|
||||
export type CatalogSyncResult = "synced" | "unchanged";
|
||||
|
||||
export async function getCurrentCatalogVersion(): Promise<string | null> {
|
||||
const storage = await getMediaStorage();
|
||||
const current = storage.file("catalog/current.json");
|
||||
if (!(await current.exists())) return null;
|
||||
return currentVersionSchema.parse(await current.json()).version;
|
||||
}
|
||||
|
||||
export async function processCatalogSync(
|
||||
requestedVersion?: string,
|
||||
): Promise<CatalogSyncResult> {
|
||||
const version = lunarisVersionSchema.parse(
|
||||
requestedVersion ?? (await getLatestLunarisVersion()),
|
||||
);
|
||||
|
||||
return withCatalogSyncLock(async () => {
|
||||
const currentVersion = await getCurrentCatalogVersion();
|
||||
if (
|
||||
currentVersion &&
|
||||
compareLunarisVersions(version, currentVersion) <= 0
|
||||
) {
|
||||
return "unchanged";
|
||||
}
|
||||
await syncCatalogVersion(version);
|
||||
return "synced";
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user