-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
156 lines (107 loc) · 3.93 KB
/
app.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from flask import Flask
from flask_socketio import SocketIO, send, emit
from flask import render_template
from flask_ngrok import run_with_ngrok
from pyngrok import ngrok
from flask_cors import CORS
import question_reader
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
socketio = SocketIO(app, cors_allowed_origins="*")
audience_votes = [0, 0, 0, 0]
# CORS(app)
# run_with_ngrok(app)
def clear_votes():
for i in range(0, 4):
audience_votes[i] = 0
def get_current_votes_status():
return audience_votes
def add_audience_vote(locked_option_idx):
# if locked_option_idx not in audience_votes:
# audience_votes[locked_option_idx] = 0
audience_votes[locked_option_idx] += 1
@app.route("/")
@app.route("/contestant")
def get_contestant():
return render_template("contestant.html")
# return render_template("inst_test.html")
# return render_template("graph_test.html")
@app.route("/host")
def get_host():
return render_template("host.html")
@app.route("/home_start")
def get_home_start():
return render_template("home_start.html")
@app.route("/spectator")
def get_spectator():
return render_template("spectator.html")
@app.route("/url_qr")
def get_url_qr():
return render_template("url_qr.html")
@socketio.on("message")
def handle_message(msg):
print("message "+str(msg))
data = msg
# send(msg, broadcast=True)
# data = {"q": "one", "0":4}
send(data, broadcast=True)
@socketio.on('get_file_names')
def get_file_names():
file_names = question_reader.get_file_names()
print("Filenames: "+str(file_names))
# sending back the questions list
emit("get_file_names", file_names)
@socketio.on('get_question_set')
def get_question_set(file_name):
questions_set = question_reader.read_question_set_file(file_name)
print("No of questions: "+str(len(questions_set)))
# sending back the questions list
emit("get_question_set", questions_set)
@socketio.on('get_audience_poll_data')
def get_audience_poll_data():
emit("audience_poll_data", get_current_votes_status(), broadcast=True)
@socketio.on('set_timer')
def set_timer(curr_timer):
# print(curr_timer)
# using events not namespace
# if we don't specify broadcast it just replies back to the client which made this call
# broadcast true will send to all the connected clients
emit("curr_timer", curr_timer, broadcast=True)
@socketio.on("set_question")
def set_answer(question_obj):
print("question : " + str(question_obj))
clear_votes()
emit("question", question_obj, broadcast=True)
@socketio.on("set_game_rules")
def set_game_rules(rules_info):
print("rules info: " + str(rules_info))
emit("game_rules", rules_info, broadcast=True)
@socketio.on("set_locked_answer")
def set_locked_answer(option_idx):
print("locked answer: "+str(option_idx))
emit("locked_answer", option_idx, broadcast=True)
@socketio.on("set_audience_locked_answer")
def set_audience_locked_answer(option_idx):
print("audience answer: "+str(option_idx))
add_audience_vote(option_idx)
@socketio.on("set_answer")
def set_answer(answer_obj):
print("correct answer: " + str(answer_obj))
emit("answer", answer_obj, broadcast=True)
@socketio.on("set_lifelines")
def set_lifelines(lifelines_obj):
print("Lifelines: " + str(lifelines_obj))
emit("lifelines", lifelines_obj, broadcast=True)
@socketio.on("set_5050")
def set_5050(removed_indexes):
print("removed_indexes: " + str(removed_indexes))
emit("lifeline_5050", removed_indexes, broadcast=True)
if __name__ == "__main__":
# to run as sudo, sudo venv/bin/python3.7 app.py
# if we run at host "0.0.0.0" then we can access the server using it's ip
# from other machines which are in the same network
# clear_votes()
# url = ngrok.connect(5000).public_url
# print(' * Tunnel URL:', url)
socketio.run(app, debug=True, host="0.0.0.0", port=8000)
# socketio.run(app, host="0.0.0.0", port=5000)