-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
117 lines (94 loc) · 3.39 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
# -*- coding: utf-8 -*-
import tornado.ioloop
import tornado.web
import jinja2
import uuid
from images import get_rand_names_except_author, get_pic
from random import shuffle
import json
import pickle
questions = dict()
top_file = "static/top.dat"
def read_top():
try:
return pickle.load(open(top_file, 'rb'))
except:
pickle.dump([('Енот уже не тот', 0)], open(top_file, 'wb'))
return pickle.load(open(top_file, 'rb'))
def change_top(needed_login, needed_score):
top = read_top()
flag = False
for n in range(len(top)):
login, score = top[n]
if needed_login == login:
flag = True
if score < needed_score:
top[n] = login, needed_score
else:
break
if not flag:
top.append((needed_login, needed_score))
top = sorted(top, key=lambda x: -x[1])
pickle.dump(top, open(top_file, 'wb'))
def make_new_question(old_count):
question_id = str(uuid.uuid4())
image = get_pic()
print(image)
first, second = get_rand_names_except_author(image['author']);
authors = [
(image['author'], question_id),
(first, question_id),
(second, question_id)
]
shuffle(authors)
for num, (author, question_id) in enumerate(authors):
authors[num] = (num, author, question_id)
questions[question_id] = {'name': image['author'], 'count': old_count}
return question_id, image, authors
class CheckAnswer(tornado.web.RequestHandler):
def post(self):
if not self.get_secure_cookie("login"):
self.render("pages/login.html")
else:
name, question_id = self.get_argument("value").split(';')
print('real', questions[question_id]['name'])
print('get', name)
if questions[question_id]['name'] == name:
questions[question_id]['count'] += 1
question_id, image, pic_list = make_new_question(questions[question_id]['count'])
self.write(json.dumps({
"verdict": "OK", "link": image['link'],"pic_name": image['title'],
"authors": pic_list,
'count': questions[question_id]['count'],
'answer': name
})
)
else:
change_top(self.get_secure_cookie("login"), int(questions[question_id]['count']))
self.write(json.dumps({"verdict": "ERR", 'count': questions[question_id]['count'], 'answer': questions[question_id]['name']}))
del questions[question_id]
class MainPage(tornado.web.RequestHandler):
def get(self):
login = self.get_argument('login', None)
if not self.get_secure_cookie("login"):
if not login:
self.render("pages/login.html")
else:
self.set_secure_cookie('login', login)
self.redirect('/')
else:
question_id, image, authors = make_new_question(0)
self.render(
'pages/main.html',
link=image['link'],
pic_name=image['title'],
authors=authors,
top=read_top()
)
routes = [
(r'/', MainPage),
(r'/answer', CheckAnswer),
]
app = tornado.web.Application(routes, cookie_secret="42")
app.listen(80)
tornado.ioloop.IOLoop.current().start()