Skip to content

Commit

Permalink
✨ 导出UIGFv4格式抽卡记录 (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
KimigaiiWuyi committed Aug 17, 2024
1 parent 4b88d5f commit bbc72ca
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 51 deletions.
23 changes: 20 additions & 3 deletions GenshinUID/genshinuid_gachalog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,30 @@ async def send_export_gacha_info(bot: Bot, ev: Event):
uid = await GsBind.get_uid_by_game(ev.user_id, ev.bot_id)
if uid is None:
return await bot.send(UID_HINT)
export = await export_gachalogs(uid)

resp = await bot.receive_resp(
'请问你要导出哪个版本的UIGF文件?\n可选:v2、v4',
)

version = '2'
if resp is not None:
msg = resp.text.strip()
if msg in ['v2', 'v4', '2', '4']:
version = msg.replace('v', '')
else:
return await bot.send(
'❌请输入正确的版本号噢!(可选:v2、v4)\n本次导出终止...'
)
await bot.send(f'即将为你导出UIGFv{version}文件,请耐心等待...')

export = await export_gachalogs(uid, version)
if export['retcode'] == 'ok':
file_name = export['name']
file_path = export['url']
return await bot.send(MessageSegment.file(file_path, file_name))
await bot.send(MessageSegment.file(file_path, file_name))
await bot.send(f'✅导出UIGFv{version}文件成功!')
else:
return await bot.send('导出抽卡记录失败...')
await bot.send('导出抽卡记录失败...')


@sv_import_lelaer_gachalog.on_fullmatch(('从小助手导入抽卡记录'))
Expand Down
114 changes: 87 additions & 27 deletions GenshinUID/genshinuid_gachalog/export_and_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
from datetime import datetime
from typing import Dict, List

import aiofiles
from httpx import get
from gsuid_core.logger import logger

from ..version import GenshinUID_version
from ..utils.resource.RESOURCE_PATH import PLAYER_PATH
from ..utils.map.GS_MAP_PATH import charList, weaponList
from ..utils.map.GS_MAP_PATH import charList, weaponList, weaponId2Name_data
from .get_gachalogs import NULL_GACHA_LOG, save_gachalogs, all_gacha_type_name

INT_TO_TYPE = {
Expand Down Expand Up @@ -56,7 +58,7 @@ async def import_gachalogs(history_url: str, type: str, uid: str) -> str:
elif type == 'json':
history_data = json.loads(history_url)
if history_data.get('code') == 300:
return "[提瓦特小助手]抽卡记录不存在"
return '[提瓦特小助手]抽卡记录不存在'
else:
data_bytes = base64.b64decode(history_url)
try:
Expand Down Expand Up @@ -98,7 +100,8 @@ async def import_gachalogs(history_url: str, type: str, uid: str) -> str:
return '请传入正确的UIGF文件!'


async def export_gachalogs(uid: str) -> dict:
async def export_gachalogs(uid: str, version: str) -> dict:
logger.info(f'[导出抽卡记录] 版本v{version}开始执行...')
path = PLAYER_PATH / uid
if not path.exists():
path.mkdir(parents=True, exist_ok=True)
Expand All @@ -110,37 +113,94 @@ async def export_gachalogs(uid: str) -> dict:
# 抽卡记录json路径
gachalogs_path = path / 'gacha_logs.json'
if gachalogs_path.exists():
with open(gachalogs_path, "r", encoding='UTF-8') as f:
raw_data = json.load(f)
result = {
'info': {
'uid': uid,
'lang': 'zh-cn',
'export_time': current_time,
'export_app': 'GenshinUID',
'export_app_version': '4.0',
'export_timestamp': round(now.timestamp()),
'uigf_version': 'v2.2',
},
'list': [],
}
for i in all_gacha_type_name:
for item in raw_data['data'][i]:
if item['gacha_type'] == '400':
item['uigf_gacha_type'] = '301'
else:
item['uigf_gacha_type'] = item['gacha_type']
result['list'].append(item)
async with aiofiles.open(gachalogs_path, 'r', encoding='UTF-8') as f:
raw_data = json.loads(await f.read())

if version == '2':
result = {
'info': {
'uid': uid,
'lang': 'zh-cn',
'export_time': current_time,
'export_app': 'GenshinUID',
'export_app_version': GenshinUID_version,
'export_timestamp': round(now.timestamp()),
'uigf_version': 'v2.2',
},
'list': [],
}
for i in all_gacha_type_name:
for item in raw_data['data'][i]:
if item['gacha_type'] == '400':
item['uigf_gacha_type'] = '301'
else:
item['uigf_gacha_type'] = item['gacha_type']
result['list'].append(item)
else:
result = {
'info': {
'export_time': current_time,
'export_app': 'GenshinUID',
'export_app_version': GenshinUID_version,
'export_timestamp': round(now.timestamp()),
'version': 'v4.0',
},
'hk4e': [],
}
rog = {'uid': uid, 'timezone': 0, 'list': []}

weaponNmae2Id = {}
charNmae2Id = {}
for i in weaponId2Name_data:
weaponNmae2Id[weaponId2Name_data[i]] = i

for i in charList:
charNmae2Id[charList[i]['CHS']] = i

for i in all_gacha_type_name:
# i: 新手祈愿, 常驻祈愿, ...
for item in raw_data['data'][i]:
new_item = {
'gacha_type': item['gacha_type'],
'time': item['time'],
'id': item['id'],
}
# 角色祈愿2 也被转为角色祈愿301
if item['gacha_type'] == '400':
new_item['uigf_gacha_type'] = '301'
else:
new_item['uigf_gacha_type'] = item['gacha_type']

if item['item_type'] == '武器':
new_item['item_id'] = int(weaponNmae2Id[item['name']])
else:
new_item['item_id'] = int(charNmae2Id[item['name']])

rog['list'].append(new_item)

result['hk4e'].append(rog)

# 保存文件
with open(path / f'UIGF_{uid}.json', 'w', encoding='UTF-8') as file:
json.dump(result, file, ensure_ascii=False)
logger.info(f'[导出抽卡记录] 版本v{version}开始保存文件...')
async with aiofiles.open(
path / f'UIGF_v{version}_{uid}.json', 'w', encoding='UTF-8'
) as file:
await file.write(
json.dumps(
result,
ensure_ascii=False,
indent=4,
)
)
logger.success('[导出抽卡记录] 导出成功!')
im = {
'retcode': 'ok',
'data': '导出成功!',
'name': f'UIGF_{uid}.json',
'name': f'UIGF_v{version}_{uid}.json',
'url': str((path / f'UIGF_{uid}.json').absolute()),
}
else:
logger.error('[导出抽卡记录] 没有找到抽卡记录!')
im = {
'retcode': 'error',
'data': '你还没有抽卡记录可以导出!',
Expand Down
46 changes: 27 additions & 19 deletions GenshinUID/genshinuid_resin/notice.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,56 +55,65 @@ async def all_check(
uid: str,
) -> Dict[str, Dict[str, Dict]]:
for mode in NOTICE.keys():
_check = await check(
mode,
raw_data,
push_data[f'{mode}_value'],
)

# 检查条件
if push_data[f'{mode}_is_push'] == 'on':
if not gsconfig.get_config('CrazyNotice').data:
if not await check(mode, raw_data, push_data[f'{mode}_value']):
if not _check:
await GsPush.update_data_by_uid(
uid, bot_id, None, **{f'{mode}_is_push': 'off'}
)
continue

# 准备推送
_check = await check(mode, raw_data, push_data[f'{mode}_value'])
if _check:
if push_data[f'{mode}_push'] == 'off':
pass
# on 推送到私聊
else:
notice = NOTICE[mode]
if isinstance(_check, int):
notice += f'(当前值: {_check})'

# 初始化
if bot_id not in msg_dict:
msg_dict[bot_id] = {'direct': {}, 'group': {}}
direct_data = msg_dict[bot_id]['direct']
group_data = msg_dict[bot_id]['group']

# on 推送到私聊
if push_data[f'{mode}_push'] == 'on':
# 添加私聊信息
if user_id not in msg_dict[bot_id]['direct']:
msg_dict[bot_id]['direct'][user_id] = notice
if user_id not in direct_data:
direct_data[user_id] = notice
else:
msg_dict[bot_id]['direct'][user_id] += notice
await GsPush.update_data_by_uid(
uid, bot_id, None, **{f'{mode}_is_push': 'on'}
)
direct_data[user_id] += notice
# 群号推送到群聊
else:
# 初始化
gid = push_data[f'{mode}_push']
if gid not in msg_dict[bot_id]['group']:
msg_dict[bot_id]['group'][gid] = {}
if gid not in group_data:
group_data[gid] = {}

if user_id not in msg_dict[bot_id]['group'][gid]:
msg_dict[bot_id]['group'][gid][user_id] = notice
if user_id not in group_data[gid]:
group_data[gid][user_id] = notice
else:
msg_dict[bot_id]['group'][gid][user_id] += notice
await GsPush.update_data_by_uid(
uid, bot_id, None, **{f'{mode}_is_push': 'on'}
)
group_data[gid][user_id] += notice

await GsPush.update_data_by_uid(
uid, bot_id, None, **{f'{mode}_is_push': 'on'}
)
return msg_dict


async def check(
mode: str, data: DailyNoteData, limit: int
mode: str,
data: DailyNoteData,
limit: int,
) -> Union[bool, int]:
if mode == 'coin':
if data['current_home_coin'] >= limit:
Expand Down Expand Up @@ -136,4 +145,3 @@ async def check(
return True
return False
return False
return False
1 change: 1 addition & 0 deletions GenshinUID/tools/data_to_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ async def artifact2attrJson() -> None:
async def restore_hakush_data():
data = await get_hakush_char_list()
data2 = await get_hakush_weapon_list()

with open(MAP_PATH / charList_fileName, 'w', encoding='UTF-8') as f:
json.dump(data, f, ensure_ascii=False)

Expand Down
4 changes: 2 additions & 2 deletions GenshinUID/utils/map/GS_MAP_PATH.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class TS(TypedDict):


with open(MAP / charList_fileName, 'r', encoding='UTF-8') as f:
charList = msgjson.decode(f.read(), type=Dict[str, Dict])
charList = msgjson.decode(f.read(), type=Dict)

with open(MAP / weaponList_fileName, 'r', encoding='UTF-8') as f:
weaponList = msgjson.decode(f.read(), type=Dict[str, Dict[str, str]])
weaponList = msgjson.decode(f.read(), type=Dict)

with open(MAP / avatarId2Name_fileName, 'r', encoding='UTF-8') as f:
avatarId2Name = msgjson.decode(f.read(), type=Dict[str, str])
Expand Down

0 comments on commit bbc72ca

Please sign in to comment.