2022-10-07 12:15:43 +00:00
|
|
|
from typing import List, Optional, Any
|
2022-10-06 16:30:38 +00:00
|
|
|
|
2024-11-30 14:31:42 +00:00
|
|
|
from pydantic import field_validator, BaseModel
|
2022-12-10 12:37:43 +00:00
|
|
|
|
|
|
|
__all__ = ("Member", "TeamRate", "FullTeamRate", "TeamRateResult")
|
2022-10-06 16:30:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Member(BaseModel):
|
|
|
|
star: int
|
2022-12-30 06:23:56 +00:00
|
|
|
avatar: str
|
2022-10-06 16:30:38 +00:00
|
|
|
name: str
|
|
|
|
|
|
|
|
|
|
|
|
class TeamRate(BaseModel):
|
|
|
|
rate: float
|
|
|
|
formation: List[Member]
|
2024-11-30 14:31:42 +00:00
|
|
|
owner_num: Optional[int] = None
|
2022-10-06 16:30:38 +00:00
|
|
|
|
2024-11-30 14:31:42 +00:00
|
|
|
@field_validator("rate", mode="before")
|
|
|
|
@classmethod
|
2022-10-07 05:02:49 +00:00
|
|
|
def str2float(cls, v): # pylint: disable=R0201
|
2022-10-10 10:43:18 +00:00
|
|
|
return float(v.replace("%", "")) / 100.0 if isinstance(v, str) else v
|
2022-10-06 16:30:38 +00:00
|
|
|
|
|
|
|
|
2022-10-07 12:15:43 +00:00
|
|
|
class FullTeamRate(BaseModel):
|
|
|
|
up: TeamRate
|
|
|
|
down: TeamRate
|
2024-11-30 14:31:42 +00:00
|
|
|
owner_num: Optional[int] = None
|
|
|
|
nice: Optional[float] = None
|
2022-10-07 12:15:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def rate(self) -> float:
|
2022-10-10 10:43:18 +00:00
|
|
|
return (self.up.rate + self.down.rate) / 2
|
2022-10-07 12:15:43 +00:00
|
|
|
|
|
|
|
|
2022-10-06 16:30:38 +00:00
|
|
|
class TeamRateResult(BaseModel):
|
2022-10-10 10:43:18 +00:00
|
|
|
version: str
|
2022-10-07 12:15:43 +00:00
|
|
|
rate_list_up: List[TeamRate]
|
|
|
|
rate_list_down: List[TeamRate]
|
|
|
|
rate_list_full: List[FullTeamRate] = []
|
|
|
|
user_count: int
|
|
|
|
|
|
|
|
def __init__(self, **data: Any):
|
|
|
|
super().__init__(**data)
|
|
|
|
for team_up in self.rate_list_up:
|
|
|
|
for team_down in self.rate_list_down:
|
|
|
|
if {member.name for member in team_up.formation} & {member.name for member in team_down.formation}:
|
|
|
|
continue
|
|
|
|
self.rate_list_full.append(FullTeamRate(up=team_up, down=team_down))
|
2022-10-06 16:30:38 +00:00
|
|
|
|
|
|
|
def sort(self, characters: List[str]):
|
2022-10-07 12:15:43 +00:00
|
|
|
for team in self.rate_list_full:
|
|
|
|
team.owner_num = sum(member.name in characters for member in team.up.formation + team.down.formation)
|
2022-10-10 10:43:18 +00:00
|
|
|
team.nice = team.owner_num / 8 + team.rate
|
2022-10-08 03:16:52 +00:00
|
|
|
self.rate_list_full.sort(key=lambda x: x.nice, reverse=True)
|
|
|
|
|
2022-10-10 10:43:18 +00:00
|
|
|
def random_team(self) -> List[FullTeamRate]:
|
|
|
|
data: List[FullTeamRate] = []
|
2022-10-08 03:16:52 +00:00
|
|
|
for team in self.rate_list_full:
|
2022-10-10 10:43:18 +00:00
|
|
|
add = True
|
|
|
|
for team_ in data:
|
|
|
|
if {member.name for member in team.up.formation} & {member.name for member in team_.up.formation}:
|
|
|
|
add = False
|
|
|
|
break
|
|
|
|
if {member.name for member in team.down.formation} & {member.name for member in team_.down.formation}:
|
|
|
|
add = False
|
|
|
|
break
|
|
|
|
if add:
|
|
|
|
data.append(team)
|
|
|
|
if len(data) >= 3:
|
|
|
|
break
|
|
|
|
return data
|