import {Gamesystem} from "./gamesystems/Gamesystem"; import {ScriptAccount} from "./scriptAccounts/ScriptAccount"; import {Transition} from "./gamesystems/transitions/Transition"; import {State} from "./gamesystems/states/State"; import {ProductGamesystem} from "./gamesystems/ProductGamesystem"; import {SimpleGamesystem} from "./gamesystems/SimpleGamesystem"; import {Character} from "./characters/Character"; import {ModelComponentType} from "./ModelComponentType"; import {TemplateType} from "./templates/TemplateType"; import {SimpleTemplateGamesystem} from "./templates/simpleGamesystem/SimpleTemplateGamesystem"; import {ProductTemplateSystem} from "./templates/productGamesystem/ProductTemplateSystem"; import {ProductTemplateCreator} from "./templates/productGamesystem/ProductTemplateCreator"; import {CharacterRelation} from "./characters/CharacterRelation"; import {ItemGroup} from "./inventory/ItemGroup"; import {AbstractItemGroup} from "./inventory/AbstractItemGroup"; import {ConcreteItemGroup} from "./inventory/ConcreteItemGroup"; import {Item} from "./inventory/Item"; import {ItemgroupUtilities} from "./utils/ItemgroupUtilities"; import {ItemGroupCharacteristic} from "./inventory/ItemgroupCharacteristic"; export class GameModel { gameModelName: string gamesystems: Gamesystem[] = []; scriptAccounts: ScriptAccount[] = []; characters: Character[] = [] itemgroups: ItemGroup[] = [] constructor(gameModelName: string) { this.gameModelName = gameModelName; } addAbstractItemgroup(groupName: string, parentgroup: AbstractItemGroup | undefined) { //Ensure that Itemgroup does not exist if(parentgroup == undefined) { if(GameModel.findItemgroupByName(groupName, this.itemgroups) == undefined) { const itemgroup = new AbstractItemGroup(groupName, "", ModelComponentType.ITEMGROUP) this.itemgroups.push(itemgroup); itemgroup.addItemgroupCharacteristic(new ItemGroupCharacteristic("Test0", "", itemgroup)); } } else { if(GameModel.findItemgroupByName(groupName, parentgroup.children) == undefined) { parentgroup.addChildItemgroup(new AbstractItemGroup(groupName, "", ModelComponentType.ITEMGROUP)); } } } addConcreteItemgroup(groupName: string, parentgroup: AbstractItemGroup | undefined) { //Ensure that Itemgroup does not exist if(parentgroup == undefined) { if(GameModel.findItemgroupByName(groupName, this.itemgroups) == undefined) { this.itemgroups.push(new ConcreteItemGroup(groupName, "", ModelComponentType.ITEMGROUP)); } } else { if(GameModel.findItemgroupByName(groupName, parentgroup.children) == undefined) { parentgroup.addChildItemgroup(new ConcreteItemGroup(groupName, "", ModelComponentType.ITEMGROUP)); } } } addItem(itemName: string, conceteItemgroupName: string) { const itemgroup = GameModel.findItemgroupByName(conceteItemgroupName, this.itemgroups); if(itemgroup instanceof ConcreteItemGroup) { const itemgroups: ItemGroup[] = ItemgroupUtilities.findItemgroupPathToItemgroup(conceteItemgroupName, this.itemgroups); if(itemgroups.length > 0) { const item = new Item(itemName, "", ModelComponentType.ITEM ) itemgroup.addItem(item, itemgroups) return item; } } } public static findItemgroupByName(name: string, itemgroups: ItemGroup[]) { const itemgroupQueue: ItemGroup[] = itemgroups.concat(); while(itemgroupQueue.length > 0 ) { const currentItemgroup = itemgroupQueue.shift()!; if(currentItemgroup.componentName === name) { return currentItemgroup; } if(currentItemgroup instanceof AbstractItemGroup) { currentItemgroup.children.forEach(itemgroup => itemgroupQueue.push(itemgroup)); } } } addGamesystem(gamesystem: Gamesystem) { if(this.findGamesystem(gamesystem.componentName) == undefined) { this.gamesystems.push(gamesystem); } } removeGamesystem(gamesystem : Gamesystem) { if(gamesystem.parentGamesystem == undefined) { this.gamesystems = this.gamesystems.filter(g => g !== gamesystem); } else { (gamesystem.parentGamesystem as ProductGamesystem).removeChildGamesystem(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; } createSimpleGamesystem(gamesystemName: string, templateType: TemplateType | undefined, pushToTop: boolean = true) { if(gamesystemName != undefined && this.findGamesystem(gamesystemName) == undefined) { let simpleGamesystem: SimpleGamesystem if(templateType == undefined) { simpleGamesystem = new SimpleGamesystem(gamesystemName, "") } else { simpleGamesystem = new SimpleTemplateGamesystem(gamesystemName, "", templateType) } if(pushToTop) { this.gamesystems.push(simpleGamesystem) } return simpleGamesystem; } return undefined; } createSimpleGamesystemWithParent(gamesystemName: string, parentGamesystemName: string, templateType: TemplateType | undefined) { const simpleGamesystem = this.createSimpleGamesystem(gamesystemName, templateType, false)!; const parentGamesystem = this.findGamesystem(parentGamesystemName) if(parentGamesystem == undefined) {return undefined} let parentProductGamesystem: ProductGamesystem; if(parentGamesystem instanceof SimpleTemplateGamesystem) { console.log("Simple Template") parentProductGamesystem = ProductTemplateCreator.constructTemplateFromSimpleGamesystem(parentGamesystem, this, templateType!) } else if(parentGamesystem instanceof SimpleGamesystem) { if(simpleGamesystem instanceof SimpleTemplateGamesystem) { parentProductGamesystem = ProductTemplateCreator.constructTemplateFromSimpleGamesystem(parentGamesystem, this, templateType!) } else { console.log("Test") parentProductGamesystem = ProductGamesystem.constructFromSimpleGamesystem(parentGamesystem, this); console.log(this.gamesystems) } } else { if(simpleGamesystem instanceof SimpleTemplateGamesystem) { parentProductGamesystem = ProductTemplateCreator.convertProductToTemplate(parentGamesystem as ProductGamesystem, this, templateType!) } else { parentProductGamesystem = parentGamesystem as ProductGamesystem } } parentProductGamesystem.addChildGamesystem(simpleGamesystem); simpleGamesystem.parentGamesystem = parentProductGamesystem; return simpleGamesystem; } createGamesystem(gamesystemName: string, parentGamesystemName: string | undefined, templateType: TemplateType | 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; } } createCharacter(characterName: string) { const searchedCharacter = this.characters.find(character => character.componentName === characterName); if(searchedCharacter == undefined) { const character = new Character(characterName, ""); this.createCharacterRelation(character) this.characters.push(character) return character } return undefined } private createCharacterRelation(addedCharacter: Character) { this.characters.forEach(character => { const characterRelation = new CharacterRelation(character, addedCharacter); character.addCharacterRelation(characterRelation); addedCharacter.addCharacterRelation(characterRelation); }) } removeScriptAccount(scriptAccount: ScriptAccount) { if(scriptAccount != undefined) { this.scriptAccounts = this.scriptAccounts.filter(s => s != scriptAccount); } } removeCharacter(character: Character) { if(character != undefined) { this.characters = this.characters.filter(c => c.componentName !== character.componentName) } } 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)); } } } addScriptAccount(scriptAccount: ScriptAccount) { this.scriptAccounts.push(scriptAccount); } generateProductSystemContents() { this.gamesystems.forEach(gamesystem => { if(gamesystem instanceof ProductGamesystem) { gamesystem.generateFromChildsystems(); } }) } getTemplateSystems(templateType: TemplateType) { const requestedTemplates: Gamesystem[] = [] const gamesystemQueue: Gamesystem[] = this.gamesystems.concat(); while(gamesystemQueue.length > 0) { const currentGamesystem = gamesystemQueue.shift()!; if(currentGamesystem instanceof SimpleTemplateGamesystem && currentGamesystem.templateType === templateType) { requestedTemplates.push(currentGamesystem) } else if(currentGamesystem instanceof ProductTemplateSystem && currentGamesystem.templateType === templateType) { requestedTemplates.push(currentGamesystem) } if(currentGamesystem instanceof ProductGamesystem) { currentGamesystem.innerGamesystems.forEach(innerGamesystem => gamesystemQueue.push(innerGamesystem)) } } return requestedTemplates; } get itemgroupsAsList() { let itemgroupQueue: ItemGroup[] = this.itemgroups.concat(); const result: ItemGroup[] = [] while(itemgroupQueue.length > 0) { const currentGroup = itemgroupQueue.shift()!; if(currentGroup instanceof AbstractItemGroup) { itemgroupQueue = itemgroupQueue.concat(currentGroup.children); } result.push(currentGroup); } return result; } }