Files
OpenFrontIO/src/core/game/TerrainMapFileLoader.ts
T
Aotumuri 43c98e506d fixed giantworldmap key (#1188)
## Description:
Fixed an issue where key was being displayed in a place it shouldn’t
have been.

https://discord.com/channels/1284581928254701718/1284581928833388619/1383774883472740433

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I understand that submitting code with bugs that could have been
caught through manual testing blocks releases and new features for all
contributors

## Please put your Discord username so you can be contacted if a bug or
regression is found:

aotumuri
2025-06-15 20:03:00 -04:00

112 lines
3.2 KiB
TypeScript

import { GameMapType } from "./Game";
import { NationMap } from "./TerrainMapLoader";
interface MapData {
mapBin: string;
miniMapBin: string;
nationMap: NationMap;
}
interface MapCache {
bin?: string;
miniMapBin?: string;
nationMap?: NationMap;
}
interface BinModule {
default: string;
}
interface NationMapModule {
default: NationMap;
}
// Mapping from GameMap enum values to file names
const MAP_FILE_NAMES: Record<GameMapType, string> = {
[GameMapType.World]: "WorldMap",
[GameMapType.GiantWorldMap]: "WorldMapGiant",
[GameMapType.Europe]: "Europe",
[GameMapType.Mena]: "Mena",
[GameMapType.NorthAmerica]: "NorthAmerica",
[GameMapType.Oceania]: "Oceania",
[GameMapType.BlackSea]: "BlackSea",
[GameMapType.Africa]: "Africa",
[GameMapType.Pangaea]: "Pangaea",
[GameMapType.Asia]: "Asia",
[GameMapType.Mars]: "Mars",
[GameMapType.SouthAmerica]: "SouthAmerica",
[GameMapType.Britannia]: "Britannia",
[GameMapType.GatewayToTheAtlantic]: "GatewayToTheAtlantic",
[GameMapType.Australia]: "Australia",
[GameMapType.Iceland]: "Iceland",
[GameMapType.EastAsia]: "EastAsia",
[GameMapType.BetweenTwoSeas]: "BetweenTwoSeas",
[GameMapType.FaroeIslands]: "FaroeIslands",
[GameMapType.DeglaciatedAntarctica]: "DeglaciatedAntarctica",
[GameMapType.EuropeClassic]: "EuropeClassic",
[GameMapType.FalklandIslands]: "FalklandIslands",
[GameMapType.Baikal]: "Baikal",
[GameMapType.Halkidiki]: "Halkidiki",
};
class GameMapLoader {
private maps: Map<GameMapType, MapCache>;
private loadingPromises: Map<GameMapType, Promise<MapData>>;
constructor() {
this.maps = new Map<GameMapType, MapCache>();
this.loadingPromises = new Map<GameMapType, Promise<MapData>>();
}
public async getMapData(map: GameMapType): Promise<MapData> {
const cachedMap = this.maps.get(map);
if (cachedMap?.bin && cachedMap?.nationMap) {
return cachedMap as MapData;
}
if (!this.loadingPromises.has(map)) {
this.loadingPromises.set(map, this.loadMapData(map));
}
const data = await this.loadingPromises.get(map)!;
this.maps.set(map, data);
return data;
}
private async loadMapData(map: GameMapType): Promise<MapData> {
const fileName = MAP_FILE_NAMES[map];
if (!fileName) {
throw new Error(`No file name mapping found for map: ${map}`);
}
const [binModule, miniBinModule, infoModule] = await Promise.all([
import(
`!!binary-loader!../../../resources/maps/${fileName}.bin`
) as Promise<BinModule>,
import(
`!!binary-loader!../../../resources/maps/${fileName}Mini.bin`
) as Promise<BinModule>,
import(
`../../../resources/maps/${fileName}.json`
) as Promise<NationMapModule>,
]);
return {
mapBin: binModule.default,
miniMapBin: miniBinModule.default,
nationMap: infoModule.default,
};
}
public isMapLoaded(map: GameMapType): boolean {
const mapData = this.maps.get(map);
return !!mapData?.bin && !!mapData?.nationMap;
}
public getLoadedMaps(): GameMapType[] {
return Array.from(this.maps.keys()).filter((map) => this.isMapLoaded(map));
}
}
export const terrainMapFileLoader = new GameMapLoader();