176 lines
6.2 KiB
Markdown
176 lines
6.2 KiB
Markdown
DIMA - BACKEND DEVELOPER MISSION
|
|
Your Role: Game Brain Developer | Your File: app.py
|
|
|
|
💡 What You're Building: You're creating the "brain" of the game - all the rules, scoring, and game logic!
|
|
|
|
📋 LESSON 1-2: SETUP & PRIZE SYSTEM
|
|
|
|
Step 1: Fork and Clone (10 minutes)
|
|
1. Go to: https://gitea.techshare.cc/technolyceum/ai6-m2
|
|
2. Click the "Fork" button (creates your copy)
|
|
3. Copy your forked repository URL
|
|
4. Open Terminal and type:
|
|
git clone [YOUR-FORKED-URL-HERE]
|
|
cd ai6-m2
|
|
git checkout -b dima-backend-work
|
|
|
|
Step 2: Create the Prize Money System (20 minutes)
|
|
Open app.py and find these lines around line 13:
|
|
|
|
# Prize structure (15 levels)
|
|
PRIZE_LEVELS = [
|
|
100, 200, 300, 500, 1000,
|
|
2000, 4000, 8000, 16000, 32000,
|
|
64000, 125000, 250000, 500000, 1000000
|
|
]
|
|
|
|
This is already implemented! Notice how the prizes increase as the player progresses through the questions.
|
|
|
|
Step 3: Review Game Session Management (15 minutes)
|
|
Look at the [start_game](file:///Users/home/YandexDisk/TECHNOLYCEUM/ict_repos/ai6-m2/app.py#L42-L48) function (around line 42):
|
|
|
|
```python
|
|
@app.route('/start')
|
|
def start_game():
|
|
# 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)))
|
|
return render_template('game.html')
|
|
```
|
|
|
|
This function initializes the game session with:
|
|
- Starting score of 0
|
|
- Current question index of 0
|
|
- One lifeline available: fifty_fifty
|
|
- A random selection of up to 15 questions from the database
|
|
|
|
📋 LESSON 3-4: QUESTION RETRIEVAL & ANSWER VALIDATION
|
|
|
|
Step 4: Review Question Retrieval (15 minutes)
|
|
Check the [get_question](file:///Users/home/YandexDisk/TECHNOLYCEUM/ict_repos/ai6-m2/app.py#L50-L66) function (around line 50):
|
|
|
|
```python
|
|
@app.route('/get_question')
|
|
def get_question():
|
|
current_q_index = session.get('current_question', 0)
|
|
|
|
# Check if game is over
|
|
if current_q_index >= min(15, len(session.get('questions', []))):
|
|
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
|
|
})
|
|
```
|
|
|
|
This function retrieves the current question and formats it for the frontend.
|
|
|
|
Step 5: Review Answer Validation (20 minutes)
|
|
Look at the [check_answer](file:///Users/home/YandexDisk/TECHNOLYCEUM/ict_repos/ai6-m2/app.py#L68-L103) function (around line 68):
|
|
|
|
```python
|
|
@app.route('/answer', methods=['POST'])
|
|
def check_answer():
|
|
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_question'] = 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
|
|
})
|
|
```
|
|
|
|
Notice the helper functions:
|
|
- [get_current_question()](file:///Users/home/YandexDisk/TECHNOLYCEUM/ict_repos/ai6-m2/app.py#L20-L28) - Gets the current question data from session
|
|
- [calculate_guaranteed_prize()](file:///Users/home/YandexDisk/TECHNOLYCEUM/ict_repos/ai6-m2/app.py#L30-L37) - Calculates prize based on guaranteed levels
|
|
|
|
📋 LESSON 5-6: LIFELINES & REFACTORING
|
|
|
|
Step 6: Review Lifeline Implementation (15 minutes)
|
|
Check the [use_lifeline](file:///Users/home/YandexDisk/TECHNOLYCEUM/ict_repos/ai6-m2/app.py#L105-L133) function (around line 105):
|
|
|
|
```python
|
|
@app.route('/lifeline/<lifeline_name>')
|
|
def use_lifeline(lifeline_name):
|
|
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
|
|
})
|
|
```
|
|
|
|
Notice the improvements:
|
|
- Uses list comprehension for finding wrong indices
|
|
- Uses helper functions to reduce code duplication
|
|
- Has clear error handling
|
|
|
|
✅ DIMA'S COMPLETION CHECKLIST
|
|
☐ Understand the prize system structure
|
|
☐ Review game session initialization
|
|
☐ Understand question retrieval logic
|
|
☐ Review answer validation process
|
|
☐ Understand lifeline implementation
|
|
☐ Recognize the use of helper functions for DRY code
|
|
☐ All code pushed to your branch
|
|
☐ Created Pull Request for teacher to review
|
|
|
|
🎉 Congratulations Dima! You built the entire game backend! |