UNPKG

cbt-game-generator

Version:

Configuration generator for CBT animation apps

157 lines (137 loc) 4.7 kB
import * as path from 'path'; import { GameType } from './index'; import { TypesGenerator } from './generators/typesGenerator'; import { ConstantsGenerator } from './generators/constantsGenerator'; import { GameConfigGenerator as GameConfigGeneratorClass } from './generators/gameConfigGenerator'; import { VariantConfigGenerator } from './generators/variantConfigGenerator'; import { MainGenerator } from './generators/mainGenerator'; import { AssetDirectoryGenerator } from './generators/assetDirectoryGenerator'; import { FileUpdater } from './generators/fileUpdater'; import { GameMappingUpdater } from './generators/gameMappingUpdater'; export interface GameConfigGeneratorOptions { skillName: string; gameName: string; gameType: GameType; numberOfVariants: number; targets: { [key: string]: string[]; // subLo -> targets mapping }; isIncremental?: boolean; // Flag to indicate if this is an incremental update } export class GameConfigGenerator { private options: GameConfigGeneratorOptions; private gamePath: string; private fileUpdater: FileUpdater; private gameMappingUpdater: GameMappingUpdater; private convertToKebabCase(str: string): string { return str .replace(/([a-z])([A-Z])/g, '$1-$2') .toLowerCase(); } constructor(options: GameConfigGeneratorOptions) { this.options = options; this.gamePath = path.join( "src/experience/games/", options.skillName.toLowerCase(), this.convertToKebabCase(options.gameName) ); this.fileUpdater = new FileUpdater( this.gamePath, this.options.gameName, this.options.numberOfVariants ); // Generate a unique ID for the game const gameId = this.generateUniqueId(); this.gameMappingUpdater = new GameMappingUpdater( this.options.skillName, this.options.gameName, gameId ); } public generate(): void { if (this.options.isIncremental) { this.handleIncrementalUpdate(); } else { this.handleFullGeneration(); this.gameMappingUpdater.update(); } // Update game mappings } private handleFullGeneration(): void { // Generate types const typesGenerator = new TypesGenerator( this.gamePath, this.options.gameName, this.options.numberOfVariants ); typesGenerator.generate(); // Generate constants const constantsGenerator = new ConstantsGenerator( this.gamePath, this.options.gameName, this.options.gameType, this.options.numberOfVariants, this.options.targets ); constantsGenerator.generate(); // Generate game config const gameConfigGenerator = new GameConfigGeneratorClass( this.gamePath, this.options.gameName, this.options.gameType ); gameConfigGenerator.generate(); // Generate configurations for each subLo Object.keys(this.options.targets).forEach((subLo, index) => { const variantConfigGenerator = new VariantConfigGenerator( this.gamePath, this.options.gameName, this.options.numberOfVariants, index ); variantConfigGenerator.generate(); }); // Generate Main.tsx const mainGenerator = new MainGenerator( this.gamePath, this.options.gameName, this.options.numberOfVariants, Object.keys(this.options.targets).length ); mainGenerator.generate(); // Generate asset directories const assetDirectoryGenerator = new AssetDirectoryGenerator( this.options.skillName, this.options.gameName, this.options.targets ); assetDirectoryGenerator.generate(); } private handleIncrementalUpdate(): void { // Get the new sub-lo index const existingSubLos = Object.keys(this.options.targets).filter(key => key.startsWith('sub-lo-') ).map(key => parseInt(key.split('-')[2])); const maxSubLo = Math.max(...existingSubLos); const newSubLoIndex = maxSubLo; // Update types this.fileUpdater.updateTypes(newSubLoIndex, true); // Update constants this.fileUpdater.updateConstants(newSubLoIndex, this.options.targets[`sub-lo-${newSubLoIndex + 1}`]); // Copy and update sub-lo files from sub-lo-1 this.fileUpdater.copyAndUpdateSubLoFiles( newSubLoIndex, this.options.targets[`sub-lo-${newSubLoIndex + 1}`] ); // Update Main.tsx this.fileUpdater.updateMain(newSubLoIndex); } private generateUniqueId(): string { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; let result = ''; for (let i = 0; i < 8; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } return result; } }