This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
352 lines (306 loc) · 9.98 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from bottle import (
Bottle, jinja2_view, run,
static_file, request
)
from collections import OrderedDict
from datetime import datetime
from functools import partial
import importlib
from itertools import groupby
import judge
import operator
from pathlib import Path
import random
import re
import sqlite3
app = application = Bottle()
jinja2_template = partial(jinja2_view, template_lookup=['templates'])
_db_name = '/tmp/codegame.db'
_db_backup = '/tmp/codegame.prv.db'
# default table schema
_create_db_tables_sql = '''\
CREATE TABLE game (
id INTEGER PRIMARY KEY ASC AUTOINCREMENT,
name TEXT,
question TEXT,
timestamp DATETIME DEFAULT NULL
);
CREATE TABLE result (
id INTEGER PRIMARY KEY ASC AUTOINCREMENT,
name TEXT,
submit TEXT,
codingtime TEXT,
timestamp DATETIME DEFAULT NULL,
judge TEXT,
judge_detail TEXT,
gameid INTEGER,
FOREIGN KEY(gameid) REFERENCES game(id)
);
'''
def connect_db():
conn = sqlite3.connect(_db_name)
conn.row_factory = sqlite3.Row
return conn
def get_games(limit=-1):
sql_latest_games = 'SELECT * FROM game ORDER BY timestamp DESC'
games = []
with connect_db() as conn:
if limit < 0:
records = conn.execute(sql_latest_games).fetchall()
else:
records = conn.execute(
sql_latest_games + ' LIMIT ?', str(limit)
).fetchall()
for record in records:
games.append({
'id': record['id'],
'name': record['name'],
'question': record['question'],
's_time': record['timestamp']
})
return games
def insert_games(games):
try:
conn = connect_db()
conn.executemany(
'INSERT INTO game(name, question, timestamp) '
'VALUES (?, ?, datetime("now"))',
map(operator.itemgetter('name', 'question'), games)
)
conn.commit()
except Exception as e:
print(e)
return False
else:
return True
def get_results(gameid=''):
_get_result_sql = 'SELECT * FROM result'
_get_result_id_sql = ' WHERE result.gameid = %s ORDER BY result.name ASC'
# list all game result
results = []
with connect_db() as conn:
if not gameid:
records = conn.execute(_get_result_sql).fetchall()
else:
sql_cmd = _get_result_sql + _get_result_id_sql % gameid
records = conn.execute(sql_cmd).fetchall()
try:
# for record in records:
# results.append({
# 'id': record['id'],
# 'name': record['name'],
# 'submit': record['submit'],
# 'codingtime': record['codingtime'],
# 'timestamp': record['timestamp'],
# 'judge': record['judge'],
# 'gameid': record['gameid']
# })
results = [dict(rec) for rec in records]
except Exception as e:
print(e)
return results
# insert result each by each
def insert_result(name, submit, codingtime, judge, judge_detail, gameid):
sql_cmd = (
'INSERT INTO result'
'(name, submit, codingtime, timestamp, judge, judge_detail, gameid) '
"VALUES (?, ?, ?, datetime('now'), ?, ?, ?)"
)
with connect_db() as conn:
try:
conn.execute(
sql_cmd,
(name, submit, codingtime, judge, judge_detail, gameid)
)
conn.commit()
except Exception as e:
print(e)
return False
return True
def parse_question_folder():
"""Parse all questions under questions/ and return file mapping."""
q_pths = Path('questions').glob('q_*.py')
extract_q_name = re.compile('^q_(.+)$').match
return {
extract_q_name(pth.stem).group(1): pth
for pth in q_pths
}
@app.route('/', method='GET')
@jinja2_template('index.html')
def index(msg=''):
return {'msg': msg}
@app.route('/question/', method='GET')
@jinja2_template('questions.html')
def list_question():
all_questions = parse_question_folder()
return {
'questions': all_questions.keys()
}
@app.route('/admin/', method='POST')
def reload_db():
# if database does not exist, create a new one without renaming.
# otherwise move the original databse as xxx.prev
try:
Path(_db_name).rename(_db_backup)
db_existed = True
except OSError:
db_existed = False
# recreate the database. If any move fails, move back the original databse.
try:
conn = sqlite3.connect(_db_name)
conn.executescript(_create_db_tables_sql)
conn.commit()
except Exception:
# roll back using old database
conn.close()
if db_existed:
Path(_db_name).unlink()
Path(_db_backup).rename(_db_name)
return False
else:
conn.close()
if db_existed:
Path(_db_backup).unlink()
return True
@app.route('/play/', method='GET')
@jinja2_template('play.html')
def play():
game = get_games(limit=1)[-1]
q_pth = parse_question_folder()[game['name']]
q_name, q_desc, q_doc, q_ex_ans = judge.read_question(q_pth)
return {
'q_name': q_name,
'q_desc': q_desc,
'q_doc': q_doc,
'example_ans': q_ex_ans
}
@app.route('/play/', method='POST')
@jinja2_template('play.html')
def submit_play():
game = get_games(limit=1)[-1]
q_pth = parse_question_folder()[game['name']]
q_name, q_desc, q_doc, q_ex_ans = judge.read_question(q_pth)
player_name = request.forms.get('player_name')
answer_text = request.forms.get('code')
importlib.reload(judge)
test_prog, test_output = judge.run_judge(q_pth, answer_text)
num_total_test = test_prog.result.testsRun
num_failed_test = len(test_prog.result.failures)
num_error_test = len(test_prog.result.errors)
judge_detail_shorten = "E: %d, F: %d (Total %d)" % (
num_error_test, num_failed_test, num_total_test)
# insert result into db
insert_result(
name=player_name,
submit=answer_text,
codingtime="60",
judge=str(test_prog.success),
judge_detail=judge_detail_shorten,
gameid=str(game['id'])
)
history = ''
return {
'q_name': q_name,
'q_desc': q_desc,
'q_doc': q_doc,
'example_ans': answer_text,
'player_name': player_name,
'result': test_output,
'passed': test_prog.success,
'judge_detail': [num_error_test, num_failed_test, num_total_test],
'history': history
}
@app.route('/judge/', method='GET')
@jinja2_template('judge.html')
def judge_status():
def parse_time_stamp(r):
r['timestamp'] = (now - datetime.strptime(
r['timestamp'],
"%Y-%m-%d %H:%M:%S"
)).total_seconds()
return r['timestamp']
latest_game = get_games(limit=1)[0]
results = get_results(latest_game['id'])
now = datetime.utcnow()
submit_by_names = OrderedDict([
(k, sorted(v, key=parse_time_stamp))
for k, v in groupby(results, lambda r: r['name'])
])
# find latest submit time
latest_success_submit = OrderedDict.fromkeys(submit_by_names.keys())
for name, submit_history_time_asc in submit_by_names.items():
last_success_time = None
for submit in submit_history_time_asc:
if submit['judge'] == 'True':
last_success_time = submit['timestamp']
latest_success_submit[name] = last_success_time
# game info
q_pth = parse_question_folder()[latest_game['name']]
q_name, q_desc, q_doc, q_ex_ans = judge.read_question(q_pth)
return {
'q_name': q_name,
'q_desc': q_desc,
'q_doc': '\n'.join(q_doc.splitlines()[2:13] + ['... (stripped)']),
'results': submit_by_names,
'latest_success': latest_success_submit
}
@app.route('/gameadmin/', method='GET')
@jinja2_template('admin.html')
def admin(msg=None):
all_questions = parse_question_folder()
return {
'msg': msg,
'questions': all_questions.keys(),
}
@app.route('/gameadmin/', method='POST')
@jinja2_template('admin.html')
def doAdmin():
operation = request.forms.get('operation', 'Random').lower()
print('%s' % request.forms.get('operation', ''))
msg = []
if operation == 'create':
# create new game
qname = request.forms.get('gamename', '')
if qname and qname in parse_question_folder().keys():
if not insert_games([{'name': qname, 'question': 'empty'}]):
msg.append(
'Error: Cannot create game: <code>%s</code>' %
qname)
else:
msg.append('Success: Create game: <code>%s</code>' % qname)
else:
# random generate game
keys = list(parse_question_folder().keys())
qname = random.choice(keys)
if not insert_games([{'name': qname, 'question': 'empty'}]):
msg.append('Error: Cannot create game <code>%s</code>' % qname)
else:
msg.append('Success: Create game <code>%s</code>' % qname)
return admin(msg=';'.join(msg))
@app.route('/test/', method='GET')
@jinja2_template('admin.html')
def testdb():
reload_db()
msg = []
if not insert_games([{'name': 'foo', 'question': 'NO USE'}]):
msg.append('insert db failed')
games = get_games()
if not games:
msg.append('there is no game in db')
ret_val = insert_result(
name='test_foo', submit='test_bar',
codingtime='57', judge='True', judge_detail='', gameid='0'
)
if not ret_val:
msg.append('Add result failed')
record = get_results()
if not record:
msg.append('Get result failed')
if not msg:
msg.append('All Test Success!')
return {'msg': '; '.join(msg)}
@app.route('/static/<path:path>')
def get_static_file(path):
return static_file(path, './static')
if __name__ == '__main__':
run(app, host='localhost', port=8080, debug=True, reloader=True)