/** * Galaxy Strike Online - Client Dungeon System * Server-driven dungeon management client */ class DungeonSystem { constructor(gameEngine) { this.game = gameEngine; // Current dungeon state (runtime only) this.currentDungeon = null; this.currentRoom = null; this.dungeonProgress = 0; this.isExploring = false; // Server data loaded from server this.serverDungeons = []; this.roomTypes = {}; this.enemyTemplates = {}; console.log('[DUNGEON SYSTEM] Client DungeonSystem initialized - server-driven mode'); // Set up socket event listeners this.setupSocketListeners(); } /** * Set up Socket.IO event listeners for dungeon data */ setupSocketListeners() { if (!this.game.socket) { console.warn('[DUNGEON SYSTEM] No socket available for event listeners'); return; } // Listen for dungeon data response this.game.socket.on('dungeons_data', (data) => { console.log('[DUNGEON SYSTEM] Received dungeons data:', data); this.serverDungeons = data.dungeons || data; console.log('[DUNGEON SYSTEM] Loaded grouped dungeons from server:', Object.keys(this.serverDungeons)); // Update UI when data is loaded this.generateDungeonList(); }); // Listen for room types response this.game.socket.on('room_types_data', (data) => { console.log('[DUNGEON SYSTEM] Received room types data:', data); this.roomTypes = data; console.log('[DUNGEON SYSTEM] Loaded room types from server'); }); // Listen for enemy templates response this.game.socket.on('enemy_templates_data', (data) => { console.log('[DUNGEON SYSTEM] Received enemy templates data:', data); this.enemyTemplates = data; console.log(`[DUNGEON SYSTEM] Loaded ${Object.keys(this.enemyTemplates).length} enemy templates from server`); // Update UI when enemy data is loaded this.generateDungeonList(); }); // Listen for dungeon start response this.game.socket.on('dungeon_started', (data) => { console.log('[DUNGEON SYSTEM] Dungeon started:', data); this.currentDungeon = data.instance; this.isExploring = true; this.dungeonProgress = 0; }); // Listen for encounter response this.game.socket.on('encounter_data', (data) => { console.log('[DUNGEON SYSTEM] Encounter received:', data); this.currentRoom = data.encounter; this.dungeonProgress++; }); // Listen for dungeon completion response this.game.socket.on('dungeon_completed', (data) => { console.log('[DUNGEON SYSTEM] Dungeon completed:', data); // Reset dungeon state this.currentDungeon = null; this.currentRoom = null; this.isExploring = false; this.dungeonProgress = 0; }); // Listen for dungeon status response this.game.socket.on('dungeon_status', (data) => { console.log('[DUNGEON SYSTEM] Dungeon status received:', data); }); console.log('[DUNGEON SYSTEM] Socket event listeners set up'); } /** * Load dungeon data from server using Socket.IO packets */ async loadServerData() { try { console.log('[DUNGEON SYSTEM] Loading dungeon data from server via packets...'); if (!this.game.socket) { console.error('[DUNGEON SYSTEM] No socket connection available'); return; } // Request dungeons from server this.game.socket.emit('get_dungeons'); // Request room types from server this.game.socket.emit('get_room_types'); // Request enemy templates from server this.game.socket.emit('get_enemy_templates'); console.log('[DUNGEON SYSTEM] Server data requests sent via packets'); } catch (error) { console.error('[DUNGEON SYSTEM] Error loading server data:', error); } } /** * Get all available dungeons */ getAllDungeons() { return this.serverDungeons; } /** * Get dungeons by difficulty */ getDungeonsByDifficulty(difficulty) { return this.serverDungeons.filter(dungeon => dungeon.difficulty === difficulty); } /** * Get specific dungeon by ID */ getDungeon(dungeonId) { return this.serverDungeons.find(dungeon => dungeon.id === dungeonId); } /** * Get room type by ID */ getRoomType(roomTypeId) { return this.roomTypes[roomTypeId]; } /** * Get enemy template by ID */ getEnemyTemplate(enemyId) { return this.enemyTemplates[enemyId]; } /** * Start exploring a dungeon using Socket.IO packets */ async startDungeon(dungeonId) { try { console.log(`[DUNGEON SYSTEM] Starting dungeon: ${dungeonId}`); if (!this.game.socket) { console.error('[DUNGEON SYSTEM] No socket connection available'); return null; } // Send packet to start dungeon this.game.socket.emit('start_dungeon', { dungeonId: dungeonId, userId: this.game.player?.id || 'anonymous' }); console.log('[DUNGEON SYSTEM] Dungeon start packet sent'); return true; } catch (error) { console.error('[DUNGEON SYSTEM] Error starting dungeon:', error); return null; } } /** * Process current room encounter using Socket.IO packets */ async processEncounter() { if (!this.currentDungeon || !this.isExploring) { console.warn('[DUNGEON SYSTEM] No active dungeon to process'); return null; } try { console.log(`[DUNGEON SYSTEM] Processing encounter for dungeon: ${this.currentDungeon.id}`); if (!this.game.socket) { console.error('[DUNGEON SYSTEM] No socket connection available'); return null; } // Send packet to process encounter this.game.socket.emit('process_encounter', { instanceId: this.currentDungeon.id, userId: this.game.player?.id || 'anonymous' }); console.log('[DUNGEON SYSTEM] Encounter process packet sent'); return true; } catch (error) { console.error('[DUNGEON SYSTEM] Error processing encounter:', error); return null; } } /** * Complete current dungeon using Socket.IO packets */ async completeDungeon() { if (!this.currentDungeon || !this.isExploring) { console.warn('[DUNGEON SYSTEM] No active dungeon to complete'); return null; } try { console.log(`[DUNGEON SYSTEM] Completing dungeon: ${this.currentDungeon.id}`); if (!this.game.socket) { console.error('[DUNGEON SYSTEM] No socket connection available'); return null; } // Send packet to complete dungeon this.game.socket.emit('complete_dungeon', { instanceId: this.currentDungeon.id, userId: this.game.player?.id || 'anonymous' }); console.log('[DUNGEON SYSTEM] Dungeon completion packet sent'); return true; } catch (error) { console.error('[DUNGEON SYSTEM] Error completing dungeon:', error); return null; } } /** * Get player's current dungeon status using Socket.IO packets */ async getDungeonStatus() { try { if (!this.game.socket) { console.error('[DUNGEON SYSTEM] No socket connection available'); return null; } // Send packet to get dungeon status this.game.socket.emit('get_dungeon_status', { userId: this.game.player?.id || 'anonymous' }); console.log('[DUNGEON SYSTEM] Dungeon status request packet sent'); return true; } catch (error) { console.error('[DUNGEON SYSTEM] Error getting dungeon status:', error); } return null; } /** * Generate dungeon list UI using server data */ generateDungeonList() { console.log('[DUNGEON SYSTEM] Generating dungeon list UI with server data...'); const dungeonListElement = document.getElementById('dungeonList'); if (!dungeonListElement) { console.error('[DUNGEON SYSTEM] Dungeon list element not found'); return; } // Clear existing content dungeonListElement.innerHTML = ''; if (!this.serverDungeons || Object.keys(this.serverDungeons).length === 0) { dungeonListElement.innerHTML = '
Loading dungeons from server...
'; return; } // Generate HTML for each difficulty category let html = ''; Object.entries(this.serverDungeons).forEach(([difficulty, dungeons]) => { if (!dungeons || dungeons.length === 0) return; const difficultyClass = difficulty === 'tutorial' ? 'tutorial' : difficulty; const difficultyTitle = difficulty === 'tutorial' ? 'Tutorial Dungeons' : difficulty.charAt(0).toUpperCase() + difficulty.slice(1) + ' Dungeons'; const difficultyIcon = this.getDifficultyIcon(difficulty); // Add difficulty header html += `