-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjerocat.py
301 lines (263 loc) · 9.79 KB
/
jerocat.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
from datetime import date
from time import sleep, time
from base import Session, engine, Base, metadata
from sqlalchemy import create_engine, select, MetaData, Table, and_
import configparser
from answer import Answer
from game import Game
from play import Play
from point import Point
from question import Question
from user import User
config = configparser.ConfigParser()
config.read('config.ini')
Base.metadata.create_all(engine)
session = Session()
class Jerocat:
# Game status
STATUS_NOTHING = 0
STATUS_PUBLIC = 1
STATUS_PRIVATE = 2
STATUS_PENDING = 3
STATUS_VALIDATING = 4
STATUS_VALIDATED = 5
STATUS_EDITING_QUESTIONS = 6
STATUS_EDITING_ANSWERS = 7
STATUS_EDITING_QUESTION_TEXT = 8
STATUS_EDITING_ANSWER_TEXT = 9
STATUS_GAMES_MENU = 10
STATUS_GAME_EDIT = 11
STATUS_GAME_PLAY = 12
def __init__(self):
pass
def answer_check(self, game, position, answer):
"""
Checks if an answer is correct
"""
q = session.query(Question).filter(
Question.game == game, Question.position == position).first()
if q == None:
return None
ans = session.query(Answer).filter(
Answer.question == q, Answer.text.ilike(answer), Answer.accepted == True).first()
return ans
def user_add(self, id=None, username=None, first_name=None, last_name=None, user=None):
'''
Insert a user
'''
if user != None:
if id == None:
id = int(user.id)
if username == None:
username = user.username
if first_name == None:
first_name = user.first_name
if last_name == None:
last_name = user.last_name
u = User(id=id, username=username,
first_name=first_name, last_name=last_name)
session.add(u)
session.commit()
return u
def user_upsert(self, user):
u = session.query(User).filter(
User.id == user.id).first()
if (u == None):
u = self.user_add(user=user)
else:
u.username = user.username
u.first_name = user.first_name
u.last_name = user.last_name
session.commit()
return u
def user_get(self, id=False, username=False):
user = None
if (id != False):
user = session.query(User).filter(User.id == id).first()
elif (username != False):
user = session.query(User).filter(
User.username == username).first()
return user
def game_add(self, name, user=None, status=0):
'''
Insert a empty game and returns its id
'''
g = Game(name=name, user=user)
session.add(g)
session.commit()
return g
def game_get(self, id=None, user=None, name=None):
'''
Insert a empty game and returns its id
'''
g = None
if (id != None):
g = session.query(Game).get(id)
elif (name != None):
g = session.query(Game).filter(Game.name == name).first()
return g
def game_delete(self, id=None):
'''
Delete a game if it has no questions
'''
g = session.query(Game).get(id)
if (len(g.questions)) > 0: # Game is not empty
return False
session.query(Game).filter(Game.id == int(id)).delete()
session.commit()
return g
def play_init(self, chat_id):
p = Play(chat_id=chat_id)
session.add(p)
session.commit()
return p
def play_get(self, chat_id):
'''
Returns the play
'''
# s = session.query(Play).get(chat_id=chat_id)
p = None
try:
p = session.query(Play).filter(Play.chat_id == chat_id).first()
finally:
if (p == None):
p = Play(chat_id=chat_id)
session.add(p)
session.commit()
return p
def play_set_game(self, play, game):
play.game = game
play.unset('pag')
session.commit()
def game_list_full(self, uid=0):
"""
Returns a list of games. If a user is send, it also list user games
TODO filter by user
"""
games = session.query(Game).all()
return games
def game_list(self, uid=0):
"""
Returns a list of games. If a user is send, it also list user games
TODO filter by user
"""
list = {}
for g in session.query(Game).all():
list[g.id] = g.name
return list
def game_allowed(self, uid=0):
"""
Check if a game is valid
"""
return True
def question_add(self, game, question):
'''
Insert a question to the game
'''
q = Question(game=game, text=question)
session.add(q)
session.commit()
position = session.query(Question).filter(
Question.game == q.game).filter(Question.id <= q.id).count()
setattr(q, 'position', position)
session.commit()
return q
def question_get(self, game=None, position=None, text=None, id=None):
if id != None:
return session.query(Question).filter(Question.id == int(id)).first()
elif position != None:
return session.query(Question).filter(Question.game == game, Question.position == position).first()
elif text != None:
return session.query(Question).filter(Question.game == game, Question.text == text).first()
def question_delete(self, game=None, position=None, text=None, id=None):
if id != None:
session.query(Point).filter(Point.question_id == int(id)).delete()
session.query(Answer).filter(
Answer.question_id == int(id)).delete()
session.query(Question).filter(Question.id == int(id)).delete()
session.commit()
# q=session.query(Question).filter(Question.id == int(id)).first()
# session.delete(q)
elif position != None:
pass
elif text != None:
pass
self.questions_reposition()
def question_update(self, id=None, text=None):
q = self.question_get(id=id)
q.text = text
session.commit()
def answer_add(self, question, answer, accepted=True):
a = Answer(question=question, text=answer, accepted=accepted)
session.add(a)
session.commit()
return a
def answer_get(self, question=None, text=None, id=None, only_accepted=True):
if only_accepted == True:
if text != None:
return session.query(Answer).filter(Answer.question == question, Answer.text == text, Answer.accepted == True).first()
elif id != None:
return session.query(Answer).filter(Answer.id == id, Answer.accepted == True).first()
else:
if text != None:
return session.query(Answer).filter(Answer.question == question, Answer.text == text).first()
elif id != None:
return session.query(Answer).filter(Answer.id == id).first()
def answer_delete(self, id=None):
if id != None:
session.query(Point).filter(Point.answer_id == int(id)).delete()
session.query(Answer).filter(Answer.id == int(id)).delete()
session.commit()
def numerize():
"""
Set the questions numbers
"""
pass
def points_get(self, play, question, only_accepted=True):
if only_accepted == True:
return session.query(Point).filter(Point.answer.has(Answer.accepted == True), Point.question_id == question.id, Point.play_id == play.id).order_by(Point.created_on).first()
else:
return session.query(Point).filter(Point.question_id == question.id, Point.play_id == play.id).order_by(Point.created_on).first()
def question_from_position(self, game, position):
return session.query(Question).filter(Question.game_id == game.id, Question.position == position).first()
def point_add(self, play, user, question, answer):
user = self.user_upsert(user)
p = Point(play, user, question, answer)
session.commit()
def play_set_attribute(self, chat_id, attribute, value):
p = session.query(Play).filter(Play.chat_id == chat_id).first()
p.attributes[attribute] = value
session.add(p)
session.commit()
return p
def save(self):
session.commit()
# get answers from game_id and question_position
def answers_list(self, game_id, question_position):
question = session.query(Question).filter(
Question.game_id == game_id, Question.position == question_position).first()
list = {}
for a in session.query(Answer).filter(Play.question_id == question.position).all():
list[a.id] = a.text
return list
def play_get_ranking(self, play):
'''
Retuns a dictionari with player names/users and correct (first) answers
'''
checked = {}
result = {}
for p in session.query(Point).join(Game).join(User, Point.user).filter(Point.answer.has(Answer.accepted == True), Point.play == play, Point.game == play.game).order_by(Point.created_on):
if checked.get(p.question_id) is None:
result[p.user_id] = result.get(p.user_id, 0)+1
checked[p.question_id] = True
return result
def questions_reposition(self):
game_id=-1
position=1
for question in session.query(Question).order_by(Question.game_id, Question.id):
if (game_id!=question.game_id):
game_id = question.game_id
position=1
question.position=position
session.commit()
position+=1