Persist Items
All checks were successful
E2E Testing / test (push) Successful in 1m36s

This commit is contained in:
Sebastian Böckelmann 2024-05-09 10:03:57 +02:00
parent fed0ca99bf
commit 1b2fd273bf
3 changed files with 68 additions and 5 deletions

View File

@ -4,11 +4,13 @@ import {AbstractItemGroup} from "../game-model/inventory/AbstractItemGroup";
import {ConcreteItemGroup} from "../game-model/inventory/ConcreteItemGroup";
import {ModelComponentType} from "../game-model/ModelComponentType";
import {SerializeConstants} from "./SerializeConstants";
import {Item} from "../game-model/inventory/Item";
export class ItemSerializer {
private serializedItemgroups: StoreComponent[] = []
private static ignoredKeys: string[] = ['type', 'unsaved', 'children', "itemgroup", "manuallyInheritedGroups", "hierarchyInheritedGroups", "items"]
private static ignoredGroupKeys: string[] = ['type', 'unsaved', 'children', "itemgroup", "manuallyInheritedGroups", "hierarchyInheritedGroups", "items"]
private static ignoredItemKeys: string[] = ['type', 'unsaved', 'hierarchyInheritedGroups']
public serializeItemgroups(itemgroups: ItemGroup[]): StoreComponent[] {
itemgroups.forEach(itemgroup => {
@ -25,6 +27,8 @@ export class ItemSerializer {
} else {
const storeComponent = this.serializeItemgroup(itemgroup);
this.serializedItemgroups.push(storeComponent)
this.serializeItemsOfItemgroup(itemgroup as ConcreteItemGroup);
}
}
@ -33,7 +37,7 @@ export class ItemSerializer {
const fileName = ItemSerializer.computeItemgroupPath(itemgroup);
const jsonString = JSON.stringify(itemgroup, (key, value) => {
if(ItemSerializer.ignoredKeys.includes(key)) {
if(ItemSerializer.ignoredGroupKeys.includes(key)) {
return undefined
} else {
@ -45,14 +49,17 @@ export class ItemSerializer {
}
}, SerializeConstants.JSON_INDENT);
console.log(fileName)
return new StoreComponent(jsonString, fileName, componentType)
}
private static computeItemgroupPath(itemgroup: ItemGroup): string {
private static computeItemgroupPath(itemgroup: ItemGroup, itemPath: boolean = false): string {
const itemgroupPath: string[] = [];
itemgroupPath.push(itemgroup.componentName);
itemgroupPath.push(itemgroup.componentName);
if(!itemPath) {
itemgroupPath.push(itemgroup.componentName);
}
while(itemgroup.parentGroup !== undefined) {
itemgroupPath.unshift(itemgroup.parentGroup.componentName);
@ -61,4 +68,36 @@ export class ItemSerializer {
return itemgroupPath.join("/");
}
private serializeItemsOfItemgroup(itemgroup: ConcreteItemGroup) {
const fileName = ItemSerializer.computeItemgroupPath(itemgroup, true);
itemgroup.items.forEach(item => {
const storeComponent = this.serializeSingleItem(fileName, item);
this.serializedItemgroups.push(storeComponent)
})
}
private serializeSingleItem(itemgroupPath: string, item: Item) {
const itemFile = itemgroupPath + "/" + item.componentName;
const jsonString = JSON.stringify(item, (key, value) => {
if(ItemSerializer.ignoredItemKeys.includes(key)) {
return undefined
}
if(value instanceof ItemGroup) {
return value.componentName
}
if(key == "key") {
return value.characteristicName
}
return value;
}, SerializeConstants.JSON_INDENT)
return new StoreComponent(jsonString, itemFile, ModelComponentType.ITEM)
}
}

View File

@ -0,0 +1,13 @@
{
"componentName": "Hose 1",
"componentDescription": "",
"manuallyInheritedGroups": [
"Inventory"
],
"itemCharacteristicValues": [
{
"key": "Test0",
"value": 0
}
]
}

View File

@ -0,0 +1,11 @@
{
"componentName": "Hose 2",
"componentDescription": "",
"manuallyInheritedGroups": [],
"itemCharacteristicValues": [
{
"key": "Test0",
"value": 0
}
]
}