163 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			163 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| /**
 | |
|  * 游戏数据记录器 - 为所有游戏提供统一的数据记录功能
 | |
|  */
 | |
| class GameDataLogger {
 | |
|     constructor() {
 | |
|         this.gameStats = {
 | |
|             totalClicks: 0,
 | |
|             correctAnswers: 0,
 | |
|             incorrectAnswers: 0,
 | |
|             totalQuestions: 0,
 | |
|             startTime: null,
 | |
|             gameType: null
 | |
|         };
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 初始化游戏数据记录
 | |
|      */
 | |
|     initGame(gameType, totalQuestions = 0) {
 | |
|         this.gameStats = {
 | |
|             totalClicks: 0,
 | |
|             correctAnswers: 0,
 | |
|             incorrectAnswers: 0,
 | |
|             totalQuestions: totalQuestions,
 | |
|             startTime: Date.now(),
 | |
|             gameType: gameType
 | |
|         };
 | |
|         
 | |
|         console.log('🎮 游戏数据记录器初始化');
 | |
|         console.log('📊 游戏信息:', {
 | |
|             游戏类型: gameType,
 | |
|             总问题数: totalQuestions,
 | |
|             开始时间: new Date().toLocaleString('zh-CN')
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 记录用户点击/选择
 | |
|      */
 | |
|     logUserAction(actionType, actionData) {
 | |
|         this.gameStats.totalClicks++;
 | |
|         
 | |
|         console.log('👆 用户操作记录');
 | |
|         console.log('📊 操作信息:', {
 | |
|             操作类型: actionType,
 | |
|             操作次数: this.gameStats.totalClicks,
 | |
|             操作时间: new Date().toLocaleString('zh-CN'),
 | |
|             游戏时长: this.getGameDuration() + '秒'
 | |
|         });
 | |
|         console.log('🎯 操作详情:', actionData);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 记录问题回答
 | |
|      */
 | |
|     logQuestionAnswer(questionData, userAnswer, isCorrect, score = 0) {
 | |
|         if (isCorrect) {
 | |
|             this.gameStats.correctAnswers++;
 | |
|         } else {
 | |
|             this.gameStats.incorrectAnswers++;
 | |
|         }
 | |
| 
 | |
|         console.log('❓ 问题回答记录');
 | |
|         console.log('📊 问题信息:', {
 | |
|             问题内容: questionData.question || '未知问题',
 | |
|             问题编号: questionData.questionNumber || '未知',
 | |
|             选项: questionData.options || '未知',
 | |
|             正确答案: questionData.correct || '未知',
 | |
|             用户答案: userAnswer,
 | |
|             是否正确: isCorrect ? '✅ 正确' : '❌ 错误',
 | |
|             得分: score
 | |
|         });
 | |
|         console.log('📈 游戏统计:', {
 | |
|             总点击次数: this.gameStats.totalClicks,
 | |
|             正确次数: this.gameStats.correctAnswers,
 | |
|             错误次数: this.gameStats.incorrectAnswers,
 | |
|             正确率: this.getAccuracy() + '%',
 | |
|             平均得分: this.getAverageScore() + '分'
 | |
|         });
 | |
|         console.log('⏰ 时间信息:', {
 | |
|             回答时间: new Date().toLocaleString('zh-CN'),
 | |
|             游戏时长: this.getGameDuration() + '秒'
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 记录游戏结果
 | |
|      */
 | |
|     logGameResult(result, finalScore, gameData) {
 | |
|         console.log('🏁 游戏结果记录');
 | |
|         console.log('📊 最终统计:', {
 | |
|             游戏类型: this.gameStats.gameType,
 | |
|             游戏结果: result,
 | |
|             最终得分: finalScore,
 | |
|             总点击次数: this.gameStats.totalClicks,
 | |
|             正确次数: this.gameStats.correctAnswers,
 | |
|             错误次数: this.gameStats.incorrectAnswers,
 | |
|             正确率: this.getAccuracy() + '%',
 | |
|             游戏时长: this.getGameDuration() + '秒',
 | |
|             结束时间: new Date().toLocaleString('zh-CN')
 | |
|         });
 | |
|         
 | |
|         if (gameData) {
 | |
|             console.log('🎯 游戏详情:', gameData);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 获取游戏时长(秒)
 | |
|      */
 | |
|     getGameDuration() {
 | |
|         if (!this.gameStats.startTime) return 0;
 | |
|         return Math.floor((Date.now() - this.gameStats.startTime) / 1000);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 获取正确率
 | |
|      */
 | |
|     getAccuracy() {
 | |
|         const total = this.gameStats.correctAnswers + this.gameStats.incorrectAnswers;
 | |
|         if (total === 0) return 0;
 | |
|         return ((this.gameStats.correctAnswers / total) * 100).toFixed(1);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 获取平均得分
 | |
|      */
 | |
|     getAverageScore() {
 | |
|         const total = this.gameStats.correctAnswers + this.gameStats.incorrectAnswers;
 | |
|         if (total === 0) return 0;
 | |
|         return (this.gameStats.correctAnswers * 10 / total).toFixed(1);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 获取游戏统计
 | |
|      */
 | |
|     getStats() {
 | |
|         return {
 | |
|             ...this.gameStats,
 | |
|             gameDuration: this.getGameDuration(),
 | |
|             accuracy: this.getAccuracy(),
 | |
|             averageScore: this.getAverageScore()
 | |
|         };
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 重置统计
 | |
|      */
 | |
|     reset() {
 | |
|         this.gameStats = {
 | |
|             totalClicks: 0,
 | |
|             correctAnswers: 0,
 | |
|             incorrectAnswers: 0,
 | |
|             totalQuestions: 0,
 | |
|             startTime: null,
 | |
|             gameType: null
 | |
|         };
 | |
|     }
 | |
| }
 | |
| 
 | |
| // 创建全局游戏数据记录器实例
 | |
| window.gameDataLogger = new GameDataLogger();
 |