Szabo Ivan c168da359e implement complete frontend:
- add lobby, game, leaderboard, results pages
- add scripts: storage, lobby, game, drawing, scoring, countries
- add styles: main, lobby, game
- unify header/footer across all pages
- show lobby name in lobby view
- remove clear board from leaderboard
2026-05-05 13:01:14 +02:00

76 lines
1.6 KiB
JavaScript

// 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,
};
})();