diff --git a/Leaderboard.js b/Leaderboard.js index e69de29..67aa8ec 100644 --- a/Leaderboard.js +++ b/Leaderboard.js @@ -0,0 +1,243 @@ +// ========== PARTICLES ANIMATION ========== +const particlesContainer = document.getElementById('particles'); + +function createParticle() { + const particle = document.createElement('div'); + particle.className = 'particle'; + + + particle.style.left = Math.random() * 100 + '%'; + particle.style.top = Math.random() * 100 + '%'; + + + const size = 3 + Math.random() * 4; + particle.style.width = size + 'px'; + particle.style.height = size + 'px'; + + const duration = 3 + Math.random() * 5; + const delay = Math.random() * 2; + const moveX = (Math.random() - 0.5) * 200; + + const animationName = `float-${Date.now()}-${Math.random()}`; + const keyframes = ` + @keyframes ${animationName} { + 0% { + transform: translateY(0) translateX(0); + opacity: 0; + } + 10% { + opacity: 1; + } + 90% { + opacity: 1; + } + 100% { + transform: translateY(-100vh) translateX(${moveX}px); + opacity: 0; + } + } + `; + + const style = document.createElement('style'); + style.innerHTML = keyframes; + document.head.appendChild(style); + + particle.style.animation = `${animationName} ${duration}s ${delay}s ease-in-out forwards`; + + particlesContainer.appendChild(particle); + + setTimeout(() => { + particle.remove(); + style.remove(); + }, (duration + delay) * 1000); +} + +setInterval(createParticle, 300); + +for (let i = 0; i < 25; i++) { + setTimeout(createParticle, i * 100); +} + +// ========== LEADERBOARD DATA MANAGEMENT ========== + +// Get leaderboard data from localStorage +function getLeaderboardData() { + const data = localStorage.getItem('leaderboard2048'); + return data ? JSON.parse(data) : []; +} + +// Save leaderboard data to localStorage +function saveLeaderboardData(data) { + localStorage.setItem('leaderboard2048', JSON.stringify(data)); +} + +// Get current logged-in user +function getCurrentUser() { + return localStorage.getItem('currentUser') || null; +} + +// Add or update score for a player +function addScore(playerName, score) { + let leaderboard = getLeaderboardData(); + + // Find if player already exists + const existingIndex = leaderboard.findIndex(p => p.name === playerName); + + if (existingIndex >= 0) { + // Update only if new score is higher + if (score > leaderboard[existingIndex].score) { + leaderboard[existingIndex].score = score; + leaderboard[existingIndex].level = Math.floor(score / 100); + leaderboard[existingIndex].date = new Date().toISOString(); + } + } else { + // Add new player + leaderboard.push({ + name: playerName, + score: score, + level: Math.floor(score / 100), + date: new Date().toISOString() + }); + } + + saveLeaderboardData(leaderboard); +} + +// ========== RENDER LEADERBOARD ========== +function renderLeaderboard() { + let leaderboardData = getLeaderboardData(); + const currentUser = getCurrentUser(); + const list = document.getElementById('leaderboardList'); + const emptyState = document.getElementById('emptyState'); + + // Sort by score (highest first) + leaderboardData.sort((a, b) => b.score - a.score); + + // Update stats + const totalPlayers = leaderboardData.length; + const topScore = leaderboardData.length > 0 ? leaderboardData[0].score : 0; + + document.getElementById('totalPlayers').textContent = totalPlayers; + document.getElementById('topScore').textContent = topScore.toLocaleString(); + + // Find current user's rank + let userRank = '--'; + if (currentUser) { + const userIndex = leaderboardData.findIndex(p => p.name === currentUser); + if (userIndex >= 0) { + userRank = userIndex + 1; + } + } + document.getElementById('yourRank').textContent = userRank; + + // Check if there's data + if (leaderboardData.length === 0) { + list.style.display = 'none'; + if (emptyState) emptyState.style.display = 'block'; + return; + } + + list.style.display = 'flex'; + if (emptyState) emptyState.style.display = 'none'; + + // Render top 10 players + const top10 = leaderboardData.slice(0, 10); + + list.innerHTML = top10.map((player, index) => { + const rank = index + 1; + let rankClass = 'rank-other'; + + if (rank === 1) rankClass = 'rank-1'; + else if (rank === 2) rankClass = 'rank-2'; + else if (rank === 3) rankClass = 'rank-3'; + + const isCurrentUser = currentUser && player.name === currentUser; + const yourRankClass = isCurrentUser ? 'your-rank' : ''; + + return ` +