-
Notifications
You must be signed in to change notification settings - Fork 203
/
user_db_conn.py
884 lines (772 loc) · 32.4 KB
/
user_db_conn.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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
import sqlite3
from enum import IntEnum
from collections import namedtuple
from discord.ext import commands
from tle.util import codeforces_api as cf
from tle.util import codeforces_common as cf_common
_DEFAULT_VC_RATING = 1500
class Gitgud(IntEnum):
GOTGUD = 0
GITGUD = 1
NOGUD = 2
FORCED_NOGUD = 3
class Duel(IntEnum):
PENDING = 0
DECLINED = 1
WITHDRAWN = 2
EXPIRED = 3
ONGOING = 4
COMPLETE = 5
INVALID = 6
class Winner(IntEnum):
DRAW = 0
CHALLENGER = 1
CHALLENGEE = 2
class DuelType(IntEnum):
UNOFFICIAL = 0
OFFICIAL = 1
class RatedVC(IntEnum):
ONGOING = 0
FINISHED = 1
class UserDbError(commands.CommandError):
pass
class DatabaseDisabledError(UserDbError):
pass
class DummyUserDbConn:
def __getattribute__(self, item):
raise DatabaseDisabledError
class UniqueConstraintFailed(UserDbError):
pass
def namedtuple_factory(cursor, row):
"""Returns sqlite rows as named tuples."""
fields = [col[0] for col in cursor.description if col[0].isidentifier()]
Row = namedtuple("Row", fields)
return Row(*row)
class UserDbConn:
def __init__(self, dbfile):
self.conn = sqlite3.connect(dbfile)
self.conn.row_factory = namedtuple_factory
self.create_tables()
def create_tables(self):
self.conn.execute(
'CREATE TABLE IF NOT EXISTS user_handle ('
'user_id TEXT,'
'guild_id TEXT,'
'handle TEXT,'
'active INTEGER,'
'PRIMARY KEY (user_id, guild_id)'
')'
)
self.conn.execute('CREATE UNIQUE INDEX IF NOT EXISTS ix_user_handle_guild_handle '
'ON user_handle (guild_id, handle)')
self.conn.execute(
'CREATE TABLE IF NOT EXISTS cf_user_cache ('
'handle TEXT PRIMARY KEY,'
'first_name TEXT,'
'last_name TEXT,'
'country TEXT,'
'city TEXT,'
'organization TEXT,'
'contribution INTEGER,'
'rating INTEGER,'
'maxRating INTEGER,'
'last_online_time INTEGER,'
'registration_time INTEGER,'
'friend_of_count INTEGER,'
'title_photo TEXT'
')'
)
# TODO: Make duel tables guild-aware.
self.conn.execute('''
CREATE TABLE IF NOT EXISTS duelist(
"user_id" INTEGER PRIMARY KEY NOT NULL,
"rating" INTEGER NOT NULL
)
''')
self.conn.execute('''
CREATE TABLE IF NOT EXISTS duel(
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"challenger" INTEGER NOT NULL,
"challengee" INTEGER NOT NULL,
"issue_time" REAL NOT NULL,
"start_time" REAL,
"finish_time" REAL,
"problem_name" TEXT,
"contest_id" INTEGER,
"p_index" INTEGER,
"status" INTEGER,
"winner" INTEGER,
"type" INTEGER
)
''')
self.conn.execute('''
CREATE TABLE IF NOT EXISTS "challenge" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"user_id" TEXT NOT NULL,
"issue_time" REAL NOT NULL,
"finish_time" REAL,
"problem_name" TEXT NOT NULL,
"contest_id" INTEGER NOT NULL,
"p_index" INTEGER NOT NULL,
"rating_delta" INTEGER NOT NULL,
"status" INTEGER NOT NULL
)
''')
self.conn.execute('''
CREATE TABLE IF NOT EXISTS "user_challenge" (
"user_id" TEXT,
"active_challenge_id" INTEGER,
"issue_time" REAL,
"score" INTEGER NOT NULL,
"num_completed" INTEGER NOT NULL,
"num_skipped" INTEGER NOT NULL,
PRIMARY KEY("user_id")
)
''')
self.conn.execute('''
CREATE TABLE IF NOT EXISTS reminder (
guild_id TEXT PRIMARY KEY,
channel_id TEXT,
role_id TEXT,
before TEXT
)
''')
self.conn.execute(
'CREATE TABLE IF NOT EXISTS starboard ('
'guild_id TEXT PRIMARY KEY,'
'channel_id TEXT'
')'
)
self.conn.execute(
'CREATE TABLE IF NOT EXISTS starboard_message ('
'original_msg_id TEXT PRIMARY KEY,'
'starboard_msg_id TEXT,'
'guild_id TEXT'
')'
)
self.conn.execute(
'CREATE TABLE IF NOT EXISTS rankup ('
'guild_id TEXT PRIMARY KEY,'
'channel_id TEXT'
')'
)
self.conn.execute(
'CREATE TABLE IF NOT EXISTS auto_role_update ('
'guild_id TEXT PRIMARY KEY'
')'
)
# Rated VCs stuff:
self.conn.execute('''
CREATE TABLE IF NOT EXISTS "rated_vcs" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"contest_id" INTEGER NOT NULL,
"start_time" REAL,
"finish_time" REAL,
"status" INTEGER,
"guild_id" TEXT
)
''')
# TODO: Do we need to explicitly specify the fk constraint or just depend on the middleware?
self.conn.execute('''
CREATE TABLE IF NOT EXISTS "rated_vc_users" (
"vc_id" INTEGER,
"user_id" TEXT NOT NULL,
"rating" INTEGER,
CONSTRAINT fk_vc
FOREIGN KEY (vc_id)
REFERENCES rated_vcs(id),
PRIMARY KEY(vc_id, user_id)
)
''')
self.conn.execute('''
CREATE TABLE IF NOT EXISTS rated_vc_settings (
guild_id TEXT PRIMARY KEY,
channel_id TEXT
)
''')
# Helper functions.
def _insert_one(self, table: str, columns, values: tuple):
n = len(values)
query = '''
INSERT OR REPLACE INTO {} ({}) VALUES ({})
'''.format(table, ', '.join(columns), ', '.join(['?'] * n))
rc = self.conn.execute(query, values).rowcount
self.conn.commit()
return rc
def _insert_many(self, table: str, columns, values: list):
n = len(columns)
query = '''
INSERT OR REPLACE INTO {} ({}) VALUES ({})
'''.format(table, ', '.join(columns), ', '.join(['?'] * n))
rc = self.conn.executemany(query, values).rowcount
self.conn.commit()
return rc
def _fetchone(self, query: str, params=None, row_factory=None):
self.conn.row_factory = row_factory
res = self.conn.execute(query, params).fetchone()
self.conn.row_factory = None
return res
def _fetchall(self, query: str, params=None, row_factory=None):
self.conn.row_factory = row_factory
res = self.conn.execute(query, params).fetchall()
self.conn.row_factory = None
return res
def new_challenge(self, user_id, issue_time, prob, delta):
query1 = '''
INSERT INTO challenge
(user_id, issue_time, problem_name, contest_id, p_index, rating_delta, status)
VALUES
(?, ?, ?, ?, ?, ?, 1)
'''
query2 = '''
INSERT OR IGNORE INTO user_challenge (user_id, score, num_completed, num_skipped)
VALUES (?, 0, 0, 0)
'''
query3 = '''
UPDATE user_challenge SET active_challenge_id = ?, issue_time = ?
WHERE user_id = ? AND active_challenge_id IS NULL
'''
cur = self.conn.cursor()
cur.execute(query1, (user_id, issue_time, prob.name, prob.contestId, prob.index, delta))
last_id, rc = cur.lastrowid, cur.rowcount
if rc != 1:
self.conn.rollback()
return 0
cur.execute(query2, (user_id,))
cur.execute(query3, (last_id, issue_time, user_id))
if cur.rowcount != 1:
self.conn.rollback()
return 0
self.conn.commit()
return 1
def check_challenge(self, user_id):
query1 = '''
SELECT active_challenge_id, issue_time FROM user_challenge
WHERE user_id = ?
'''
res = self.conn.execute(query1, (user_id,)).fetchone()
if res is None: return None
c_id, issue_time = res
query2 = '''
SELECT problem_name, contest_id, p_index, rating_delta FROM challenge
WHERE id = ?
'''
res = self.conn.execute(query2, (c_id,)).fetchone()
if res is None: return None
return c_id, issue_time, res[0], res[1], res[2], res[3]
def get_gudgitters(self):
query = '''
SELECT user_id, score FROM user_challenge
'''
return self.conn.execute(query).fetchall()
def howgud(self, user_id):
query = '''
SELECT rating_delta FROM challenge WHERE user_id = ? AND finish_time IS NOT NULL
'''
return self.conn.execute(query, (user_id,)).fetchall()
def get_noguds(self, user_id):
query = ('SELECT problem_name '
'FROM challenge '
f'WHERE user_id = ? AND status = {Gitgud.NOGUD}')
return {name for name, in self.conn.execute(query, (user_id,)).fetchall()}
def gitlog(self, user_id):
query = f'''
SELECT issue_time, finish_time, problem_name, contest_id, p_index, rating_delta, status
FROM challenge WHERE user_id = ? AND status != {Gitgud.FORCED_NOGUD} ORDER BY issue_time DESC
'''
return self.conn.execute(query, (user_id,)).fetchall()
def complete_challenge(self, user_id, challenge_id, finish_time, delta):
query1 = f'''
UPDATE challenge SET finish_time = ?, status = {Gitgud.GOTGUD}
WHERE id = ? AND status = {Gitgud.GITGUD}
'''
query2 = '''
UPDATE user_challenge SET score = score + ?, num_completed = num_completed + 1,
active_challenge_id = NULL, issue_time = NULL
WHERE user_id = ? AND active_challenge_id = ?
'''
rc = self.conn.execute(query1, (finish_time, challenge_id)).rowcount
if rc != 1:
self.conn.rollback()
return 0
rc = self.conn.execute(query2, (delta, user_id, challenge_id)).rowcount
if rc != 1:
self.conn.rollback()
return 0
self.conn.commit()
return 1
def skip_challenge(self, user_id, challenge_id, status):
query1 = '''
UPDATE user_challenge SET active_challenge_id = NULL, issue_time = NULL
WHERE user_id = ? AND active_challenge_id = ?
'''
query2 = f'''
UPDATE challenge SET status = ? WHERE id = ? AND status = {Gitgud.GITGUD}
'''
rc = self.conn.execute(query1, (user_id, challenge_id)).rowcount
if rc != 1:
self.conn.rollback()
return 0
rc = self.conn.execute(query2, (status, challenge_id)).rowcount
if rc != 1:
self.conn.rollback()
return 0
self.conn.commit()
return 1
def cache_cf_user(self, user):
query = ('INSERT OR REPLACE INTO cf_user_cache '
'(handle, first_name, last_name, country, city, organization, contribution, '
' rating, maxRating, last_online_time, registration_time, friend_of_count, title_photo) '
'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)')
with self.conn:
return self.conn.execute(query, user).rowcount
def fetch_cf_user(self, handle):
query = ('SELECT handle, first_name, last_name, country, city, organization, contribution, '
' rating, maxRating, last_online_time, registration_time, friend_of_count, title_photo '
'FROM cf_user_cache '
'WHERE UPPER(handle) = UPPER(?)')
user = self.conn.execute(query, (handle,)).fetchone()
return cf_common.fix_urls(cf.User._make(user)) if user else None
def set_handle(self, user_id, guild_id, handle):
query = ('SELECT user_id '
'FROM user_handle '
'WHERE guild_id = ? AND handle = ?')
existing = self.conn.execute(query, (guild_id, handle)).fetchone()
if existing and int(existing[0]) != user_id:
raise UniqueConstraintFailed
query = ('INSERT OR REPLACE INTO user_handle '
'(user_id, guild_id, handle, active) '
'VALUES (?, ?, ?, 1)')
with self.conn:
return self.conn.execute(query, (user_id, guild_id, handle)).rowcount
def set_inactive(self, guild_id_user_id_pairs):
query = ('UPDATE user_handle '
'SET active = 0 '
'WHERE guild_id = ? AND user_id = ?')
with self.conn:
return self.conn.executemany(query, guild_id_user_id_pairs).rowcount
def get_handle(self, user_id, guild_id):
query = ('SELECT handle '
'FROM user_handle '
'WHERE user_id = ? AND guild_id = ?')
res = self.conn.execute(query, (user_id, guild_id)).fetchone()
return res[0] if res else None
def get_user_id(self, handle, guild_id):
query = ('SELECT user_id '
'FROM user_handle '
'WHERE UPPER(handle) = UPPER(?) AND guild_id = ?')
res = self.conn.execute(query, (handle, guild_id)).fetchone()
return int(res[0]) if res else None
def remove_handle(self, handle, guild_id):
query = ('DELETE FROM user_handle '
'WHERE UPPER(handle) = UPPER(?) AND guild_id = ?')
with self.conn:
return self.conn.execute(query, (handle, guild_id)).rowcount
def get_handles_for_guild(self, guild_id):
query = ('SELECT user_id, handle '
'FROM user_handle '
'WHERE guild_id = ? AND active = 1')
res = self.conn.execute(query, (guild_id,)).fetchall()
return [(int(user_id), handle) for user_id, handle in res]
def get_cf_users_for_guild(self, guild_id):
query = ('SELECT u.user_id, c.handle, c.first_name, c.last_name, c.country, c.city, '
' c.organization, c.contribution, c.rating, c.maxRating, c.last_online_time, '
' c.registration_time, c.friend_of_count, c.title_photo '
'FROM user_handle AS u '
'LEFT JOIN cf_user_cache AS c '
'ON u.handle = c.handle '
'WHERE u.guild_id = ? AND u.active = 1')
res = self.conn.execute(query, (guild_id,)).fetchall()
return [(int(t[0]), cf.User._make(t[1:])) for t in res]
def get_reminder_settings(self, guild_id):
query = '''
SELECT channel_id, role_id, before
FROM reminder
WHERE guild_id = ?
'''
return self.conn.execute(query, (guild_id,)).fetchone()
def set_reminder_settings(self, guild_id, channel_id, role_id, before):
query = '''
INSERT OR REPLACE INTO reminder (guild_id, channel_id, role_id, before)
VALUES (?, ?, ?, ?)
'''
self.conn.execute(query, (guild_id, channel_id, role_id, before))
self.conn.commit()
def clear_reminder_settings(self, guild_id):
query = '''DELETE FROM reminder WHERE guild_id = ?'''
self.conn.execute(query, (guild_id,))
self.conn.commit()
def get_starboard(self, guild_id):
query = ('SELECT channel_id '
'FROM starboard '
'WHERE guild_id = ?')
return self.conn.execute(query, (guild_id,)).fetchone()
def set_starboard(self, guild_id, channel_id):
query = ('INSERT OR REPLACE INTO starboard '
'(guild_id, channel_id) '
'VALUES (?, ?)')
self.conn.execute(query, (guild_id, channel_id))
self.conn.commit()
def clear_starboard(self, guild_id):
query = ('DELETE FROM starboard '
'WHERE guild_id = ?')
self.conn.execute(query, (guild_id,))
self.conn.commit()
def add_starboard_message(self, original_msg_id, starboard_msg_id, guild_id):
query = ('INSERT INTO starboard_message '
'(original_msg_id, starboard_msg_id, guild_id) '
'VALUES (?, ?, ?)')
self.conn.execute(query, (original_msg_id, starboard_msg_id, guild_id))
self.conn.commit()
def check_exists_starboard_message(self, original_msg_id):
query = ('SELECT 1 '
'FROM starboard_message '
'WHERE original_msg_id = ?')
res = self.conn.execute(query, (original_msg_id,)).fetchone()
return res is not None
def remove_starboard_message(self, *, original_msg_id=None, starboard_msg_id=None):
assert (original_msg_id is None) ^ (starboard_msg_id is None)
if original_msg_id is not None:
query = ('DELETE FROM starboard_message '
'WHERE original_msg_id = ?')
rc = self.conn.execute(query, (original_msg_id,)).rowcount
else:
query = ('DELETE FROM starboard_message '
'WHERE starboard_msg_id = ?')
rc = self.conn.execute(query, (starboard_msg_id,)).rowcount
self.conn.commit()
return rc
def clear_starboard_messages_for_guild(self, guild_id):
query = ('DELETE FROM starboard_message '
'WHERE guild_id = ?')
rc = self.conn.execute(query, (guild_id,)).rowcount
self.conn.commit()
return rc
def check_duel_challenge(self, userid):
query = f'''
SELECT id FROM duel
WHERE (challengee = ? OR challenger = ?) AND (status == {Duel.ONGOING} OR status == {Duel.PENDING})
'''
return self.conn.execute(query, (userid, userid)).fetchone()
def check_duel_accept(self, challengee):
query = f'''
SELECT id, challenger, problem_name FROM duel
WHERE challengee = ? AND status == {Duel.PENDING}
'''
return self.conn.execute(query, (challengee,)).fetchone()
def check_duel_decline(self, challengee):
query = f'''
SELECT id, challenger FROM duel
WHERE challengee = ? AND status == {Duel.PENDING}
'''
return self.conn.execute(query, (challengee,)).fetchone()
def check_duel_withdraw(self, challenger):
query = f'''
SELECT id, challengee FROM duel
WHERE challenger = ? AND status == {Duel.PENDING}
'''
return self.conn.execute(query, (challenger,)).fetchone()
def check_duel_draw(self, userid):
query = f'''
SELECT id, challenger, challengee, start_time, type FROM duel
WHERE (challenger = ? OR challengee = ?) AND status == {Duel.ONGOING}
'''
return self.conn.execute(query, (userid, userid)).fetchone()
def check_duel_complete(self, userid):
query = f'''
SELECT id, challenger, challengee, start_time, problem_name, contest_id, p_index, type FROM duel
WHERE (challenger = ? OR challengee = ?) AND status == {Duel.ONGOING}
'''
return self.conn.execute(query, (userid, userid)).fetchone()
def create_duel(self, challenger, challengee, issue_time, prob, dtype):
query = f'''
INSERT INTO duel (challenger, challengee, issue_time, problem_name, contest_id, p_index, status, type) VALUES (?, ?, ?, ?, ?, ?, {Duel.PENDING}, ?)
'''
duelid = self.conn.execute(query, (challenger, challengee, issue_time, prob.name, prob.contestId, prob.index, dtype)).lastrowid
self.conn.commit()
return duelid
def cancel_duel(self, duelid, status):
query = f'''
UPDATE duel SET status = ? WHERE id = ? AND status = {Duel.PENDING}
'''
rc = self.conn.execute(query, (status, duelid)).rowcount
if rc != 1:
self.conn.rollback()
return 0
self.conn.commit()
return rc
def invalidate_duel(self, duelid):
query = f'''
UPDATE duel SET status = {Duel.INVALID} WHERE id = ? AND status = {Duel.ONGOING}
'''
rc = self.conn.execute(query, (duelid,)).rowcount
if rc != 1:
self.conn.rollback()
return 0
self.conn.commit()
return rc
def start_duel(self, duelid, start_time):
query = f'''
UPDATE duel SET start_time = ?, status = {Duel.ONGOING}
WHERE id = ? AND status = {Duel.PENDING}
'''
rc = self.conn.execute(query, (start_time, duelid)).rowcount
if rc != 1:
self.conn.rollback()
return 0
self.conn.commit()
return rc
def complete_duel(self, duelid, winner, finish_time, winner_id = -1, loser_id = -1, delta = 0, dtype = DuelType.OFFICIAL):
query = f'''
UPDATE duel SET status = {Duel.COMPLETE}, finish_time = ?, winner = ? WHERE id = ? AND status = {Duel.ONGOING}
'''
rc = self.conn.execute(query, (finish_time, winner, duelid)).rowcount
if rc != 1:
self.conn.rollback()
return 0
if dtype == DuelType.OFFICIAL:
self.update_duel_rating(winner_id, +delta)
self.update_duel_rating(loser_id, -delta)
self.conn.commit()
return 1
def update_duel_rating(self, userid, delta):
query = '''
UPDATE duelist SET rating = rating + ? WHERE user_id = ?
'''
rc = self.conn.execute(query, (delta, userid)).rowcount
self.conn.commit()
return rc
def get_duel_wins(self, userid):
query = f'''
SELECT start_time, finish_time, problem_name, challenger, challengee FROM duel
WHERE ((challenger = ? AND winner == {Winner.CHALLENGER}) OR (challengee = ? AND winner == {Winner.CHALLENGEE})) AND status = {Duel.COMPLETE}
'''
return self.conn.execute(query, (userid, userid)).fetchall()
def get_duels(self, userid):
query = f'''
SELECT id, start_time, finish_time, problem_name, challenger, challengee, winner FROM duel WHERE (challengee = ? OR challenger = ?) AND status == {Duel.COMPLETE} ORDER BY start_time DESC
'''
return self.conn.execute(query, (userid, userid)).fetchall()
def get_duel_problem_names(self, userid):
query = f'''
SELECT problem_name FROM duel WHERE (challengee = ? OR challenger = ?) AND (status == {Duel.COMPLETE} OR status == {Duel.INVALID})
'''
return self.conn.execute(query, (userid, userid)).fetchall()
def get_pair_duels(self, userid1, userid2):
query = f'''
SELECT id, start_time, finish_time, problem_name, challenger, challengee, winner FROM duel
WHERE ((challenger = ? AND challengee = ?) OR (challenger = ? AND challengee = ?)) AND status == {Duel.COMPLETE} ORDER BY start_time DESC
'''
return self.conn.execute(query, (userid1, userid2, userid2, userid1)).fetchall()
def get_recent_duels(self):
query = f'''
SELECT id, start_time, finish_time, problem_name, challenger, challengee, winner FROM duel WHERE status == {Duel.COMPLETE} ORDER BY start_time DESC LIMIT 7
'''
return self.conn.execute(query).fetchall()
def get_ongoing_duels(self):
query = f'''
SELECT start_time, problem_name, challenger, challengee FROM duel
WHERE status == {Duel.ONGOING} ORDER BY start_time DESC
'''
return self.conn.execute(query).fetchall()
def get_num_duel_completed(self, userid):
query = f'''
SELECT COUNT(*) FROM duel WHERE (challengee = ? OR challenger = ?) AND status == {Duel.COMPLETE}
'''
return self.conn.execute(query, (userid, userid)).fetchone()[0]
def get_num_duel_draws(self, userid):
query = f'''
SELECT COUNT(*) FROM duel WHERE (challengee = ? OR challenger = ?) AND winner == {Winner.DRAW}
'''
return self.conn.execute(query, (userid, userid)).fetchone()[0]
def get_num_duel_losses(self, userid):
query = f'''
SELECT COUNT(*) FROM duel
WHERE ((challengee = ? AND winner == {Winner.CHALLENGER}) OR (challenger = ? AND winner == {Winner.CHALLENGEE})) AND status = {Duel.COMPLETE}
'''
return self.conn.execute(query, (userid, userid)).fetchone()[0]
def get_num_duel_declined(self, userid):
query = f'''
SELECT COUNT(*) FROM duel WHERE challengee = ? AND status == {Duel.DECLINED}
'''
return self.conn.execute(query, (userid,)).fetchone()[0]
def get_num_duel_rdeclined(self, userid):
query = f'''
SELECT COUNT(*) FROM duel WHERE challenger = ? AND status == {Duel.DECLINED}
'''
return self.conn.execute(query, (userid,)).fetchone()[0]
def get_duel_rating(self, userid):
query = '''
SELECT rating FROM duelist WHERE user_id = ?
'''
return self.conn.execute(query, (userid,)).fetchone()[0]
def is_duelist(self, userid):
query = '''
SELECT 1 FROM duelist WHERE user_id = ?
'''
return self.conn.execute(query, (userid,)).fetchone()
def register_duelist(self, userid):
query = '''
INSERT OR IGNORE INTO duelist (user_id, rating)
VALUES (?, 1500)
'''
with self.conn:
return self.conn.execute(query, (userid,)).rowcount
def get_duelists(self):
query = '''
SELECT user_id, rating FROM duelist ORDER BY rating DESC
'''
return self.conn.execute(query).fetchall()
def get_complete_official_duels(self):
query = f'''
SELECT challenger, challengee, winner, finish_time FROM duel WHERE status={Duel.COMPLETE}
AND type={DuelType.OFFICIAL} ORDER BY finish_time ASC
'''
return self.conn.execute(query).fetchall()
def get_rankup_channel(self, guild_id):
query = ('SELECT channel_id '
'FROM rankup '
'WHERE guild_id = ?')
channel_id = self.conn.execute(query, (guild_id,)).fetchone()
return int(channel_id[0]) if channel_id else None
def set_rankup_channel(self, guild_id, channel_id):
query = ('INSERT OR REPLACE INTO rankup '
'(guild_id, channel_id) '
'VALUES (?, ?)')
with self.conn:
self.conn.execute(query, (guild_id, channel_id))
def clear_rankup_channel(self, guild_id):
query = ('DELETE FROM rankup '
'WHERE guild_id = ?')
with self.conn:
return self.conn.execute(query, (guild_id,)).rowcount
def enable_auto_role_update(self, guild_id):
query = ('INSERT OR REPLACE INTO auto_role_update '
'(guild_id) '
'VALUES (?)')
with self.conn:
return self.conn.execute(query, (guild_id,)).rowcount
def disable_auto_role_update(self, guild_id):
query = ('DELETE FROM auto_role_update '
'WHERE guild_id = ?')
with self.conn:
return self.conn.execute(query, (guild_id,)).rowcount
def has_auto_role_update_enabled(self, guild_id):
query = ('SELECT 1 '
'FROM auto_role_update '
'WHERE guild_id = ?')
return self.conn.execute(query, (guild_id,)).fetchone() is not None
def reset_status(self, id):
inactive_query = '''
UPDATE user_handle
SET active = 0
WHERE guild_id = ?
'''
self.conn.execute(inactive_query, (id,))
self.conn.commit()
def update_status(self, guild_id: str, active_ids: list):
placeholders = ', '.join(['?'] * len(active_ids))
if not active_ids: return 0
active_query = '''
UPDATE user_handle
SET active = 1
WHERE user_id IN ({})
AND guild_id = ?
'''.format(placeholders)
rc = self.conn.execute(active_query, (*active_ids, guild_id)).rowcount
self.conn.commit()
return rc
# Rated VC stuff
def create_rated_vc(self, contest_id: int, start_time: float, finish_time: float, guild_id: str, user_ids: [str]):
""" Creates a rated vc and returns its id.
"""
query = ('INSERT INTO rated_vcs '
'(contest_id, start_time, finish_time, status, guild_id) '
'VALUES ( ?, ?, ?, ?, ?)')
id = None
with self.conn:
id = self.conn.execute(query, (contest_id, start_time, finish_time, RatedVC.ONGOING, guild_id)).lastrowid
for user_id in user_ids:
query = ('INSERT INTO rated_vc_users '
'(vc_id, user_id) '
'VALUES (? , ?)')
self.conn.execute(query, (id, user_id))
return id
def get_rated_vc(self, vc_id: int):
query = ('SELECT * '
'FROM rated_vcs '
'WHERE id = ? ')
vc = self._fetchone(query, params=(vc_id,), row_factory=namedtuple_factory)
return vc
def get_ongoing_rated_vc_ids(self):
query = ('SELECT id '
'FROM rated_vcs '
'WHERE status = ? '
)
vcs = self._fetchall(query, params=(RatedVC.ONGOING,), row_factory=namedtuple_factory)
vc_ids = [vc.id for vc in vcs]
return vc_ids
def get_rated_vc_user_ids(self, vc_id: int):
query = ('SELECT user_id '
'FROM rated_vc_users '
'WHERE vc_id = ? '
)
users = self._fetchall(query, params=(vc_id,), row_factory=namedtuple_factory)
user_ids = [user.user_id for user in users]
return user_ids
def finish_rated_vc(self, vc_id: int):
query = ('UPDATE rated_vcs '
'SET status = ? '
'WHERE id = ? ')
with self.conn:
self.conn.execute(query, (RatedVC.FINISHED, vc_id))
def update_vc_rating(self, vc_id: int, user_id: str, rating: int):
query = ('INSERT OR REPLACE INTO rated_vc_users '
'(vc_id, user_id, rating) '
'VALUES (?, ?, ?) ')
with self.conn:
self.conn.execute(query, (vc_id, user_id, rating))
def get_vc_rating(self, user_id: str, default_if_not_exist: bool = True):
query = ('SELECT MAX(vc_id) AS latest_vc_id, rating '
'FROM rated_vc_users '
'WHERE user_id = ? AND rating IS NOT NULL'
)
rating = self._fetchone(query, params=(user_id, ), row_factory=namedtuple_factory).rating
if rating is None:
if default_if_not_exist:
return _DEFAULT_VC_RATING
return None
return rating
def get_vc_rating_history(self, user_id: str):
""" Return [vc_id, rating].
"""
query = ('SELECT vc_id, rating '
'FROM rated_vc_users '
'WHERE user_id = ? AND rating IS NOT NULL'
)
ratings = self._fetchall(query, params=(user_id,), row_factory=namedtuple_factory)
return ratings
def set_rated_vc_channel(self, guild_id, channel_id):
query = ('INSERT OR REPLACE INTO rated_vc_settings '
' (guild_id, channel_id) VALUES (?, ?)'
)
with self.conn:
self.conn.execute(query, (guild_id, channel_id))
def get_rated_vc_channel(self, guild_id):
query = ('SELECT channel_id '
'FROM rated_vc_settings '
'WHERE guild_id = ?')
channel_id = self.conn.execute(query, (guild_id,)).fetchone()
return int(channel_id[0]) if channel_id else None
def remove_last_ratedvc_participation(self, user_id: str):
query = ('SELECT MAX(vc_id) AS vc_id '
'FROM rated_vc_users '
'WHERE user_id = ? '
)
vc_id = self._fetchone(query, params=(user_id, ), row_factory=namedtuple_factory).vc_id
query = ('DELETE FROM rated_vc_users '
'WHERE user_id = ? AND vc_id = ? ')
with self.conn:
return self.conn.execute(query, (user_id, vc_id)).rowcount
def close(self):
self.conn.close()