cbt-game-generator
Version:
Configuration generator for CBT animation apps
286 lines (263 loc) • 9.06 kB
text/typescript
import * as fs from 'fs';
import * as path from 'path';
export class GameConfigGenerator {
private gameConfigDir: string;
constructor(
private gamePath: string,
private gameName: string,
private gameType: string
) {
this.gameConfigDir = path.join(this.gamePath, 'configuration', 'game-config');
}
generate(): void {
fs.mkdirSync(this.gameConfigDir, { recursive: true });
this.generateGameBaseConfig();
this.generateFunction();
}
private generateGameBaseConfig(): void {
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);
}
private generateFunction(): void {
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);
}
private convertToKebabCase(str: string): string {
return str
.replace(/([a-z])([A-Z])/g, '$1-$2')
.toLowerCase();
}
}