Skip to content

Commit

Permalink
WIP: Changes to playerinfo model
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelseeger committed Jun 13, 2024
1 parent 22f49d7 commit b1098e9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
28 changes: 22 additions & 6 deletions obs_tools/playerinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
from datetime import datetime, timedelta, timezone
from os.path import getmtime, join

import numpy as np

from config import config
from replays.db import replaydb
from replays.types import PlayerInfo, Replay, to_bson_binary
from replays.types import Alias, PlayerInfo, Replay, to_bson_binary
from replays.util import is_barcode

log = logging.getLogger(f"{config.name}.{__name__}")
Expand Down Expand Up @@ -102,12 +104,12 @@ def save_player_info(replay: Replay):

player_info.name = opponent.name

# add name to aliases if it's not already there
if player_info.name not in player_info.aliases:
player_info.aliases.append(player_info.name)
alias = Alias(
name=opponent.name, portrait=to_bson_binary(portrait), seen_on=replay.date
)

if portrait is not None and to_bson_binary(portrait) not in player_info.portraits:
player_info.portraits.append(portrait)
if alias not in player_info.aliases:
player_info.aliases.append(alias)

result = replaydb.upsert(player_info)

Expand All @@ -117,3 +119,17 @@ def save_player_info(replay: Replay):
)

return result


def resolve_player(name: str, portrait: np.ndarray) -> PlayerInfo:

q = {"aliases": name}
candidates = replaydb.db.find_many(PlayerInfo, query=q)

player_info = PlayerInfo(name=name, portrait=portrait)
existing_player_info: PlayerInfo = replaydb.find(player_info)

if existing_player_info:
return existing_player_info
else:
return player_info
21 changes: 19 additions & 2 deletions replays/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,30 @@ class Metadata(DbModel):
_collection: ClassVar = "replays.meta"


class Alias(BaseModel):
name: str
portrait: BsonBinary | None = None
seen_on: datetime | None = None

def __str__(self) -> str:
return f"{self.name}"

def __repr__(self) -> str:
return self.__str__()

def __eq__(self, other: object) -> bool:
if isinstance(other, Alias):
return self.name == other.name and self.portrait == other.portrait
else:
return False


class PlayerInfo(DbModel):
id: ToonHandle
name: str = None
aliases: List[str] = []
aliases: List[Alias] = []
toon_handle: ToonHandle = None
portrait: BsonBinary | None = None
portraits: List[BsonBinary] = []
_collection: ClassVar = "replays.players"


Expand Down

0 comments on commit b1098e9

Please sign in to comment.