2020-03-18 17:06:15 +00:00
|
|
|
from sqlalchemy import *
|
|
|
|
from sqlalchemy.orm import *
|
|
|
|
from sqlalchemy.ext.declarative import declared_attr
|
2020-06-19 15:39:46 +00:00
|
|
|
import steam.steamid
|
2020-03-19 15:26:11 +00:00
|
|
|
from ..types import BrawlhallaRank, BrawlhallaTier, BrawlhallaMetal
|
2020-03-18 17:06:15 +00:00
|
|
|
|
|
|
|
|
2020-03-19 15:26:11 +00:00
|
|
|
# noinspection PyAttributeOutsideInit
|
2020-03-18 17:06:15 +00:00
|
|
|
class Brawlhalla:
|
|
|
|
__tablename__ = "brawlhalla"
|
|
|
|
|
|
|
|
@declared_attr
|
|
|
|
def brawlhalla_id(self):
|
|
|
|
return Column(Integer, primary_key=True)
|
|
|
|
|
|
|
|
@declared_attr
|
|
|
|
def _steamid(self):
|
2020-03-23 23:02:55 +00:00
|
|
|
return Column(BigInteger, ForeignKey("steam._steamid"), unique=True)
|
2020-03-18 17:06:15 +00:00
|
|
|
|
|
|
|
@declared_attr
|
|
|
|
def steam(self):
|
|
|
|
return relationship("Steam", backref=backref("brawlhalla", uselist=False))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def steamid(self):
|
2020-06-19 15:39:46 +00:00
|
|
|
return steam.steamid.SteamID(self._steamid)
|
2020-03-18 17:06:15 +00:00
|
|
|
|
|
|
|
@declared_attr
|
|
|
|
def name(self):
|
2020-03-19 15:26:11 +00:00
|
|
|
return Column(String, nullable=False)
|
2020-03-18 17:06:15 +00:00
|
|
|
|
|
|
|
@declared_attr
|
|
|
|
def rating_1v1(self):
|
|
|
|
return Column(Integer)
|
|
|
|
|
2020-03-19 15:26:11 +00:00
|
|
|
@declared_attr
|
2020-03-18 17:06:15 +00:00
|
|
|
def tier_1v1(self):
|
2020-03-19 15:26:11 +00:00
|
|
|
return Column(Enum(BrawlhallaTier))
|
|
|
|
|
|
|
|
@declared_attr
|
|
|
|
def metal_1v1(self):
|
|
|
|
return Column(Enum(BrawlhallaMetal))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def rank_1v1(self):
|
2020-06-20 01:49:48 +00:00
|
|
|
if self.metal_1v1 is None:
|
|
|
|
return None
|
2020-03-19 15:26:11 +00:00
|
|
|
return BrawlhallaRank(metal=self.metal_1v1, tier=self.tier_1v1)
|
|
|
|
|
|
|
|
@rank_1v1.setter
|
|
|
|
def rank_1v1(self, value):
|
|
|
|
if not isinstance(value, BrawlhallaRank):
|
|
|
|
raise TypeError("rank_1v1 can only be set to BrawlhallaRank values.")
|
|
|
|
self.metal_1v1 = value.metal
|
|
|
|
self.tier_1v1 = value.tier
|
|
|
|
|
2020-03-23 23:02:55 +00:00
|
|
|
@property
|
|
|
|
def duos(self):
|
|
|
|
return [*self._duos_one, *self._duos_two]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def rating_2v2(self):
|
2020-06-19 15:39:46 +00:00
|
|
|
duos = sorted(self.duos, key=lambda d: -d.rating_2v2)
|
2020-03-23 23:02:55 +00:00
|
|
|
if len(duos) == 0:
|
|
|
|
return None
|
|
|
|
return duos[0].rating_2v2
|
|
|
|
|
|
|
|
@property
|
|
|
|
def tier_2v2(self):
|
2020-06-19 15:39:46 +00:00
|
|
|
duos = sorted(self.duos, key=lambda d: -d.rating_2v2)
|
2020-03-23 23:02:55 +00:00
|
|
|
if len(duos) == 0:
|
|
|
|
return None
|
|
|
|
return duos[0].tier_2v2
|
|
|
|
|
|
|
|
@property
|
|
|
|
def metal_2v2(self):
|
2020-06-19 15:39:46 +00:00
|
|
|
duos = sorted(self.duos, key=lambda d: -d.rating_2v2)
|
2020-03-23 23:02:55 +00:00
|
|
|
if len(duos) == 0:
|
|
|
|
return None
|
|
|
|
return duos[0].metal_2v2
|
|
|
|
|
|
|
|
@property
|
|
|
|
def rank_2v2(self):
|
2020-06-19 15:39:46 +00:00
|
|
|
duos = sorted(self.duos, key=lambda d: -d.rating_2v2)
|
2020-03-23 23:02:55 +00:00
|
|
|
if len(duos) == 0:
|
|
|
|
return None
|
|
|
|
return duos[0].rank_2v2
|
|
|
|
|
2020-06-19 15:39:46 +00:00
|
|
|
def json(self):
|
|
|
|
one_rank = self.rank_1v1
|
|
|
|
two_rank = self.rank_2v2
|
|
|
|
return {
|
|
|
|
"name": self.name,
|
|
|
|
"1v1": {
|
|
|
|
"rating": self.rating_1v1,
|
|
|
|
"metal": one_rank.metal.name,
|
|
|
|
"tier": one_rank.tier.name
|
2020-06-20 01:49:48 +00:00
|
|
|
} if one_rank is not None else None,
|
2020-06-19 15:39:46 +00:00
|
|
|
"2v2": {
|
|
|
|
"rating": self.rating_2v2,
|
|
|
|
"metal": two_rank.metal.name,
|
|
|
|
"tier": two_rank.tier.name
|
2020-06-20 01:49:48 +00:00
|
|
|
} if two_rank is not None else None
|
2020-06-19 15:39:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 15:26:11 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return f"<Brawlhalla account {self._steamid}>"
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-06-19 15:39:46 +00:00
|
|
|
return f"[c]brawlhalla:{self.brawlhalla_id}[/c]"
|