5db04109a2
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
29 lines
868 B
TypeScript
29 lines
868 B
TypeScript
import type { SyncMove, HistoryEntry } from './types';
|
|
|
|
/** Coordinate notation, e.g. "e2e4" or "e7e8q". */
|
|
export function toCoordinate(move: SyncMove): string {
|
|
return `${move.from}${move.to}${move.promotion ?? ''}`;
|
|
}
|
|
|
|
export interface SavedGame {
|
|
variant: 'duplicate-chess';
|
|
version: 1;
|
|
moves: HistoryEntry[];
|
|
}
|
|
|
|
export function serialize(history: HistoryEntry[]): string {
|
|
const data: SavedGame = { variant: 'duplicate-chess', version: 1, moves: history };
|
|
return JSON.stringify(data, null, 2);
|
|
}
|
|
|
|
export function deserialize(json: string): HistoryEntry[] {
|
|
const data = JSON.parse(json) as Partial<SavedGame>;
|
|
if (data.variant !== 'duplicate-chess' || !Array.isArray(data.moves)) {
|
|
throw new Error('Not a duplicate-chess save file');
|
|
}
|
|
if (data.version !== 1) {
|
|
throw new Error('Unsupported save version');
|
|
}
|
|
return data.moves;
|
|
}
|