Simplified codes
This commit is contained in:
127
app.py
127
app.py
@@ -1,44 +1,141 @@
|
||||
from flask import Flask, render_template, request, jsonify, session
|
||||
import json
|
||||
import random
|
||||
import os
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = 'quizimoto_secret_key'
|
||||
|
||||
# TODO: Dima - Implement prize structure (15 levels)
|
||||
# Load questions from file
|
||||
with open('questions.json', 'r') as f:
|
||||
questions_data = json.load(f)
|
||||
|
||||
# Prize structure (15 levels)
|
||||
PRIZE_LEVELS = [
|
||||
# Format: [1000, 2000, 4000, 8000, 16000, 32000, ... up to 15 values]
|
||||
100, 200, 300, 500, 1000,
|
||||
2000, 4000, 8000, 16000, 32000,
|
||||
64000, 125000, 250000, 500000, 1000000
|
||||
]
|
||||
|
||||
GUARANTEED_LEVELS = [5, 10] # First and second guaranteed levels
|
||||
|
||||
def get_current_question():
|
||||
"""Get current question data from session"""
|
||||
current_q_index = session.get('current_question', 0)
|
||||
questions = session.get('questions', [])
|
||||
|
||||
if current_q_index >= len(questions):
|
||||
return None
|
||||
|
||||
return questions[current_q_index], current_q_index
|
||||
|
||||
def calculate_guaranteed_prize(current_index):
|
||||
"""Calculate the guaranteed prize based on current question index"""
|
||||
final_score = 0
|
||||
for level in sorted(GUARANTEED_LEVELS, reverse=True):
|
||||
if current_index + 1 >= level:
|
||||
final_score = PRIZE_LEVELS[level - 1] if level <= len(PRIZE_LEVELS) else 0
|
||||
break
|
||||
return final_score
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
|
||||
@app.route('/start')
|
||||
def start_game():
|
||||
# TODO: Dima - Initialize game session
|
||||
# Set up: score, current_question, lifelines, questions
|
||||
# Initialize game session
|
||||
session['score'] = 0
|
||||
session['current_question'] = 0
|
||||
session['lifelines'] = ['fifty_fifty']
|
||||
session['questions'] = random.sample(questions_data, min(15, len(questions_data))) # Select up to 15 random questions
|
||||
return render_template('game.html')
|
||||
|
||||
@app.route('/get_question')
|
||||
def get_question():
|
||||
# TODO: Dima - Serve questions to frontend
|
||||
# Return: question, options, question_number, current_prize
|
||||
return jsonify({"error": "Not implemented"})
|
||||
current_q_index = session.get('current_question', 0)
|
||||
|
||||
# Check if game is over (15 questions answered or no more questions)
|
||||
if current_q_index >= len(session.get('questions', [])) or current_q_index >= 15:
|
||||
return jsonify({
|
||||
"game_over": True,
|
||||
"final_score": session.get('score', 0)
|
||||
})
|
||||
|
||||
question_data = session['questions'][current_q_index]
|
||||
|
||||
return jsonify({
|
||||
"question_number": current_q_index + 1,
|
||||
"question": question_data['question'],
|
||||
"options": question_data['options'],
|
||||
"current_prize": PRIZE_LEVELS[current_q_index] if current_q_index < len(PRIZE_LEVELS) else 1000000,
|
||||
"game_over": False
|
||||
})
|
||||
|
||||
@app.route('/answer', methods=['POST'])
|
||||
def check_answer():
|
||||
# TODO: Dima - Validate answers and update score
|
||||
# Check if answer is correct, update session, return result
|
||||
return jsonify({"error": "Not implemented"})
|
||||
data = request.get_json()
|
||||
answer = data.get('answer', '')
|
||||
|
||||
question_info = get_current_question()
|
||||
if not question_info:
|
||||
return jsonify({"error": "No more questions"})
|
||||
|
||||
question_data, current_q_index = question_info
|
||||
correct = answer == question_data['correct_answer']
|
||||
|
||||
if correct:
|
||||
session['current_q_index'] = current_q_index + 1
|
||||
session['score'] = PRIZE_LEVELS[current_q_index] if current_q_index < len(PRIZE_LEVELS) else session.get('score', 0)
|
||||
|
||||
# Check if game is won (15th question answered correctly)
|
||||
game_won = session['current_question'] >= min(15, len(session.get('questions', [])))
|
||||
|
||||
return jsonify({
|
||||
"correct": True,
|
||||
"correct_answer": question_data['correct_answer'],
|
||||
"final_score": session['score'],
|
||||
"game_over": game_won
|
||||
})
|
||||
else:
|
||||
# Game over with last guaranteed prize
|
||||
final_score = calculate_guaranteed_prize(current_q_index)
|
||||
|
||||
return jsonify({
|
||||
"correct": False,
|
||||
"correct_answer": question_data['correct_answer'],
|
||||
"final_score": final_score,
|
||||
"game_over": True
|
||||
})
|
||||
|
||||
@app.route('/lifeline/<lifeline_name>')
|
||||
def use_lifeline(lifeline_name):
|
||||
# TODO: Dima - Implement 50:50 and Phone a Friend
|
||||
# Note: Ask AI will be version 2
|
||||
return jsonify({"error": "Not implemented"})
|
||||
if lifeline_name != 'fifty_fifty':
|
||||
return jsonify({"error": "Unknown lifeline"})
|
||||
|
||||
if 'fifty_fifty' not in session.get('lifelines', []):
|
||||
return jsonify({"error": "Lifeline already used"})
|
||||
|
||||
question_info = get_current_question()
|
||||
if not question_info:
|
||||
return jsonify({"error": "No current question"})
|
||||
|
||||
# Remove the lifeline from available lifelines
|
||||
session['lifelines'].remove('fifty_fifty')
|
||||
|
||||
question_data, _ = question_info
|
||||
correct_answer = question_data['correct_answer']
|
||||
options = question_data['options']
|
||||
|
||||
# Find indices of wrong answers to remove (we need to remove 2)
|
||||
wrong_indices = [i for i, option in enumerate(options) if option != correct_answer]
|
||||
|
||||
# Randomly select 2 wrong indices to remove
|
||||
indices_to_remove = random.sample(wrong_indices, min(2, len(wrong_indices)))
|
||||
indices_to_remove.sort(reverse=True) # Sort in descending order for safe removal
|
||||
|
||||
return jsonify({
|
||||
"remove_indices": indices_to_remove
|
||||
})
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
app.run(debug=True)
|
||||
Reference in New Issue
Block a user