|
| 1 | +import type { BoardThemeKey } from './Settings' |
| 2 | +import type { ColorChoice, GameMode } from './chess/types' |
| 3 | +import { snapMaiaElo, type MaiaElo } from './engine/maiaEngine' |
| 4 | + |
| 5 | +export type PlayerConfig = { |
| 6 | + gameMode: GameMode |
| 7 | + colorChoice: ColorChoice |
| 8 | + elo: MaiaElo |
| 9 | + boardTheme: BoardThemeKey |
| 10 | + takebackLimit: number |
| 11 | + allowEloChangeMidGame: boolean |
| 12 | +} |
| 13 | + |
| 14 | +type StoredPlayerConfig = Omit<PlayerConfig, 'takebackLimit'> & { |
| 15 | + takebackLimit: number | null |
| 16 | +} |
| 17 | + |
| 18 | +const LOCAL_STORAGE_KEY = 'vibeChess.config' |
| 19 | + |
| 20 | +const getIpcRenderer = () => { |
| 21 | + if (typeof window === 'undefined') return null |
| 22 | + const anyWindow = window as typeof window & { require?: (module: string) => unknown } |
| 23 | + if (!anyWindow?.process?.versions?.electron || !anyWindow.require) return null |
| 24 | + try { |
| 25 | + return anyWindow.require('electron').ipcRenderer as { |
| 26 | + invoke: (channel: string, ...args: unknown[]) => Promise<unknown> |
| 27 | + } |
| 28 | + } catch { |
| 29 | + return null |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +const isGameMode = (value: unknown): value is GameMode => |
| 34 | + value === 'vs-maia' || value === '1v1' |
| 35 | + |
| 36 | +const isColorChoice = (value: unknown): value is ColorChoice => |
| 37 | + value === 'white' || value === 'black' || value === 'random' |
| 38 | + |
| 39 | +const isBoardThemeKey = (value: unknown): value is BoardThemeKey => |
| 40 | + value === 'green' || value === 'brown' || value === 'blue' || value === 'gray' |
| 41 | + |
| 42 | +const normalizeConfig = (value: unknown): Partial<PlayerConfig> | null => { |
| 43 | + if (!value || typeof value !== 'object') return null |
| 44 | + const data = value as Record<string, unknown> |
| 45 | + const result: Partial<PlayerConfig> = {} |
| 46 | + |
| 47 | + if (isGameMode(data.gameMode)) result.gameMode = data.gameMode |
| 48 | + if (isColorChoice(data.colorChoice)) result.colorChoice = data.colorChoice |
| 49 | + if (typeof data.elo === 'number' && Number.isFinite(data.elo)) { |
| 50 | + result.elo = snapMaiaElo(data.elo) |
| 51 | + } |
| 52 | + if (isBoardThemeKey(data.boardTheme)) result.boardTheme = data.boardTheme |
| 53 | + |
| 54 | + if (data.takebackLimit === null) { |
| 55 | + result.takebackLimit = Infinity |
| 56 | + } else if (typeof data.takebackLimit === 'number' && Number.isFinite(data.takebackLimit)) { |
| 57 | + result.takebackLimit = Math.max(0, Math.floor(data.takebackLimit)) |
| 58 | + } |
| 59 | + |
| 60 | + if (typeof data.allowEloChangeMidGame === 'boolean') { |
| 61 | + result.allowEloChangeMidGame = data.allowEloChangeMidGame |
| 62 | + } |
| 63 | + |
| 64 | + return result |
| 65 | +} |
| 66 | + |
| 67 | +const toStoredConfig = (config: PlayerConfig): StoredPlayerConfig => ({ |
| 68 | + ...config, |
| 69 | + takebackLimit: |
| 70 | + config.takebackLimit === Infinity |
| 71 | + ? null |
| 72 | + : Math.max(0, Math.floor(config.takebackLimit)), |
| 73 | +}) |
| 74 | + |
| 75 | +export const loadPlayerConfig = async (): Promise<Partial<PlayerConfig> | null> => { |
| 76 | + const ipc = getIpcRenderer() |
| 77 | + if (ipc) { |
| 78 | + try { |
| 79 | + const data = await ipc.invoke('config:read') |
| 80 | + return normalizeConfig(data) |
| 81 | + } catch { |
| 82 | + return null |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + try { |
| 87 | + const raw = localStorage.getItem(LOCAL_STORAGE_KEY) |
| 88 | + if (!raw) return null |
| 89 | + return normalizeConfig(JSON.parse(raw)) |
| 90 | + } catch { |
| 91 | + return null |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +export const savePlayerConfig = async (config: PlayerConfig): Promise<void> => { |
| 96 | + const payload: StoredPlayerConfig = toStoredConfig(config) |
| 97 | + |
| 98 | + const ipc = getIpcRenderer() |
| 99 | + if (ipc) { |
| 100 | + await ipc.invoke('config:write', payload) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + try { |
| 105 | + localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(payload)) |
| 106 | + } catch { |
| 107 | + // Ignore storage failures in browser mode. |
| 108 | + } |
| 109 | +} |
0 commit comments