UNPKG

cbt-game-generator

Version:

Configuration generator for CBT animation apps

114 lines (98 loc) 3.41 kB
import * as fs from 'fs'; import * as path from 'path'; export class TypesGenerator { private typesDir: string; constructor( private gamePath: string, private gameName: string, private numberOfVariants: number ) { this.typesDir = path.join(this.gamePath, 'types'); } generate(): void { fs.mkdirSync(this.typesDir, { recursive: true }); this.generateConfiguration(); this.generateTagTypes(); } private generateConfiguration(): void { const configContent = `import { EGames } from "../../../../shared/constants/Games"; import { EGameLevel, EReinforcements, } from "../../../../shared/constants/gameStates"; import { ICommonConfiguration } from "../../../../shared/types/commonConfiguration"; import { ISources } from "../../../../shared/types/resources"; import { E${this.gameName}Targets } from "../constants/targets"; import { E${this.gameName}, E${this.gameName}Variants, } from "../constants/variantsConfig"; import { T${this.gameName}Tags } from "./tagTypes"; export interface IVariantsMapping extends Record<E${this.gameName}, T${this.gameName}Tags> { ${Array.from({ length: this.numberOfVariants }, (_, i) => ` [E${this.gameName}Variants.V${i + 1}]: E${this.gameName}Targets;` ).join('\n')} } export interface I${this.gameName}<Variant extends E${this.gameName}Variants> extends ICommonConfiguration<EGames.${this.gameName}, Variant> { segments: ISegments; } export interface ISegments { speechInteractionDuration?: number; speechSecondInteractionDuration?: number; actions: ISegmentActions[]; } export interface ISegmentActions { target: E${this.gameName}Targets; backgroundDisplay: IDisplayObjects; display: IDisplayObjects; showVisualCue: boolean; lottieAudio: IAudioPrompt; changeLanguage?: boolean; [EGameLevel.INTRO]: IGameLevelCommon; [EGameLevel.INTRO_REPEAT]: IGameLevelCommon; [EGameLevel.INTERACTION]: IGameLevelCommon; [EGameLevel.INTRO_2]: IGameLevelCommon; [EGameLevel.INTERACTION_2]: IGameLevelCommon; [EGameLevel.INTRO_3]: IGameLevelCommon; [EGameLevel.INTERACTION_3]: IGameLevelCommon; [EGameLevel.REINFORCEMENT]: IReinforcement; } export interface IDisplay { mainDisplay: IDisplayObjects[]; clueDisplay: IDisplayObjects[]; lottieDisplay: IDisplayObjects[]; } export interface IGameLevelCommon { audio?: IAudioPrompt[]; video?: IAudioPrompt[]; showPulse: boolean; } export interface IDisplayObjects { id: ISources["id"]; } export interface IAudioPrompt { id: ISources["id"]; delay?: number; } interface IReinforcement { [EReinforcements.SUCCESS]: IGameLevelCommon; [EReinforcements.PARTIAL_SUCCESS]: IGameLevelCommon; [EReinforcements.FAILURE]: IGameLevelCommon; showPulse?: boolean; }`; fs.writeFileSync(path.join(this.typesDir, 'configuration.ts'), configContent); } private generateTagTypes(): void { const tagTypesContent = `import { E${this.gameName}Targets } from "../constants/targets"; import { E${this.gameName}Variants } from "../constants/variantsConfig"; import { I${this.gameName} } from "./configuration"; export type T${this.gameName}Tags = E${this.gameName}Targets; export type T${this.gameName} = ${Array.from({ length: this.numberOfVariants }, (_, i) => ` | I${this.gameName}<E${this.gameName}Variants.V${i + 1}>` ).join('\n')};`; fs.writeFileSync(path.join(this.typesDir, 'tagTypes.ts'), tagTypesContent); } }