Skip to content

Commit

Permalink
feat(game): 新增自动签到功能
Browse files Browse the repository at this point in the history
该功能新增了一个数据表字段,若需升级请手动执行该sql语句:
ALTER TABLE game add auto_signin int default 0 comment '自动签到' after last_signin_time;
  • Loading branch information
Colsrch committed Jun 14, 2022
1 parent d5e87ab commit 0fa3c86
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
23 changes: 13 additions & 10 deletions app/entities/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""查询金币"""
Expand All @@ -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:
"""查询签到状态"""
Expand All @@ -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)
Expand All @@ -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]
)
32 changes: 32 additions & 0 deletions app/plugin/basic/game.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
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
from app.entities.game import BotGame
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()

Expand All @@ -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='经济系统'
)
Expand Down Expand Up @@ -151,13 +159,36 @@ 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:
logger.exception(e)
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:
Expand All @@ -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 '好感度',
Expand Down

0 comments on commit 0fa3c86

Please sign in to comment.