From 95b138197562d6eb7b223fff90f78e9c81b1711f Mon Sep 17 00:00:00 2001 From: SiHuaN Date: Sat, 8 Oct 2022 11:16:52 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E9=9A=8F=E6=9C=BA=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=B7=B1=E6=B8=8A=E6=8E=A8=E8=8D=90=E9=98=9F=E4=BC=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/apihelper/abyss_team.py | 15 ++++++++++++++- plugins/genshin/abyss_team.py | 6 +++--- tests/test_abyss_team_data.py | 8 +++++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/modules/apihelper/abyss_team.py b/modules/apihelper/abyss_team.py index 700e056..bcf9c72 100644 --- a/modules/apihelper/abyss_team.py +++ b/modules/apihelper/abyss_team.py @@ -1,3 +1,4 @@ +import secrets import time from typing import List, Optional, Any @@ -25,6 +26,7 @@ class FullTeamRate(BaseModel): up: TeamRate down: TeamRate owner_num: Optional[int] + nice: Optional[float] @property def rate(self) -> float: @@ -48,7 +50,18 @@ class TeamRateResult(BaseModel): def sort(self, characters: List[str]): 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) + team.nice = team.owner_num / 8 * team.rate + self.rate_list_full.sort(key=lambda x: x.nice, reverse=True) + + def random_team(self, characters: List[str]) -> FullTeamRate: + self.sort(characters) + max_nice = self.rate_list_full[0].nice + nice_teams: List[FullTeamRate] = [] + for team in self.rate_list_full: + if team.nice < max_nice: + break + nice_teams.append(team) + return secrets.choice(nice_teams) class AbyssTeamData: diff --git a/plugins/genshin/abyss_team.py b/plugins/genshin/abyss_team.py index 9481c03..d554071 100644 --- a/plugins/genshin/abyss_team.py +++ b/plugins/genshin/abyss_team.py @@ -60,12 +60,12 @@ class AbyssTeam(Plugin, BasePlugin): team_data = await self.team_data.get_data() # 尝试获取用户已绑定的原神账号信息 user_data = await self._get_data_from_user(user) - team_data.sort(user_data) + random_team = team_data.random_team(user_data) abyss_team_data = { "up": [], "down": [] } - for i in team_data.rate_list_full[0].up.formation: + for i in random_team.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.rate_list_full[0].down.formation: + for i in random_team.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 e6ecfb0..5ef3a2d 100644 --- a/tests/test_abyss_team_data.py +++ b/tests/test_abyss_team_data.py @@ -30,9 +30,11 @@ async def test_abyss_team_data(abyss_team_data: AbyssTeamData): 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 + random_team = team_data.random_team(["迪奥娜", "芭芭拉", "凯亚", "琴"]) + assert isinstance(random_team, FullTeamRate) + member_up = {i.name for i in random_team.up.formation} + member_down = {i.name for i in random_team.down.formation} + assert not member_up & member_down 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.rate_list_full[0].up.formation: