From cb2094aa49783c8a92f64d4cfd4719c2d289f1b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=B6ckelmann?= Date: Thu, 9 May 2024 15:24:36 +0200 Subject: [PATCH] Parse Itemgroups --- app/storage/loader/GameModelLoader.js | 4 +- app/storage/loader/ItemLoader.js | 43 ++++++++++ src/app/app.component.ts | 6 ++ src/app/project/game-model/GameModel.ts | 10 --- .../parser/itemParser/ItemgroupParser.ts | 80 +++++++++++++++++++ src/app/project/serializer/ItemSerializer.ts | 55 +++++++++---- testModel/items/Clothing/Clothing.json | 1 - testModel/items/Clothing/Hose/Hose.json | 14 +--- .../items/Clothing/Oberteil/Oberteil.json | 14 +--- testModel/items/Inventory/Inventory.json | 12 --- 10 files changed, 174 insertions(+), 65 deletions(-) create mode 100644 app/storage/loader/ItemLoader.js diff --git a/app/storage/loader/GameModelLoader.js b/app/storage/loader/GameModelLoader.js index 1ff3639..5ffc6d5 100644 --- a/app/storage/loader/GameModelLoader.js +++ b/app/storage/loader/GameModelLoader.js @@ -17,8 +17,8 @@ class GameModelLoader { const storedScriptAccounts = this.loadScriptAccountComponents(); const storedGamesystems = this.loadGamesystems(); const storedCharacters = this.loadCharacters(); - const storedItemgroupsd = this.loadItemgroups(); - return new StoredGameModel_1.StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, [], []); + const storedItemgroups = this.loadItemgroups(); + return new StoredGameModel_1.StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, storedItemgroups, []); } loadScriptAccountComponents() { const scriptAccountDir = path.join(this.gameModelDir, ModelComponentFileDirectory_1.ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME); diff --git a/app/storage/loader/ItemLoader.js b/app/storage/loader/ItemLoader.js new file mode 100644 index 0000000..a454616 --- /dev/null +++ b/app/storage/loader/ItemLoader.js @@ -0,0 +1,43 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.ItemLoader = void 0; +const StoreComponent_1 = require("../StoreComponent"); +const FileUtils_1 = require("../FileUtils"); +const fs = require("node:fs"); +const path = require("node:path"); +const ModelComponentType_1 = require("../../../src/app/project/game-model/ModelComponentType"); +class ItemLoader { + constructor(itemDir) { + this.itemDir = itemDir; + } + loadItemgroups() { + return this.loadItemgroupsRecursively(this.itemDir); + } + loadItemgroupsRecursively(currentDir) { + let loadedItemgroups = []; + const itemgroupFiles = FileUtils_1.FileUtils.listFilesInDirectory(currentDir); + itemgroupFiles.forEach(itemgroupFile => { + if (fs.lstatSync(itemgroupFile).isDirectory()) { + const childgroup = this.loadItemgroupsRecursively(itemgroupFile); + loadedItemgroups = loadedItemgroups.concat(childgroup); + } + else if (path.basename(itemgroupFile).endsWith(".json") && this.fileRepresentsItemgroup(itemgroupFile)) { + const loadedItemgroup = this.loadSingleItemgroup(itemgroupFile); + loadedItemgroups.push(loadedItemgroup); + } + }); + return loadedItemgroups; + } + loadSingleItemgroup(itemgroupFile) { + if (itemgroupFile.endsWith(".json")) { + const data = fs.readFileSync(itemgroupFile, "utf-8"); + return new StoreComponent_1.StoreComponent(data, itemgroupFile, ModelComponentType_1.ModelComponentType.ITEMGROUP); + } + return undefined; + } + fileRepresentsItemgroup(file) { + return path.basename(path.dirname(file)) === path.parse(path.basename(file)).name; + } +} +exports.ItemLoader = ItemLoader; +//# sourceMappingURL=ItemLoader.js.map \ No newline at end of file diff --git a/src/app/app.component.ts b/src/app/app.component.ts index d0e1c85..81392b6 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -28,6 +28,7 @@ import {TemplateType} from "./project/game-model/templates/TemplateType"; import {TemplateTypeUtilities} from "./project/game-model/templates/TemplateTypeUtilities"; import {SimpleTemplateGamesystem} from "./project/game-model/templates/simpleGamesystem/SimpleTemplateGamesystem"; import {ItemSerializer} from "./project/serializer/ItemSerializer"; +import {ItemgroupParser} from "./project/parser/itemParser/ItemgroupParser"; @Component({ selector: 'app-root', @@ -224,6 +225,11 @@ export class AppComponent implements OnInit{ const characterParser = new CharacterParser(characterTemplateSystems, characterRelationTemplateSystems, gameModel.scriptAccounts); gameModel.characters = characterParser.parseCharacters(storedGameModel.storedCharacters) + const itemgroupParser = new ItemgroupParser(); + itemgroupParser.parseItemgroups(storedGameModel.storedItemgroups); + + gameModel.itemgroups = itemgroupParser.getParsedTopItemgroups(); + this.gameModel = gameModel; } diff --git a/src/app/project/game-model/GameModel.ts b/src/app/project/game-model/GameModel.ts index c134c4d..dde1cc3 100644 --- a/src/app/project/game-model/GameModel.ts +++ b/src/app/project/game-model/GameModel.ts @@ -28,16 +28,6 @@ export class GameModel { constructor(gameModelName: string) { this.gameModelName = gameModelName; - - this.addAbstractItemgroup("Clothing", undefined); - this.addAbstractItemgroup("Inventory", undefined); - this.addConcreteItemgroup("Oberteil", GameModel.findItemgroupByName("Clothing", this.itemgroups)! as AbstractItemGroup); - this.addConcreteItemgroup("Hose", GameModel.findItemgroupByName("Clothing", this.itemgroups)! as AbstractItemGroup); - - const item = this.addItem("Hose 1", "Hose"); - item!.addInheritedGroup(GameModel.findItemgroupByName("Inventory", this.itemgroups!)!) - console.log(GameModel.findItemgroupByName("Inventory", this.itemgroups!)!.manuallyInheritedItems) - this.addItem("Hose 2", "Hose"); } addAbstractItemgroup(groupName: string, parentgroup: AbstractItemGroup | undefined) { diff --git a/src/app/project/parser/itemParser/ItemgroupParser.ts b/src/app/project/parser/itemParser/ItemgroupParser.ts index 36b509d..de65648 100644 --- a/src/app/project/parser/itemParser/ItemgroupParser.ts +++ b/src/app/project/parser/itemParser/ItemgroupParser.ts @@ -1,8 +1,88 @@ import {ItemGroup} from "../../game-model/inventory/ItemGroup"; +import {StoreComponent} from "../../../../../app/storage/StoreComponent"; +import {ItemGroupCharacteristic} from "../../game-model/inventory/ItemgroupCharacteristic"; +import {AbstractItemGroup} from "../../game-model/inventory/AbstractItemGroup"; +import {ModelComponentType} from "../../game-model/ModelComponentType"; +import {ConcreteItemGroup} from "../../game-model/inventory/ConcreteItemGroup"; export class ItemgroupParser { private parsedItemgroups: ItemGroup[] = [] + private topParsedItemgroups: ItemGroup[] = [] + parseItemgroups(storeComponents: StoreComponent[]) { + const topologicalSort = this.topologicalSort(storeComponents); + topologicalSort.forEach(storeComponent => this.parseSingleItemgroup(storeComponent)); + return this.parsedItemgroups; + } + + private topologicalSort(storeComponents: StoreComponent[]) { + const sortedObjects: StoreComponent[] = [] + const visited: {[key: string]: boolean} = {}; + + const visit = (object: StoreComponent) => { + const jsonData = JSON.parse(object.jsonString) + if(visited[jsonData.componentName]) return; + visited[jsonData.componentName] = true; + + if(jsonData.parent) { + const parentObject = storeComponents.find(obj => JSON.parse(obj.jsonString).componentName === jsonData.parent); + if(parentObject) { + visit(parentObject); + } + } + sortedObjects.push(object); + } + + storeComponents.forEach(object => visit(object)); + + return sortedObjects; + } + + parseSingleItemgroup(storeComponent: StoreComponent) { + const data = JSON.parse(storeComponent.jsonString); + + const componentName = data.componentName; + const componentDescription = data.componentDescription; + const parentgroupName = data.parentgroup; + + let itemgroup; + if(data.itemgroupType == 0) { + itemgroup = new ConcreteItemGroup(componentName, componentDescription, ModelComponentType.ITEMGROUP); + } else { + itemgroup = new AbstractItemGroup(componentName, componentDescription, ModelComponentType.ITEMGROUP); + } + + const parentgroup = this.parsedItemgroups.find(parent => parent.componentName === parentgroupName); + + if(parentgroup != undefined) { + const abstractParentgroup = parentgroup as AbstractItemGroup; + abstractParentgroup.addChildItemgroup(itemgroup) + } else { + this.parsedItemgroups.push(itemgroup); + this.topParsedItemgroups.push(itemgroup); + } + + this.parseItemgroupCharacteristics(data.itemGroupCharacteristics, itemgroup); + } + + parseItemgroupCharacteristics(itemgroupCharacteristicsData: any, itemgroup: ItemGroup): ItemGroupCharacteristic[] { + const itemgroupCharacteristics: ItemGroupCharacteristic[] = [] + for(let i=0; i this.serializeSingleItemgroupHierarchy(child)); } else { - const storeComponent = this.serializeItemgroup(itemgroup); + const storeComponent = this.serializeConcreteItemgroup(itemgroup as ConcreteItemGroup); this.serializedItemgroups.push(storeComponent) this.serializeItemsOfItemgroup(itemgroup as ConcreteItemGroup); } } - private serializeItemgroup(itemgroup: ItemGroup): StoreComponent { + private serializeAbstractItemgroup(itemgroup: AbstractItemGroup): StoreComponent { const componentType = ModelComponentType.ITEMGROUP; const fileName = ItemSerializer.computeItemgroupPath(itemgroup); - const jsonString = JSON.stringify(itemgroup, (key, value) => { - if(ItemSerializer.ignoredGroupKeys.includes(key)) { - return undefined - } else { - - if(key === 'key') { - return value.characteristicName - } else { - return value; + const jsonObject = { + componentName: itemgroup.componentName, + componentDescription: itemgroup.componentDescription, + itemGroupCharacteristics: itemgroup.itemGroupCharacteristics.map(characteristic => { + return { + characteristicName: characteristic.characteristicName, + characteristicDescription: characteristic.characteristicDescription } + }), + parentgroup: itemgroup.parentGroup, + itemgroupType: ItemgroupType.ABSTRACT + }; - } + const jsonString = JSON.stringify(jsonObject, (key, value) => { + return value; + }, SerializeConstants.JSON_INDENT); + return new StoreComponent(jsonString, fileName, componentType) + } + + private serializeConcreteItemgroup(itemgroup: ConcreteItemGroup) { + const componentType = ModelComponentType.ITEMGROUP; + const fileName = ItemSerializer.computeItemgroupPath(itemgroup); + + const jsonObject = { + componentName: itemgroup.componentName, + componentDescription: itemgroup.componentDescription, + itemGroupCharacteristics: itemgroup.itemGroupCharacteristics.map(characteristic => { + return { + characteristicName: characteristic.characteristicName, + characteristicDescription: characteristic.characteristicDescription + } + }), + itemgroupType: ItemgroupType.CONCRETE, + parentgroup: itemgroup.parentGroup?.componentName + }; + + const jsonString = JSON.stringify(jsonObject, (key, value) => { + return value; }, SerializeConstants.JSON_INDENT); return new StoreComponent(jsonString, fileName, componentType) } diff --git a/testModel/items/Clothing/Clothing.json b/testModel/items/Clothing/Clothing.json index e3e3412..af1c0a1 100644 --- a/testModel/items/Clothing/Clothing.json +++ b/testModel/items/Clothing/Clothing.json @@ -7,6 +7,5 @@ "characteristicDescription": "" } ], - "manuallyInheritedItems": [], "itemgroupType": 1 } \ No newline at end of file diff --git a/testModel/items/Clothing/Hose/Hose.json b/testModel/items/Clothing/Hose/Hose.json index 2b39d60..241268e 100644 --- a/testModel/items/Clothing/Hose/Hose.json +++ b/testModel/items/Clothing/Hose/Hose.json @@ -2,18 +2,6 @@ "componentName": "Hose", "componentDescription": "", "itemGroupCharacteristics": [], - "manuallyInheritedItems": [], "itemgroupType": 0, - "parentGroup": { - "componentName": "Clothing", - "componentDescription": "", - "itemGroupCharacteristics": [ - { - "characteristicName": "Test0", - "characteristicDescription": "" - } - ], - "manuallyInheritedItems": [], - "itemgroupType": 1 - } + "parentgroup": "Clothing" } \ No newline at end of file diff --git a/testModel/items/Clothing/Oberteil/Oberteil.json b/testModel/items/Clothing/Oberteil/Oberteil.json index 2173a00..1079ae4 100644 --- a/testModel/items/Clothing/Oberteil/Oberteil.json +++ b/testModel/items/Clothing/Oberteil/Oberteil.json @@ -2,18 +2,6 @@ "componentName": "Oberteil", "componentDescription": "", "itemGroupCharacteristics": [], - "manuallyInheritedItems": [], "itemgroupType": 0, - "parentGroup": { - "componentName": "Clothing", - "componentDescription": "", - "itemGroupCharacteristics": [ - { - "characteristicName": "Test0", - "characteristicDescription": "" - } - ], - "manuallyInheritedItems": [], - "itemgroupType": 1 - } + "parentgroup": "Clothing" } \ No newline at end of file diff --git a/testModel/items/Inventory/Inventory.json b/testModel/items/Inventory/Inventory.json index 01b3765..27b2fe9 100644 --- a/testModel/items/Inventory/Inventory.json +++ b/testModel/items/Inventory/Inventory.json @@ -7,17 +7,5 @@ "characteristicDescription": "" } ], - "manuallyInheritedItems": [ - { - "componentName": "Hose 1", - "componentDescription": "", - "itemCharacteristicValues": [ - { - "key": "Test0", - "value": 0 - } - ] - } - ], "itemgroupType": 1 } \ No newline at end of file