initial commit
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import { defaultGame } from "../prefabs/game";
|
||||
import { Game, SerializedGame, SerializedNodeBehaviour } from "../types";
|
||||
import { Behaviour, behaviours } from "./functions";
|
||||
|
||||
export function serializeBehaviour(behaviour: Behaviour): SerializedNodeBehaviour {
|
||||
switch (behaviour.kind) {
|
||||
case 'converter':
|
||||
return {
|
||||
id: behaviour.id,
|
||||
kind: 'converter',
|
||||
inputs: {
|
||||
pins: behaviour.inputs.pins.map((pin) => ({
|
||||
...pin,
|
||||
rate: 11,
|
||||
})),
|
||||
},
|
||||
outputs: {
|
||||
pins: behaviour.outputs.pins.map((pin) => ({
|
||||
...pin,
|
||||
rate: 11,
|
||||
})),
|
||||
},
|
||||
};
|
||||
default:
|
||||
throw new Error('Unknown behaviour');
|
||||
}
|
||||
}
|
||||
|
||||
export function deserializeBehaviour(value: SerializedNodeBehaviour): Behaviour {
|
||||
switch (value.kind) {
|
||||
case 'converter':
|
||||
return new (behaviours.converter)(value);
|
||||
default:
|
||||
throw new Error('Unknown behaviour');
|
||||
}
|
||||
}
|
||||
|
||||
export function serialize(game: Game): SerializedGame {
|
||||
return {
|
||||
time: game.time,
|
||||
nodes: game.nodes.map((node) => ({
|
||||
id: node.id,
|
||||
name: node.name,
|
||||
behaviour: serializeBehaviour(node.behaviour),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
export function deserialize(value: SerializedGame): Game {
|
||||
return new Game(value);
|
||||
}
|
||||
|
||||
export function serializeToString(game: Game): string {
|
||||
return JSON.stringify(serialize(game));
|
||||
}
|
||||
|
||||
export function deserializeFromString(value: string): Game {
|
||||
return deserialize(JSON.parse(value));
|
||||
}
|
||||
|
||||
const KEY_GAME = 'incgane';
|
||||
|
||||
export function save(game: Game) {
|
||||
localStorage.setItem(KEY_GAME, serializeToString(game));
|
||||
}
|
||||
|
||||
export function load(): Game {
|
||||
try {
|
||||
const raw = localStorage.getItem(KEY_GAME);
|
||||
if (raw)
|
||||
return deserializeFromString(raw);
|
||||
} catch (e) {
|
||||
console.warn('Не удалось прочитать игру из localStorage', e);
|
||||
}
|
||||
const fresh = deserialize(defaultGame());
|
||||
save(fresh);
|
||||
return fresh;
|
||||
}
|
||||
Reference in New Issue
Block a user