-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
190 lines (148 loc) · 4.94 KB
/
server.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
185
186
187
188
189
190
from flask import Flask, render_template, jsonify, request, redirect, url_for
import json
app = Flask(__name__)
progress = 1
state = "home"
score = 0
combo_path = "static/data/combo.json"
with open(combo_path, "r") as combo_data:
combo_dic = json.load(combo_data)
# 1: single with pic opts, 2: single without pic, 3: rank, 4: multiple
quiz_1 = {
"type": 3,
"title": "Rank the following cards according to the game's card hierarchy.",
"options": ["9C", "2C", "Abomb"],
"correct": ["2", "1", "0"],
}
quiz_2 = {
"type": 1,
"title": "Which of the following is a bomb?",
"options": ["9C", "2C", "Abomb"],
"correct": ["2"],
}
quiz_6 = {
"type": 3,
"title": "Rank the following triplet with an attached pair.",
"options": ["AA222", "AAA33", "AAA22"],
"correct": ["0", "2", "1"],
}
quiz_3 = {
"type": 2,
"title": "What’s the best option to deal in this turn?",
"options": ["A", "2", "Joker", "Q"],
"q_img": "qt_1",
"correct": ["0"],
}
quiz_4 = {
"type": 2,
"title": "What’s the best option to deal in this turn?",
"options": ["10,10,10,2", "10,10,10,9", "Joker", "K,K,2,2"],
"q_img": "qt_2",
"correct": ["1"],
}
quiz_5 = {
"type": 4,
"title": "Which of the following are bombs?",
"options": ["9C", "2C", "Abomb", "Abomb"],
"correct": ["2", "3"],
}
mp_qz = [quiz_1, quiz_2, quiz_3, quiz_4, quiz_5, quiz_6]
@app.errorhandler(404)
def not_found(e):
# defining function
return render_template("404_page.html")
@app.route("/")
def welcome():
return render_template("welcome.html", progress=progress)
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/increment_progress")
def increment_progress():
global progress
progress = min(progress + 1, 10)
return redirect_to_current_progress()
@app.route("/decrement_progress")
def decrement_progress():
global progress
progress = max(progress - 1, 1)
return redirect_to_current_progress()
@app.route("/current_progress")
def current_progress():
return redirect_to_current_progress()
def redirect_to_current_progress():
if progress <= 3:
return redirect(url_for("rules", rule_id=progress))
if progress == 4:
return redirect(url_for("rank"))
if progress == 10:
return redirect(url_for("finish"))
else:
return redirect(url_for("combo", combo_id=progress - 4))
@app.route("/rules/<int:rule_id>")
def rules(rule_id):
rule_titles = {1: "Basic Rules", 2: "Bidding Rules", 3: "Winning Rules"}
title = rule_titles.get(rule_id, "Unknown Rule")
return render_template("rules.html", title=title, rule_id=rule_id)
@app.route("/combo/<int:combo_id>")
def combo(combo_id):
max = 3 * combo_id
if combo_id == 5:
combo = [combo_dic[str(max - 2)], combo_dic[str(max - 1)]]
else:
combo = [combo_dic[str(max - 2)], combo_dic[str(max - 1)], combo_dic[str(max)]]
return render_template("combo.html", combo_id=combo_id, combo=combo)
@app.route("/rank")
def rank():
return render_template("rank.html", rank=combo_dic["rank"])
@app.route("/finish")
def finish():
return render_template("finish.html", rank=combo_dic["rank"])
# @app.route("/quiz")
# def quiz():
# return render_template("quiz.html")
@app.route("/quiz", methods=["GET", "POST"])
def quiz():
if request.method == "POST":
response = request.json
total = len(mp_qz)
correct_num = 0
data_for_template = []
for i, quiz in enumerate(mp_qz):
user_answer = response["answers"][i]
correct_answer = quiz["correct"]
is_correct = (
(user_answer == correct_answer)
if quiz["type"] == 3
else (sorted(user_answer) == sorted(correct_answer))
)
correct_num += is_correct
data_for_template.append(
{
"question": quiz,
"user_answer": user_answer,
"correct_answer": correct_answer,
"is_correct": is_correct,
}
)
score = round((correct_num / total) * 100)
if score >= 90:
message = "Outstanding performance! Congratulations!"
elif score >= 75:
message = "Great job! You really know your stuff."
elif score >= 50:
message = "Good effort! With a little more practice, you can master it."
elif score >= 25:
message = "Fair attempt. Keep studying and try again!"
else:
message = "It seems like you struggled. Review the material and try again!"
return render_template(
"quiz_result.html", score=score, message=message, data=data_for_template
)
else:
return render_template("quiz.html", data={"quiz": mp_qz})
@app.route("/end")
def end():
return render_template("end.html", score=score)
if __name__ == "__main__":
app.run(debug=True)