-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb_model.py
430 lines (297 loc) · 13.6 KB
/
db_model.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
from datetime import datetime, timedelta
from typing import Dict
from flask_bcrypt import Bcrypt
from flask_login import UserMixin
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import relationship, backref
from abstract import Abstract
db = SQLAlchemy()
bcrypt = Bcrypt()
class Settings(db.Model):
__tablename__ = "settings"
key = db.Column(db.String(256), primary_key=True)
value = db.Column(db.Text)
def __init__(self, key):
self.key = key
class Puzzlehunt(db.Model):
__tablename__ = "puzzlehunts"
id_puzzlehunt = db.Column(db.Integer, primary_key=True)
puzzlehunt = db.Column(db.String(256))
def __init__(self, puzzlehunt):
self.puzzlehunt = puzzlehunt
def get_settings(self) -> Dict[str, "PuzzlehuntSettings"]:
return Puzzlehunt.get_settings_for_id(self.id_puzzlehunt)
@staticmethod
def get_settings_for_id(id_puzzlehunt) -> Dict[str, "PuzzlehuntSettings"]:
return {ps.key: ps for ps in
PuzzlehuntSettings.query.filter_by(id_puzzlehunt=id_puzzlehunt)}
@staticmethod
def get_current_id():
current_puzzlehunt = Settings.query.get("current_puzzlehunt")
if current_puzzlehunt is None:
raise ValueError("Current puzzlehunt is not set in the settings. The database was probably initialized incorrectly.")
return int(current_puzzlehunt.value)
@staticmethod
def get_current() -> "Puzzlehunt":
return Puzzlehunt.query.get(Puzzlehunt.get_current_id())
class PuzzlehuntSettings(db.Model):
__tablename__ = "puzzlehunt_settings"
id_puzzlehunt = db.Column(db.Integer, db.ForeignKey(Puzzlehunt.id_puzzlehunt, ondelete='CASCADE'), primary_key=True)
key = db.Column(db.String(256), primary_key=True)
value = db.Column(db.Text)
def __init__(self, id_puzzlehunt, key):
self.id_puzzlehunt = id_puzzlehunt
self.key = key
class User(UserMixin, Abstract):
__required_attributes__ = ["name"]
@property
def is_admin(self):
return False
@property
def id(self):
return
class Admin(User):
@property
def is_admin(self):
return True
@property
def id(self):
return -1
name = "admin"
class Team(db.Model, User):
__tablename__ = "teams"
id_team = db.Column(db.Integer, primary_key=True)
id_puzzlehunt = db.Column(db.Integer, db.ForeignKey(Puzzlehunt.id_puzzlehunt, ondelete='RESTRICT'))
name = db.Column(db.String(256), nullable=False)
password = db.Column(db.String(256), nullable=False)
phone = db.Column(db.String(256), nullable=True)
note = db.Column(db.Text, nullable=True)
@property
def id(self):
return self.id_team
def __init__(self, current_puzzlehunt, name, password_plain, phone, note):
self.id_puzzlehunt = current_puzzlehunt
self.name = name
self.set_password(password_plain)
self.phone = phone
self.note = note
def set_password(self, password_plain):
self.password = bcrypt.generate_password_hash(password_plain)
class Puzzle(db.Model):
__tablename__ = "puzzles"
id_puzzle = db.Column(db.Integer, primary_key=True)
id_puzzlehunt = db.Column(db.Integer, db.ForeignKey(Puzzlehunt.id_puzzlehunt, ondelete='RESTRICT'))
puzzle = db.Column(db.String(256))
assignment = db.Column(db.Text)
order = db.Column(db.Integer)
def __init__(self, current_puzzlehunt, puzzle, assignment, order):
self.id_puzzlehunt = current_puzzlehunt
self.puzzle = puzzle
self.assignment = assignment
self.order = order
def get_prerequisites(self):
p = Puzzle.query.join(PuzzlePrerequisite, Puzzle.id_puzzle == PuzzlePrerequisite.id_previous_puzzle)\
.filter_by(id_new_puzzle=self.id_puzzle).all()
return p
def _get_used_hints(self, id_team):
return TeamUsedHint.query\
.filter_by(id_team=id_team)\
.join(Hint)\
.filter(Hint.id_puzzle == self.id_puzzle)
def get_used_hints(self, id_team):
return self._get_used_hints(id_team).with_entities(Hint).all()
def get_available_hints(self, id_team):
return Hint.query \
.filter(Hint.id_puzzle == self.id_puzzle) \
.filter(Hint.id_hint.not_in(
self._get_used_hints(id_team)
.with_entities(Hint.id_hint)))\
.all()
class PuzzlePrerequisite(db.Model):
__tablename__ = "puzzle_prerequisites"
id_previous_puzzle = db.Column(db.Integer, db.ForeignKey(Puzzle.id_puzzle, ondelete='RESTRICT'), primary_key=True)
id_new_puzzle = db.Column(db.Integer, db.ForeignKey(Puzzle.id_puzzle, ondelete='RESTRICT'), primary_key=True)
def __init__(self, id_previous_puzzle, id_new_puzzle):
self.id_previous_puzzle = id_previous_puzzle
self.id_new_puzzle = id_new_puzzle
class Code(db.Model):
__tablename__ = "codes"
id_code = db.Column(db.Integer, primary_key=True)
id_puzzlehunt = db.Column(db.Integer, db.ForeignKey(Puzzlehunt.id_puzzlehunt, ondelete='RESTRICT'))
code = db.Column(db.String(256))
message = db.Column(db.Text)
def __init__(self, current_puzzlehunt, code, message):
self.id_puzzlehunt = current_puzzlehunt
self.code = code
self.message = message
class ArrivalCode(db.Model):
__tablename__ = "arrival_codes"
id_arrival_code = db.Column(db.Integer, primary_key=True)
id_puzzle = db.Column(db.Integer, db.ForeignKey(Puzzle.id_puzzle, ondelete='RESTRICT'))
code = db.Column(db.String(256))
message = db.Column(db.Text)
puzzle = relationship("Puzzle", backref=backref("arrival_codes", uselist=False))
def __init__(self, puzzle, code, message):
self.id_puzzle = puzzle
self.code = code
self.message = message
class SolutionCode(db.Model):
__tablename__ = "solution_codes"
id_solution_code = db.Column(db.Integer, primary_key=True)
id_puzzle = db.Column(db.Integer, db.ForeignKey(Puzzle.id_puzzle, ondelete='RESTRICT'))
code = db.Column(db.String(256))
message = db.Column(db.Text)
puzzle = relationship("Puzzle", backref=backref("solution_codes", uselist=False))
def __init__(self, puzzle, code, message):
self.id_puzzle = puzzle
self.code = code
self.message = message
class HistoryEntry(Abstract):
__required_attributes__ = ["icon_html", "history_entry_html", "edit_url", "timestamp"]
timestamp: datetime
@property
def formatted_timestamp(self):
return self.timestamp.strftime('%d.%m.%Y %H:%M:%S')
class TeamArrived(db.Model, HistoryEntry):
__tablename__ = "team_arrivals"
id_team = db.Column(db.Integer, db.ForeignKey(Team.id_team, ondelete='CASCADE'), primary_key=True)
id_puzzle = db.Column(db.Integer, db.ForeignKey(Puzzle.id_puzzle, ondelete='RESTRICT'), primary_key=True)
id_arrival_code = db.Column(db.Integer, db.ForeignKey(ArrivalCode.id_arrival_code, ondelete='RESTRICT'))
timestamp = db.Column(db.DateTime)
puzzle = relationship("Puzzle", backref=backref("team_arrivals", uselist=False))
arrival_code = relationship("ArrivalCode", backref=backref("team_arrivals", uselist=False))
team = relationship("Team", backref=backref("team_arrivals", cascade="all, delete-orphan"))
def __init__(self, id_team, id_puzzle, id_arrival_code):
self.id_team = id_team
self.id_puzzle = id_puzzle
self.id_arrival_code = id_arrival_code
self.timestamp = datetime.now()
@property
def icon_html(self):
return '<i class="bi bi-file-earmark-richtext-fill text-info"></i>'
@property
def history_entry_html(self):
return f'Příchod: {self.puzzle.puzzle}'
@property
def edit_url(self):
return f'/history/{self.id_team}/arrival/{self.id_puzzle}'
class TeamSolved(db.Model, HistoryEntry):
__tablename__ = "team_solves"
id_team = db.Column(db.Integer, db.ForeignKey(Team.id_team, ondelete='CASCADE'), primary_key=True)
id_puzzle = db.Column(db.Integer, db.ForeignKey(Puzzle.id_puzzle, ondelete='RESTRICT'), primary_key=True)
id_solution_code = db.Column(db.Integer, db.ForeignKey(SolutionCode.id_solution_code, ondelete='RESTRICT'))
timestamp = db.Column(db.DateTime)
puzzle = relationship("Puzzle", backref=backref("team_solves", uselist=False))
solution_code = relationship("SolutionCode", backref=backref("team_solves", uselist=False))
team = relationship("Team", backref=backref("team_solves", cascade="all, delete-orphan"))
def __init__(self, id_team, id_puzzle, id_solution_code):
self.id_team = id_team
self.id_puzzle = id_puzzle
self.id_solution_code = id_solution_code
self.timestamp = datetime.now()
@property
def icon_html(self):
return '<i class="bi bi-check-circle-fill text-success"></i>'
@property
def history_entry_html(self):
return f'Vyřešeno: {self.puzzle.puzzle}'
@property
def edit_url(self):
return f'/history/{self.id_team}/solve/{self.id_puzzle}'
class TeamSubmittedCode(db.Model, HistoryEntry):
__tablename__ = "team_submitted_codes"
id_team = db.Column(db.Integer, db.ForeignKey(Team.id_team, ondelete='CASCADE'), primary_key=True)
id_code = db.Column(db.Integer, db.ForeignKey(Code.id_code, ondelete='RESTRICT'), primary_key=True)
timestamp = db.Column(db.DateTime)
code = relationship("Code", backref=backref("team_submitted_codes", uselist=False))
team = relationship("Team", backref=backref("team_submitted_codes", cascade="all, delete-orphan"))
def __init__(self, id_team, id_code):
self.id_team = id_team
self.id_code = id_code
self.timestamp = datetime.now()
@property
def icon_html(self):
return '<i class="bi bi-file-earmark-code-fill text-primary"></i>'
@property
def history_entry_html(self):
return f'Zadán kód: "{self.code.code}"'
@property
def edit_url(self):
return f'/history/{self.id_team}/code/{self.id_code}'
class WrongCode(db.Model, HistoryEntry):
__tablename__ = "wrong_codes"
id_wrong_code = db.Column(db.Integer, primary_key=True)
id_team = db.Column(db.Integer, db.ForeignKey(Team.id_team, ondelete='CASCADE'))
code = db.Column(db.String(256))
timestamp = db.Column(db.DateTime)
team = relationship("Team", backref=backref("wrong_codes", cascade="all, delete-orphan"))
def __init__(self, id_team, code):
self.id_team = id_team
self.code = code
self.timestamp = datetime.now()
@property
def icon_html(self):
return '<i class="bi bi-x-octagon-fill text-danger"></i>'
@property
def history_entry_html(self):
return f'Špatný kód: "{self.code}"'
@property
def edit_url(self):
return f'/history/{self.id_team}/wrong/{self.id_wrong_code}'
class Hint(db.Model):
__tablename__ = "hints"
id_hint = db.Column(db.Integer, primary_key=True)
id_puzzle = db.Column(db.Integer, db.ForeignKey(Puzzle.id_puzzle, ondelete='RESTRICT'))
order = db.Column(db.Integer)
minutes_to_open = db.Column(db.Integer)
hint = db.Column(db.Text)
puzzle = relationship("Puzzle", backref=backref("hints", uselist=False))
def __init__(self, id_puzzle, order, minutes_to_open, hint):
self.id_puzzle = id_puzzle
self.order = order
self.minutes_to_open = minutes_to_open
self.hint = hint
def is_open(self, arrival_time: datetime, id_team):
if self._hints_are_ordered() and not self._all_previous_hints_used(id_team):
return False
return self._hint_time_passed(arrival_time)
@staticmethod
def _hints_are_ordered():
puzzlehunt_settings = Puzzlehunt.get_current().get_settings()
return "hints_are_ordered" in puzzlehunt_settings and puzzlehunt_settings["hints_are_ordered"].value == "True"
def _all_previous_hints_used(self, id_team):
previous_hints_count = TeamUsedHint.query\
.filter_by(id_team=id_team)\
.join(Hint)\
.filter(Hint.id_puzzle == self.id_puzzle)\
.count()
return previous_hints_count + 1 >= self.order
def _hint_time_passed(self, arrival_time: datetime):
return datetime.now() > arrival_time + timedelta(minutes=self.minutes_to_open)
def _time_to_hint(self, arrival_time: datetime) -> timedelta:
time_since_arrival = datetime.now() - arrival_time
return timedelta(minutes=self.minutes_to_open) - time_since_arrival
def seconds_to_hint(self, arrival_time: datetime) -> int:
return self._time_to_hint(arrival_time).seconds
def requires_previous(self, id_team):
return self._hints_are_ordered() and not self._all_previous_hints_used(id_team)
class TeamUsedHint(db.Model, HistoryEntry):
__tablename__ = "team_used_hints"
id_team = db.Column(db.Integer, db.ForeignKey(Team.id_team, ondelete='CASCADE'), primary_key=True)
id_hint = db.Column(db.Integer, db.ForeignKey(Hint.id_hint, ondelete='RESTRICT'), primary_key=True)
timestamp = db.Column(db.DateTime)
team = relationship("Team", backref=backref("team_used_hints", cascade="all, delete-orphan"))
hint = relationship("Hint", backref=backref("team_used_hints", uselist=False))
def __init__(self, id_team, id_hint):
self.id_team = id_team
self.id_hint = id_hint
self.timestamp = datetime.now()
@property
def icon_html(self):
return '<i class="bi bi-lightbulb-fill text-warning"></i>'
@property
def history_entry_html(self):
return f'Zobrazení {self.hint.order}. nápovědy u šifry "{self.hint.puzzle.puzzle}"'
@property
def edit_url(self):
return f'/history/{self.id_team}/hint/{self.id_hint}'