// storage.js — localStorage helpers const Storage = (() => { const KEYS = { PLAYER_NAME: "gd_playerName", LOBBY_NAME: "gd_lobbyName", GAME_STATE: "gd_gameState", LEADERBOARD: "gd_leaderboard", }; function savePlayerName(name) { localStorage.setItem(KEYS.PLAYER_NAME, name.trim()); } function getPlayerName() { return localStorage.getItem(KEYS.PLAYER_NAME) || "Anonymous"; } function saveLobbyName(name) { localStorage.setItem(KEYS.LOBBY_NAME, name.trim()); } function getLobbyName() { return localStorage.getItem(KEYS.LOBBY_NAME) || "My Lobby"; } function saveGameState(state) { localStorage.setItem(KEYS.GAME_STATE, JSON.stringify(state)); } function getGameState() { try { return JSON.parse(localStorage.getItem(KEYS.GAME_STATE)) || null; } catch { return null; } } function clearGameState() { localStorage.removeItem(KEYS.GAME_STATE); } function saveLeaderboard(entry) { const board = getLeaderboard(); board.push(entry); board.sort((a, b) => b.totalScore - a.totalScore); const top20 = board.slice(0, 20); localStorage.setItem(KEYS.LEADERBOARD, JSON.stringify(top20)); } function getLeaderboard() { try { return JSON.parse(localStorage.getItem(KEYS.LEADERBOARD)) || []; } catch { return []; } } function clearLeaderboard() { localStorage.removeItem(KEYS.LEADERBOARD); } return { savePlayerName, getPlayerName, saveLobbyName, getLobbyName, saveGameState, getGameState, clearGameState, saveLeaderboard, getLeaderboard, clearLeaderboard, }; })();