From 0fa3c860125e28a050f8323fc25d420b42194ab7 Mon Sep 17 00:00:00 2001 From: Colsrch Date: Wed, 15 Jun 2022 01:20:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(game):=20=E6=96=B0=E5=A2=9E=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E7=AD=BE=E5=88=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 该功能新增了一个数据表字段,若需升级请手动执行该sql语句: ALTER TABLE game add auto_signin int default 0 comment '自动签到' after last_signin_time; --- app/entities/game.py | 23 +++++++++++++---------- app/plugin/basic/game.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/app/entities/game.py b/app/entities/game.py index e57d63f..b124526 100644 --- a/app/entities/game.py +++ b/app/entities/game.py @@ -122,12 +122,10 @@ async def sign_in(self) -> None: await self.grant_intimacy(IntimacyGet[consecutive_days - 1]) # 好感度调整 with MysqlDao() as db: - res = db.update( + db.update( "UPDATE game SET coin=%s, coins=coins+%s, last_signin_time=CURDATE(), total_days=total_days+1 " "WHERE qid=%s", [self.coin, self.coin, self.qq] ) - if not res: - raise Exception() async def get_coins(self) -> int: """查询金币""" @@ -139,16 +137,14 @@ async def get_coins(self) -> int: return res[0][0] async def update_coin(self, coin) -> None: - """修改积分 + """修改金币 :param coin: str, 金币变动值 """ with MysqlDao() as db: - res = db.update( + db.update( "UPDATE game SET coins=coins+%s WHERE qid=%s", [coin, self.qq] ) - if not res: - raise Exception() async def get_sign_in_status(self) -> bool: """查询签到状态""" @@ -160,6 +156,7 @@ async def get_sign_in_status(self) -> bool: return res[0][0] async def reduce_coin(self, coin: int) -> bool: + """消耗金币""" if await self.get_coins() < coin: return False await self.update_coin(-coin) @@ -170,9 +167,15 @@ async def update_english_answer(self, num) -> None: :param num: str, 答题变动值 """ with MysqlDao() as db: - res = db.update( + db.update( "UPDATE game SET english_answer=english_answer+%s WHERE qid=%s", [num, self.qq] ) - if not res: - raise Exception() + + async def auto_signin(self, status: int) -> None: + """自动签到开关""" + with MysqlDao() as db: + db.update( + "UPDATE game SET auto_signin=%s WHERE qid=%s", + [status, self.qq] + ) diff --git a/app/plugin/basic/game.py b/app/plugin/basic/game.py index cf58ef7..a2f59ed 100644 --- a/app/plugin/basic/game.py +++ b/app/plugin/basic/game.py @@ -1,11 +1,14 @@ import random from arclet.alconna import Alconna, Option, Args, Arpamar +from graia.ariadne.app import Ariadne from graia.ariadne.message.chain import MessageChain from graia.ariadne.message.element import Image, Plain, At +from graia.scheduler import GraiaScheduler, timers from loguru import logger from prettytable import PrettyTable +from app.core.app import AppCore from app.core.commander import CommandDelegateManager from app.core.config import Config from app.core.database import InitDB @@ -13,10 +16,14 @@ from app.entities.user import BotUser from app.plugin.base import Plugin from app.util.dao import MysqlDao +from app.util.send_message import safeSendFriendMessage from app.util.text2image import create_image from app.util.tools import to_thread from .game_res.sign_image_generator import get_sign_image +core: AppCore = AppCore.get_core_instance() +app: Ariadne = core.get_app() +sche: GraiaScheduler = core.get_scheduler() manager: CommandDelegateManager = CommandDelegateManager.get_instance() database: InitDB = InitDB.get_instance() @@ -33,6 +40,7 @@ Option('tf', help_text='转账', args=Args['at': At, 'money': int]), Option('迁移', help_text='迁移旧版金币'), Option('rank', help_text='显示群内已注册成员资金排行榜'), + Option('auto', args=Args['status': int], help_text='每天消耗10%金币自动签到') ], help_text='经济系统' ) @@ -151,6 +159,12 @@ async def process(self: Plugin, command: Arpamar, _: Alconna): Image(data_bytes=await create_image(msg.get_string())) ])) return resp + elif auto := options.get('auto'): + status = auto['status'] + if status not in [0, 1]: + return self.args_error() + await BotGame((getattr(self, 'friend', None) or getattr(self, 'member', None)).id).auto_signin(status) + return MessageChain.create('开启成功,将于每日8点为您自动签到!' if status else '关闭成功!') else: return self.args_error() except Exception as e: @@ -158,6 +172,23 @@ async def process(self: Plugin, command: Arpamar, _: Alconna): return self.unkown_error() +@sche.schedule(timers.crontabify('0 8 * * * 0')) +async def auto_sing(): + logger.info('auto signin is running...') + with MysqlDao() as db: + res = db.query('SELECT qid FROM game WHERE auto_signin=1') + for qq, in res: + user = BotGame(qq, random.randint(1, 101)) + if not await user.get_sign_in_status(): + consume = int(await user.get_coins() * 0.1) + if await user.reduce_coin(consume): + await user.sign_in() + logger.success(f'账号: {qq} 自动签到完成~') + await safeSendFriendMessage(qq, MessageChain.create(f'今日份签到完成,消耗{consume}金币\n请发送.gp get查收')) + else: + await safeSendFriendMessage(qq, MessageChain.create("您的金币不足,无法完成自动签到")) + + @database.init() async def init_db(): with MysqlDao() as db: @@ -167,6 +198,7 @@ async def init_db(): consecutive_days int default 0 not null comment '连续签到天数', total_days int default 0 not null comment '累计签到天数', last_signin_time date comment '上次签到时间', + auto_signin int default 0 not null comment '自动签到', coin int comment '今日获得货币', coins int default 0 not null comment '货币', intimacy int default 0 not null comment '好感度',