inventory-items-2 #44

Closed
sebastian wants to merge 32 commits from inventory-items-2 into inventory
10 changed files with 174 additions and 65 deletions
Showing only changes of commit cb2094aa49 - Show all commits

View File

@ -17,8 +17,8 @@ class GameModelLoader {
const storedScriptAccounts = this.loadScriptAccountComponents(); const storedScriptAccounts = this.loadScriptAccountComponents();
const storedGamesystems = this.loadGamesystems(); const storedGamesystems = this.loadGamesystems();
const storedCharacters = this.loadCharacters(); const storedCharacters = this.loadCharacters();
const storedItemgroupsd = this.loadItemgroups(); const storedItemgroups = this.loadItemgroups();
return new StoredGameModel_1.StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, [], []); return new StoredGameModel_1.StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, storedItemgroups, []);
} }
loadScriptAccountComponents() { loadScriptAccountComponents() {
const scriptAccountDir = path.join(this.gameModelDir, ModelComponentFileDirectory_1.ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME); const scriptAccountDir = path.join(this.gameModelDir, ModelComponentFileDirectory_1.ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME);

View 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

View File

@ -28,6 +28,7 @@ import {TemplateType} from "./project/game-model/templates/TemplateType";
import {TemplateTypeUtilities} from "./project/game-model/templates/TemplateTypeUtilities"; import {TemplateTypeUtilities} from "./project/game-model/templates/TemplateTypeUtilities";
import {SimpleTemplateGamesystem} from "./project/game-model/templates/simpleGamesystem/SimpleTemplateGamesystem"; import {SimpleTemplateGamesystem} from "./project/game-model/templates/simpleGamesystem/SimpleTemplateGamesystem";
import {ItemSerializer} from "./project/serializer/ItemSerializer"; import {ItemSerializer} from "./project/serializer/ItemSerializer";
import {ItemgroupParser} from "./project/parser/itemParser/ItemgroupParser";
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
@ -224,6 +225,11 @@ export class AppComponent implements OnInit{
const characterParser = new CharacterParser(characterTemplateSystems, characterRelationTemplateSystems, gameModel.scriptAccounts); const characterParser = new CharacterParser(characterTemplateSystems, characterRelationTemplateSystems, gameModel.scriptAccounts);
gameModel.characters = characterParser.parseCharacters(storedGameModel.storedCharacters) gameModel.characters = characterParser.parseCharacters(storedGameModel.storedCharacters)
const itemgroupParser = new ItemgroupParser();
itemgroupParser.parseItemgroups(storedGameModel.storedItemgroups);
gameModel.itemgroups = itemgroupParser.getParsedTopItemgroups();
this.gameModel = gameModel; this.gameModel = gameModel;
} }

View File

@ -28,16 +28,6 @@ export class GameModel {
constructor(gameModelName: string) { constructor(gameModelName: string) {
this.gameModelName = gameModelName; 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) { addAbstractItemgroup(groupName: string, parentgroup: AbstractItemGroup | undefined) {

View File

@ -1,8 +1,88 @@
import {ItemGroup} from "../../game-model/inventory/ItemGroup"; 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 { export class ItemgroupParser {
private parsedItemgroups: ItemGroup[] = [] 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;
}
} }

View File

@ -5,12 +5,13 @@ import {ConcreteItemGroup} from "../game-model/inventory/ConcreteItemGroup";
import {ModelComponentType} from "../game-model/ModelComponentType"; import {ModelComponentType} from "../game-model/ModelComponentType";
import {SerializeConstants} from "./SerializeConstants"; import {SerializeConstants} from "./SerializeConstants";
import {Item} from "../game-model/inventory/Item"; import {Item} from "../game-model/inventory/Item";
import {ItemgroupType} from "../game-model/inventory/ItemgroupType";
export class ItemSerializer { export class ItemSerializer {
private serializedItemgroups: StoreComponent[] = [] private serializedItemgroups: StoreComponent[] = []
private serializedItems: 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'] private static ignoredItemKeys: string[] = ['type', 'unsaved', 'hierarchyInheritedGroups']
public serializeItemgroups(itemgroups: ItemGroup[]): StoreComponent[] { public serializeItemgroups(itemgroups: ItemGroup[]): StoreComponent[] {
@ -22,33 +23,59 @@ export class ItemSerializer {
private serializeSingleItemgroupHierarchy(itemgroup: ItemGroup) { private serializeSingleItemgroupHierarchy(itemgroup: ItemGroup) {
if(itemgroup instanceof AbstractItemGroup) { if(itemgroup instanceof AbstractItemGroup) {
const storeComponent = this.serializeItemgroup(itemgroup) const storeComponent = this.serializeAbstractItemgroup(itemgroup)
this.serializedItemgroups.push(storeComponent) this.serializedItemgroups.push(storeComponent)
itemgroup.children.forEach(child => this.serializeSingleItemgroupHierarchy(child)); itemgroup.children.forEach(child => this.serializeSingleItemgroupHierarchy(child));
} else { } else {
const storeComponent = this.serializeItemgroup(itemgroup); const storeComponent = this.serializeConcreteItemgroup(itemgroup as ConcreteItemGroup);
this.serializedItemgroups.push(storeComponent) this.serializedItemgroups.push(storeComponent)
this.serializeItemsOfItemgroup(itemgroup as ConcreteItemGroup); this.serializeItemsOfItemgroup(itemgroup as ConcreteItemGroup);
} }
} }
private serializeItemgroup(itemgroup: ItemGroup): StoreComponent { private serializeAbstractItemgroup(itemgroup: AbstractItemGroup): StoreComponent {
const componentType = ModelComponentType.ITEMGROUP; const componentType = ModelComponentType.ITEMGROUP;
const fileName = ItemSerializer.computeItemgroupPath(itemgroup); const fileName = ItemSerializer.computeItemgroupPath(itemgroup);
const jsonString = JSON.stringify(itemgroup, (key, value) => { const jsonObject = {
if(ItemSerializer.ignoredGroupKeys.includes(key)) { componentName: itemgroup.componentName,
return undefined componentDescription: itemgroup.componentDescription,
} else { itemGroupCharacteristics: itemgroup.itemGroupCharacteristics.map(characteristic => {
return {
if(key === 'key') { characteristicName: characteristic.characteristicName,
return value.characteristicName characteristicDescription: characteristic.characteristicDescription
} else {
return value;
} }
}),
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); }, SerializeConstants.JSON_INDENT);
return new StoreComponent(jsonString, fileName, componentType) return new StoreComponent(jsonString, fileName, componentType)
} }

View File

@ -7,6 +7,5 @@
"characteristicDescription": "" "characteristicDescription": ""
} }
], ],
"manuallyInheritedItems": [],
"itemgroupType": 1 "itemgroupType": 1
} }

View File

@ -2,18 +2,6 @@
"componentName": "Hose", "componentName": "Hose",
"componentDescription": "", "componentDescription": "",
"itemGroupCharacteristics": [], "itemGroupCharacteristics": [],
"manuallyInheritedItems": [],
"itemgroupType": 0, "itemgroupType": 0,
"parentGroup": { "parentgroup": "Clothing"
"componentName": "Clothing",
"componentDescription": "",
"itemGroupCharacteristics": [
{
"characteristicName": "Test0",
"characteristicDescription": ""
}
],
"manuallyInheritedItems": [],
"itemgroupType": 1
}
} }

View File

@ -2,18 +2,6 @@
"componentName": "Oberteil", "componentName": "Oberteil",
"componentDescription": "", "componentDescription": "",
"itemGroupCharacteristics": [], "itemGroupCharacteristics": [],
"manuallyInheritedItems": [],
"itemgroupType": 0, "itemgroupType": 0,
"parentGroup": { "parentgroup": "Clothing"
"componentName": "Clothing",
"componentDescription": "",
"itemGroupCharacteristics": [
{
"characteristicName": "Test0",
"characteristicDescription": ""
}
],
"manuallyInheritedItems": [],
"itemgroupType": 1
}
} }

View File

@ -7,17 +7,5 @@
"characteristicDescription": "" "characteristicDescription": ""
} }
], ],
"manuallyInheritedItems": [
{
"componentName": "Hose 1",
"componentDescription": "",
"itemCharacteristicValues": [
{
"key": "Test0",
"value": 0
}
]
}
],
"itemgroupType": 1 "itemgroupType": 1
} }