File size: 2,125 Bytes
cdfb731
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit, join_room, leave_room
import random

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
socketio = SocketIO(app)

# Store questions and participants
questions = [
    {"question": "What is the capital of France?", "options": ["Paris", "London", "Berlin", "Rome"]},
    {"question": "What is the largest planet?", "options": ["Earth", "Mars", "Jupiter", "Saturn"]}
]
current_question = {"index": 0, "answers": {}}
participants = {}

@app.route('/')
def index():
    return "Welcome to the Quiz App"

@app.route('/client')
def client():
    return render_template('client.html')

@app.route('/host')
def host():
    return render_template('host.html')

@socketio.on('join')
def on_join(data):
    username = data['username']
    participants[request.sid] = username
    join_room('quiz')
    emit('update_participants', participants, room='quiz')
    print(f"{username} joined the quiz.")

@socketio.on('disconnect')
def on_leave():
    if request.sid in participants:
        username = participants[request.sid]
        leave_room('quiz')
        del participants[request.sid]
        emit('update_participants', participants, room='quiz')
        print(f"{username} left the quiz.")

@socketio.on('request_question')
def send_question():
    index = current_question['index']
    question = questions[index]
    emit('new_question', question, room=request.sid)

@socketio.on('submit_answer')
def receive_answer(data):
    username = participants.get(request.sid, "Unknown")
    answer = data['answer']
    current_question['answers'][username] = answer
    emit('update_results', current_question['answers'], room='quiz')

@socketio.on('next_question')
def next_question():
    current_question['index'] += 1
    current_question['answers'] = {}
    if current_question['index'] < len(questions):
        question = questions[current_question['index']]
        emit('new_question', question, room='quiz')
    else:
        emit('quiz_end', room='quiz')

if __name__ == '__main__':
    socketio.run(app, debug=True)