-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
250 lines (214 loc) · 7.91 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
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
import os
import time
import json
import logging
import jinja2
import webapp2
from google.appengine.api import taskqueue
from google.appengine.ext import ndb
JINJA_ENV = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class Game(ndb.Model):
round = ndb.IntegerProperty(default=0)
cleared = ndb.BooleanProperty(default=False)
headlines = ndb.StringProperty(repeated=True)
likes = ndb.JsonProperty(default={})
def nextRound(self):
self.round += 1
self.cleared = False
for k in self.likes:
if self.likes[k] > 0:
self.likes[k] *= -1
def clearRound(self):
self.cleared = True
self.headlines = []
self.likes = {}
def getInfo(self):
return {
'headlines': self.headlines,
'likes': self.likes
}
def reset(self):
self.round = 0
self.cleared = False
self.headlines = []
self.likes = {}
# for i in range(1,9):
# User(name='axon'+i, team=1, headlines={
# 1: ['', ''],
# 2: ['1gogan_truth1', '1gogan_lie1'],
# 3: ['', ''],
# 4: ['', '']}).put()
# for i in range(9,17):
# User(name='axon'+i, team=1, headlines={
# 1: ['', ''],
# 2: ['1gogan_truth2', '1gogan_lie2'],
# 3: ['', ''],
# 4: ['', '']}).put()
# for i in range(17,25):
# User(name='axon'+i, team=1, headlines={
# 1: ['', ''],
# 2: ['', ''],
# 3: ['2gogan_truth1', '2gogan_lie1'],
# 4: ['', '']}).put()
# for i in range(25,33):
# User(name='axon'+i, team=1, headlines={
# 1: ['', ''],
# 2: ['', ''],
# 3: ['2gogan_truth2', '2gogan_lie2'],
# 4: ['', '']}).put()
# for i in range(33,41):
# User(name='axon'+i, team=1, headlines={
# 1: ['', ''],
# 2: ['', ''],
# 3: ['', ''],
# 4: ['3gogan_truth1', '3gogan_lie1']}).put()
# for i in range(41,49):
# User(name='axon'+i, team=1, headlines={
# 1: ['', ''],
# 2: ['', ''],
# 3: ['', ''],
# 4: ['3gogan_truth2', '3gogan_lie2']}).put()
# for i in range(49,57):
# User(name='axon'+i, team=1, headlines={
# 1: ['', ''],
# 2: ['1roosa_truth1', '1roosa_lie1'],
# 3: ['', ''],
# 4: ['', '']}).put()
# for i in range(57,65):
# User(name='axon'+i, team=1, headlines={
# 1: ['', ''],
# 2: ['1roosa_truth2', '1roosa_lie2'],
# 3: ['', ''],
# 4: ['', '']}).put()
# for i in range(65,73):
# User(name='axon'+i, team=1, headlines={
# 1: ['', ''],
# 2: ['', ''],
# 3: ['2roosa_truth1', '2roosa_lie1'],
# 4: ['', '']}).put()
# for i in range(73,81):
# User(name='axon'+i, team=1, headlines={
# 1: ['', ''],
# 2: ['', ''],
# 3: ['2roosa_truth2', '2roosa_lie2'],
# 4: ['', '']}).put()
# for i in range(81,89):
# User(name='axon'+i, team=1, headlines={
# 1: ['', ''],
# 2: ['', ''],
# 3: ['', ''],
# 4: ['3roosa_truth1', '3roosa_lie1']}).put()
# for i in range(89,97):
# User(name='axon'+i, team=1, headlines={
# 1: ['', ''],
# 2: ['', ''],
# 3: ['', ''],
# 4: ['3roosa_truth2', '3roosa_lie2']}).put()
class User(ndb.Model):
name = ndb.StringProperty()
team = ndb.IntegerProperty()
headlines = ndb.JsonProperty()
class LoginHandler(webapp2.RequestHandler):
def get(self):
login_template = JINJA_ENV.get_template('index.html')
self.response.out.write(login_template.render())
class UserHandler(webapp2.RequestHandler):
def get(self):
username = self.request.get('user')
user = User.query(User.name == username).get()
private_headlines = user.headlines[str(1)]
user_template = JINJA_ENV.get_template('game.html')
self.response.out.write(user_template.render({
'username': username,
'private_headlines': private_headlines,
}))
class AdminHandler(webapp2.RequestHandler):
def get(self):
admin_template = JINJA_ENV.get_template('admin.html')
self.response.out.write(admin_template.render())
class ReadStateHandler(webapp2.RequestHandler):
def post(self):
username = self.request.body
user = User.query(User.name == username).get()
game = Game.query().get()
print game.likes
state = {
'cleared': game.cleared,
'round': str(game.round+1),
'private_headlines': user.headlines[str(game.round+1)],
'public_headlines': game.headlines
}
self.response.write(json.dumps(state))
class UnityReadHandler(webapp2.RequestHandler):
def get(self):
game = Game.query().get()
state = {
'public_headlines': game.headlines,
'likes': game.likes
}
self.response.write(json.dumps(state))
class ResetHandler(webapp2.RequestHandler):
def post(self):
q = taskqueue.Queue('headlines')
q.add(taskqueue.Task(payload='reset', method='PULL'))
class IncrementHeadlineHandler(webapp2.RequestHandler):
def post(self):
headline = self.request.body
q = taskqueue.Queue('headlines')
q.add(taskqueue.Task(payload=headline, method='PULL'))
class IncrementRoundHandler(webapp2.RequestHandler):
def post(self):
q = taskqueue.Queue('headlines')
q.add(taskqueue.Task(payload='increment-round', method='PULL'))
class ClearRoundHandler(webapp2.RequestHandler):
def post(self):
q = taskqueue.Queue('headlines')
q.add(taskqueue.Task(payload='clear-round', method='PULL'))
class HeadlinesWorker(webapp2.RequestHandler):
def get(self):
q = taskqueue.Queue('headlines')
while True:
try:
tasks = q.lease_tasks(3600, 100)
except:
time.sleep(1)
continue
if tasks:
#@ndb.transactional
def update_counts():
game = Game.query().get()
for t in tasks:
headline = t.payload
if headline == 'increment-round':
game.nextRound()
elif headline == 'clear-round':
game.clearRound()
elif headline == 'reset':
game.reset()
elif headline in game.headlines:
game.likes[headline] += 1
else:
game.likes[headline] = 1
game.headlines.append(headline)
game.put()
try:
update_counts()
except Exception as e:
logging.exception(e)
else:
q.delete_tasks(tasks)
time.sleep(1)
app = webapp2.WSGIApplication(
[
('/', LoginHandler),
('/user', UserHandler),
('/admin', AdminHandler),
('/read-state', ReadStateHandler),
('/unity-read', UnityReadHandler),
('/reset', ResetHandler),
('/increment-headline', IncrementHeadlineHandler),
('/increment-round', IncrementRoundHandler),
('/clear-page', ClearRoundHandler),
('/_ah/start', HeadlinesWorker),
], debug=True)