import type { StoragePort } from './storage'; import type { FlowStats } from './flow'; export const FLOW_SAVE_KEY = 'mode:02-connect-1234:balloons-v2'; export interface FlowRecord { version: 2; bestScore: number; bestNumber: number; bestCombo: number; bestFlown: number; sound: boolean } export function freshRecord(): FlowRecord { return { version: 2, bestScore: 0, bestNumber: 0, bestCombo: 0, bestFlown: 0, sound: true }; } export function loadRecord(storage: StoragePort): FlowRecord { const result = freshRecord(); try { const saved = JSON.parse(storage.read(FLOW_SAVE_KEY) || 'null'); if (saved?.version !== 2) return result; for (const key of ['bestScore', 'bestNumber', 'bestCombo', 'bestFlown'] as const) if (Number.isSafeInteger(saved[key]) && saved[key] >= 0) result[key] = saved[key]; if (typeof saved.sound === 'boolean') result.sound = saved.sound; } catch { /* 损坏或不可用存储不影响游玩 */ } return result; } export function recordRound(record: FlowRecord, stats: FlowStats): void { record.bestScore = Math.max(record.bestScore, stats.score); record.bestNumber = Math.max(record.bestNumber, stats.maxNumber); record.bestCombo = Math.max(record.bestCombo, stats.bestCombo); record.bestFlown = Math.max(record.bestFlown, stats.flown); } export function saveRecord(storage: StoragePort, record: FlowRecord): boolean { try { storage.write(FLOW_SAVE_KEY, JSON.stringify(record)); return true; } catch { return false; } }