From 12f80bde9ee3abf49685e6d20dfffe92ac212dd3 Mon Sep 17 00:00:00 2001 From: SiHuaN Date: Fri, 7 Oct 2022 20:15:43 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=E6=B7=B1=E6=B8=8A?= =?UTF-8?q?=E6=8E=A8=E8=8D=90=E9=98=9F=E4=BC=8D=E4=B8=8A=E4=B8=8B=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E5=90=8C=E4=B8=80=E8=A7=92=E8=89=B2=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 洛水居室 Co-authored-by: omg-xtao <100690902+omg-xtao@users.noreply.github.com> --- modules/apihelper/abyss_team.py | 44 ++++++++++++++++++++++----------- plugins/genshin/abyss_team.py | 4 +-- tests/test_abyss_team_data.py | 22 +++++++++++------ 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/modules/apihelper/abyss_team.py b/modules/apihelper/abyss_team.py index 7170a9e..700e056 100644 --- a/modules/apihelper/abyss_team.py +++ b/modules/apihelper/abyss_team.py @@ -1,5 +1,5 @@ import time -from typing import List, Optional +from typing import List, Optional, Any import httpx from pydantic import BaseModel, parse_obj_as, validator @@ -14,25 +14,41 @@ class Member(BaseModel): class TeamRate(BaseModel): rate: float formation: List[Member] - ownerNum: Optional[int] + owner_num: Optional[int] @validator('rate', pre=True) def str2float(cls, v): # pylint: disable=R0201 return float(v.replace('%', '')) / 100.0 if isinstance(v, str) else v +class FullTeamRate(BaseModel): + up: TeamRate + down: TeamRate + owner_num: Optional[int] + + @property + def rate(self) -> float: + return self.up.rate + self.down.rate + + class TeamRateResult(BaseModel): - rateListUp: List[TeamRate] - rateListDown: List[TeamRate] - userCount: int + 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)) def sort(self, characters: List[str]): - for team in self.rateListUp: - team.ownerNum = len(set(characters) & {member.name for member in team.formation}) - for team in self.rateListDown: - team.ownerNum = len(set(characters) & {member.name for member in team.formation}) - self.rateListUp.sort(key=lambda x: (x.ownerNum / 4 * x.rate), reverse=True) - self.rateListDown.sort(key=lambda x: (x.ownerNum / 4 * x.rate), reverse=True) + for team in self.rate_list_full: + team.owner_num = sum(member.name in characters for member in team.up.formation + team.down.formation) + self.rate_list_full.sort(key=lambda x: (x.owner_num / 8 * x.rate), reverse=True) class AbyssTeamData: @@ -58,9 +74,9 @@ class AbyssTeamData: data_up_json = data_up.json()["result"] data_down = await self.client.post(self.TEAM_RATE_API, json={"version": self.VERSION, "layer": 2}) data_down_json = data_down.json()["result"] - self.data = TeamRateResult(rateListUp=parse_obj_as(List[TeamRate], data_up_json["rateList"]), - rateListDown=parse_obj_as(List[TeamRate], data_down_json["rateList"]), - userCount=data_up_json["userCount"]) + self.data = TeamRateResult(rate_list_up=parse_obj_as(List[TeamRate], data_up_json["rateList"]), + rate_list_down=parse_obj_as(List[TeamRate], data_down_json["rateList"]), + user_count=data_up_json["userCount"]) self.time = time.time() return self.data.copy(deep=True) diff --git a/plugins/genshin/abyss_team.py b/plugins/genshin/abyss_team.py index df00955..9481c03 100644 --- a/plugins/genshin/abyss_team.py +++ b/plugins/genshin/abyss_team.py @@ -65,7 +65,7 @@ class AbyssTeam(Plugin, BasePlugin): "up": [], "down": [] } - for i in team_data.rateListUp[0].formation: + for i in team_data.rate_list_full[0].up.formation: temp = { "icon": (await self.assets_service.avatar(roleToId(i.name)).icon()).as_uri(), "name": i.name, @@ -73,7 +73,7 @@ class AbyssTeam(Plugin, BasePlugin): "hava": (i.name in user_data) if user_data else True, } abyss_team_data["up"].append(temp) - for i in team_data.rateListDown[0].formation: + for i in team_data.rate_list_full[0].down.formation: temp = { "icon": (await self.assets_service.avatar(roleToId(i.name)).icon()).as_uri(), "name": i.name, diff --git a/tests/test_abyss_team_data.py b/tests/test_abyss_team_data.py index 93564ed..e6ecfb0 100644 --- a/tests/test_abyss_team_data.py +++ b/tests/test_abyss_team_data.py @@ -4,7 +4,7 @@ import pytest import pytest_asyncio from flaky import flaky -from modules.apihelper.abyss_team import AbyssTeamData, TeamRateResult, TeamRate +from modules.apihelper.abyss_team import AbyssTeamData, TeamRateResult, TeamRate, FullTeamRate LOGGER = logging.getLogger(__name__) @@ -22,12 +22,18 @@ async def abyss_team_data(): async def test_abyss_team_data(abyss_team_data: AbyssTeamData): team_data = await abyss_team_data.get_data() assert isinstance(team_data, TeamRateResult) - assert isinstance(team_data.rateListUp[0], TeamRate) - assert isinstance(team_data.rateListUp[-1], TeamRate) - assert isinstance(team_data.rateListDown[0], TeamRate) - assert isinstance(team_data.rateListDown[-1], TeamRate) - assert team_data.userCount > 0 - for i in team_data.rateListUp[0].formation: + assert isinstance(team_data.rate_list_up[0], TeamRate) + assert isinstance(team_data.rate_list_up[-1], TeamRate) + assert isinstance(team_data.rate_list_down[0], TeamRate) + assert isinstance(team_data.rate_list_down[-1], TeamRate) + assert team_data.user_count > 0 + team_data.sort(["迪奥娜", "芭芭拉", "凯亚", "琴"]) + assert isinstance(team_data.rate_list_full[0], FullTeamRate) + assert isinstance(team_data.rate_list_full[-1], FullTeamRate) + memberUp = {i.name for i in team_data.rate_list_full[0].up.formation} + memberDown = {i.name for i in team_data.rate_list_full[0].down.formation} + assert not memberUp & memberDown + for i in team_data.rate_list_full[0].down.formation: LOGGER.info("rate down info:name %s star %s", i.name, i.star) - for i in team_data.rateListDown[0].formation: + for i in team_data.rate_list_full[0].up.formation: LOGGER.info("rate up info:name %s star %s", i.name, i.star)