ConceptCreator/src/app/project/serializer/CharacterSerializer.ts
Sebastian Böckelmann 86097a898a
All checks were successful
E2E Testing / test (push) Successful in 1m33s
Persist Character Inventory Slots
2024-05-11 14:52:40 +02:00

65 lines
2.5 KiB
TypeScript

import {Character} from "../game-model/characters/Character";
import {StoreComponent} from "../../../../app/storage/StoreComponent";
import {SerializeConstants} from "./SerializeConstants";
import {ModelComponentType} from "../game-model/ModelComponentType";
import {SimpleTemplateGamesystem} from "../game-model/templates/simpleGamesystem/SimpleTemplateGamesystem";
import {Gamesystem} from "../game-model/gamesystems/Gamesystem";
import {ItemGroup} from "../game-model/inventory/ItemGroup";
import {InventorySlotSerializer} from "./InventorySlotSerializer";
export class CharacterSerializer {
private static ignoredKeys: string[] = ['unsaved', 'type', 'incomingTransitions', 'outgoingTransitions', 'initial', 'conditions', 'stateDescription', 'templateType', 'parentGamesystem', 'scriptAccountActions', 'scriptAccountConditions', 'characterRelations']
public static serializeCharacters(characters: Character[]): StoreComponent[] {
const storedCharacters: StoreComponent[] = []
characters.forEach(character => storedCharacters.push(this.serializeSingleCharacter(character)))
return storedCharacters;
}
private static serializeSingleCharacter(character: Character): StoreComponent{
const fileName = character.componentName
const templateGamesystemBackup = character.characterSpecificTemplateSystems.concat();
character.characterSpecificTemplateSystems = character.characterSpecificTemplateSystems.filter(system => system instanceof SimpleTemplateGamesystem)
const jsonString = JSON.stringify(character, (key, value) => {
if(value instanceof Gamesystem) {
return {
...value,
componentDescription: undefined,
parentGamesystem: undefined
}
}
if(key == 'inventorySlots') {
return InventorySlotSerializer.serializeInventorySlots(value)
}
if(key === 'scriptAccount') {
return value.componentName
}
if(key === 'conditionMap' || key === 'actionMap' || key === 'initialMap') {
if(value.get(character) == undefined) {
return []
}
return value.get(character)
}
if(key === 'startingState' || key === 'endingState') {
return value.stateLabel
}
if(this.ignoredKeys.includes(key)) {
return undefined
} else {
return value;
}
}, SerializeConstants.JSON_INDENT)
console.log(jsonString)
character.characterSpecificTemplateSystems = templateGamesystemBackup
return new StoreComponent(jsonString, fileName, ModelComponentType.CHARACTER);
}
}