55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
// file: Web/game-snake-poc/frontend/ts/assets.ts
|
|
// version: 1
|
|
|
|
export interface SnakeWebAssets {
|
|
engineGeneration: number;
|
|
game: string;
|
|
gameKind: string;
|
|
}
|
|
|
|
interface CommonRuntimeAsset {
|
|
schema: number;
|
|
scope: string;
|
|
engine_generation: number;
|
|
}
|
|
|
|
interface GameRuntimeAsset {
|
|
schema: number;
|
|
game: string;
|
|
kind: string;
|
|
}
|
|
|
|
async function fetchJson<T>(relativePath: string): Promise<T> {
|
|
const response = await fetch(new URL(relativePath, document.baseURI));
|
|
if (!response.ok) {
|
|
throw new Error(`Asset runtime indisponible (${response.status}) : ${relativePath}`);
|
|
}
|
|
return (await response.json()) as T;
|
|
}
|
|
|
|
function validateCommonRuntime(asset: CommonRuntimeAsset): void {
|
|
if (asset.schema !== 1 || asset.scope !== "common" || asset.engine_generation !== 1) {
|
|
throw new Error("Asset common://data/runtime.json invalide pour le POC Web Snake.");
|
|
}
|
|
}
|
|
|
|
function validateGameRuntime(asset: GameRuntimeAsset): void {
|
|
if (asset.schema !== 1 || asset.game !== "game-snake-poc" || asset.kind !== "snake") {
|
|
throw new Error("Asset game://data/game.json invalide pour le POC Web Snake.");
|
|
}
|
|
}
|
|
|
|
export async function loadSnakeWebAssets(): Promise<SnakeWebAssets> {
|
|
const [commonRuntime, gameRuntime] = await Promise.all([
|
|
fetchJson<CommonRuntimeAsset>("./common/data/runtime.json"),
|
|
fetchJson<GameRuntimeAsset>("./game/data/game.json"),
|
|
]);
|
|
validateCommonRuntime(commonRuntime);
|
|
validateGameRuntime(gameRuntime);
|
|
return {
|
|
engineGeneration: commonRuntime.engine_generation,
|
|
game: gameRuntime.game,
|
|
gameKind: gameRuntime.kind,
|
|
};
|
|
}
|