cbt-game-generator
Version:
Configuration generator for CBT animation apps
116 lines (115 loc) • 5.75 kB
JavaScript
"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 path = __importStar(require("path"));
const typesGenerator_1 = require("./generators/typesGenerator");
const constantsGenerator_1 = require("./generators/constantsGenerator");
const gameConfigGenerator_1 = require("./generators/gameConfigGenerator");
const variantConfigGenerator_1 = require("./generators/variantConfigGenerator");
const mainGenerator_1 = require("./generators/mainGenerator");
const assetDirectoryGenerator_1 = require("./generators/assetDirectoryGenerator");
const fileUpdater_1 = require("./generators/fileUpdater");
const gameMappingUpdater_1 = require("./generators/gameMappingUpdater");
class GameConfigGenerator {
convertToKebabCase(str) {
return str
.replace(/([a-z])([A-Z])/g, '$1-$2')
.toLowerCase();
}
constructor(options) {
this.options = options;
this.gamePath = path.join("src/experience/games/", options.skillName.toLowerCase(), this.convertToKebabCase(options.gameName));
this.fileUpdater = new fileUpdater_1.FileUpdater(this.gamePath, this.options.gameName, this.options.numberOfVariants);
// Generate a unique ID for the game
const gameId = this.generateUniqueId();
this.gameMappingUpdater = new gameMappingUpdater_1.GameMappingUpdater(this.options.skillName, this.options.gameName, gameId);
}
generate() {
if (this.options.isIncremental) {
this.handleIncrementalUpdate();
}
else {
this.handleFullGeneration();
this.gameMappingUpdater.update();
}
// Update game mappings
}
handleFullGeneration() {
// Generate types
const typesGenerator = new typesGenerator_1.TypesGenerator(this.gamePath, this.options.gameName, this.options.numberOfVariants);
typesGenerator.generate();
// Generate constants
const constantsGenerator = new constantsGenerator_1.ConstantsGenerator(this.gamePath, this.options.gameName, this.options.gameType, this.options.numberOfVariants, this.options.targets);
constantsGenerator.generate();
// Generate game config
const gameConfigGenerator = new gameConfigGenerator_1.GameConfigGenerator(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_1.VariantConfigGenerator(this.gamePath, this.options.gameName, this.options.numberOfVariants, index);
variantConfigGenerator.generate();
});
// Generate Main.tsx
const mainGenerator = new mainGenerator_1.MainGenerator(this.gamePath, this.options.gameName, this.options.numberOfVariants, Object.keys(this.options.targets).length);
mainGenerator.generate();
// Generate asset directories
const assetDirectoryGenerator = new assetDirectoryGenerator_1.AssetDirectoryGenerator(this.options.skillName, this.options.gameName, this.options.targets);
assetDirectoryGenerator.generate();
}
handleIncrementalUpdate() {
// 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);
}
generateUniqueId() {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < 8; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
}
exports.GameConfigGenerator = GameConfigGenerator;