1
// 3. Define API endpoint for getting questions (line 8-10)
2
@app.route('/get_question')
3
def get_question():
4
// 1. Get current question number from session
5
question_num = session.get('current_question', 0)
6
7
// 2. Get question from database
8
question = mongo.db.questions.find_one({"question_number": question_num})
9
10
// 3. Format response
11
if question:
12
return jsonify({
13
"question": question['question'],
14
"options": question['options'],
15
"question_number": question_num,
16
"current_prize": calculate_prize(question_num)
17
})
18
else:
19
return jsonify({"game_over": True, "final_score": calculate_final_score()})
20
21
// 4. Define API endpoint for submitting answers (line 12-14)
22
@app.route('/answer', methods=['POST'])
23
def answer_question():
24
// 1. Get user's answer
25
user_answer = request.json.get('answer')
26
27
// 2. Get current question
28
question_num = session.get('current_question', 0)
29
current_question = mongo.db.questions.find_one({"question_number": question_num})
30
31
// 3. Check if answer is correct
32
is_correct = current_question['options'][current_question['correct_answer']] == user_answer
33
34
// 4. Update game state
35
if is_correct:
36
session['current_question'] = question_num + 1
37
return jsonify({
38
"correct": True,
39
"correct_answer": current_question['correct_answer'],
40
"game_over": question_num + 1 >= TOTAL_QUESTIONS
41
})
42
else:
43
return jsonify({
44
"correct": False,
45
"correct_answer": current_question['correct_answer'],
46
"game_over": True
47
})