feat: add Ref city hover details to public maps
This commit is contained in:
@@ -1,18 +1,23 @@
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
|
||||
import { MapDefinitionSchema, type MapDefinition } from '@sammo-ts/logic';
|
||||
import { MapDefinitionSchema, RegionMapSchema, type MapDefinition } from '@sammo-ts/logic';
|
||||
|
||||
import { resolveWorkspaceRoot } from '../paths.js';
|
||||
|
||||
const REPO_ROOT = resolveWorkspaceRoot();
|
||||
const DEFAULT_MAP_ROOT = path.resolve(REPO_ROOT, 'resources', 'map');
|
||||
const DEFAULT_REGION_MAP_PATH = path.resolve(DEFAULT_MAP_ROOT, 'region_map.json');
|
||||
|
||||
export interface MapLoaderOptions {
|
||||
mapRoot?: string;
|
||||
filePrefix?: string;
|
||||
}
|
||||
|
||||
export interface RegionMapLoaderOptions {
|
||||
regionMapPath?: string;
|
||||
}
|
||||
|
||||
const readJsonFile = async (filePath: string): Promise<unknown> => {
|
||||
const raw = await fs.readFile(filePath, 'utf8');
|
||||
return JSON.parse(raw) as unknown;
|
||||
@@ -34,3 +39,17 @@ export const loadMapDefinitionByName = async (mapName: string, options?: MapLoad
|
||||
const mapPath = resolveMapDefinitionPath(mapName, options);
|
||||
return loadMapDefinition(mapPath);
|
||||
};
|
||||
|
||||
export const loadRegionDisplayMapByName = async (
|
||||
mapName: string,
|
||||
options?: RegionMapLoaderOptions
|
||||
): Promise<Record<number, string>> => {
|
||||
const raw = await readJsonFile(options?.regionMapPath ?? DEFAULT_REGION_MAP_PATH);
|
||||
const regionMaps = RegionMapSchema.parse(raw);
|
||||
const selected = regionMaps[mapName] ?? {};
|
||||
return Object.fromEntries(
|
||||
Object.entries(selected)
|
||||
.map(([key, value]) => [Number(key), value] as const)
|
||||
.filter(([key]) => Number.isSafeInteger(key))
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user