diff --git a/app/main.ts b/app/main.ts index f6e57f9..cee7f28 100644 --- a/app/main.ts +++ b/app/main.ts @@ -8,6 +8,7 @@ import {ModelComponentFileDirectory} from "./storage/ModelComponentFileDirectory import {GamesystemStorage} from "./storage/storing/GamesystemStorage"; import {Character} from "../src/app/project/game-model/characters/Character"; import {CharacterStorage} from "./storage/storing/CharacterStorage"; +import {ItemgroupStorage} from "./storage/storing/ItemgroupStorage"; let win: BrowserWindow | null = null; const args = process.argv.slice(1), @@ -270,6 +271,9 @@ function recieveGameModelToStore(gameModel: StoredGameModel) { const characterStorage = new CharacterStorage(path.join(projectDirectory, ModelComponentFileDirectory.CHARACTER_DIR_NAME)) characterStorage.storeCharacters(gameModel.storedCharacters) + + const itemgroupStorage = new ItemgroupStorage(path.join(projectDirectory, ModelComponentFileDirectory.ITEMGROUP_DIR_NAME)) + itemgroupStorage.storeItemgroups(gameModel.storedItemgroups); } /*function deleteComponent(component: DeleteModel) { diff --git a/app/storage/ModelComponentFileDirectory.js b/app/storage/ModelComponentFileDirectory.js index 8d28902..eda48c4 100644 --- a/app/storage/ModelComponentFileDirectory.js +++ b/app/storage/ModelComponentFileDirectory.js @@ -7,4 +7,5 @@ exports.ModelComponentFileDirectory = ModelComponentFileDirectory; ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME = "script-accounts"; ModelComponentFileDirectory.GAMESYSTEM_DIR_NAME = "gamesystems"; ModelComponentFileDirectory.CHARACTER_DIR_NAME = "characters"; +ModelComponentFileDirectory.ITEMGROUP_DIR_NAME = "items"; //# sourceMappingURL=ModelComponentFileDirectory.js.map \ No newline at end of file diff --git a/app/storage/ModelComponentFileDirectory.ts b/app/storage/ModelComponentFileDirectory.ts index a0b9c8c..c68a714 100644 --- a/app/storage/ModelComponentFileDirectory.ts +++ b/app/storage/ModelComponentFileDirectory.ts @@ -2,4 +2,5 @@ export class ModelComponentFileDirectory { public static SCRIPTACCOUNT_DIR_NAME = "script-accounts" public static GAMESYSTEM_DIR_NAME = "gamesystems"; public static CHARACTER_DIR_NAME = "characters"; + static ITEMGROUP_DIR_NAME = "items"; } diff --git a/app/storage/StoredGameModel.js b/app/storage/StoredGameModel.js index 57bfbe7..2db851f 100644 --- a/app/storage/StoredGameModel.js +++ b/app/storage/StoredGameModel.js @@ -2,11 +2,12 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.StoredGameModel = void 0; class StoredGameModel { - constructor(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters) { + constructor(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, storedItemgroups) { this.gameModelName = gameModelName; this.storedGamesystems = storedGamesystems; this.storedScriptAccounts = storedScriptAccounts; this.storedCharacters = storedCharacters; + this.storedItemgroups = storedItemgroups; } } exports.StoredGameModel = StoredGameModel; diff --git a/app/storage/StoredGameModel.ts b/app/storage/StoredGameModel.ts index d9d762b..7d9f330 100644 --- a/app/storage/StoredGameModel.ts +++ b/app/storage/StoredGameModel.ts @@ -6,13 +6,15 @@ export class StoredGameModel { storedGamesystems: StoreComponent[] storedScriptAccounts: StoreComponent[] storedCharacters: StoreComponent[] + storedItemgroups: StoreComponent[] constructor(gameModelName: string, storedScriptAccounts: StoreComponent[], storedGamesystems: StoreComponent[], - storedCharacters: StoreComponent[]) { + storedCharacters: StoreComponent[], storedItemgroups: StoreComponent[]) { this.gameModelName = gameModelName; this.storedGamesystems = storedGamesystems; this.storedScriptAccounts = storedScriptAccounts; this.storedCharacters = storedCharacters; + this.storedItemgroups = storedItemgroups; } } diff --git a/app/storage/loader/GameModelLoader.js b/app/storage/loader/GameModelLoader.js index 480dc36..d78f3b3 100644 --- a/app/storage/loader/GameModelLoader.js +++ b/app/storage/loader/GameModelLoader.js @@ -16,7 +16,7 @@ class GameModelLoader { const storedScriptAccounts = this.loadScriptAccountComponents(); const storedGamesystems = this.loadGamesystems(); const storedCharacters = this.loadCharacters(); - return new StoredGameModel_1.StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters); + return new StoredGameModel_1.StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, []); } loadScriptAccountComponents() { const scriptAccountDir = path.join(this.gameModelDir, ModelComponentFileDirectory_1.ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME); diff --git a/app/storage/loader/GameModelLoader.ts b/app/storage/loader/GameModelLoader.ts index 615c354..fffebef 100644 --- a/app/storage/loader/GameModelLoader.ts +++ b/app/storage/loader/GameModelLoader.ts @@ -22,7 +22,7 @@ export class GameModelLoader { const storedGamesystems = this.loadGamesystems(); const storedCharacters = this.loadCharacters() - return new StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters); + return new StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, []); } private loadScriptAccountComponents() { diff --git a/app/storage/storing/ItemgroupStorage.js b/app/storage/storing/ItemgroupStorage.js new file mode 100644 index 0000000..028aa92 --- /dev/null +++ b/app/storage/storing/ItemgroupStorage.js @@ -0,0 +1,29 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.ItemgroupStorage = void 0; +const FileUtils_1 = require("../FileUtils"); +const path = require("node:path"); +const fs = require("node:fs"); +class ItemgroupStorage { + constructor(itemgroupDir) { + this.itemgroupDir = itemgroupDir; + FileUtils_1.FileUtils.prepareDirectoryFroWriting(this.itemgroupDir); + } + storeItemgroups(itemgroups) { + console.log("Store Itemgroups"); + itemgroups.forEach(itemgroup => { + this.storeItemgroup(itemgroup); + }); + } + storeItemgroup(itemgroup) { + const file = path.join(...itemgroup.fileName.split("/")); + const completeFileName = path.join(this.itemgroupDir, file); + const itemgroupDirectory = path.join(...itemgroup.fileName.split("/").slice(0, -1)); + const completeItemgroupDirectory = path.join(this.itemgroupDir, itemgroupDirectory); + console.log(completeItemgroupDirectory); + FileUtils_1.FileUtils.prepareDirectoryFroWriting(completeItemgroupDirectory); + fs.writeFileSync(completeFileName + ".json", itemgroup.jsonString, "utf-8"); + } +} +exports.ItemgroupStorage = ItemgroupStorage; +//# sourceMappingURL=ItemgroupStorage.js.map \ No newline at end of file diff --git a/app/storage/storing/ItemgroupStorage.ts b/app/storage/storing/ItemgroupStorage.ts new file mode 100644 index 0000000..40953ca --- /dev/null +++ b/app/storage/storing/ItemgroupStorage.ts @@ -0,0 +1,34 @@ +import {FileUtils} from "../FileUtils"; +import {StoreComponent} from "../StoreComponent"; +import * as path from "node:path"; +import * as fs from "node:fs"; + +export class ItemgroupStorage { + private itemgroupDir: string + + + constructor(itemgroupDir: string) { + this.itemgroupDir = itemgroupDir; + FileUtils.prepareDirectoryFroWriting(this.itemgroupDir); + } + + public storeItemgroups(itemgroups: StoreComponent[]) { + console.log("Store Itemgroups") + itemgroups.forEach(itemgroup => { + this.storeItemgroup(itemgroup) + }) + } + + private storeItemgroup(itemgroup: StoreComponent) { + const file = path.join(... itemgroup.fileName.split("/")); + const completeFileName = path.join(this.itemgroupDir, file); + + const itemgroupDirectory = path.join(... itemgroup.fileName.split("/").slice(0, -1)) + const completeItemgroupDirectory = path.join(this.itemgroupDir, itemgroupDirectory) + console.log(completeItemgroupDirectory) + FileUtils.prepareDirectoryFroWriting(completeItemgroupDirectory); + fs.writeFileSync(completeFileName + ".json", itemgroup.jsonString, "utf-8") + } + + +} diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 8272f56..063c373 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -27,6 +27,7 @@ import {CharacterParser} from "./project/parser/characterParser/CharacterParser" 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"; @Component({ selector: 'app-root', @@ -231,7 +232,10 @@ export class AppComponent implements OnInit{ const storedScriptAccounts = ScriptAccountSerializer.serializeScriptAccounts(this.gameModel.scriptAccounts) const storedGamesystems: StoreComponent[] = GamesystemSerializer.serializeGamesystems(this.gameModel.gamesystems) const storedCharacters: StoreComponent[] = CharacterSerializer.serializeCharacters(this.gameModel.characters) - const storeModel = new StoredGameModel(this.gameModel.gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters) + + const itemSerializer = new ItemSerializer(); + const storedItemgroups: StoreComponent[] = itemSerializer.serializeItemgroups(this.gameModel!.itemgroups); + const storeModel = new StoredGameModel(this.gameModel.gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, storedItemgroups) if(this.electronService.isElectron) { this.electronService.ipcRenderer.send('save-model', storeModel) diff --git a/src/app/project/game-model/GameModel.ts b/src/app/project/game-model/GameModel.ts index a7d5c6c..c134c4d 100644 --- a/src/app/project/game-model/GameModel.ts +++ b/src/app/project/game-model/GameModel.ts @@ -16,6 +16,7 @@ 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 @@ -29,11 +30,13 @@ export class GameModel { this.gameModelName = gameModelName; this.addAbstractItemgroup("Clothing", undefined); - this.addConcreteItemgroup("Inventory", 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); - this.addItem("Hose 1", "Hose"); + 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"); } @@ -41,7 +44,9 @@ export class GameModel { //Ensure that Itemgroup does not exist if(parentgroup == undefined) { if(GameModel.findItemgroupByName(groupName, this.itemgroups) == undefined) { - this.itemgroups.push(new AbstractItemGroup(groupName, "", ModelComponentType.ITEMGROUP)); + 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) { @@ -69,7 +74,9 @@ export class GameModel { const itemgroups: ItemGroup[] = ItemgroupUtilities.findItemgroupPathToItemgroup(conceteItemgroupName, this.itemgroups); if(itemgroups.length > 0) { - itemgroup.addItem(new Item(itemName, "", ModelComponentType.ITEM ), itemgroups) + const item = new Item(itemName, "", ModelComponentType.ITEM ) + itemgroup.addItem(item, itemgroups) + return item; } } } diff --git a/src/app/project/serializer/GamesystemSerializer.ts b/src/app/project/serializer/GamesystemSerializer.ts index 2ec8b43..3d8a8ca 100644 --- a/src/app/project/serializer/GamesystemSerializer.ts +++ b/src/app/project/serializer/GamesystemSerializer.ts @@ -27,9 +27,7 @@ export class GamesystemSerializer { private static serializeSimpleGamesystem(simpleGamesystem: SimpleGamesystem): StoreComponent { const fileName = this.computeSimpleGamesystemPath(simpleGamesystem); - if(simpleGamesystem.componentName === "Weather(Child)") { - console.log(fileName) - } + console.log(fileName) const jsonString = JSON.stringify(simpleGamesystem, (key, value) => { if(this.IGNORED_SIMPLE_ATTRIBUTES.includes(key)) { diff --git a/src/app/project/serializer/ItemSerializer.ts b/src/app/project/serializer/ItemSerializer.ts new file mode 100644 index 0000000..88916eb --- /dev/null +++ b/src/app/project/serializer/ItemSerializer.ts @@ -0,0 +1,64 @@ +import {ItemGroup} from "../game-model/inventory/ItemGroup"; +import {StoreComponent} from "../../../../app/storage/StoreComponent"; +import {AbstractItemGroup} from "../game-model/inventory/AbstractItemGroup"; +import {ConcreteItemGroup} from "../game-model/inventory/ConcreteItemGroup"; +import {ModelComponentType} from "../game-model/ModelComponentType"; +import {SerializeConstants} from "./SerializeConstants"; + +export class ItemSerializer { + + private serializedItemgroups: StoreComponent[] = [] + private static ignoredKeys: string[] = ['type', 'unsaved', 'children', "itemgroup", "manuallyInheritedGroups", "hierarchyInheritedGroups", "items"] + + public serializeItemgroups(itemgroups: ItemGroup[]): StoreComponent[] { + itemgroups.forEach(itemgroup => { + this.serializeSingleItemgroupHierarchy(itemgroup) + }) + return this.serializedItemgroups; + } + + private serializeSingleItemgroupHierarchy(itemgroup: ItemGroup) { + if(itemgroup instanceof AbstractItemGroup) { + const storeComponent = this.serializeItemgroup(itemgroup) + this.serializedItemgroups.push(storeComponent) + itemgroup.children.forEach(child => this.serializeSingleItemgroupHierarchy(child)); + } else { + const storeComponent = this.serializeItemgroup(itemgroup); + this.serializedItemgroups.push(storeComponent) + } + } + + private serializeItemgroup(itemgroup: ItemGroup): StoreComponent { + const componentType = ModelComponentType.ITEMGROUP; + const fileName = ItemSerializer.computeItemgroupPath(itemgroup); + + const jsonString = JSON.stringify(itemgroup, (key, value) => { + if(ItemSerializer.ignoredKeys.includes(key)) { + return undefined + } else { + + if(key === 'key') { + return value.characteristicName + } else { + return value; + } + + } + }, SerializeConstants.JSON_INDENT); + console.log(fileName) + return new StoreComponent(jsonString, fileName, componentType) + } + + private static computeItemgroupPath(itemgroup: ItemGroup): string { + const itemgroupPath: string[] = []; + itemgroupPath.push(itemgroup.componentName); + itemgroupPath.push(itemgroup.componentName); + + while(itemgroup.parentGroup !== undefined) { + itemgroupPath.unshift(itemgroup.parentGroup.componentName); + itemgroup = itemgroup.parentGroup; + } + + return itemgroupPath.join("/"); + } +} diff --git a/testModel/items/Clothing/Clothing.json b/testModel/items/Clothing/Clothing.json new file mode 100644 index 0000000..845e3dd --- /dev/null +++ b/testModel/items/Clothing/Clothing.json @@ -0,0 +1,11 @@ +{ + "componentName": "Clothing", + "componentDescription": "", + "itemGroupCharacteristics": [ + { + "characteristicName": "Test0", + "characteristicDescription": "" + } + ], + "manuallyInheritedItems": [] +} \ No newline at end of file diff --git a/testModel/items/Clothing/Hose/Hose.json b/testModel/items/Clothing/Hose/Hose.json new file mode 100644 index 0000000..cacc167 --- /dev/null +++ b/testModel/items/Clothing/Hose/Hose.json @@ -0,0 +1,17 @@ +{ + "componentName": "Hose", + "componentDescription": "", + "itemGroupCharacteristics": [], + "manuallyInheritedItems": [], + "parentGroup": { + "componentName": "Clothing", + "componentDescription": "", + "itemGroupCharacteristics": [ + { + "characteristicName": "Test0", + "characteristicDescription": "" + } + ], + "manuallyInheritedItems": [] + } +} \ No newline at end of file diff --git a/testModel/items/Clothing/Oberteil/Oberteil.json b/testModel/items/Clothing/Oberteil/Oberteil.json new file mode 100644 index 0000000..00be2b1 --- /dev/null +++ b/testModel/items/Clothing/Oberteil/Oberteil.json @@ -0,0 +1,17 @@ +{ + "componentName": "Oberteil", + "componentDescription": "", + "itemGroupCharacteristics": [], + "manuallyInheritedItems": [], + "parentGroup": { + "componentName": "Clothing", + "componentDescription": "", + "itemGroupCharacteristics": [ + { + "characteristicName": "Test0", + "characteristicDescription": "" + } + ], + "manuallyInheritedItems": [] + } +} \ No newline at end of file diff --git a/testModel/items/Inventory/Inventory.json b/testModel/items/Inventory/Inventory.json new file mode 100644 index 0000000..7b72ad9 --- /dev/null +++ b/testModel/items/Inventory/Inventory.json @@ -0,0 +1,22 @@ +{ + "componentName": "Inventory", + "componentDescription": "", + "itemGroupCharacteristics": [ + { + "characteristicName": "Test0", + "characteristicDescription": "" + } + ], + "manuallyInheritedItems": [ + { + "componentName": "Hose 1", + "componentDescription": "", + "itemCharacteristicValues": [ + { + "key": "Test0", + "value": 0 + } + ] + } + ] +} \ No newline at end of file