UNPKG

cbt-game-generator

Version:

Configuration generator for CBT animation apps

316 lines (298 loc) 10.6 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.GameConfigGenerator = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); class GameConfigGenerator { constructor(gamePath, gameName, gameType) { this.gamePath = gamePath; this.gameName = gameName; this.gameType = gameType; this.gameConfigDir = path.join(this.gamePath, 'configuration', 'game-config'); } generate() { fs.mkdirSync(this.gameConfigDir, { recursive: true }); this.generateGameBaseConfig(); this.generateFunction(); } generateGameBaseConfig() { const baseConfigContent = `import { I${this.gameName} } from "../../types/configuration"; import { E${this.gameName}Variants } from "../../constants/variantsConfig"; export const ${this.gameName}BaseProps: Partial<I${this.gameName}<E${this.gameName}Variants>> = { gameType: "${this.gameType}", assetDuration: 0, criteria: 1, listOfAssets: [], }; export const assetsBaseConfig = {};`; fs.writeFileSync(path.join(this.gameConfigDir, 'gameBaseConfig.ts'), baseConfigContent); } generateFunction() { const functionContent = `import { EGameLevel } from "../../../../../shared/constants/gameStates"; import { ESourcesType } from "../../../../../shared/constants/resources"; import { E${this.gameName}Targets } from "../../constants/targets"; import { E${this.gameName}, EReinforcementTypes, AssetsMapping, } from "../../constants/variantsConfig"; export const generateActions = ( variant: "v1" | "v2" | "v3", ${this.gameName.toLowerCase()}Config: Record<string, any>, changeLanguage: boolean = false, ) => { return Object.entries(${this.gameName.toLowerCase()}Config).map(([target, room]) => ({ target: E${this.gameName}Targets[target as keyof typeof E${this.gameName}Targets], display: { id: E${this.gameName}[\`\${target}_LOTTIE\` as keyof typeof E${this.gameName}], }, backgroundDisplay: { id: E${this.gameName}[\`\${target}_WEBP\` as keyof typeof E${this.gameName}], }, facePrompt: { id: E${this.gameName}[\`\${target}_FACE_PROMPT\` as keyof typeof E${this.gameName}], }, halfFacePrompt: { id: E${this.gameName}[\`\${target}_FACE_PROMPT_HALF\` as keyof typeof E${this.gameName}], }, showVisualCue: variant != "v3", changeLanguage: changeLanguage, [EGameLevel.INTRO]: variant === "v1" ? { audio: [ { id: E${this.gameName}[\`WHAT_IS_THIS_AUDIO\` as keyof typeof E${this.gameName}], }, { id: E${this.gameName}[\`\${room}_AUDIO\` as keyof typeof E${this.gameName}], }, ], } : { audio: [ { id: E${this.gameName}[\`WHAT_IS_THIS_AUDIO\` as keyof typeof E${this.gameName}], }, ], }, [EGameLevel.INTRO_REPEAT]: variant === "v1" ? { audio: [ { id: E${this.gameName}[\`WHAT_IS_THIS_AUDIO\` as keyof typeof E${this.gameName}], }, ], } : {}, [EGameLevel.INTERACTION]: {}, [EGameLevel.INTRO_2]: variant == "v2" ? { audio: [ { id: E${this.gameName}[\`WHAT_IS_THIS_AUDIO\` as keyof typeof E${this.gameName}], }, { id: E${this.gameName}[\`\${room}_AUDIO\` as keyof typeof E${this.gameName}], }, ], } : { audio: [ { id: E${this.gameName}[\`WHAT_IS_THIS_AUDIO\` as keyof typeof E${this.gameName}], }, ], }, [EGameLevel.INTERACTION_2]: {}, [EGameLevel.INTRO_3]: variant === "v2" || variant === "v3" ? { audio: [], video: [ { id: E${this.gameName}[\`\${room}_FACE_PROMPT\` as keyof typeof E${this.gameName}], }, ], } : { audio: [ { id: E${this.gameName}[\`WHAT_IS_THIS_AUDIO\` as keyof typeof E${this.gameName}], }, ], video: [ { id: E${this.gameName}[\`\${room}_FACE_PROMPT\` as keyof typeof E${this.gameName}], }, ], }, [EGameLevel.INTERACTION_3]: {}, [EGameLevel.REINFORCEMENT]: { [EReinforcementTypes.SUCCESS]: { audio: [ { id: E${this.gameName}[\`SUCCESS_REINFORCEMENT\` as keyof typeof E${this.gameName}], }, { id: E${this.gameName}[\`\${room}_AUDIO\` as keyof typeof E${this.gameName}], }, { id: E${this.gameName}[\`CLAPPING_AUDIO\` as keyof typeof E${this.gameName}], }, ], }, [EReinforcementTypes.PARTIAL_SUCCESS]: { audio: [ { id: E${this.gameName}[\`PARTIAL_SUCCESS_REINFORCEMENT\` as keyof typeof E${this.gameName}], }, { id: E${this.gameName}[\`\${room}_AUDIO\` as keyof typeof E${this.gameName}], }, ], }, [EReinforcementTypes.FAILURE]: { audio: generateReinforcementAudio(room), video: [ { id: E${this.gameName}[\`\${room}_FACE_PROMPT_HALF\` as keyof typeof E${this.gameName}], }, ], }, }, [EGameLevel.EXIT]: { audio: [ { id: E${this.gameName}[\`EXIT_AUDIO\` as keyof typeof E${this.gameName}], }, ], }, })); }; export const generatesAudio = (room: any, variant: string) => { let audio: { id: E${this.gameName} }[] = []; audio = [ { id: E${this.gameName}[\`\${room}_AUDIO\` as keyof typeof E${this.gameName}], }, ]; return audio; }; export const generateReinforcementAudio = (room: any) => { let audio: { id: E${this.gameName} }[] = []; audio = [ { id: E${this.gameName}[\`THIS_IS_A_AUDIO\` as keyof typeof E${this.gameName}], }, { id: E${this.gameName}[\`\${room}_AUDIO\` as keyof typeof E${this.gameName}], }, ]; return audio; }; const getRandomItem = (array: AssetsMapping[]): AssetsMapping => { return array[Math.floor(Math.random() * array.length)]; }; const generateLottiePath = (lo: string, selected?: any) => { return [ \`expressive-skill/${this.convertToKebabCase(this.gameName)}/textures/\${lo}/\${selected.lottie}\`, selected.lottie2 ? \`expressive-skill/${this.convertToKebabCase(this.gameName)}/textures/\${lo}/\${selected.lottie2}\` : null, ].filter((each) => each !== null); }; const generateWebpPath = (lo: string, selected?: any) => { return [ \`expressive-skill/${this.convertToKebabCase(this.gameName)}/textures/\${lo}/\${selected.backgroundWebp}\`, selected.backgroundWebp2 ? \`expressive-skill/${this.convertToKebabCase(this.gameName)}/textures/\${lo}/\${selected.backgroundWebp2}\` : null, ].filter((each) => each !== null); }; export const generateRandomSelections = ( assetLottieWebpMapping: Record<string, AssetsMapping[]>, lo: string, ) => { const results: Record<string, any> = {}; Object.entries(assetLottieWebpMapping).forEach(([key, values]) => { const selected = getRandomItem(values); const lottieKey = \`\${key}_LOTTIE\`; const webpKey = \`\${key}_WEBP\`; const facePromptKey = \`\${key}_FACE_PROMPT\`; const halfFacePromptKey = \`\${key}_FACE_PROMPT_HALF\`; results[E${this.gameName}[facePromptKey as keyof typeof E${this.gameName}]] = { name: facePromptKey, type: ESourcesType.VIDEO, id: E${this.gameName}[facePromptKey as keyof typeof E${this.gameName}], path: [ \`expressive-skill/${this.convertToKebabCase(this.gameName)}/videos/\${lo}/\${selected.facePrompt}\`, ], }; results[E${this.gameName}[halfFacePromptKey as keyof typeof E${this.gameName}]] = { name: halfFacePromptKey, type: ESourcesType.VIDEO, id: E${this.gameName}[halfFacePromptKey as keyof typeof E${this.gameName}], path: [ \`expressive-skill/${this.convertToKebabCase(this.gameName)}/videos/\${lo}/\${selected.halfFacePrompt}\`, ], }; results[E${this.gameName}[lottieKey as keyof typeof E${this.gameName}]] = { name: lottieKey, type: ESourcesType.LOTTIE, id: E${this.gameName}[lottieKey as keyof typeof E${this.gameName}], path: generateLottiePath(lo, selected), }; results[E${this.gameName}[webpKey as keyof typeof E${this.gameName}]] = { name: webpKey, type: ESourcesType.TEXTURE, id: E${this.gameName}[webpKey as keyof typeof E${this.gameName}], path: generateWebpPath(lo, selected), }; }); return results; };`; fs.writeFileSync(path.join(this.gameConfigDir, 'function.ts'), functionContent); } convertToKebabCase(str) { return str .replace(/([a-z])([A-Z])/g, '$1-$2') .toLowerCase(); } } exports.GameConfigGenerator = GameConfigGenerator;