/** * 游戏跟踪器 - 记录游戏结果 */ class GameTracker { constructor() { this.currentGameId = null; this.gameStartTime = null; this.attemptsCount = 0; this.gameData = null; } /** * 开始游戏跟踪 */ startGame(gameId, gameData = null) { this.currentGameId = gameId; this.gameStartTime = Date.now(); this.attemptsCount = 0; this.gameData = gameData; // 详细数据打印 console.log('🎮 游戏开始跟踪'); console.log('📊 游戏信息:', { 游戏ID: gameId, 游戏类型: gameData?.gameType || '未知', 难度: gameData?.difficulty || '未知', 开始时间: new Date().toLocaleString('zh-CN'), 总问题数: gameData?.totalQuestions || '未知' }); console.log('🎯 开始游戏跟踪:', gameId); } /** * 记录游戏尝试 */ recordAttempt() { this.attemptsCount++; // 详细数据打印 console.log('🔄 游戏尝试记录'); console.log('📊 尝试信息:', { 游戏ID: this.currentGameId, 尝试次数: this.attemptsCount, 尝试时间: new Date().toLocaleString('zh-CN'), 游戏时长: this.getCurrentPlayTime() + '秒' }); console.log('🎯 记录游戏尝试:', this.attemptsCount); } /** * 记录游戏胜利 */ async recordWin(score = 0, gameData = null) { await this.recordGameResult('win', score, gameData); } /** * 记录游戏失败 */ async recordLose(score = 0, gameData = null) { await this.recordGameResult('lose', score, gameData); } /** * 记录正确答案 */ async recordCorrect(score = 0, gameData = null) { await this.recordGameResult('correct', score, gameData); } /** * 记录错误答案 */ async recordIncorrect(score = 0, gameData = null) { await this.recordGameResult('incorrect', score, gameData); } /** * 记录游戏未完成 */ async recordIncomplete(score = 0, gameData = null) { await this.recordGameResult('incomplete', score, gameData); } /** * 记录游戏结果 */ async recordGameResult(gameResult, score = 0, additionalGameData = null) { if (!this.currentGameId || !this.gameStartTime) { console.warn('游戏跟踪未开始或已结束'); return; } const playTimeSeconds = Math.floor((Date.now() - this.gameStartTime) / 1000); const finalGameData = additionalGameData || this.gameData; // 详细数据打印 console.log('📝 游戏结果记录'); console.log('📊 游戏统计:', { 游戏ID: this.currentGameId, 游戏结果: gameResult, 最终得分: score, 游戏时长: playTimeSeconds + '秒', 尝试次数: this.attemptsCount, 结束时间: new Date().toLocaleString('zh-CN') }); if (finalGameData) { console.log('🎯 游戏详情:', { 问题内容: finalGameData.question || '未知问题', 用户选择: finalGameData.selected || '未知选择', 正确答案: finalGameData.correct || '未知答案', 总问题数: finalGameData.totalQuestions || '未知', 正确次数: finalGameData.correctAnswers || '未知' }); } console.log('🎮 记录游戏结果:', { gameId: this.currentGameId, result: gameResult, score: score, playTime: playTimeSeconds, attempts: this.attemptsCount }); try { await window.apiService.recordGameResult( this.currentGameId, gameResult, score, playTimeSeconds, this.attemptsCount, finalGameData ? JSON.stringify(finalGameData) : null ); console.log('✅ 游戏结果已成功发送到后端'); } catch (error) { console.error('❌ 记录游戏结果失败:', error); } // 重置状态 this.reset(); } /** * 重置跟踪状态 */ reset() { this.currentGameId = null; this.gameStartTime = null; this.attemptsCount = 0; this.gameData = null; } /** * 获取当前游戏时长 */ getCurrentPlayTime() { if (!this.gameStartTime) return 0; return Math.floor((Date.now() - this.gameStartTime) / 1000); } /** * 检查是否正在跟踪游戏 */ isTracking() { return this.currentGameId !== null && this.gameStartTime !== null; } /** * 获取当前游戏ID */ getCurrentGameId() { return this.currentGameId; } /** * 获取尝试次数 */ getAttemptsCount() { return this.attemptsCount; } } // 创建全局游戏跟踪器实例 window.gameTracker = new GameTracker();