20 lines
625 B
TypeScript
20 lines
625 B
TypeScript
import type { GameState } from './state';
|
|
|
|
export function serializeGame(game: GameState): string {
|
|
// Use a deterministic JSON serializer for snapshots; currently use JSON.stringify
|
|
return JSON.stringify(game);
|
|
}
|
|
|
|
export function deserializeGame(serialized: string): GameState {
|
|
try {
|
|
return JSON.parse(serialized) as GameState;
|
|
} catch (e) {
|
|
// If parsing fails, return a minimal empty game state to avoid crashes
|
|
return { players: [], placements: { corners: [], roads: [] } } as GameState;
|
|
}
|
|
}
|
|
|
|
export function cloneGame(game: GameState): GameState {
|
|
return deserializeGame(serializeGame(game));
|
|
}
|