From 94c0b249cb30775c3bc74c22e76db33f5341e0f9 Mon Sep 17 00:00:00 2001 From: xris Date: Tue, 24 Oct 2023 22:25:00 +0800 Subject: [PATCH] :recycle: Refactor `daily_material` sort_item Co-authored-by: omg-xtao <100690902+omg-xtao@users.noreply.github.com> --- plugins/genshin/daily/material.py | 33 ++++++++++++------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/plugins/genshin/daily/material.py b/plugins/genshin/daily/material.py index 02b5d82e..b1108be5 100644 --- a/plugins/genshin/daily/material.py +++ b/plugins/genshin/daily/material.py @@ -57,27 +57,20 @@ def sort_item(items: List["ItemData"]) -> Iterable["ItemData"]: 排序规则:持有(星级 > 等级 > 命座/精炼) > 未持有(星级 > 等级 > 命座/精炼) """ - return ( - ArkoWrapper(items) - .sort(lambda x: x.level or -1, reverse=True) - .groupby(lambda x: x.level is None) # 根据持有与未持有进行分组并排序 - .map( - lambda x: ( - ArkoWrapper(x[1]) - .sort(lambda y: y.rarity, reverse=True) - .groupby(lambda y: y.rarity) # 根据星级分组并排序 - .map( - lambda y: ( - ArkoWrapper(y[1]) - .sort(lambda z: z.refinement or z.constellation or -1, reverse=True) - .groupby(lambda z: z.refinement or z.constellation or -1) # 根据命座/精炼进行分组并排序 - .map(lambda i: ArkoWrapper(i[1]).sort(lambda j: j.id)) - ) - ) - ) + + def key(item: "ItemData"): + # 有个小小的意外逻辑,不影响排序输出,如果要修改可能需要注意下: + # constellation 可以为 None 或 0,此时都会被判断为 False + # 此时 refinment or constellation or -1 都会返回 -1 + # 但不影响排序结果 + return ( + item.level is not None, # 根据持有与未持有进行分组并排序 + item.rarity, # 根据星级分组并排序 + item.refinement or item.constellation or -1, # 根据命座/精炼进行分组并排序 + item.id, # 默认按照物品 ID 进行排序 ) - .flat(3) - ) + + return sorted(items, key=key, reverse=True) def get_material_serial_name(names: Iterable[str]) -> str: