Skip to content

Commit

Permalink
✨ Support wish import lazy refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
omg-xtao committed Dec 1, 2024
1 parent 682cfb1 commit 4af2cab
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 88 deletions.
61 changes: 35 additions & 26 deletions modules/gacha_log/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import aiofiles
from simnet import StarRailClient, Region
from simnet.errors import AuthkeyTimeout, InvalidAuthkey
from simnet.models.base import add_timezone
from simnet.models.starrail.wish import StarRailBannerType
from simnet.utils.player import recognize_starrail_server

Expand Down Expand Up @@ -111,7 +112,7 @@ async def remove_history_info(self, user_id: str, uid: str) -> bool:
return False

async def move_history_info(self, user_id: str, uid: str, new_user_id: str) -> bool:
"""移动历史抽卡记录数据
"""移动历史跃迁记录数据
:param user_id: 用户id
:param uid: 原神uid
:param new_user_id: 新用户id
Expand Down Expand Up @@ -237,7 +238,7 @@ async def import_gacha_log_data(self, user_id: int, player_id: int, data: dict,
# 检查导入后的数据是否合法
await self.verify_data(i)
i.sort(key=lambda x: (x.time, x.id))
gacha_log.update_time = datetime.datetime.now()
gacha_log.update_time = add_timezone(datetime.datetime.now())
gacha_log.import_type = import_type.value
await self.save_gacha_log_info(str(user_id), uid, gacha_log)
await self.recount_one_from_uid(user_id, player_id)
Expand All @@ -256,21 +257,38 @@ def get_game_client(player_id: int) -> StarRailClient:
else:
return StarRailClient(player_id=player_id, region=Region.OVERSEAS, lang="zh-cn")

async def get_gacha_log_data(self, user_id: int, player_id: int, authkey: str) -> int:
async def get_gacha_log_data(self, user_id: int, player_id: int, authkey: str, is_lazy: bool) -> int:
"""使用authkey获取跃迁记录数据,并合并旧数据
:param user_id: 用户id
:param player_id: 玩家id
:param authkey: authkey
:param is_lazy: 是否快速导入
:return: 更新结果
"""
new_num = 0
gacha_log, _ = await self.load_history_info(str(user_id), str(player_id))
# 将唯一 id 放入临时数据中,加快查找速度
temp_id_data = {pool_name: {i.id: i for i in pool_data} for pool_name, pool_data in gacha_log.item_list.items()}
temp_id_data = {pool_name: [i.id for i in pool_data] for pool_name, pool_data in gacha_log.item_list.items()}
client = self.get_game_client(player_id)
try:
for pool_id, pool_name in GACHA_TYPE_LIST.items():
wish_history = await client.wish_history(pool_id.value, authkey=authkey)
if pool_name not in temp_id_data:
temp_id_data[pool_name] = []
if pool_name not in gacha_log.item_list:
gacha_log.item_list[pool_name] = []
min_id = 0
if is_lazy and gacha_log.item_list[pool_name]:
with contextlib.suppress(ValueError):
min_id = int(gacha_log.item_list[pool_name][-1].id)

wish_history = await client.wish_history(pool_id.value, authkey=authkey, min_id=min_id)

if not is_lazy:
min_id = wish_history[0].id if wish_history else min_id
if min_id:
gacha_log.item_list[pool_name][:] = filter(
lambda i: int(i.id) < min_id, gacha_log.item_list[pool_name]
)
for data in wish_history:
item = GachaItem(
id=str(data.id),
Expand All @@ -280,28 +298,15 @@ async def get_gacha_log_data(self, user_id: int, player_id: int, authkey: str) -
item_id=str(data.item_id),
item_type=data.type,
rank_type=str(data.rarity),
time=datetime.datetime(
data.time.year,
data.time.month,
data.time.day,
data.time.hour,
data.time.minute,
data.time.second,
),
time=data.time,
)

if pool_name not in temp_id_data:
temp_id_data[pool_name] = {}
if pool_name not in gacha_log.item_list:
gacha_log.item_list[pool_name] = []
if item.id not in temp_id_data[pool_name].keys():
if item.id not in temp_id_data[pool_name] or (not is_lazy and min_id):
gacha_log.item_list[pool_name].append(item)
temp_id_data[pool_name][item.id] = item
temp_id_data[pool_name].append(item.id)
new_num += 1
else:
old_item: GachaItem = temp_id_data[pool_name][item.id]
old_item.gacha_id = item.gacha_id
old_item.item_id = item.item_id

await asyncio.sleep(1)
except AuthkeyTimeout as exc:
raise GachaLogAuthkeyTimeout from exc
except InvalidAuthkey as exc:
Expand All @@ -310,11 +315,15 @@ async def get_gacha_log_data(self, user_id: int, player_id: int, authkey: str) -
await client.shutdown()
for i in gacha_log.item_list.values():
i.sort(key=lambda x: (x.time, x.id))
gacha_log.update_time = datetime.datetime.now()
gacha_log.update_time = add_timezone(datetime.datetime.now())
gacha_log.import_type = ImportType.PaiGram.value
await self.save_gacha_log_info(str(user_id), str(player_id), gacha_log)
return new_num

@staticmethod
def format_time(time: str) -> datetime.datetime:
return add_timezone(datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S"))

@staticmethod
def check_avatar_up(name: str, gacha_time: datetime.datetime) -> bool:
if name in {"姬子", "瓦尔特", "布洛妮娅", "杰帕德", "克拉拉", "彦卿", "白露"}:
Expand Down Expand Up @@ -547,8 +556,8 @@ async def get_analysis_data(
self, gacha_log: "GachaLogInfo", pool: StarRailBannerType, assets: Optional["AssetsService"]
):
"""
获取抽卡记录分析数据
:param gacha_log: 抽卡记录
获取跃迁记录分析数据
:param gacha_log: 跃迁记录
:param pool: 池子类型
:param assets: 资源服务
:return: 分析数据
Expand Down
4 changes: 2 additions & 2 deletions modules/gacha_log/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ class GachaLogMigrate(IMigrateData, GachaLog):
old_uid_list: List[int]

async def migrate_data_msg(self) -> str:
return f"{len(self.old_uid_list)} 个账号的抽卡记录数据"
return f"{len(self.old_uid_list)} 个账号的跃迁记录数据"

async def migrate_data(self) -> bool:
uid_list = []
for uid in self.old_uid_list:
if not await self.move_history_info(str(self.old_user_id), str(uid), str(self.new_user_id)):
uid_list.append(str(uid))
if uid_list:
raise MigrateDataException(f"抽卡记录数据迁移失败:uid {','.join(uid_list)}")
raise MigrateDataException(f"跃迁记录数据迁移失败:uid {','.join(uid_list)}")
return True

@classmethod
Expand Down
16 changes: 9 additions & 7 deletions modules/gacha_log/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from enum import Enum
from typing import Any, Dict, List, Union

from pydantic import field_validator, BaseModel
from pydantic import field_validator

from simnet.models.base import APIModel as BaseModel, DateTimeField, add_timezone

from metadata.shortname import not_real_roles, roleToId, lightConeToId
from modules.gacha_log.const import SRGF_VERSION
Expand All @@ -21,15 +23,15 @@ class FiveStarItem(BaseModel):
type: str
isUp: bool
isBig: bool
time: datetime.datetime
time: DateTimeField


class FourStarItem(BaseModel):
name: str
icon: str
count: int
type: str
time: datetime.datetime
time: DateTimeField


class GachaItem(BaseModel):
Expand All @@ -40,7 +42,7 @@ class GachaItem(BaseModel):
item_id: str = ""
item_type: str
rank_type: str
time: datetime.datetime
time: DateTimeField

@field_validator("name")
@classmethod
Expand Down Expand Up @@ -75,7 +77,7 @@ def check_rank_type(cls, rank):
class GachaLogInfo(BaseModel):
user_id: str
uid: str
update_time: datetime.datetime
update_time: DateTimeField
import_type: str = ""
item_list: Dict[str, List[GachaItem]] = {
"角色跃迁": [],
Expand All @@ -100,8 +102,8 @@ def __init__(self, five: List[str], four: List[str], name: str, to: str, **kwarg
self.four = four
self.from_ = kwargs.get("from")
self.to = to
self.from_time = datetime.datetime.strptime(self.from_, "%Y-%m-%d %H:%M:%S")
self.to_time = datetime.datetime.strptime(self.to, "%Y-%m-%d %H:%M:%S")
self.from_time = add_timezone(datetime.datetime.strptime(self.from_, "%Y-%m-%d %H:%M:%S"))
self.to_time = add_timezone(datetime.datetime.strptime(self.to, "%Y-%m-%d %H:%M:%S"))
self.start = self.from_time
self.start_init = False
self.end = self.to_time
Expand Down
4 changes: 2 additions & 2 deletions modules/gacha_log/online_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class GachaLogWebConfig(Settings):
"""抽卡记录在线查询配置"""
"""跃迁记录在线查询配置"""

url: Optional[str] = ""
token: Optional[str] = ""
Expand All @@ -25,7 +25,7 @@ class GachaLogWebConfig(Settings):


class GachaLogOnlineView:
"""抽卡记录在线查询"""
"""跃迁记录在线查询"""

gacha_log_path: Path

Expand Down
10 changes: 5 additions & 5 deletions modules/gacha_log/ranks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@


class GachaLogError(Exception):
"""抽卡记录异常"""
"""跃迁记录异常"""


class GachaLogRanks:
"""抽卡记录排行榜"""
"""跃迁记录排行榜"""

gacha_log_path: Path
ITEM_LIST_MAP = {
Expand Down Expand Up @@ -62,8 +62,8 @@ async def get_analysis_data(
self, gacha_log: "GachaLogInfo", pool: StarRailBannerType, assets: Optional["AssetsService"]
):
"""
获取抽卡记录分析数据
:param gacha_log: 抽卡记录
获取跃迁记录分析数据
:param gacha_log: 跃迁记录
:param pool: 池子类型
:param assets: 资源服务
:return: 分析数据
Expand All @@ -87,7 +87,7 @@ async def recount_one_data(self, file_path: Path) -> List[GachaLogRank]:
try:
gacha_log = GachaLogInfo.parse_obj(await self.load_json(file_path))
if gacha_log.get_import_type != ImportType.PaiGram:
raise GachaLogError("不支持的抽卡记录类型")
raise GachaLogError("不支持的跃迁记录类型")
except ValueError as e:
raise GachaLogError from e
player_id = int(gacha_log.uid)
Expand Down
2 changes: 1 addition & 1 deletion plugins/admin/set_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def set_command(self, update: "Update", context: "ContextTypes.DEFAULT_TYP
group_command = [
BotCommand("help", "帮助"),
BotCommand("warp_log", "查看跃迁记录"),
BotCommand("warp_log_online_view", "抽卡记录在线浏览"),
BotCommand("warp_log_online_view", "跃迁记录在线浏览"),
BotCommand("warp_log_rank", "抽卡排行榜"),
BotCommand("warp_waiting_list", "未复刻列表"),
BotCommand("action_log", "查询登录记录"),
Expand Down
Loading

0 comments on commit 4af2cab

Please sign in to comment.