Класс Analytics Dashboard
class GameDashboard { constructor() { this.metrics = { activePlayers:
1247, gamesPlayed: 8942, avgSession: 4.2, completionRate: 68 };
this.updateInterval = null; this.isRealTime = false; } // Обновление
метрик updateMetrics() { // Симуляция изменения данных
this.metrics.activePlayers += this.randomChange(50, 0.1);
this.metrics.gamesPlayed += this.randomChange(100, 0.05);
this.metrics.avgSession += this.randomChange(0.5, 0.05);
this.metrics.completionRate += this.randomChange(2, 0.02); //
Ограничиваем значения this.metrics.completionRate = Math.max(0,
Math.min(100, this.metrics.completionRate)); this.metrics.avgSession =
Math.max(1, this.metrics.avgSession); this.renderMetrics();
this.logMetricsUpdate(); } // Случайное изменение с трендом
randomChange(maxChange, trend = 0) { const random = (Math.random() -
0.5) * 2; // -1 до 1 const trendFactor = Math.random() < 0.7 ? 1 : -1;
// 70% позитивный тренд return (random * maxChange + trend *
maxChange) * trendFactor; } // Отображение метрик renderMetrics() {
this.animateValue('active-players', this.metrics.activePlayers, true);
this.animateValue('games-played', this.metrics.gamesPlayed, true);
this.animateValue('avg-session', this.metrics.avgSession.toFixed(1) +
'м'); this.animateValue('completion-rate',
Math.round(this.metrics.completionRate) + '%'); } // Анимация
изменения значений animateValue(elementId, newValue, isNumber = false)
{ const element = document.getElementById(elementId); const startValue
= isNumber ? parseInt(element.textContent.replace(/[^\d]/g, '')) :
element.textContent; element.style.transform = 'scale(1.1)';
element.style.color = '#4caf50'; // Обновляем значение if (isNumber) {
element.textContent = newValue.toLocaleString(); } else {
element.textContent = newValue; } // Возвращаем исходное состояние
setTimeout(() => { element.style.transform = 'scale(1)';
element.style.color = ''; }, 500); } // Включение режима реального
времени startRealTimeUpdates() { this.isRealTime = true;
this.updateInterval = setInterval(() => { this.updateMetrics(); },
5000); // Обновление каждые 5 секунд } // Выключение режима реального
времени stopRealTimeUpdates() { this.isRealTime = false; if
(this.updateInterval) { clearInterval(this.updateInterval); } } //
Экспорт данных exportMetrics() { const data = { timestamp: new
Date().toISOString(), metrics: this.metrics, trends:
this.calculateTrends() }; return JSON.stringify(data, null, 2); } //
Расчет трендов calculateTrends() { // Здесь должна быть логика
сравнения с предыдущими периодами return { activePlayers: { change:
12, period: 'week' }, gamesPlayed: { change: 8, period: 'day' },
avgSession: { change: -5, period: 'day' }, completionRate: { change:
3, period: 'week' } }; } logMetricsUpdate() { console.log('Метрики
обновлены:', { время: new Date().toLocaleTimeString(), активные:
this.metrics.activePlayers, игры: this.metrics.gamesPlayed, сессия:
this.metrics.avgSession, завершение: this.metrics.completionRate }); }
}