|  |  |  | @ -0,0 +1,109 @@ | 
			
		
	
		
			
				
					|  |  |  |  | from flask import Flask, jsonify, request | 
			
		
	
		
			
				
					|  |  |  |  | import mysql.connector | 
			
		
	
		
			
				
					|  |  |  |  | import random | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | app = Flask(__name__) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | # 数据库连接配置 | 
			
		
	
		
			
				
					|  |  |  |  | db_config = { | 
			
		
	
		
			
				
					|  |  |  |  |     'host': 'localhost', | 
			
		
	
		
			
				
					|  |  |  |  |     'user': 'root', | 
			
		
	
		
			
				
					|  |  |  |  |     'password': '123456', | 
			
		
	
		
			
				
					|  |  |  |  |     'database': 'stu' | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | # 获取学生名单 | 
			
		
	
		
			
				
					|  |  |  |  | def get_students(): | 
			
		
	
		
			
				
					|  |  |  |  |     try: | 
			
		
	
		
			
				
					|  |  |  |  |         connection = mysql.connector.connect(**db_config) | 
			
		
	
		
			
				
					|  |  |  |  |         cursor = connection.cursor(dictionary=True) | 
			
		
	
		
			
				
					|  |  |  |  |         cursor.execute("SELECT * FROM studentlist") | 
			
		
	
		
			
				
					|  |  |  |  |         students = cursor.fetchall() | 
			
		
	
		
			
				
					|  |  |  |  |         connection.close() | 
			
		
	
		
			
				
					|  |  |  |  |         return students | 
			
		
	
		
			
				
					|  |  |  |  |     except mysql.connector.Error as err: | 
			
		
	
		
			
				
					|  |  |  |  |         print(f"Error: {err}") | 
			
		
	
		
			
				
					|  |  |  |  |         return [] | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | # 更新学生积分 | 
			
		
	
		
			
				
					|  |  |  |  | def update_student_score(student_id, new_score): | 
			
		
	
		
			
				
					|  |  |  |  |     try: | 
			
		
	
		
			
				
					|  |  |  |  |         connection = mysql.connector.connect(**db_config) | 
			
		
	
		
			
				
					|  |  |  |  |         cursor = connection.cursor() | 
			
		
	
		
			
				
					|  |  |  |  |         cursor.execute("UPDATE studentlist SET 积分 = %s WHERE 学号 = %s", (new_score, student_id)) | 
			
		
	
		
			
				
					|  |  |  |  |         connection.commit() | 
			
		
	
		
			
				
					|  |  |  |  |         print(f"Updated student_id {student_id} with new score {new_score}") | 
			
		
	
		
			
				
					|  |  |  |  |         connection.close() | 
			
		
	
		
			
				
					|  |  |  |  |     except mysql.connector.Error as err: | 
			
		
	
		
			
				
					|  |  |  |  |         print(f"Error: {err}") | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | # 根据积分设置权重,积分越高,权重越低 | 
			
		
	
		
			
				
					|  |  |  |  | def calculate_weights(students): | 
			
		
	
		
			
				
					|  |  |  |  |     max_score = max([s['积分'] for s in students]) | 
			
		
	
		
			
				
					|  |  |  |  |     weights = [(max_score - s['积分'] + 1) for s in students] | 
			
		
	
		
			
				
					|  |  |  |  |     return weights | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | # 随机点名,积分越高,权重越低 | 
			
		
	
		
			
				
					|  |  |  |  | def pick_random_student(students): | 
			
		
	
		
			
				
					|  |  |  |  |     weights = calculate_weights(students) | 
			
		
	
		
			
				
					|  |  |  |  |     selected_student = random.choices(students, weights=weights, k=1)[0] | 
			
		
	
		
			
				
					|  |  |  |  |     return selected_student | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | # 增加课堂出席积分 | 
			
		
	
		
			
				
					|  |  |  |  | def add_attendance_score(student): | 
			
		
	
		
			
				
					|  |  |  |  |     student['积分'] = student['积分'] + 1 | 
			
		
	
		
			
				
					|  |  |  |  |     update_student_score(student['学号'], student['积分']) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | # 回答问题后的积分更新 | 
			
		
	
		
			
				
					|  |  |  |  | def update_answer_score(student, repeated_correctly, answer_score): | 
			
		
	
		
			
				
					|  |  |  |  |     if repeated_correctly: | 
			
		
	
		
			
				
					|  |  |  |  |         student['积分'] = student['积分'] + 0.5 | 
			
		
	
		
			
				
					|  |  |  |  |     else: | 
			
		
	
		
			
				
					|  |  |  |  |         student['积分'] = student['积分'] - 1 | 
			
		
	
		
			
				
					|  |  |  |  |      | 
			
		
	
		
			
				
					|  |  |  |  |     student['积分'] += answer_score | 
			
		
	
		
			
				
					|  |  |  |  |     update_student_score(student['学号'], student['积分']) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | # 获取随机点名学生 | 
			
		
	
		
			
				
					|  |  |  |  | @app.route('/get_random_student', methods=['GET']) | 
			
		
	
		
			
				
					|  |  |  |  | def get_random_student(): | 
			
		
	
		
			
				
					|  |  |  |  |     students = get_students() | 
			
		
	
		
			
				
					|  |  |  |  |     selected_student = pick_random_student(students) | 
			
		
	
		
			
				
					|  |  |  |  |     return jsonify({'name': selected_student['姓名'], 'student_id': selected_student['学号']}) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | # 更新出席积分 | 
			
		
	
		
			
				
					|  |  |  |  | @app.route('/update_attendance_score', methods=['POST']) | 
			
		
	
		
			
				
					|  |  |  |  | def handle_attendance_score(): | 
			
		
	
		
			
				
					|  |  |  |  |     data = request.json | 
			
		
	
		
			
				
					|  |  |  |  |     print(f"Received data: {data}") | 
			
		
	
		
			
				
					|  |  |  |  |     student_id = data['student_id'] | 
			
		
	
		
			
				
					|  |  |  |  |      | 
			
		
	
		
			
				
					|  |  |  |  |     students = get_students() | 
			
		
	
		
			
				
					|  |  |  |  |     for student in students: | 
			
		
	
		
			
				
					|  |  |  |  |         if student['学号'] == student_id: | 
			
		
	
		
			
				
					|  |  |  |  |             add_attendance_score(student)             | 
			
		
	
		
			
				
					|  |  |  |  |             break | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     return jsonify({'message': 'Attendance score updated', 'student_id': student_id}) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | # 更新回答问题的积分 | 
			
		
	
		
			
				
					|  |  |  |  | @app.route('/update_answer_score', methods=['POST']) | 
			
		
	
		
			
				
					|  |  |  |  | def handle_answer_score(): | 
			
		
	
		
			
				
					|  |  |  |  |     data = request.json | 
			
		
	
		
			
				
					|  |  |  |  |     print(f"Received data: {data}") | 
			
		
	
		
			
				
					|  |  |  |  |     student_id = data['student_id'] | 
			
		
	
		
			
				
					|  |  |  |  |     repeated_correctly = data['repeated_correctly'] | 
			
		
	
		
			
				
					|  |  |  |  |     answer_score = data['answer_score']  # 前端传来的回答得分(0.5到3之间) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     students = get_students() | 
			
		
	
		
			
				
					|  |  |  |  |     for student in students: | 
			
		
	
		
			
				
					|  |  |  |  |         if student['学号'] == student_id: | 
			
		
	
		
			
				
					|  |  |  |  |             update_answer_score(student, repeated_correctly, answer_score) | 
			
		
	
		
			
				
					|  |  |  |  |             break | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     return jsonify({'message': 'Answer score updated', 'student_id': student_id}) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | if __name__ == '__main__': | 
			
		
	
		
			
				
					|  |  |  |  |     app.run(debug=True) |