/
quizgame.py
220 lines (186 loc) · 7.37 KB
/
quizgame.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import sqlite3
from contextlib import closing
from random import shuffle
from flask import Flask, render_template, request, redirect, g, session, flash
from flaskext.bcrypt import Bcrypt
from access_db import *
#configs
DEBUG = True
SECRET_KEY = "placeholder"
database = 'quizgame.db'
schema = 'schema.sql'
csv = 'quiz_questions.csv'
app = Flask(__name__)
bcrypt = Bcrypt(app)
app.config.from_object(__name__)
def get_questions(n, ordered=False):
question_nums = get_question_nums()
if not ordered:
shuffle(question_nums)
questions = []
for i in range(n):
question = get_question(question_nums[i])
questions.append(question)
return questions
@app.before_request
def before_request():
g.db = connect_db()
@app.teardown_request
def teardown_request(exception):
db = getattr(g, 'db', None)
if db is not None:
db.close()
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
username = request.form['username']
if username in get_usernames():
pw_hash = get_password(username)
if bcrypt.check_password_hash(pw_hash, request.form['password']):
session['username'] = username
session['logged_in'] = True
flash('You were logged in')
return redirect('database')
else:
error = 'Incorrect password'
else:
error = 'Invalid username'
return render_template('login.html', error=error)
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect('/')
@app.route('/create_account', methods=['GET', 'POST'])
def create_user():
error = None
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
password_confirm = request.form['password_confirm']
if password != password_confirm:
error = "Passwords don't match"
elif username in get_usernames():
error = "Username already in use. Please choose another."
else:
add_user(username, password)
flash('Account created')
return redirect('login')
return render_template('create_account.html', error=error)
@app.route('/database', methods=['GET', 'POST'])
def database_access():
if request.method == 'POST':
update_type = request.form['change']
i = request.form['id']
q = request.form['question']
a1, a2, a3, a4 = request.form['ans1'], request.form['ans2'], request.form['ans3'], request.form['ans4']
c = request.form['correct']
n = get_num_questions() + 1
question_info = [n, q, a1, a2, a3, a4, c]
names = ['num', 'question', 'ans1', 'ans2', 'ans3', 'ans4', 'correct']
try:
if not i and (update_type == 'delete' or update_type == 'update'):
raise Exception('Must enter question number to update or delete')
if update_type == 'delete':
add_proposed([i] + question_info + ['delete', session['username']])
flash('Question submitted for deletion')
else:
for item in question_info:
if item == "":
if update_type == 'add':
raise Exception('Input cannot be left blank')
else:
if update_type == 'update':
pass
if update_type == 'add':
add_proposed([i] + question_info + ['add', session['username']])
flash('Question submitted for addition')
else:
add_proposed([i] + question_info + ['update', session['username']])
flash('Question submitted for update')
except (sqlite3.OperationalError, Exception) as e:
flash('Invalid question entry: ' + str(e))
questions = get_questions(get_num_questions(), ordered=True)
return render_template('database.html', questions=questions)
@app.route('/admin', methods=['GET','POST'])
def admin():
if request.method == 'POST':
pass
additions, updates, deletions = get_proposed()
questions = get_questions(get_num_questions(), ordered=True)
return render_template('admin.html', add=additions, update=updates, delete=deletions, questions=questions)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
total_questions = get_num_questions()
options = range(5, total_questions + 1, 5)
if options[-1] != total_questions:
options.append(total_questions)
return render_template('welcome.html', options=options)
else:
nquestions = int(request.form['nquestions'])
app.questions = get_questions(nquestions)
app.nquestions = nquestions
app.curquestion = 0
app.score = 0
num_to_show = 10
app.highscores = get_highscores(num_to_show)
if len(app.highscores) >= num_to_show:
app.lowest_highscore = app.highscores[-1][1]
else:
app.lowest_highscore = None
return redirect('main')
@app.route('/main')
def main():
if app.curquestion >= app.nquestions:
return redirect('/end')
else:
return redirect('/next')
@app.route('/end', methods=['GET','POST'])
def end():
score = app.score/app.nquestions * 100
if request.method == 'GET' and score >= app.lowest_highscore:
return render_template('end.html', score=score, \
highscores=app.highscores, get_info=True)
elif request.method == 'POST':
name = request.form['name']
add_to_highscores(name, score)
# update highscores
app.highscores = get_highscores(10)
return render_template('end.html', score=score, \
highscores=app.highscores, get_info=False)
else:
return render_template('end.html', score=score, \
highscores=app.highscores, get_info=False)
@app.route('/next', methods=['GET', 'POST'])
def next():
if request.method == 'GET':
app.question_info = app.questions[app.curquestion]
app.n = app.curquestion + 1
app.q = app.question_info['question']
a1, a2, a3, a4 = app.question_info['A'], app.question_info['B'], \
app.question_info['C'], app.question_info['D']
return render_template('question.html', num=app.n, question=app.q, \
ans1=a1, ans2=a2, ans3=a3, ans4=a4)
else:
# gets response to question and stays on question if not answered
ans = request.form
if len(ans) == 0:
return redirect('main')
else:
ans = ans['response']
app.curquestion += 1
correct = app.question_info['correct']
if ans == correct:
app.score += 1.0
feedback = 'Correct!'
else:
feedback = 'Incorrect! The correct answer was "%s"' \
%(app.question_info[correct])
flash(feedback)
return redirect('main')
if __name__ == '__main__':
if get_num_questions() == 0:
create_database(database, schema, csv)
app.run()