main #48
@ -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);
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										43
									
								
								app/storage/loader/ItemLoader.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								app/storage/loader/ItemLoader.js
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
@ -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;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
 | 
			
		||||
@ -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<itemgroupCharacteristicsData.length; i++) {
 | 
			
		||||
      this.parseSingleItemgroupCharacteristic(itemgroupCharacteristicsData[i], itemgroup)
 | 
			
		||||
    }
 | 
			
		||||
    return itemgroupCharacteristics;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private parseSingleItemgroupCharacteristic(itemgroupCharacteristicData: any, itemgroup: ItemGroup) {
 | 
			
		||||
    const name = itemgroupCharacteristicData.characteristicName;
 | 
			
		||||
    const description = itemgroupCharacteristicData.characteristicDescription;
 | 
			
		||||
 | 
			
		||||
    return new ItemGroupCharacteristic(name, description, itemgroup);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  getParsedTopItemgroups() {
 | 
			
		||||
    return this.topParsedItemgroups;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -5,12 +5,13 @@ import {ConcreteItemGroup} from "../game-model/inventory/ConcreteItemGroup";
 | 
			
		||||
import {ModelComponentType} from "../game-model/ModelComponentType";
 | 
			
		||||
import {SerializeConstants} from "./SerializeConstants";
 | 
			
		||||
import {Item} from "../game-model/inventory/Item";
 | 
			
		||||
import {ItemgroupType} from "../game-model/inventory/ItemgroupType";
 | 
			
		||||
 | 
			
		||||
export class ItemSerializer {
 | 
			
		||||
 | 
			
		||||
  private serializedItemgroups: StoreComponent[] = []
 | 
			
		||||
  private serializedItems: StoreComponent[] = []
 | 
			
		||||
  private static ignoredGroupKeys: string[] = ['type', 'unsaved', 'children', "itemgroup", "manuallyInheritedGroups", "hierarchyInheritedGroups", "items"]
 | 
			
		||||
  private static ignoredGroupKeys: string[] = ['type', 'unsaved', "itemgroup", "manuallyInheritedGroups", "hierarchyInheritedGroups", "items"]
 | 
			
		||||
  private static ignoredItemKeys: string[] = ['type', 'unsaved', 'hierarchyInheritedGroups']
 | 
			
		||||
 | 
			
		||||
  public serializeItemgroups(itemgroups: ItemGroup[]): StoreComponent[] {
 | 
			
		||||
@ -22,33 +23,59 @@ export class ItemSerializer {
 | 
			
		||||
 | 
			
		||||
  private serializeSingleItemgroupHierarchy(itemgroup: ItemGroup) {
 | 
			
		||||
    if(itemgroup instanceof AbstractItemGroup) {
 | 
			
		||||
      const storeComponent = this.serializeItemgroup(itemgroup)
 | 
			
		||||
      const storeComponent = this.serializeAbstractItemgroup(itemgroup)
 | 
			
		||||
      this.serializedItemgroups.push(storeComponent)
 | 
			
		||||
      itemgroup.children.forEach(child => 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 {
 | 
			
		||||
    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
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
        if(key === 'key') {
 | 
			
		||||
          return value.characteristicName
 | 
			
		||||
        } else {
 | 
			
		||||
    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)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@ -7,6 +7,5 @@
 | 
			
		||||
            "characteristicDescription": ""
 | 
			
		||||
        }
 | 
			
		||||
    ],
 | 
			
		||||
    "manuallyInheritedItems": [],
 | 
			
		||||
    "itemgroupType": 1
 | 
			
		||||
}
 | 
			
		||||
@ -2,18 +2,6 @@
 | 
			
		||||
    "componentName": "Hose",
 | 
			
		||||
    "componentDescription": "",
 | 
			
		||||
    "itemGroupCharacteristics": [],
 | 
			
		||||
    "manuallyInheritedItems": [],
 | 
			
		||||
    "itemgroupType": 0,
 | 
			
		||||
    "parentGroup": {
 | 
			
		||||
        "componentName": "Clothing",
 | 
			
		||||
        "componentDescription": "",
 | 
			
		||||
        "itemGroupCharacteristics": [
 | 
			
		||||
            {
 | 
			
		||||
                "characteristicName": "Test0",
 | 
			
		||||
                "characteristicDescription": ""
 | 
			
		||||
            }
 | 
			
		||||
        ],
 | 
			
		||||
        "manuallyInheritedItems": [],
 | 
			
		||||
        "itemgroupType": 1
 | 
			
		||||
    }
 | 
			
		||||
    "parentgroup": "Clothing"
 | 
			
		||||
}
 | 
			
		||||
@ -2,18 +2,6 @@
 | 
			
		||||
    "componentName": "Oberteil",
 | 
			
		||||
    "componentDescription": "",
 | 
			
		||||
    "itemGroupCharacteristics": [],
 | 
			
		||||
    "manuallyInheritedItems": [],
 | 
			
		||||
    "itemgroupType": 0,
 | 
			
		||||
    "parentGroup": {
 | 
			
		||||
        "componentName": "Clothing",
 | 
			
		||||
        "componentDescription": "",
 | 
			
		||||
        "itemGroupCharacteristics": [
 | 
			
		||||
            {
 | 
			
		||||
                "characteristicName": "Test0",
 | 
			
		||||
                "characteristicDescription": ""
 | 
			
		||||
            }
 | 
			
		||||
        ],
 | 
			
		||||
        "manuallyInheritedItems": [],
 | 
			
		||||
        "itemgroupType": 1
 | 
			
		||||
    }
 | 
			
		||||
    "parentgroup": "Clothing"
 | 
			
		||||
}
 | 
			
		||||
@ -7,17 +7,5 @@
 | 
			
		||||
            "characteristicDescription": ""
 | 
			
		||||
        }
 | 
			
		||||
    ],
 | 
			
		||||
    "manuallyInheritedItems": [
 | 
			
		||||
        {
 | 
			
		||||
            "componentName": "Hose 1",
 | 
			
		||||
            "componentDescription": "",
 | 
			
		||||
            "itemCharacteristicValues": [
 | 
			
		||||
                {
 | 
			
		||||
                    "key": "Test0",
 | 
			
		||||
                    "value": 0
 | 
			
		||||
                }
 | 
			
		||||
            ]
 | 
			
		||||
        }
 | 
			
		||||
    ],
 | 
			
		||||
    "itemgroupType": 1
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user