import { type Behaviour, Converter, Storage } from "../behaviours"; import type { SerializedBehaviour, SerializedConverter, SerializedStorage } from "../types"; export function deserializeBehaviour(value: SerializedBehaviour): Behaviour { switch (value.kind) { case 'converter': return new Converter(value); case 'storage': return new Storage(value); default: throw new Error('Unknown behaviour'); } } export function serializeBehaviour(behaviour: Behaviour): SerializedBehaviour { const base = { id: behaviour.id, kind: behaviour.kind, inputLinks: { pins: behaviour.inputLinks }, }; switch (behaviour.kind) { case 'converter': { const converter = behaviour as Converter; return { ...base, cycleTime: converter.cycleTime, storage: converter.getStorage(), rules: { cycleLength: converter.cycleLength, inputRates: converter.inputRates, outputRates: converter.outputRates, } } as (SerializedBehaviour & SerializedConverter);; } case 'storage': const storage = behaviour as Storage; return { ...base, capacity: storage.capacity, acceptsResources: storage.acceptsResources, items: storage.getStorage(), } as (SerializedBehaviour & SerializedStorage); default: throw new Error('Unknown behaviour'); } }