10
10
from enum import unique
11
11
from functools import cached_property
12
12
from typing import TYPE_CHECKING
13
- from typing import Any
14
13
from typing import TypedDict
15
14
from typing import cast
16
15
36
35
from app .objects .score import Grade
37
36
from app .objects .score import Score
38
37
from app .repositories import logs as logs_repo
38
+ from app .repositories import players as players_repo
39
39
from app .repositories import stats as stats_repo
40
40
from app .state .services import Geolocation
41
41
from app .utils import escape_enum
@@ -238,7 +238,6 @@ def __init__(
238
238
239
239
self .id = id
240
240
self .name = name
241
- self .safe_name = self .make_safe (self .name )
242
241
self .priv = priv
243
242
self .pw_bcrypt = pw_bcrypt
244
243
self .token = token
@@ -282,8 +281,6 @@ def _noop_enqueue(data: bytes) -> None:
282
281
283
282
self .pres_filter = PresenceFilter .Nil
284
283
285
- # XXX: below is mostly implementation-specific & internal stuff
286
-
287
284
# store most recent score for each gamemode.
288
285
self .recent_scores : dict [GameMode , Score | None ] = {
289
286
mode : None for mode in GameMode
@@ -297,6 +294,10 @@ def _noop_enqueue(data: bytes) -> None:
297
294
def __repr__ (self ) -> str :
298
295
return f"<{ self .name } ({ self .id } )>"
299
296
297
+ @property
298
+ def safe_name (self ) -> str :
299
+ return make_safe_name (self .name )
300
+
300
301
@property
301
302
def is_online (self ) -> bool :
302
303
return bool (self .token != "" )
@@ -384,11 +385,6 @@ def generate_token() -> str:
384
385
"""Generate a random uuid as a token."""
385
386
return str (uuid .uuid4 ())
386
387
387
- @staticmethod
388
- def make_safe (name : str ) -> str :
389
- """Return a name safe for usage in sql."""
390
- return make_safe_name (name )
391
-
392
388
def logout (self ) -> None :
393
389
"""Log `self` out of the server."""
394
390
# invalidate the user's token.
@@ -421,45 +417,45 @@ def logout(self) -> None:
421
417
422
418
async def update_privs (self , new : Privileges ) -> None :
423
419
"""Update `self`'s privileges to `new`."""
420
+
424
421
self .priv = new
422
+ if "bancho_priv" in vars (self ):
423
+ del self .bancho_priv # wipe cached_property
425
424
426
- await app . state . services . database . execute (
427
- "UPDATE users SET priv = :priv WHERE id = :user_id" ,
428
- { " priv" : self .priv , "user_id" : self . id } ,
425
+ await players_repo . update (
426
+ id = self . id ,
427
+ priv = self .priv ,
429
428
)
430
429
431
- if "bancho_priv" in self .__dict__ :
432
- del self .bancho_priv # wipe cached_property
433
-
434
430
async def add_privs (self , bits : Privileges ) -> None :
435
431
"""Update `self`'s privileges, adding `bits`."""
432
+
436
433
self .priv |= bits
434
+ if "bancho_priv" in vars (self ):
435
+ del self .bancho_priv # wipe cached_property
437
436
438
- await app . state . services . database . execute (
439
- "UPDATE users SET priv = :priv WHERE id = :user_id" ,
440
- { " priv" : self .priv , "user_id" : self . id } ,
437
+ await players_repo . update (
438
+ id = self . id ,
439
+ priv = self .priv ,
441
440
)
442
441
443
- if "bancho_priv" in self .__dict__ :
444
- del self .bancho_priv # wipe cached_property
445
-
446
442
if self .is_online :
447
443
# if they're online, send a packet
448
444
# to update their client-side privileges
449
445
self .enqueue (app .packets .bancho_privileges (self .bancho_priv ))
450
446
451
447
async def remove_privs (self , bits : Privileges ) -> None :
452
448
"""Update `self`'s privileges, removing `bits`."""
449
+
453
450
self .priv &= ~ bits
451
+ if "bancho_priv" in vars (self ):
452
+ del self .bancho_priv # wipe cached_property
454
453
455
- await app . state . services . database . execute (
456
- "UPDATE users SET priv = :priv WHERE id = :user_id" ,
457
- { " priv" : self .priv , "user_id" : self . id } ,
454
+ await players_repo . update (
455
+ id = self . id ,
456
+ priv = self .priv ,
458
457
)
459
458
460
- if "bancho_priv" in self .__dict__ :
461
- del self .bancho_priv # wipe cached_property
462
-
463
459
if self .is_online :
464
460
# if they're online, send a packet
465
461
# to update their client-side privileges
@@ -542,9 +538,9 @@ async def silence(self, admin: Player, duration: float, reason: str) -> None:
542
538
"""Silence `self` for `duration` seconds, and log to sql."""
543
539
self .silence_end = int (time .time () + duration )
544
540
545
- await app . state . services . database . execute (
546
- "UPDATE users SET silence_end = :silence_end WHERE id = :user_id" ,
547
- { " silence_end" : self .silence_end , "user_id" : self . id } ,
541
+ await players_repo . update (
542
+ id = self . id ,
543
+ silence_end = self .silence_end ,
548
544
)
549
545
550
546
await logs_repo .create (
@@ -570,9 +566,9 @@ async def unsilence(self, admin: Player, reason: str) -> None:
570
566
"""Unsilence `self`, and log to sql."""
571
567
self .silence_end = int (time .time ())
572
568
573
- await app . state . services . database . execute (
574
- "UPDATE users SET silence_end = :silence_end WHERE id = :user_id" ,
575
- { " silence_end" : self .silence_end , "user_id" : self . id } ,
569
+ await players_repo . update (
570
+ id = self . id ,
571
+ silence_end = self .silence_end ,
576
572
)
577
573
578
574
await logs_repo .create (
@@ -1006,9 +1002,9 @@ async def stats_from_sql_full(self, db_conn: databases.core.Connection) -> None:
1006
1002
1007
1003
def update_latest_activity_soon (self ) -> None :
1008
1004
"""Update the player's latest activity in the database."""
1009
- task = app . state . services . database . execute (
1010
- "UPDATE users SET latest_activity = UNIX_TIMESTAMP() WHERE id = :user_id" ,
1011
- { "user_id" : self . id } ,
1005
+ task = players_repo . update (
1006
+ id = self . id ,
1007
+ latest_activity = int ( time . time ()) ,
1012
1008
)
1013
1009
app .state .loop .create_task (task )
1014
1010
0 commit comments