/** * LeaderboardSystem.js * ===================== * KRVAVA ลฝETEV - Leaderboard & Online Integration * * Features: * - Local leaderboards * - Steam API integration (ready) * - Firebase integration (ready) * - Multiple categories * - Score submission & retrieval * * @author NovaFarma Team * @date 2025-12-23 */ export default class LeaderboardSystem { constructor(scene) { this.scene = scene; // Integration mode this.mode = 'local'; // 'local', 'steam', 'firebase' // Local leaderboards this.localLeaderboards = new Map(); // Online connection this.isOnline = false; this.steamInitialized = false; this.firebaseInitialized = false; // Leaderboard categories this.categories = { horde_waves: { name: 'Horde Waves Survived', icon: '๐ŸŒŠ', type: 'high_score' }, farm_size: { name: 'Largest Farm', icon: '๐ŸŒพ', type: 'high_score' }, zombie_army: { name: 'Most Zombies Tamed', icon: '๐ŸงŸ', type: 'high_score' }, boss_speedrun: { name: 'Troll King Speedrun', icon: 'โš”๏ธ', type: 'low_time' }, wealth: { name: 'Total Wealth', icon: '๐Ÿ’ฐ', type: 'high_score' }, generations: { name: 'Most Generations', icon: '๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ', type: 'high_score' }, collection: { name: 'Album Completion %', icon: '๐Ÿ“š', type: 'high_score' } }; console.log('๐Ÿ† LeaderboardSystem initialized'); // Initialize local storage this.initializeLocalLeaderboards(); // Try to connect to online services this.initializeOnlineServices(); } /** * Initialize local leaderboards */ initializeLocalLeaderboards() { Object.keys(this.categories).forEach(category => { this.localLeaderboards.set(category, []); }); // Load from localStorage this.loadLocalLeaderboards(); console.log('โœ… Local leaderboards initialized'); } /** * Load from localStorage */ loadLocalLeaderboards() { try { const saved = localStorage.getItem('krvava_zetev_leaderboards'); if (saved) { const data = JSON.parse(saved); Object.keys(data).forEach(category => { this.localLeaderboards.set(category, data[category]); }); console.log('๐Ÿ“ฅ Loaded local leaderboards from storage'); } } catch (error) { console.error('Failed to load local leaderboards:', error); } } /** * Save to localStorage */ saveLocalLeaderboards() { try { const data = {}; this.localLeaderboards.forEach((entries, category) => { data[category] = entries; }); localStorage.setItem('krvava_zetev_leaderboards', JSON.stringify(data)); } catch (error) { console.error('Failed to save local leaderboards:', error); } } /** * Initialize online services */ initializeOnlineServices() { // Try Steam if (typeof window.Steamworks !== 'undefined') { this.initializeSteam(); } // Try Firebase if (typeof window.firebase !== 'undefined') { this.initializeFirebase(); } if (!this.steamInitialized && !this.firebaseInitialized) { console.log('๐Ÿ“ด Running in offline mode - local leaderboards only'); } } /** * Initialize Steam API */ initializeSteam() { try { // Steam API initialization // TODO: Replace with actual Steamworks.js integration console.log('๐ŸŽฎ Initializing Steam API...'); // Example Steam API calls (pseudo-code): // window.Steamworks.init(); // window.Steamworks.getPlayerName(); this.steamInitialized = true; this.mode = 'steam'; this.isOnline = true; console.log('โœ… Steam API connected!'); this.showNotification({ title: 'Steam Connected', text: '๐ŸŽฎ Online leaderboards enabled!', icon: 'โœ…' }); } catch (error) { console.error('Steam initialization failed:', error); this.steamInitialized = false; } } /** * Initialize Firebase */ initializeFirebase() { try { // Firebase initialization // TODO: Replace with actual Firebase config console.log('๐Ÿ”ฅ Initializing Firebase...'); const firebaseConfig = { apiKey: "YOUR_API_KEY", authDomain: "krvava-zetev.firebaseapp.com", databaseURL: "https://krvava-zetev.firebaseio.com", projectId: "krvava-zetev", storageBucket: "krvava-zetev.appspot.com" }; // firebase.initializeApp(firebaseConfig); // firebase.database(); this.firebaseInitialized = true; if (!this.steamInitialized) { this.mode = 'firebase'; this.isOnline = true; } console.log('โœ… Firebase connected!'); } catch (error) { console.error('Firebase initialization failed:', error); this.firebaseInitialized = false; } } /** * Submit score */ submitScore(category, score, playerName = 'Player', metadata = {}) { const entry = { playerName: playerName, score: score, date: new Date().toISOString(), metadata: metadata }; console.log(`๐Ÿ“Š Submitting score: ${category} = ${score}`); // Submit to appropriate service switch (this.mode) { case 'steam': this.submitToSteam(category, entry); break; case 'firebase': this.submitToFirebase(category, entry); break; default: this.submitToLocal(category, entry); } return true; } /** * Submit to local leaderboard */ submitToLocal(category, entry) { let leaderboard = this.localLeaderboards.get(category) || []; const categoryData = this.categories[category]; // Add entry leaderboard.push(entry); // Sort if (categoryData.type === 'high_score') { leaderboard.sort((a, b) => b.score - a.score); } else { leaderboard.sort((a, b) => a.score - b.score); } // Keep top 100 leaderboard = leaderboard.slice(0, 100); this.localLeaderboards.set(category, leaderboard); this.saveLocalLeaderboards(); console.log(`โœ… Score submitted to local leaderboard: ${category}`); } /** * Submit to Steam */ submitToSteam(category, entry) { try { // Steam leaderboard submission // Pseudo-code: // window.Steamworks.uploadLeaderboardScore(category, entry.score); console.log(`๐ŸŽฎ Submitted to Steam: ${category} = ${entry.score}`); // Also save locally as backup this.submitToLocal(category, entry); } catch (error) { console.error('Steam submission failed:', error); this.submitToLocal(category, entry); } } /** * Submit to Firebase */ submitToFirebase(category, entry) { try { // Firebase database submission // Pseudo-code: // firebase.database().ref(`leaderboards/${category}`).push(entry); console.log(`๐Ÿ”ฅ Submitted to Firebase: ${category} = ${entry.score}`); // Also save locally as backup this.submitToLocal(category, entry); } catch (error) { console.error('Firebase submission failed:', error); this.submitToLocal(category, entry); } } /** * Get leaderboard */ async getLeaderboard(category, limit = 10) { switch (this.mode) { case 'steam': return await this.getSteamLeaderboard(category, limit); case 'firebase': return await this.getFirebaseLeaderboard(category, limit); default: return this.getLocalLeaderboard(category, limit); } } /** * Get local leaderboard */ getLocalLeaderboard(category, limit = 10) { const leaderboard = this.localLeaderboards.get(category) || []; return leaderboard.slice(0, limit); } /** * Get Steam leaderboard */ async getSteamLeaderboard(category, limit = 10) { try { // Steam leaderboard retrieval // Pseudo-code: // const entries = await window.Steamworks.getLeaderboardEntries(category, limit); // return entries; console.log(`๐ŸŽฎ Fetching Steam leaderboard: ${category}`); // Fallback to local return this.getLocalLeaderboard(category, limit); } catch (error) { console.error('Steam fetch failed:', error); return this.getLocalLeaderboard(category, limit); } } /** * Get Firebase leaderboard */ async getFirebaseLeaderboard(category, limit = 10) { try { // Firebase leaderboard retrieval // Pseudo-code: // const snapshot = await firebase.database() // .ref(`leaderboards/${category}`) // .orderByChild('score') // .limitToLast(limit) // .once('value'); // return snapshot.val(); console.log(`๐Ÿ”ฅ Fetching Firebase leaderboard: ${category}`); // Fallback to local return this.getLocalLeaderboard(category, limit); } catch (error) { console.error('Firebase fetch failed:', error); return this.getLocalLeaderboard(category, limit); } } /** * Get player rank */ async getPlayerRank(category, playerName) { const leaderboard = await this.getLeaderboard(category, 1000); const rank = leaderboard.findIndex(entry => entry.playerName === playerName); return rank >= 0 ? rank + 1 : null; } /** * Get all categories */ getCategories() { return Object.keys(this.categories).map(id => ({ id: id, ...this.categories[id] })); } /** * Check if online */ isConnectedOnline() { return this.isOnline; } /** * Get connection status */ getConnectionStatus() { return { mode: this.mode, isOnline: this.isOnline, steam: this.steamInitialized, firebase: this.firebaseInitialized }; } /** * Helper: Show notification */ showNotification(notification) { console.log(`๐Ÿ“ข ${notification.icon} ${notification.title}: ${notification.text}`); const ui = this.scene.scene.get('UIScene'); if (ui && ui.showNotification) { ui.showNotification(notification); } } }