ConceptCreator/src/app/project/serializer/ItemSerializer.ts
Sebastian Böckelmann f01d8c4dab
All checks were successful
E2E Testing / test (push) Successful in 1m33s
Parse Items
2024-05-09 16:03:52 +02:00

136 lines
4.8 KiB
TypeScript

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";
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', "itemgroup", "manuallyInheritedGroups", "hierarchyInheritedGroups", "items"]
private static ignoredItemKeys: string[] = ['type', 'unsaved']
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.serializeAbstractItemgroup(itemgroup)
this.serializedItemgroups.push(storeComponent)
itemgroup.children.forEach(child => this.serializeSingleItemgroupHierarchy(child));
} else {
const storeComponent = this.serializeConcreteItemgroup(itemgroup as ConcreteItemGroup);
this.serializedItemgroups.push(storeComponent)
this.serializeItemsOfItemgroup(itemgroup as ConcreteItemGroup);
}
}
private serializeAbstractItemgroup(itemgroup: AbstractItemGroup): StoreComponent {
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
}
}),
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)
}
private static computeItemgroupPath(itemgroup: ItemGroup, itemPath: boolean = false): string {
const itemgroupPath: string[] = [];
itemgroupPath.push(itemgroup.componentName);
if(!itemPath) {
itemgroupPath.push(itemgroup.componentName);
}
while(itemgroup.parentGroup !== undefined) {
itemgroupPath.unshift(itemgroup.parentGroup.componentName);
itemgroup = itemgroup.parentGroup;
}
return itemgroupPath.join("/");
}
private serializeItemsOfItemgroup(itemgroup: ConcreteItemGroup) {
const fileName = ItemSerializer.computeItemgroupPath(itemgroup, true);
itemgroup.items.forEach(item => {
const storeComponent = this.serializeSingleItem(fileName, item);
this.serializedItems.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)
}
getSerializedItems() {
return this.serializedItems;
}
}