-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.py
687 lines (576 loc) · 20.1 KB
/
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
"""Defines the database models for this module."""
import functools
import datetime
from typing import Optional, Tuple, Callable, Sequence
import os
import sqlalchemy
import sqlalchemy.orm
import sqlalchemy.ext.declarative # for typing
from sqlalchemy import func, asc, desc
import logging
import modelutils
import constants as const
from orm import (
Logfile,
Server,
Player,
Species,
Background,
God,
Version,
Branch,
Place,
Game,
Milestone,
Account,
Ktyp,
Verb,
Skill,
CsdcContestant,
get_session,
)
class DBError(BaseException):
"""Generic wrapper for sqlalchemy errors passed out of this module."""
pass
class DBIntegrityError(BaseException):
"""Generic wrapper for sqlalchemy sqlalchemy.exc.IntegrityError errors."""
pass
def _reraise_dberror(function: Callable) -> Callable:
"""Re-raise errors from decorated function as DBError or DBIntegrityError.
Doesn't re-wrap DBError/DBIntegrityError exceptions.
"""
def f(*args, **kwargs): # type: ignore
"""Wrap Sqlalchemy errors."""
try:
return function(*args, **kwargs)
except BaseException as e:
if isinstance(e, KeyboardInterrupt):
raise
elif isinstance(e, DBError) or isinstance(e, DBIntegrityError):
raise
elif isinstance(e, sqlalchemy.exc.IntegrityError):
raise DBIntegrityError from e
else:
raise DBError from e
return f
@functools.lru_cache(maxsize=16)
def get_server(s: sqlalchemy.orm.session.Session, name: str) -> Server:
"""Get a server, creating it if needed."""
server = s.query(Server).filter(Server.name == name).first()
if server:
return server
else:
server = Server(name=name)
s.add(server)
s.commit()
return server
@functools.lru_cache(maxsize=128)
def get_account_id(s: sqlalchemy.orm.session.Session, name: str, server: Server) -> int:
"""Get an account id, creating the account if needed.
Note that player names are not case sensitive, so names are stored with
their canonical capitalisation but we always compare the lowercase version.
"""
player_id = get_player_id(s, name)
acc = (
s.query(Account.id)
.filter(func.lower(Account.name) == name.lower(), Account.server == server)
.one_or_none()
)
if acc:
return acc[0]
else:
acc = Account(name=name, server=server, player_id=player_id)
s.add(acc)
s.commit()
return acc.id
@functools.lru_cache(maxsize=128)
def get_player(s: sqlalchemy.orm.session.Session, name: str) -> Player:
"""Get a player's object, creating them if needed.
Note that player names are not case sensitive, so names are stored with
their canonical capitalisation but we always compare the lowercase version.
"""
player = (
s.query(Player).filter(func.lower(Player.name) == name.lower()).one_or_none()
)
if player:
return player
else:
return _add_player(s, name)
@functools.lru_cache(maxsize=128)
def get_player_id(s: sqlalchemy.orm.session.Session, name: str, create=True) -> Player:
"""Get a player's id, creating them if needed.
Note that player names are not case sensitive, so names are stored with
their canonical capitalisation but we always compare the lowercase version.
"""
player = (
s.query(Player.id).filter(func.lower(Player.name) == name.lower()).one_or_none()
)
if player:
return player[0]
elif create:
return _add_player(s, name).id
else:
return None
def _add_player(s, name: str) -> Player:
player = Player(name=name)
s.add(player)
s.commit()
return player
def setup_species(s: sqlalchemy.orm.session.Session) -> None:
"""Load species data into the database."""
new = []
for sp in const.SPECIES:
if not s.query(Species).filter(Species.short == sp.short).first():
logging.info("Adding species '%s'" % sp.full)
new.append({"short": sp.short, "name": sp.full})
s.bulk_insert_mappings(Species, new)
s.commit()
def setup_backgrounds(s: sqlalchemy.orm.session.Session) -> None:
"""Load background data into the database."""
new = []
for bg in const.BACKGROUNDS:
if not s.query(Background).filter(Background.short == bg.short).first():
logging.info("Adding background '%s'" % bg.full)
new.append({"short": bg.short, "name": bg.full})
s.bulk_insert_mappings(Background, new)
s.commit()
def setup_gods(s: sqlalchemy.orm.session.Session) -> None:
"""Load god data into the database."""
new = []
for god in const.GODS:
if not s.query(God).filter(God.name == god.name).first():
logging.info("Adding god '%s'" % god.name)
new.append({"name": god.name})
s.bulk_insert_mappings(God, new)
s.commit()
def setup_ktyps(s: sqlalchemy.orm.session.Session) -> None:
"""Load ktyp data into the database."""
new = []
for ktyp in const.KTYPS:
if not s.query(Ktyp).filter(Ktyp.name == ktyp).first():
logging.info("Adding ktyp '%s'" % ktyp)
new.append({"name": ktyp})
s.bulk_insert_mappings(Ktyp, new)
s.commit()
def setup_verbs(s: sqlalchemy.orm.session.Session) -> None:
"""Load verb data into the database."""
new = []
for verb in const.VERBS:
if not s.query(Verb).filter(Verb.name == verb).first():
logging.info("Adding verb '%s'" % verb)
new.append({"name": verb})
s.bulk_insert_mappings(Verb, new)
s.commit()
def setup_skills(s: sqlalchemy.orm.session.Session) -> None:
"""Load skill data into the database."""
new = []
for sk in const.SKILLS:
if not s.query(Skill).filter(Skill.name == sk).first():
logging.info("Adding skill '%s'" % sk)
new.append({"name": sk})
s.bulk_insert_mappings(Skill, new)
s.commit()
@functools.lru_cache(maxsize=32)
def get_version(s: sqlalchemy.orm.session.Session, v: str) -> Version:
"""Get a version, creating it if needed."""
version = s.query(Version).filter(Version.v == v).first()
if version:
return version
else:
logging.info("Adding version '%s'" % v)
version = Version(v=v)
s.add(version)
s.commit()
return version
def setup_branches(s: sqlalchemy.orm.session.Session) -> None:
"""Load branch data into the database."""
new = []
for br in const.BRANCHES:
if not s.query(Branch).filter(Branch.short == br.short).first():
logging.info("Adding branch '%s'" % br.full)
new.append(
{
"short": br.short,
"name": br.full,
"multilevel": br.multilevel,
}
)
s.bulk_insert_mappings(Branch, new)
s.commit()
@functools.lru_cache(maxsize=256)
def get_place(s: sqlalchemy.orm.session.Session, branch: Branch, lvl: int) -> Place:
"""Get a place, creating it if needed."""
place = s.query(Place).filter(Place.branch == branch, Place.level == lvl).first()
if place:
return place
else:
place = Place(branch=branch, level=lvl)
s.add(place)
s.commit()
return place
@functools.lru_cache(maxsize=256)
def get_place_from_string(s: sqlalchemy.orm.session.Session, spot: str) -> Place:
"""Get a place in crawl Place:Lev string format"""
code = spot.split(":") + [1]; # default to lvl 1 if missing
return get_place(s, get_branch(s, code[0]), int(code[1]));
@functools.lru_cache(maxsize=64)
def get_species(s: sqlalchemy.orm.session.Session, sp: str) -> Species:
"""Get a species by short code, creating it if needed."""
species = s.query(Species).filter(Species.short == sp).first()
if species:
return species
else:
species = Species(short=sp, name=sp)
s.add(species)
s.commit()
logging.warning(
"Found new species %s, please add me to constants.py"
" and update the database." % sp
)
return species
@functools.lru_cache(maxsize=64)
def get_background(s: sqlalchemy.orm.session.Session, bg: str) -> Background:
"""Get a background by short code, creating it if needed."""
background = s.query(Background).filter(Background.short == bg).first()
if background:
return background
else:
background = Background(short=bg, name=bg)
s.add(background)
s.commit()
logging.warning(
"Found new background %s, please add me to constants.py"
" and update the database." % bg
)
return background
@functools.lru_cache(maxsize=32)
def get_god(s: sqlalchemy.orm.session.Session, name: str) -> God:
"""Get a god by name, creating it if needed."""
god = s.query(God).filter(God.name == name).first()
if god:
return god
else:
god = God(name=name)
s.add(god)
s.commit()
logging.warning(
"Found new god %s, please add me to constants.py"
" and update the database." % name
)
return god
@functools.lru_cache(maxsize=64)
def get_ktyp(s: sqlalchemy.orm.session.Session, name: str) -> Ktyp:
"""Get a ktyp by name, creating it if needed."""
ktyp = s.query(Ktyp).filter(Ktyp.name == name).first()
if ktyp:
return ktyp
else:
ktyp = Ktyp(name=name)
s.add(ktyp)
s.commit()
logging.warning("Found new ktyp %s, please add me to constants.py" % name)
return ktyp
@functools.lru_cache(maxsize=64)
def get_verb(s: sqlalchemy.orm.session.Session, name: str) -> Ktyp:
"""Get a verb/type by name, creating it if needed."""
verb = s.query(Verb).filter(Verb.name == name).first()
if verb:
return verb
else:
verb = Verb(name=name)
s.add(verb)
s.commit()
logging.warning("Found new verb %s, please add me to constants.py" % name)
return verb
@functools.lru_cache(maxsize=64)
def get_branch(s: sqlalchemy.orm.session.Session, br: str) -> Branch:
"""Get a branch by short name, creating it if needed."""
branch = s.query(Branch).filter(Branch.short == br).first()
if branch:
return branch
else:
branch = Branch(short=br, name=br, multilevel=True)
s.add(branch)
s.commit()
logging.warning(
"Found new branch %s, please add me to constants.py"
" and update the database." % br
)
return branch
@functools.lru_cache(maxsize=64)
def get_skill(s: sqlalchemy.orm.session.Session, name: str) -> Ktyp:
"""Get a verb/type by name, creating it if needed."""
skill = s.query(Skill).filter(Skill.name == name).first()
if skill:
return skill
else:
skill = Skill(name=name)
s.add(skill)
s.commit()
logging.warning("Found new skill %s, please add me to constants.py" % name)
return skill
@_reraise_dberror
def add_games(s: sqlalchemy.orm.session.Session, games: Sequence[dict]) -> None:
"""Normalise and add multiple games to the database."""
s.bulk_insert_mappings(Game, games)
@_reraise_dberror
def add_event(s: sqlalchemy.orm.session.Session, data: dict) -> None:
"""Normalise and add a milestone event.
XXX: DOES NOT COMMIT YOU MUST COMMIT (For speedy reasons)"""
data["gid"] = "%s:%s:%s" % (data["name"], data["src_abbr"], data["start"])
if data["type"] == "begin":
_new_game(s, data)
elif data["type"] == "death.final":
_end_game(s, data)
branch = get_branch(s, data["br"])
m = {
"gid" : data["gid"],
"xl" : data["xl"],
"place_id" : get_place(s, branch, data["lvl"]).id,
"oplace_id" : get_place_from_string(s, data["oplace"]).id,
"god_id" : get_god(s, data["god"]).id,
"turn" : data["turn"],
"dur" : data["dur"],
"runes" : data["runes"],
"time" : modelutils.crawl_date_to_datetime(data["time"]),
"potionsused": data["potionsused"],
"scrollsused": data["scrollsused"],
"skill_id" : get_skill(s, data["sk"]).id,
"sklev" : data["sklev"],
"verb_id" : get_verb(s, data["type"]).id,
"msg" : data["milestone"],
"status" : data["status"],
}
s.add(Milestone(**m))
@_reraise_dberror
def _new_game(s: sqlalchemy.orm.session.Session, data:dict) -> None:
"""Create a game row on game begin."""
branch = get_branch(s, data["br"])
server = get_server(s, data["src_abbr"])
g = {
"gid": data["gid"],
"account_id": get_account_id(s, data["name"], server),
"player_id": get_player_id(s, data["name"]),
"species_id": get_species(s, data["char"][:2]).id,
"background_id": get_background(s, data["char"][2:]).id,
"version_id": get_version(s, data["v"]).id,
"start": modelutils.crawl_date_to_datetime(data["start"])
}
s.add(Game(**g))
@_reraise_dberror
def _end_game(s: sqlalchemy.orm.session.Session, data:dict) -> None:
g = _games(s, gid=data["gid"]).first()
g.end = modelutils.crawl_date_to_datetime(data["end"])
g.ktyp = get_ktyp(s, data["ktyp"])
g.score = data["sc"]
g.dam = data.get("dam", 0)
g.tdam = data.get("tdam", g.dam)
g.sdam = data.get("sdam", g.dam)
def get_logfile_progress(
s: sqlalchemy.orm.session.Session, url: str
) -> Logfile:
"""Get a logfile progress records, creating it if needed."""
log = (
s.query(Logfile).filter(Logfile.source_url == url).one_or_none()
)
if log:
return log
else:
log = Logfile(source_url=url)
s.add(log)
s.commit()
return log
def save_logfile_progress(
s: sqlalchemy.orm.session.Session, source_url: str, current_key: int
) -> None:
"""Save the position for a logfile."""
log = get_logfile_progress(s, source_url)
log.current_key = current_key
s.add(log)
def list_accounts(
s: sqlalchemy.orm.session.Session, *, blacklisted: Optional[bool] = None
) -> Sequence[Account]:
"""Get a list of all accounts.
If blacklisted is specified, only return accounts with that blacklisted
value.
"""
q = s.query(Account)
if blacklisted is not None:
q = q.filter(
Account.blacklisted
== (sqlalchemy.true() if blacklisted else sqlalchemy.false())
)
results = q.all()
return results
def list_players(s: sqlalchemy.orm.session.Session) -> Sequence[Player]:
"""Get a list of all players."""
q = s.query(Player)
return q.all()
@_reraise_dberror
def add_contestant(s: sqlalchemy.orm.session.Session, name: str) -> None:
"""Add a Csdc Contestant"""
pid = get_player_id(s, name, False)
if pid is None:
return;
c = s.query(CsdcContestant).filter(CsdcContestant.player_id == pid).one_or_none()
if c:
return;
else:
s.add(CsdcContestant(player_id = get_player_id(s, name),division = 1))
s.commit()
def _generic_char_type_lister(
s: sqlalchemy.orm.session.Session,
*,
cls: sqlalchemy.ext.declarative.api.DeclarativeMeta,
) -> Sequence:
q = s.query(cls)
return q.order_by(getattr(cls, "name")).all()
def list_species(
s: sqlalchemy.orm.session.Session) -> Sequence[Species]:
"""Return a list of species.
"""
return _generic_char_type_lister(s, cls=Species)
def list_backgrounds(
s: sqlalchemy.orm.session.Session) -> Sequence[Background]:
"""Return a list of backgrounds.
"""
return _generic_char_type_lister(s, cls=Background)
def list_gods(
s: sqlalchemy.orm.session.Session) -> Sequence[God]:
"""Return a list of gods.
"""
return _generic_char_type_lister(s, cls=God)
def _games(
s: sqlalchemy.orm.session.Session,
*,
player: Optional[Player] = None,
account: Optional[Account] = None,
scored: Optional[bool] = None,
limit: Optional[int] = None,
gid: Optional[str] = None,
winning: Optional[bool] = None,
boring: Optional[bool] = None,
reverse_order: Optional[bool] = False
) -> sqlalchemy.orm.query.Query:
"""Build a query to match games with certain conditions.
Parameters:
player: If specified, only games with a matching player
account: If specified, only games with a matching account
scored: If specified, only games with a matching scored
limit: If specified, up to limit games
gid: If specified, only game with matching gid
winning: If specified, only games where ktyp==/!='winning'
boring: If specifies, only games where ktyp not boring
reverse_order: Return games least->most recent
Returns:
query object you can call.
"""
q = s.query(Game)
if player is not None:
q = q.filter(Game.player_id == player.id)
if account is not None:
q = q.join(Game.account).filter(Account.id == account.id)
if scored is not None:
q = q.filter(
Game.scored == (sqlalchemy.true() if scored else sqlalchemy.false())
)
if gid is not None:
q = q.filter(Game.gid == gid)
if winning is not None:
ktyp = get_ktyp(s, "winning")
if winning:
q = q.filter(Game.ktyp_id == ktyp.id)
else:
q = q.filter(Game.ktyp_id != ktyp.id)
if boring is not None:
boring_ktyps = [
get_ktyp(s, ktyp).id for ktyp in ("quitting", "leaving", "wizmode")
]
if boring:
q = q.filter(Game.ktyp_id.in_(boring_ktyps))
else:
q = q.filter(Game.ktyp_id.notin_(boring_ktyps))
if reverse_order is not None:
q = q.order_by(Game.end.desc() if not reverse_order else Game.end.asc())
if limit is not None:
q = q.limit(limit)
return q
def list_games(
s: sqlalchemy.orm.session.Session,
*,
player: Optional[Player] = None,
account: Optional[Account] = None,
scored: Optional[bool] = None,
limit: Optional[int] = None,
gid: Optional[str] = None,
winning: Optional[bool] = None,
boring: Optional[bool] = None,
reverse_order: bool = False
) -> Sequence[Game]:
"""Get a list of all games that match specified conditions.
See _games documentation for parameters.
Return:
list of Games.
"""
return _games(
s,
player=player,
account=account,
scored=scored,
limit=limit,
gid=gid,
winning=winning,
boring=boring,
reverse_order=reverse_order,
).all()
def count_games(
s: sqlalchemy.orm.session.Session,
*,
player: Optional[Player] = None,
account: Optional[Account] = None,
scored: Optional[bool] = None,
gid: Optional[str] = None,
winning: Optional[bool] = None,
boring: Optional[bool] = None
) -> int:
"""Get a count of all games that match specified conditions.
See _games documentation for parameters.
Return:
count of matching Games.
"""
return _games(
s,
player=player,
account=account,
scored=scored,
gid=gid,
winning=winning,
boring=boring,
).count()
def setup_database():
with get_session() as sess:
if os.environ.get('SCOREBOARD_SKIP_DB_SETUP') == None:
setup_species(sess)
setup_backgrounds(sess)
setup_gods(sess)
setup_branches(sess)
setup_ktyps(sess)
setup_verbs(sess)
setup_skills(sess)
def get_game(s: sqlalchemy.orm.session.Session, **kwargs: dict) -> Game:
"""Get a single game. See get_games docstring/type signature."""
kwargs.setdefault("limit", 1) # type: ignore
result = list_games(s, **kwargs) # type: ignore
if not result:
return None
else:
return result[0]
milestones_alias = sqlalchemy.orm.aliased(Milestone)
latestmilestone_gid = sqlalchemy.orm.query.Query(milestones_alias.id).filter(
milestones_alias.gid == Milestone.gid
).order_by(
desc(milestones_alias.time)
).limit(1)
latestmilestones = sqlalchemy.orm.query.Query(Milestone).filter(
Milestone.id.in_(latestmilestone_gid)
).cte(name="latestmilestones")