🎨 daily_material: optimize get skills level code

Co-authored-by: 洛水居室 <luoshuijs@outlook.com>
This commit is contained in:
SiHuaN 2022-10-29 11:53:08 +08:00 committed by 洛水居室
parent 31d71fd6a1
commit 755c224ca0
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC

View File

@ -129,22 +129,25 @@ class DailyMaterial(Plugin, BasePlugin):
@staticmethod @staticmethod
async def _get_skills_data(client: Client, character_id: int) -> Optional[List[int]]: async def _get_skills_data(client: Client, character_id: int) -> Optional[List[int]]:
detail = None """获取角色技能的数据"""
for _ in range(5): for _ in range(5):
try: try:
detail = await client.get_character_details(character_id) detail = await client.get_character_details(character_id)
except Exception as e: # pylint: disable=W0703 except Exception as e: # pylint: disable=W0703
logger.warning(f"daily_material 解析角色 id 为 [bold]{character_id}[/]的数据时遇到了错误:{e}", extra={"markup": True}) if isinstance(e, GenshinException):
if isinstance(e, GenshinException) and "Too Many Requests" in e.msg: # 如果是 Too Many Requests 异常,则等待一段时间后重试
await asyncio.sleep(0.2) if "Too Many Requests" in e.msg:
continue await asyncio.sleep(0.2)
# 输入数据不可能是旅行者 continue
# if character.name != "旅行者": # 如果是其他异常,则直接抛出
# raise e raise e
# logger.debug(f"解析旅行者数据时遇到了错误:{e}")
else: else:
break break
if not detail: else:
# 如果重试了5次都失败了则直接返回 None
logger.warning(
f"daily_material 解析角色 id 为 [bold]{character_id}[/]的数据时遇到了 Too Many Requests 错误", extra={"markup": True}
)
return None return None
# 不用针对旅行者、草主进行特殊处理,因为输入数据不会有旅行者。 # 不用针对旅行者、草主进行特殊处理,因为输入数据不会有旅行者。
# 不用计算命座加成因为这个是展示天赋升级情况10 级为最高。计算命座会引起混淆。 # 不用计算命座加成因为这个是展示天赋升级情况10 级为最高。计算命座会引起混淆。
@ -153,7 +156,6 @@ class DailyMaterial(Plugin, BasePlugin):
async def _get_data_from_user(self, user: User) -> Tuple[Optional[Client], Dict[str, List[Any]]]: async def _get_data_from_user(self, user: User) -> Tuple[Optional[Client], Dict[str, List[Any]]]:
"""获取已经绑定的账号的角色、武器信息""" """获取已经绑定的账号的角色、武器信息"""
client = None
user_data = {"avatar": [], "weapon": []} user_data = {"avatar": [], "weapon": []}
try: try:
logger.debug("尝试获取已绑定的原神账号") logger.debug("尝试获取已绑定的原神账号")
@ -195,7 +197,11 @@ class DailyMaterial(Plugin, BasePlugin):
logger.info(f"未查询到用户({user.full_name} {user.id}) 所绑定的账号信息") logger.info(f"未查询到用户({user.full_name} {user.id}) 所绑定的账号信息")
except InvalidCookies: except InvalidCookies:
logger.info(f"用户({user.full_name} {user.id}) 所绑定的账号信息已失效") logger.info(f"用户({user.full_name} {user.id}) 所绑定的账号信息已失效")
return client, user_data else:
# 没有异常返回数据
return client, user_data
# 有上述异常的, client 会返回 None
return None, user_data
@handler.command("daily_material", block=False) @handler.command("daily_material", block=False)
@restricts(restricts_time_of_groups=20, without_overlapping=True) @restricts(restricts_time_of_groups=20, without_overlapping=True)
@ -253,57 +259,60 @@ class DailyMaterial(Plugin, BasePlugin):
await message.reply_chat_action(ChatAction.TYPING) await message.reply_chat_action(ChatAction.TYPING)
render_data = RenderData(title=title, time=time, uid=client.uid if client else client) render_data = RenderData(title=title, time=time, uid=client.uid if client else client)
for type_ in ["avatar", "weapon"]:
areas = [] # 如果 client 为 None 就没必要进技能处理 因为已经触发 InvalidCookies 等异常
for area_data in local_data[type_]: # 遍历每个区域的信息:蒙德、璃月、稻妻、须弥 if client:
items = [] for type_ in ["avatar", "weapon"]:
for id_ in area_data["items"]: # 遍历所有该区域下当天weekday可以培养的角色、武器 areas = []
added = False for area_data in local_data[type_]: # 遍历每个区域的信息:蒙德、璃月、稻妻、须弥
for i in user_data[type_]: # 从已经获取的角色数据中查找对应角色、武器 items = []
if id_ == str(i.id): for id_ in area_data["items"]: # 遍历所有该区域下当天weekday可以培养的角色、武器
if i.rarity > 3: # 跳过 3 星及以下的武器 added = False
if type_ == "avatar": # 给角色添加天赋信息 for i in user_data[type_]: # 从已经获取的角色数据中查找对应角色、武器
skills = await self._get_skills_data(client, i.gid) if id_ == str(i.id):
i.skills = skills if i.rarity > 3: # 跳过 3 星及以下的武器
items.append(i) if type_ == "avatar": # 给角色添加天赋信息
added = True skills = await self._get_skills_data(client, i.gid)
if added: i.skills = skills
continue items.append(i)
try: added = True
item = HONEY_DATA[type_][id_] if added:
except KeyError: # 跳过不存在或者已忽略的角色、武器 continue
logger.warning(f"未在 honey 数据中找到 {type_} {id_} 的信息") try:
continue item = HONEY_DATA[type_][id_]
if item[2] < 4: # 跳过 3 星及以下的武器 except KeyError: # 跳过不存在或者已忽略的角色、武器
continue logger.warning(f"未在 honey 数据中找到 {type_} {id_} 的信息")
items.append( continue
ItemData( # 添加角色数据中未找到的 if item[2] < 4: # 跳过 3 星及以下的武器
id=id_, continue
name=item[1], items.append(
rarity=item[2], ItemData( # 添加角色数据中未找到的
icon=(await getattr(self.assets_service, type_)(id_).icon()).as_uri(), id=id_,
name=item[1],
rarity=item[2],
icon=(await getattr(self.assets_service, type_)(id_).icon()).as_uri(),
)
)
materials = []
for mid in area_data["materials"]: # 添加这个区域当天weekday的培养素材
try:
path = (await self.assets_service.material(mid).icon()).as_uri()
material = HONEY_DATA["material"][mid]
materials.append(ItemData(id=mid, icon=path, name=material[1], rarity=material[2]))
except AssetsCouldNotFound as exc:
logger.error(f"出错了呜呜呜 ~ {repr(exc)}")
await notice.edit_text("出错了呜呜呜 ~ 派蒙找不到一些素材")
return
areas.append(
AreaData(
name=area_data["name"],
materials=materials,
# template previewer pickle cannot serialize generator
items=list(sort_item(items)),
material_name=get_material_serial_name(map(lambda x: x.name, materials)),
) )
) )
materials = [] setattr(render_data, {"avatar": "character"}.get(type_, type_), areas)
for mid in area_data["materials"]: # 添加这个区域当天weekday的培养素材
try:
path = (await self.assets_service.material(mid).icon()).as_uri()
material = HONEY_DATA["material"][mid]
materials.append(ItemData(id=mid, icon=path, name=material[1], rarity=material[2]))
except AssetsCouldNotFound as exc:
logger.error(f"出错了呜呜呜 ~ {repr(exc)}")
await notice.edit_text("出错了呜呜呜 ~ 派蒙找不到一些素材")
return
areas.append(
AreaData(
name=area_data["name"],
materials=materials,
# template previewer pickle cannot serialize generator
items=list(sort_item(items)),
material_name=get_material_serial_name(map(lambda x: x.name, materials)),
)
)
setattr(render_data, {"avatar": "character"}.get(type_, type_), areas)
await message.reply_chat_action(ChatAction.TYPING) await message.reply_chat_action(ChatAction.TYPING)