46 lines
1.6 KiB
TypeScript
46 lines
1.6 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 {Gamesystem} from "../game-model/gamesystems/Gamesystem";
|
|
import {ScriptAccount} from "../game-model/scriptAccounts/ScriptAccount";
|
|
|
|
export class CharacterSerializer {
|
|
|
|
private static ignoredKeys: string[] = ['unsaved', 'type', 'incomingTransitions', 'outgoingTransitions', 'initial', 'conditions', 'stateDescription', 'templateType']
|
|
|
|
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 jsonString = JSON.stringify(character, (key, value) => {
|
|
if(value instanceof Gamesystem) {
|
|
return {
|
|
...value,
|
|
componentDescription: undefined
|
|
}
|
|
}
|
|
|
|
if(key === 'scriptAccount') {
|
|
return value.componentName
|
|
}
|
|
|
|
if(key === 'conditionMap') {
|
|
return value.get(character)
|
|
}
|
|
|
|
if(this.ignoredKeys.includes(key)) {
|
|
return undefined
|
|
} else {
|
|
return value;
|
|
}
|
|
}, SerializeConstants.JSON_INDENT)
|
|
|
|
return new StoreComponent(jsonString, fileName, ModelComponentType.CHARACTER);
|
|
}
|
|
}
|