Skip to content

Commit

Permalink
Merge pull request #74 from Cypas/dev_cypas
Browse files Browse the repository at this point in the history
fix:BX计算相关修复;perf:优化主人端sync_stat_ink显示效果
  • Loading branch information
Cypas authored Jun 30, 2024
2 parents 1827853 + 55a9377 commit 65da315
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 75 deletions.
53 changes: 31 additions & 22 deletions nonebot_plugin_splatoon3_nso/handle/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
dict_get_or_set_user_info
from ..utils import get_msg_id
from ..utils.bot import *
from nonebot import logger

global_admin_session_token: str = ""

Expand Down Expand Up @@ -44,9 +45,15 @@ async def admin_cmd(bot: Bot, event: Event, args: Message = CommandArg()):
continue
msg = "push推送被管理员强制关闭,大概率是需要重启bot,请稍等几分钟完成重启后,重新对bot发送/push 命令\n"
# 获取统计数据
st_msg, _ = close_push(u.platform, u.user_id)
user_bot, user_event, st_msg, _ = close_push(u.platform, u.user_id)
msg += st_msg
await notify_to_private(u.platform, u.user_id, msg)
if user_bot and user_event:
try:
await bot_send(user_bot, user_event, message=msg)
except Exception as e:
msg_id = get_msg_id(u.platform, u.user_id)
logger.warning(
f'msg_id:{msg_id} private notice error: {e}')
push_cnt += 1
time.sleep(0.5)

Expand Down Expand Up @@ -108,30 +115,31 @@ async def admin_cmd(bot: Bot, event: Event, args: Message = CommandArg()):
else:
platform = bot.adapter.get_name()
my_user_id = event.get_user_id()
dict_get_or_set_user_info(platform, my_user_id, session_token=global_admin_session_token, access_token="",
dict_get_or_set_user_info(platform, my_user_id, session_token=global_admin_session_token,
access_token="",
g_token="", bullet_token="")
await bot_send(bot, event, message=f"token已恢复")

case "help":
"""指令目录"""
msg = "所有命令都需要加上/admin 前缀\n"\
"get_push 获取当前push统计\n"\
"close_push 关闭当前全部push\n"\
"get_x_player 获取x赛top\n"\
"get_event_top 获取活动top\n"\
"clean_cache 清理数据库缓存\n"\
"set_report 写日报\n"\
"send_report 发送日报\n"\
"get_user_friends 获取全部用户好友列表\n"\
"refresh_token 刷新全部缓存用户token\n"\
"update_s3si_ts 更新s3sti脚本\n"\
"sync_stat_ink 开始全部用户同步stat\n"\
"clean_s3s_cache 清空s3s缓存文件夹\n"\
"clean_user_info_dict 清理用户数据缓存字典以及client\n"\
"status 当前缓存用户状态以及ss截图调用情况\n"\
"kook_leave {guild_id} kook离开服务器\n"\
"copy_token {user_id} 复制同平台某用户token,便于调试\n"\
"restore_token 还原自身本来token\n"
msg = "所有命令都需要加上/admin 前缀\n" \
"get_push 获取当前push统计\n" \
"close_push 关闭当前全部push\n" \
"get_x_player 获取x赛top\n" \
"get_event_top 获取活动top\n" \
"clean_cache 清理数据库缓存\n" \
"set_report 写日报\n" \
"send_report 发送日报\n" \
"get_user_friends 获取全部用户好友列表\n" \
"refresh_token 刷新全部缓存用户token\n" \
"update_s3si_ts 更新s3sti脚本\n" \
"sync_stat_ink 开始全部用户同步stat\n" \
"clean_s3s_cache 清空s3s缓存文件夹\n" \
"clean_user_info_dict 清理用户数据缓存字典以及client\n" \
"status 当前缓存用户状态以及ss截图调用情况\n" \
"kook_leave {guild_id} kook离开服务器\n" \
"copy_token {user_id} 复制同平台某用户token,便于调试\n" \
"restore_token 还原自身本来token\n"
await bot_send(bot, event, message=msg)

if plain_text.startswith("kook_leave"):
Expand Down Expand Up @@ -164,6 +172,7 @@ async def admin_cmd(bot: Bot, event: Event, args: Message = CommandArg()):
# 设置别人的值
dict_get_or_set_user_info(platform, my_user_id, session_token=user.session_token, access_token="",
g_token="", bullet_token="")
await bot_send(bot, event, message=f"已复制账号 db_id:{user.id},msg_id:{get_msg_id(user.platform, user.user_id)},\ngame_name:{user.game_name}\n还原:/admin restore_token")
await bot_send(bot, event,
message=f"已复制账号 db_id:{user.id},msg_id:{get_msg_id(user.platform, user.user_id)},\ngame_name:{user.game_name}\n还原:/admin restore_token")
else:
await bot_send(bot, event, message=f"无效命令, lens:{len(args)}")
108 changes: 73 additions & 35 deletions nonebot_plugin_splatoon3_nso/handle/cron/stat_ink.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import asyncio
import base64
import time
from datetime import datetime as dt
import os
import json
import subprocess


from ...data.db_sqlite import UserTable
from ...config import plugin_config
from ...s3s.iksm import F_GEN_URL_2
Expand All @@ -14,6 +14,7 @@
from ...data.data_source import model_get_all_stat_user
from ..send_msg import notify_to_private, report_notify_to_channel, cron_notify_to_channel
from .utils import user_remove_duplicates, cron_logger
from ...utils.bot import Kook_ActionFailed


async def sync_stat_ink():
Expand All @@ -30,41 +31,70 @@ async def sync_stat_ink():
# 去重
db_users = user_remove_duplicates(db_users)

sync_count = 0
_pool = 4
complete_cnt = 0
upload_cnt = 0
error_cnt = 0
else_error_cnt = 0
notice_error_cnt = 0
_pool = 6
for i in range(0, len(db_users), _pool):
pool_users_list = db_users[i:i + _pool]
tasks = [sync_stat_ink_func(db_user) for db_user in pool_users_list]
res = await asyncio.gather(*tasks)
for r in res:
if r:
sync_count += 1
is_complete, is_upload, is_error, is_notice_error, is_else_error = r
if is_complete:
complete_cnt += 1
if is_upload:
upload_cnt += 1
if is_error:
error_cnt += 1
if is_notice_error:
notice_error_cnt += 1
if is_else_error:
else_error_cnt += 1
# 耗时
str_time = convert_td(dt.utcnow() - t)
cron_msg = (f"sync_stat_ink end: {str_time}\n"
f"sync_count: {sync_count}")
f"complete_cnt: {complete_cnt}, upload_cnt: {upload_cnt}\n"
f"error_cnt: {error_cnt},notice_error_cnt: {notice_error_cnt},else_error_cnt: {else_error_cnt}")
cron_logger.info(cron_msg)
await cron_notify_to_channel("sync_stat_ink", "end", f"耗时:{str_time}\n同步数量: {sync_count}")
notice_msg = (f"耗时:{str_time}\n完成: {complete_cnt},同步: {upload_cnt}\n"
f"错误: {error_cnt},通知错误: {notice_error_cnt},配置错误: {else_error_cnt}")
await cron_notify_to_channel("sync_stat_ink", "end", notice_msg)


async def sync_stat_ink_func(db_user: UserTable):
"""同步stat.ink"""
is_complete, is_upload, is_error, is_else_error, is_notice_error = False, False, False, False, False

cron_logger.debug(f"get user: {db_user.user_name}, have stat_key: {db_user.stat_key}")

msg = get_post_stat_msg(db_user)
res = get_post_stat_msg(db_user)
if not isinstance(res, tuple):
is_else_error = True
return is_complete, is_upload, is_error, is_notice_error, is_else_error

msg, error_msg = res
is_complete = True
if msg:
is_upload = True
if db_user.stat_notify:
cron_logger.debug(f"{db_user.id}, {db_user.user_name}, {msg}")
# 通知到频道
# await report_notify_to_channel(db_user.platform, db_user.user_id, msg, _type="job")
# 通知到私信
msg += "\n/stat_notify close 关闭stat.ink同步情况推送"
await notify_to_private(db_user.platform, db_user.user_id, msg)
return True
else:
return False
try:
await notify_to_private(db_user.platform, db_user.user_id, msg)
except Exception as e:
cron_logger.error(f"db_user_id:{db_user.user_id} private notice error: {e}")
is_notice_error = True

if error_msg:
is_error = True

return is_complete, is_upload, is_error, is_notice_error, is_else_error


def get_post_stat_msg(db_user):
Expand All @@ -77,26 +107,27 @@ def get_post_stat_msg(db_user):
res = exported_to_stat_ink(db_user.id, db_user.session_token, db_user.stat_key, g_token=db_user.g_token,
bullet_token=db_user.bullet_token)

if not res:
if not isinstance(res, tuple):
return
msg = ""
battle_cnt, coop_cnt, url, error_msg = res
if battle_cnt or coop_cnt:
msg = "> Exported"
if battle_cnt:
msg += f" {battle_cnt} battles"
if coop_cnt:
msg += f" {coop_cnt} jobs"

battle_cnt, coop_cnt, url = res
msg = "> Exported"
if battle_cnt:
msg += f" {battle_cnt} battles"
if coop_cnt:
msg += f" {coop_cnt} jobs"

if battle_cnt and not coop_cnt:
url += "/spl3"
elif coop_cnt and not battle_cnt:
url += "/salmon3"
msg += f" to\n{url}\n\n"
if battle_cnt and not coop_cnt:
url += "/spl3"
elif coop_cnt and not battle_cnt:
url += "/salmon3"
msg += f" to\n{url}\n\n"

log_msg = msg.replace("\n", "")
cron_logger.info(f"{db_user.id}, {db_user.user_name}, {log_msg}")
log_msg = msg.replace("\n", "")
cron_logger.info(f"{db_user.id}, {db_user.user_name}, {log_msg}")

return msg
return msg, error_msg


async def update_s3si_ts():
Expand Down Expand Up @@ -210,15 +241,23 @@ def exported_to_stat_ink(user_id, session_token, api_key, user_lang="zh-CN", g_t
# run deno
cmd = f'{deno_path} run -Ar ./s3si.ts -n -p {path_config_file}'
cron_logger.info(cmd)
rtn: subprocess.CompletedProcess[bytes] = subprocess.run(cmd.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)

res = rtn.stdout.decode('utf-8')
error = rtn.stderr.decode('utf-8')
res = ""
error = ""
battle_cnt = 0
coop_cnt = 0
url = ''
error_msg = ""

try:
rtn: subprocess.CompletedProcess[bytes] = subprocess.run(cmd.split(' '), stdout=subprocess.PIPE,
stderr=subprocess.PIPE, env=env, timeout=300)
res = rtn.stdout.decode('utf-8')
error = rtn.stderr.decode('utf-8')
except subprocess.TimeoutExpired:
error_msg = f"deno run timeout\n"
except Exception as e:
error_msg = f"deno run err:\n{e}"
if error:
# error里面混有deno debug内容,需要经过过滤
for line in error.split('\n'):
Expand All @@ -231,10 +270,10 @@ def exported_to_stat_ink(user_id, session_token, api_key, user_lang="zh-CN", g_t
error_msg += f"{line}\n"

if error_msg:
cron_logger.warning(f'user_db_id:{user_id} cli error,result:\n{error_msg}')
cron_logger.error(f'user_db_id:{user_id} deno cli error,result:\n{error_msg}')
elif res:
# success
cron_logger.info(f'user_db_id:{user_id} cli success,result:\n{res}')
cron_logger.info(f'user_db_id:{user_id} deno cli success,result:\n{res}')

for line in res.split('\n'):
line = line.strip()
Expand All @@ -248,8 +287,7 @@ def exported_to_stat_ink(user_id, session_token, api_key, user_lang="zh-CN", g_t
url = line.split('to ')[1].split('spl3')[0].split('salmon3')[0][:-1]

cron_logger.info(f'user_db_id:{user_id} result: {battle_cnt}, {coop_cnt}, {url}')
if battle_cnt or coop_cnt:
return battle_cnt, coop_cnt, url
return battle_cnt, coop_cnt, url, error_msg


def strToBase64(s):
Expand Down
14 changes: 10 additions & 4 deletions nonebot_plugin_splatoon3_nso/handle/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ async def start_push(bot: Bot, event: Event, args: Message = CommandArg()):
job_id = f"{msg_id}_push"
logger.info(f"add push_job {job_id}")
job_data = {
'bot': bot,
'event': event,
'platform': platform,
'user_id': user_id,
'msg_id': msg_id,
Expand Down Expand Up @@ -143,7 +145,7 @@ async def stop_push(bot: Bot, event: Event):
msg_id = get_msg_id(platform, user_id)
user = dict_get_or_set_user_info(platform, user_id, push=0)

st_msg, push_time_minute = close_push(platform, user_id)
_, _, st_msg, push_time_minute = close_push(platform, user_id)
if isinstance(bot, Tg_Bot):
msg = f"Stop push!"
elif isinstance(bot, All_BOT):
Expand Down Expand Up @@ -192,7 +194,7 @@ async def push_latest_battle(bot: Bot, event: Event, job_data: dict, filters: di
msg = ""

# 获取统计数据
st_msg, push_time_minute = close_push(platform, user_id)
_, _, st_msg, push_time_minute = close_push(platform, user_id)
if isinstance(bot, All_BOT):
msg = f"服务器连续多次请求报错,停止推送,bot可能遇到了网络问题,请加 新人导航 频道内的q群联系主人,本次推送持续 {push_time_minute}分钟\n\n"
msg += st_msg
Expand Down Expand Up @@ -224,7 +226,7 @@ async def push_latest_battle(bot: Bot, event: Event, job_data: dict, filters: di
msg = 'No game record for 20 minutes, stop push.'

# 获取统计数据
st_msg, push_time_minute = close_push(platform, user_id)
_, _, st_msg, push_time_minute = close_push(platform, user_id)
if isinstance(bot, All_BOT):
msg = f"20分钟内没有游戏记录,停止推送,本次推送持续 {push_time_minute}分钟, {job_data.get('match_push_cnt') or 0}次对局\n"
if not user.stat_key and user.push_cnt <= 10:
Expand Down Expand Up @@ -281,6 +283,8 @@ def close_push(platform, user_id):
job_data = None
msg = ""
push_time_minute = 0
bot = None
event = None
try:
r = scheduler.get_job(job_id)
job_data = r.args[2] or {}
Expand All @@ -291,10 +295,12 @@ def close_push(platform, user_id):
if job_data:
push_statistics: PushStatistics = job_data.get("push_statistics")
push_interval = job_data.get("push_interval")
bot = job_data.get("bot")
event = job_data.get("event")
if push_statistics:
msg += push_statistics.get_battle_st_msg()
msg += push_statistics.get_coop_st_msg()
# 计算推送持续时间
push_cnt = job_data.get('this_push_cnt', 0)
push_time_minute: float = float(push_cnt * push_interval) / 60
return msg, push_time_minute
return bot, event, msg, push_time_minute
Loading

0 comments on commit 65da315

Please sign in to comment.