Support bind mult players by one cookie

This commit is contained in:
xtaodada 2024-07-30 16:20:31 +08:00
parent b9117f00a5
commit 02d7b2ee58
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
3 changed files with 52 additions and 23 deletions

@ -1 +1 @@
Subproject commit fdaeaa19e918d3046a3278feff9ffb1f52c56d39 Subproject commit fb76a75ee1d10eb3c7c52abccde7f15a94726386

View File

@ -1,11 +1,12 @@
import contextlib
from datetime import datetime from datetime import datetime
from typing import Dict, Optional from typing import Dict, Optional, List
from arkowrapper import ArkoWrapper from arkowrapper import ArkoWrapper
from simnet import StarRailClient, Region from simnet import StarRailClient, Region
from simnet.errors import DataNotPublic, InvalidCookies, BadRequest as SimnetBadRequest from simnet.errors import DataNotPublic, InvalidCookies, BadRequest as SimnetBadRequest
from simnet.models.lab.record import Account 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.ext import CallbackContext, ConversationHandler, filters
from telegram.helpers import escape_markdown from telegram.helpers import escape_markdown
@ -40,6 +41,7 @@ class AccountCookiesPluginData(TelegramObject):
account_id: int = 0 account_id: int = 0
# player_id: int = 0 # player_id: int = 0
starrail_account: Optional[Account] = None starrail_account: Optional[Account] = None
starrail_accounts: List[Account] = []
device: Optional[AccountCookiesPluginDeviceData] = None device: Optional[AccountCookiesPluginDeviceData] = None
def reset(self): def reset(self):
@ -47,10 +49,11 @@ class AccountCookiesPluginData(TelegramObject):
self.cookies = {} self.cookies = {}
self.account_id = 0 self.account_id = 0
self.starrail_account = None self.starrail_account = None
self.starrail_accounts = []
self.device = None 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): class AccountCookiesPlugin(Plugin.Conversation):
@ -334,17 +337,20 @@ class AccountCookiesPlugin(Plugin.Conversation):
if account_cookies_plugin_data.account_id is None: if account_cookies_plugin_data.account_id is None:
await message.reply_text("无法获取账号ID请检查Cookie是否正确或请稍后重试") await message.reply_text("无法获取账号ID请检查Cookie是否正确或请稍后重试")
return ConversationHandler.END return ConversationHandler.END
starrail_account: Optional[Account] = None if not starrail_accounts:
level: int = 0 await message.reply_text("未找到游戏账号,请确认账号信息无误。")
# 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("未找到星穹铁道账号,请确认账号信息无误。")
return ConversationHandler.END 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( player_info = await self.players_service.get(
user.id, player_id=starrail_account.uid, region=account_cookies_plugin_data.region 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" f"服务器名称:`{starrail_account.server_name}`\n"
) )
await message.reply_markdown_v2(text, reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)) 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 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): async def update_devices(self, account_id: int, device: AccountCookiesPluginDeviceData):
if not device: if not device:
return return

View File

@ -332,13 +332,7 @@ class PlayersManagesPlugin(Plugin):
await callback_query.edit_message_text(f"账号 {player_id} 信息未找到") await callback_query.edit_message_text(f"账号 {player_id} 信息未找到")
return return
main_player = await self.players_service.get(user.id, is_chosen=True) await self.players_service.set_player_to_main(user_id, player_id)
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)
buttons = [ buttons = [
[ [
@ -386,6 +380,8 @@ class PlayersManagesPlugin(Plugin):
await self.players_service.delete(player) await self.players_service.delete(player)
cookies = await self.cookies_service.get(player.user_id, player.account_id, player.region) cookies = await self.cookies_service.get(player.user_id, player.account_id, player.region)
if cookies: if 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) await self.cookies_service.delete(cookies)
player_info = await self.player_info_service.get_form_sql(player) player_info = await self.player_info_service.get_form_sql(player)
if player_info is not None: if player_info is not None: