首先需要获取相应数据这里可以直接访问**「heros.xlsx」https://www.aliyundrive.com/s/3iNomqfBrLc **进行下载,然后将相应的数据录入mysql数据库,这里可以参考(54条消息) 将Excel文件导入mysql数据库_qq_43784519的博客-CSDN博客。
游戏的基本规则:
/王者 /start #开始游戏
/王者 /end #结束游戏
/王者 答案 #回答问题
所以本次程序使用on_command
响应器:
wangzhe = on_command('王者')
通过event.message
获得QQ消息,然后将消息进行拆分:
# msg = "/王者 /start"
msg = str(event.message)
msg = msg.split()
# msg[0] = /王者
# msg[1] = /start
判断是否为初次进入游戏,即mgs[1] ?= '/start'
,若是则连接数据库,生成对应的题目和答案,并进行初始化,将on_state = True
:
if not on_state:
# 连接数据库
db = pymysql.connect(host='127.0.0.1', user='root', password='123456', database='hero')
conn = db.cursor() # 获取指针以操作数据库
sql = f'select ename from story '
conn.execute(sql)
data = conn.fetchall()
t = random.randint(0,110)
ename = data[t][0] # 获得随机一个英雄编号
sql = f'select * from story where ename = "{ename}"'
conn.execute(sql)
data = conn.fetchall()
question = data[0][2] #题目
answer = data[0][1] #答案
conn.close()
db.close()
num = 3
on_state = True
await wangzhe.send(Message(f"【开始答题】\n【提示:{question}】" ))
若否进入答题状态:
if msg[1] != '/start' :
# 开启答题状态
print(answer)
if on_state: # 若on_state = true 则开始答题
这时又要判断on_state ?= True
,若不是则直接返回,若是,则继续判断msg[1] ?= '/end'
,若是则直接结束游戏:
if msg[1] == '/end':
# 结束答题
on_state = False
await wangzhe.send(Message("【答题结束,管理关闭】\n" + '【【英雄:{}】'.format(answer)))
return
同时判断答题次数是否耗尽,若耗尽则结束游戏:
if num == 0:
# 4次答题机会用完,结束本题
on_state = False
await wangzhe.send(Message("【答题结束,超时作答】\n" + '【英雄:{}】'.format(answer)))
return
num = num - 1 # 答题次数-1
如果输入的答案是正确答案,将相应信息保存到数据库,并游戏结束。
if msg[1] == answer:
#回答正确
on_state = False
# 连接数据库
db = pymysql.connect(host='127.0.0.1', user='root', password='123456', database='hero')
conn = db.cursor() # 获取指针以操作数据库
sql = f'select * from cart where user_id = {user_id}'
conn.execute(sql)
data = conn.fetchall()
if not data:
sql = f'insert into cart values({user_id},{group_id},5)'
print(sql)
conn.execute(sql)
db.commit()
else:
sql = f'update cart set score = score + 5 where user_id = {user_id}'
print(sql)
conn.execute(sql)
db.commit()
conn.close()
db.close()
if not data
是用来判断数据库中是否已经存在对应的qq号和群号,若不存在,则向数据库中添加相应数据;若存在,直接对相应数据进行修改。
若回答错误,则减少一次回答机会,其他操作与回答正常类似:
# 连接数据库
db = pymysql.connect(host='127.0.0.1', user='root', password='123456', database='hero')
conn = db.cursor() # 获取指针以操作数据库
sql = f'select * from cart where user_id = {user_id}'
conn.execute(sql)
data = conn.fetchall()
print(data,type(data))
if not data:
sql = f'insert into cart(user_id,group_id,score) values ({user_id},{group_id},-3)'
print(sql)
conn.execute(sql)
db.commit()
else:
sql = f'update cart set score = score - 3 where user_id = {user_id}'
print(sql)
conn.execute(sql)
db.commit()
conn.close()
db.close()
通过查询数据库获得相应数据:
user_id = event.user_id
group_id = event.group_id
# 连接数据库
db = pymysql.connect(host='127.0.0.1', user='root', password='123456', database='hero')
conn = db.cursor() # 获取指针以操作数据库
sql = f'select score from cart where user_id = {user_id} and group_id = {group_id}'
conn.execute(sql)
data = conn.fetchall()
at = f"[CQ:at,qq={user_id}]"
# 连接数据库
db = pymysql.connect(host='127.0.0.1', user='root', password='123456', database='hero')
conn = db.cursor() # 获取指针以操作数据库
group_id = event.group_id
sql = f'select * from cart where group_id = {group_id} order by score desc limit 0,3'
conn.execute(sql)
data = conn.fetchall()
print(data)
text = '排名 qq 积分\n'
i = 1
for item in data:
# '%5s%35s%20s%10s'%(rank.center(5),song.center(35),singer.center(20),(time[1]+':'+time[7:9]
text += ' ' + str(i) + ' ' + str(item[0])+ ' ' + str(item[2]) + '\n'
i= i + 1
import nonebot
from nonebot import on_keyword,on_command
from nonebot.adapters.onebot.v11 import Bot, Message, MessageSegment,GroupMessageEvent
from nonebot.typing import T_State #bot使用的对象和字典
import pymysql # 数据库
import random
from nonebot.rule import to_me
__plugin_name__ = 'wangzhe'
__plugin_usage__ = '用法: /王者,根据提示猜王者英雄名称。'
wangzhe = on_command('王者')
global question # 问题
global answer # 答案
on_state =False # 实时状态,初始为否
num = 3 # 测试次数
@wangzhe.handle()
async def handle_first_receive(bot: Bot, event: GroupMessageEvent, state: T_State):
global question,answer,num,on_state
msg = str(event.message)
msg = msg.split()
print(msg)
user_id = event.user_id
group_id = event.group_id
if msg[1] != '/start' :
# 开启答题状态
print(answer)
if on_state: # 若on_state = true 则开始答题
if num == 0:
# 4次答题机会用完,结束本题
on_state = False
await wangzhe.send(Message("【答题结束,超时作答】\n" + '【英雄:{}】'.format(answer)))
return
num = num - 1 # 答题次数-1
if msg[1] == '/end':
# 结束答题
on_state = False
await wangzhe.send(Message("【答题结束,管理关闭】\n" + '【【英雄:{}】'.format(answer)))
return
if msg[1] == answer:
#回答正确
on_state = False
# 连接数据库
db = pymysql.connect(host='127.0.0.1', user='root', password='123456', database='hero')
conn = db.cursor() # 获取指针以操作数据库
sql = f'select * from cart where user_id = {user_id}'
conn.execute(sql)
data = conn.fetchall()
if not data:
sql = f'insert into cart values({user_id},{group_id},5)'
print(sql)
conn.execute(sql)
db.commit()
else:
sql = f'update cart set score = score + 5 where user_id = {user_id}'
print(sql)
conn.execute(sql)
db.commit()
conn.close()
db.close()
await wangzhe.send(Message(
"【答题结束,成功作答】\n" + "【恭喜QQ:{}】 \n【积分 + 5】\n".format(event.user_id) + '【英雄:{}】'.format(answer)))
return
else:
# 回答错误
# 连接数据库
db = pymysql.connect(host='127.0.0.1', user='root', password='123456', database='hero')
conn = db.cursor() # 获取指针以操作数据库
sql = f'select * from cart where user_id = {user_id}'
conn.execute(sql)
data = conn.fetchall()
print(data,type(data))
if not data:
sql = f'insert into cart(user_id,group_id,score) values ({user_id},{group_id},-3)'
print(sql)
conn.execute(sql)
db.commit()
else:
sql = f'update cart set score = score - 3 where user_id = {user_id}'
print(sql)
conn.execute(sql)
db.commit()
conn.close()
db.close()
at = f"[CQ:at,qq={user_id}]"
await wangzhe.send(Message(at+ "\n【本题剩余({})次机会】\n".format(num+1)+ "【你的答案是:{}】\n".format(msg[1])+ "【积分 - 3】"))
return
else:
return
else:
if not on_state:
# 连接数据库
db = pymysql.connect(host='127.0.0.1', user='root', password='123456', database='hero')
conn = db.cursor() # 获取指针以操作数据库
sql = f'select ename from story '
conn.execute(sql)
data = conn.fetchall()
t = random.randint(0,110)
ename = data[t][0] # 获得随机一个英雄编号
sql = f'select * from story where ename = "{ename}"'
conn.execute(sql)
data = conn.fetchall()
question = data[0][2]
answer = data[0][1]
conn.close()
db.close()
num = 3
on_state = True
await wangzhe.send(Message(f"【开始答题】\n【提示:{question}】" ))
else:
return
jifen = on_keyword({"我的积分"},rule = to_me())
@jifen.handle()
async def get_jifen(bot: Bot, event: GroupMessageEvent, state: T_State):
user_id = event.user_id
group_id = event.group_id
# 连接数据库
db = pymysql.connect(host='127.0.0.1', user='root', password='123456', database='hero')
conn = db.cursor() # 获取指针以操作数据库
sql = f'select score from cart where user_id = {user_id} and group_id = {group_id}'
conn.execute(sql)
data = conn.fetchall()
at = f"[CQ:at,qq={user_id}]"
await jifen.send(Message(at + "\n【您当前积分为{}】".format(data[0][0])))
rank = on_keyword({"排行榜"},rule = to_me())
@rank.handle()
async def get_rank(bot: Bot, event: GroupMessageEvent, state: T_State):
# 连接数据库
db = pymysql.connect(host='127.0.0.1', user='root', password='123456', database='hero')
conn = db.cursor() # 获取指针以操作数据库
group_id = event.group_id
sql = f'select * from cart where group_id = {group_id} order by score desc limit 0,3'
conn.execute(sql)
data = conn.fetchall()
print(data)
text = '排名 qq 积分\n'
i = 1
for item in data:
# '%5s%35s%20s%10s'%(rank.center(5),song.center(35),singer.center(20),(time[1]+':'+time[7:9]
text += ' ' + str(i) + ' ' + str(item[0])+ ' ' + str(item[2]) + '\n'
i= i + 1
await rank.send(Message(text))