import {Gamesystem} from "./gamesystems/Gamesystem"; import {ScriptAccount} from "./scriptAccounts/ScriptAccount"; import {Transition} from "./gamesystems/Transition"; import {State} from "./gamesystems/State"; import {ProductGamesystem} from "./gamesystems/ProductGamesystem"; import {SimpleGamesystem} from "./gamesystems/SimpleGamesystem"; import {StorageModel} from "./StorageModel"; export class GameModel { private readonly _gameModelName: string private _gamesystems: Gamesystem[] = []; private _scriptAccounts: ScriptAccount[] = []; constructor(gameModelName: string) { this._gameModelName = gameModelName; } get gameModelName(): string { return this._gameModelName; } get gamesystems(): Gamesystem[] { return this._gamesystems; } get scriptAccounts(): ScriptAccount[] { return this._scriptAccounts; } addGamesystem(gamesystem: Gamesystem) { if(this.findGamesystem(gamesystem.componentName) == undefined) { this._gamesystems.push(gamesystem); } } removeGamesystem(gamesystem : Gamesystem) { this._gamesystems = this._gamesystems.filter(g => g !== gamesystem); } createScriptAccount(scriptAccountName: string) { if(scriptAccountName != undefined && scriptAccountName.length > 0) { const scriptAccount = new ScriptAccount(scriptAccountName, ""); const searchedScriptAccount = this.scriptAccounts.find(s => s.componentName === scriptAccount.componentName); if(searchedScriptAccount == undefined) { this.scriptAccounts.push(scriptAccount); return scriptAccount; } } return undefined; } createGamesystem(gamesystemName: string, parentGamesystemName: string | undefined) { if(gamesystemName != undefined && this.findGamesystem(gamesystemName) == undefined) { const simpleGamesystem = new SimpleGamesystem(gamesystemName); if(parentGamesystemName != undefined) { const parentGamesystem = this.findGamesystem(parentGamesystemName); if(parentGamesystem instanceof SimpleGamesystem) { const parentProductGamesystem = ProductGamesystem.constructFromSimpleGamesystem(parentGamesystem, this); parentProductGamesystem.addChildGamesystem(simpleGamesystem); simpleGamesystem.parentGamesystem = parentProductGamesystem; } else { const productParentGamesystem = parentGamesystem as ProductGamesystem; productParentGamesystem.addChildGamesystem(simpleGamesystem); simpleGamesystem.parentGamesystem = productParentGamesystem; } } else { this.gamesystems.push(simpleGamesystem); } return simpleGamesystem; } } removeScriptAccount(scriptAccount: ScriptAccount) { if(scriptAccount != undefined) { this._scriptAccounts = this.scriptAccounts.filter(s => s != scriptAccount); } } findGamesystem(gamesystemName: string) { const gamesystemQueue : Gamesystem, Transition>[] = []; this.gamesystems.forEach(gamesystem => gamesystemQueue.push(gamesystem)); while(gamesystemQueue.length > 0) { const currentGamesystem = gamesystemQueue.shift()!; if(currentGamesystem.componentName == gamesystemName) { return currentGamesystem; } else if(currentGamesystem instanceof ProductGamesystem) { const currentProductGamesystem = currentGamesystem as ProductGamesystem; currentProductGamesystem.innerGamesystems.forEach(gamesystem => gamesystemQueue.push(gamesystem)); } } } save() { const storageModels: StorageModel[] = []; this.scriptAccounts.forEach(scriptAccount => { const scriptAccountJson = scriptAccount.save(); storageModels.push({ fileName: scriptAccount.componentName, jsonString: scriptAccountJson, storageDir: "script-accounts" }) console.log(scriptAccount) }) return storageModels; } }