/
main.py
184 lines (159 loc) · 6.26 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""A quiz game that reads from a json file"""
import base64, hug, json, random, requests, uuid
players = {}
questions = []
static = {}
class Player:
def __init__(self, name):
self.name = name
self.id = str(uuid.uuid4())
self.attempted = 0
self.correct = 0
self.restarts = 0
global players
players[self.id] = self
def next_q(self):
global questions
if self.attempted < len(questions):
self.attempted += 1
return questions[self.attempted - 1]
else:
return -1
def cur_a(self):
global questions
return questions[self.attempted - 1].get_correct()
def inc_correct(self):
self.correct += 1
def restart(self):
self.attempted = 0
self.correct = 0
self.restarts += 1
class Question:
def __init__(self, question, answers, correct):
self.question = question
self.answers = answers
self.correct = correct
global questions
questions.append(self)
def get_question(self):
return self.question
def get_answers(self):
return self.answers
def get_correct(self):
return self.correct
@hug.get("/init", output=hug.output_format.text)
def init(name):
"""Initialises a player"""
print(name.replace("&", "&").replace("<", "<").replace(">", ">"))
cur = Player(name.replace("&", "&").replace("<", "<").replace(">", ">"))
return cur.id
@hug.get("/delete")
def init(uuid):
"""Deletes a player"""
try:
global players
del players[uuid]
hug.redirect.to("/")
except:
hug.redirect.to("/")
@hug.get("/admin", output=hug.output_format.html)
def admin():
"""The admin page. Provides a UI for reloading templates and questions, seeing the current players and removing them from the game"""
global players
global questions
global static
player_card = "<li id='{}'><div class='collapsible-header'>{}<i class='material-icons right' onclick='delete_player(\"{}\")')'>delete</i></div><div class='collapsible-body'>• <span title='{}'>Hover for UUID</span><br>• Questions: {}/{}<br>• Restarts: {}</div></li>\n"
players_concat = ""
for i in players:
players_concat += player_card.format(players[i].id, players[i].name, players[i].id, players[i].id, players[i].correct, players[i].attempted, players[i].restarts)
question_card = "<li><div class='collapsible-header'>{}</div><div class='collapsible-body'>{}Correct: {}</div></li>\n"
questions_concat = ""
for i in questions:
ans = ""
for x in i.get_answers():
ans += "• " + x + "<br>"
questions_concat += question_card.format(i.get_question(), ans, i.get_correct())
return static["admin"].format(players_concat, questions_concat)
@hug.get("/reload")
def load_statics():
"""Reload all static content from their files"""
global questions
global static
for file in ["admin", "end", "index", "question"]:
with open("templates/" + file + ".html") as f:
static[file] = f.read()
for file in ["client"]:
with open("scripts/" + file + ".js") as f:
static[file] = f.read()
with open("questions.json") as f:
questions = []
for question in json.loads(f.read()):
Question(question["question"], question["answers"], question["correct"])
return "Success!"
@hug.get("/next", output=hug.output_format.html)
def get_next_q(uuid):
"""Returns a page containing the next question."""
global players
global static
if uuid not in players:
hug.redirect.to("/")
else:
q = players[uuid].next_q()
if q != -1:
buttons = ""
colours = ["#F44336", "#FF9800", "#03A9F4", "#009688"]
for i in q.get_answers():
buttons += "<a class='waves-effect waves-light btn-large' style='width: 100%; background-color: " + colours.pop(0) + ";' onclick='get_answer(\"" + uuid + "\", \"" + i + "\")'>" + i + "</a><br><br>\n"
return static["question"].format(q.get_question(), buttons)
else:
return static["end"].format(players[uuid].name, str(players[uuid].correct), str(players[uuid].attempted), uuid, uuid)
@hug.get("/ans", output=hug.output_format.text)
def get_ans(uuid):
"""Get the correct answer for the question the given player is on"""
if uuid not in players:
hug.redirect.to("/")
else:
return players[uuid].cur_a()
@hug.get("/correct")
def correct(uuid):
"""Increment the correct counter for the given player"""
if uuid not in players:
hug.redirect.to("/")
else:
players[uuid].inc_correct()
return "Success!"
@hug.get("/restart")
def restart(uuid):
"""Restart the quiz for the given player"""
if uuid not in players:
hug.redirect.to("/")
else:
players[uuid].restart()
hug.redirect.to("/next?uuid=" + uuid)
@hug.get("/", output=hug.output_format.html)
def get_index():
"""The index page/starting page"""
return static["index"]
@hug.get("/scripts/client.js", output=hug.output_format.text)
def get_clientjs():
"""The main client code"""
return static["client"]
@hug.get("/get_otdb_qs")
def get_otdb_qs(quant):
"""Fills the question list with the requested number of questions from the Open Trivia Database"""
global questions
questions = []
r = requests.get("http://www.opentdb.com/api.php?amount=" + str(quant) + "&encode=base64").json()
for q in r["results"]:
if base64.b64decode(q["type"]).decode() == "multiple":
answers = [base64.b64decode(q["correct_answer"]).decode()]
for a in q["incorrect_answers"]:
answers.append(base64.b64decode(a).decode())
random.shuffle(answers)
else:
answers = ["True", "False"]
Question(base64.b64decode(q["question"]).decode(), answers, base64.b64decode(q["correct_answer"]).decode())
return "Success!"
if __name__ == '__main__':
load_statics()
hug.API(__name__).http.serve(80, True)