/* Chat Statistics Module */ class ChatStatistics { constructor() { this.currentData = null; this.statsContainer = null; this.init(); } init() { this.createStatsContainer(); // Button wird jetzt von statistics-integration.js hinzugefügt } createStatsContainer() { // Create stats container const statsHTML = `
`; document.body.insertAdjacentHTML('beforeend', statsHTML); this.statsContainer = document.getElementById('statsContainer'); // Add event listeners document.getElementById('statsClose').addEventListener('click', () => this.hideStats()); document.getElementById('statsOverlay').addEventListener('click', () => this.hideStats()); } analyzeChat(messages) { console.log('Analyzing chat with', messages.length, 'messages'); this.currentData = this.calculateStatistics(messages); // Trigger custom event that button is ready const event = new CustomEvent('statsReady', { detail: this.currentData }); document.dispatchEvent(event); } calculateStatistics(messages) { const stats = { totalMessages: 0, chatMessages: 0, joinLeaveMessages: 0, players: new Map(), timeDistribution: new Map(), wordCount: new Map(), messageLength: [] }; messages.forEach(message => { stats.totalMessages++; if (message.type === 'chat') { stats.chatMessages++; // Player statistics const player = message.player; if (!stats.players.has(player)) { stats.players.set(player, { name: player, role: message.role, messageCount: 0, totalChars: 0, avgMessageLength: 0 }); } const playerStats = stats.players.get(player); playerStats.messageCount++; playerStats.totalChars += message.message.length; playerStats.avgMessageLength = Math.round(playerStats.totalChars / playerStats.messageCount); // Time distribution (by hour) const hour = message.timestamp.split(':')[0]; stats.timeDistribution.set(hour, (stats.timeDistribution.get(hour) || 0) + 1); // Word analysis const words = message.message.toLowerCase().match(/\b\w+\b/g) || []; words.forEach(word => { if (word.length > 3) { // Only count words longer than 3 characters stats.wordCount.set(word, (stats.wordCount.get(word) || 0) + 1); } }); // Message length stats.messageLength.push(message.message.length); } else if (message.type === 'join' || message.type === 'leave') { stats.joinLeaveMessages++; } }); return stats; } showStats() { if (!this.currentData) return; this.renderStatistics(); document.getElementById('statsContainer').style.display = 'block'; document.getElementById('statsOverlay').style.display = 'block'; document.body.style.overflow = 'hidden'; } hideStats() { document.getElementById('statsContainer').style.display = 'none'; document.getElementById('statsOverlay').style.display = 'none'; document.body.style.overflow = 'auto'; } renderStatistics() { const data = this.currentData; const content = document.getElementById('statsContent'); // Convert Maps to sorted arrays const topPlayers = Array.from(data.players.values()) .sort((a, b) => b.messageCount - a.messageCount) .slice(0, 10); const topWords = Array.from(data.wordCount.entries()) .sort((a, b) => b[1] - a[1]) .slice(0, 15); const timeActivity = Array.from(data.timeDistribution.entries()) .sort((a, b) => a[0] - b[0]); const avgMessageLength = data.messageLength.length > 0 ? Math.round(data.messageLength.reduce((a, b) => a + b, 0) / data.messageLength.length) : 0; content.innerHTML = `