From 02d7b2ee58f39979387e63eae33edd6b62ef6a27 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 30 Jul 2024 16:20:31 +0800 Subject: [PATCH] :sparkles: Support bind mult players by one cookie --- gram_core | 2 +- plugins/account/cookies.py | 61 +++++++++++++++++++++++++++++--------- plugins/account/players.py | 12 +++----- 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/gram_core b/gram_core index fdaeaa1..fb76a75 160000 --- a/gram_core +++ b/gram_core @@ -1 +1 @@ -Subproject commit fdaeaa19e918d3046a3278feff9ffb1f52c56d39 +Subproject commit fb76a75ee1d10eb3c7c52abccde7f15a94726386 diff --git a/plugins/account/cookies.py b/plugins/account/cookies.py index 24e986d..c0296ea 100644 --- a/plugins/account/cookies.py +++ b/plugins/account/cookies.py @@ -1,11 +1,12 @@ +import contextlib from datetime import datetime -from typing import Dict, Optional +from typing import Dict, Optional, List from arkowrapper import ArkoWrapper from simnet import StarRailClient, Region from simnet.errors import DataNotPublic, InvalidCookies, BadRequest as SimnetBadRequest from simnet.models.lab.record import Account -from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, TelegramObject, Update +from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, TelegramObject, Update, Message from telegram.ext import CallbackContext, ConversationHandler, filters from telegram.helpers import escape_markdown @@ -40,6 +41,7 @@ class AccountCookiesPluginData(TelegramObject): account_id: int = 0 # player_id: int = 0 starrail_account: Optional[Account] = None + starrail_accounts: List[Account] = [] device: Optional[AccountCookiesPluginDeviceData] = None def reset(self): @@ -47,10 +49,11 @@ class AccountCookiesPluginData(TelegramObject): self.cookies = {} self.account_id = 0 self.starrail_account = None + self.starrail_accounts = [] self.device = None -CHECK_SERVER, INPUT_COOKIES, COMMAND_RESULT = range(10100, 10103) +CHECK_SERVER, INPUT_COOKIES, INPUT_PLAYERS, COMMAND_RESULT = range(10100, 10104) class AccountCookiesPlugin(Plugin.Conversation): @@ -334,17 +337,20 @@ class AccountCookiesPlugin(Plugin.Conversation): if account_cookies_plugin_data.account_id is None: await message.reply_text("无法获取账号ID,请检查Cookie是否正确或请稍后重试") return ConversationHandler.END - starrail_account: Optional[Account] = None - level: int = 0 - # todo : 多账号绑定 - for temp in starrail_accounts: - if temp.level >= level: # 获取账号等级最高的 - level = temp.level - starrail_account = temp - if starrail_account is None: - await message.reply_text("未找到星穹铁道账号,请确认账号信息无误。") + if not starrail_accounts: + await message.reply_text("未找到游戏账号,请确认账号信息无误。") return ConversationHandler.END - account_cookies_plugin_data.starrail_account = starrail_account + account_cookies_plugin_data.cookies = cookies.to_dict() + account_cookies_plugin_data.starrail_accounts = starrail_accounts + await self.send_choose_players_message(message, starrail_accounts) + return INPUT_PLAYERS + + async def choose_to_save_player( + self, update: "Update", account_cookies_plugin_data: AccountCookiesPluginData + ) -> int: + user = update.effective_user + message = update.effective_message + starrail_account = account_cookies_plugin_data.starrail_account player_info = await self.players_service.get( user.id, player_id=starrail_account.uid, region=account_cookies_plugin_data.region ) @@ -371,9 +377,36 @@ class AccountCookiesPlugin(Plugin.Conversation): f"服务器名称:`{starrail_account.server_name}`\n" ) await message.reply_markdown_v2(text, reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)) - account_cookies_plugin_data.cookies = cookies.to_dict() return COMMAND_RESULT + @staticmethod + async def send_choose_players_message(message: "Message", genshin_accounts: List["Account"]): + text = "请选择要绑定的角色!" + reply_keyboard = [[f"{escape_markdown(x.nickname, version=2)} - {x.uid}"] for x in genshin_accounts] + reply_keyboard.append(["退出"]) + await message.reply_text(text, reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)) + + @conversation.state(state=INPUT_PLAYERS) + @handler.message(filters=filters.TEXT & ~filters.COMMAND, block=False) + async def input_players(self, update: Update, context: CallbackContext) -> int: + message = update.effective_message + account_cookies_plugin_data: AccountCookiesPluginData = context.chat_data.get("account_cookies_plugin_data") + if message.text == "退出": + await message.reply_text("退出任务", reply_markup=ReplyKeyboardRemove()) + return ConversationHandler.END + uid = None + with contextlib.suppress(ValueError, IndexError): + uid = int(message.text.split("-")[-1].strip()) + if not uid: + await message.reply_text("选择错误,请重新选择") + return INPUT_PLAYERS + starrail_account = next((x for x in account_cookies_plugin_data.starrail_accounts if x.uid == uid), None) + if not starrail_account: + await message.reply_text("选择错误,请重新选择") + return INPUT_PLAYERS + account_cookies_plugin_data.starrail_account = starrail_account + return await self.choose_to_save_player(update, account_cookies_plugin_data) + async def update_devices(self, account_id: int, device: AccountCookiesPluginDeviceData): if not device: return diff --git a/plugins/account/players.py b/plugins/account/players.py index cd29503..8f1e446 100644 --- a/plugins/account/players.py +++ b/plugins/account/players.py @@ -332,13 +332,7 @@ class PlayersManagesPlugin(Plugin): await callback_query.edit_message_text(f"账号 {player_id} 信息未找到") return - main_player = await self.players_service.get(user.id, is_chosen=True) - if main_player and player.id != main_player.id: - main_player.is_chosen = False - await self.players_service.update(main_player) - - player.is_chosen = True - await self.players_service.update(player) + await self.players_service.set_player_to_main(user_id, player_id) buttons = [ [ @@ -386,7 +380,9 @@ class PlayersManagesPlugin(Plugin): await self.players_service.delete(player) cookies = await self.cookies_service.get(player.user_id, player.account_id, player.region) if cookies: - await self.cookies_service.delete(cookies) + players = await self.players_service.get_all_by_account_id(cookies.account_id, cookies.region) + if len(players) == 0: + await self.cookies_service.delete(cookies) player_info = await self.player_info_service.get_form_sql(player) if player_info is not None: await self.player_info_service.delete(player_info)