Archived
1
0
Fork 0
This commit is contained in:
多羅狼 2023-06-08 00:19:25 +08:00 committed by GitHub
parent 3b4346a6b4
commit 1c5c72801a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 24 deletions

View file

@ -109,13 +109,16 @@ async def _(msg: Bot.MessageSession):
@ccode.command('stop {{chemical_code.stop.help}}')
async def s(msg: Bot.MessageSession):
state = play_state.get(msg.target.targetId, {}).get('ccode', False) # 尝试获取 play_state 中是否有此对象的游戏状态
state = play_state.get(msg.target.targetId, False) # 尝试获取 play_state 中是否有此对象的游戏状态
if state: # 若有
if state['active']: # 检查是否为活跃状态
play_state[msg.target.targetId]['ccode']['active'] = False # 标记为非活跃状态
await msg.finish(
msg.locale.t('chemical_code.stop.message', answer=play_state[msg.target.targetId]["answer"]),
quote=False) # 发送存储于 play_state 中的答案
if play_state[msg.target.targetId]['game'] == 'ccode': #检查游戏类型
play_state[msg.target.targetId]['active'] = False # 标记为非活跃状态
await msg.finish(
msg.locale.t('chemical_code.stop.message', answer=play_state[msg.target.targetId]["answer"]),
quote=False) # 发送存储于 play_state 中的答案
else:
await msg.finish(msg.locale.t('chemical_code.stop.message.failed'))
else:
await msg.finish(msg.locale.t('chemical_code.stop.message.none'))
else:
@ -133,15 +136,14 @@ async def chemical_code_by_id(msg: Bot.MessageSession):
async def chemical_code(msg: Bot.MessageSession, id=None, captcha_mode=False):
# 要求传入消息会话和 ChemSpider IDID 留空将会使用缺省值 None
if msg.target.targetId in play_state and play_state[msg.target.targetId][
'ccode']['active']: # 检查对象(群组或私聊)是否在 play_state 中有记录及是否为活跃状态
if msg.target.targetId in play_state and play_state[msg.target.targetId]['active']: # 检查对象(群组或私聊)是否在 play_state 中有记录及是否为活跃状态
await msg.finish(msg.locale.t('chemical_code.message.running'))
play_state.update({msg.target.targetId: {'ccode':{'active': True}}}) # 若无,则创建一个新的记录并标记为活跃状态
play_state.update({msg.target.targetId: {'game': 'ccode', 'active': True}}) # 若无,则创建一个新的记录并标记为活跃状态
try:
csr = await search_csr(id) # 尝试获取 ChemSpider ID 对应的化学式列表
except Exception as e: # 意外情况
traceback.print_exc() # 打印错误信息
play_state[msg.target.targetId]['ccode']['active'] = False # 将对象标记为非活跃状态
play_state[msg.target.targetId]['active'] = False # 将对象标记为非活跃状态
return await msg.finish(msg.locale.t('chemical_code.message.error'))
# print(csr)
play_state[msg.target.targetId]['answer'] = csr['name'] # 将正确答案标记于 play_state 中存储的对象中
@ -170,7 +172,7 @@ async def chemical_code(msg: Bot.MessageSession, id=None, captcha_mode=False):
async def ans(msg: Bot.MessageSession, answer): # 定义回答函数的功能
wait = await msg.waitAnyone() # 等待对象内的任意人回答
if play_state[msg.target.targetId]['ccode']['active']: # 检查对象是否为活跃状态
if play_state[msg.target.targetId]['active']: # 检查对象是否为活跃状态
if (wait_text := wait.asDisplay(text_only=True)) != answer: # 如果回答不正确
if re.match(r'^[A-Za-z0-9]+$', wait_text):
try:
@ -219,14 +221,14 @@ async def chemical_code(msg: Bot.MessageSession, id=None, captcha_mode=False):
return await ans(wait, answer) # 进行下一轮检查
else:
await wait.sendMessage(wait.locale.t('chemical_code.message.correct'))
play_state[msg.target.targetId]['ccode']['active'] = False # 将对象标记为非活跃状态
play_state[msg.target.targetId]['active'] = False # 将对象标记为非活跃状态
async def timer(start): # 计时器函数
if play_state[msg.target.targetId]['ccode']['active']: # 检查对象是否为活跃状态
if play_state[msg.target.targetId]['active']: # 检查对象是否为活跃状态
if datetime.now().timestamp() - start > 60 * set_timeout: # 如果超过2分钟
await msg.sendMessage(
msg.locale.t('chemical_code.message.timeup', answer=play_state[msg.target.targetId]["answer"]))
play_state[msg.target.targetId]['ccode']['active'] = False
play_state[msg.target.targetId]['active'] = False
else: # 如果未超时
await asyncio.sleep(1) # 等待1秒
await timer(start) # 重新调用计时器函数
@ -240,10 +242,10 @@ async def chemical_code(msg: Bot.MessageSession, id=None, captcha_mode=False):
else:
result = await msg.waitNextMessage(
[Image(newpath), Plain(msg.locale.t('chemical_code.message.captcha', times=set_timeout))])
if play_state[msg.target.targetId]['ccode']['active']: # 检查对象是否为活跃状态
if play_state[msg.target.targetId]['active']: # 检查对象是否为活跃状态
if result.asDisplay(text_only=True) == csr['name']:
await result.sendMessage(msg.locale.t('chemical_code.message.correct'))
else:
await result.sendMessage(
msg.locale.t('chemical_code.message.incorrect', answer=play_state[msg.target.targetId]["answer"]))
play_state[msg.target.targetId]['ccode']['active'] = False
play_state[msg.target.targetId]['active'] = False

View file

@ -15,9 +15,10 @@
"chemical_code.message.incorrect.remind3": "提示:已经很接近了,请重试。",
"chemical_code.message.incorrect.remind4": "提示:可能有元素遗漏,请检查。",
"chemical_code.message.incorrect.remind5": "提示:元素顺序不正确,请检查。",
"chemical_code.message.running": "当前有一局游戏正在进行中。",
"chemical_code.message.running": "当前有游戏正在进行中。",
"chemical_code.message.timeup": "已超时,正确答案是 ${answer}。",
"chemical_code.stop.help": "停止当前的游戏。",
"chemical_code.stop.message": "已停止,正确答案是 ${answer}。",
"chemical_code.stop.message.none": "当前没有游戏在进行中。"
"chemical_code.stop.message.failed": "当前此游戏未在进行,可能有其他游戏正在进行。",
"chemical_code.stop.message.none": "当前没有游戏正在进行。"
}

View file

@ -15,7 +15,7 @@
"chemical_code.message.incorrect.remind3": "提示:已经很接近了,请重试。",
"chemical_code.message.incorrect.remind4": "提示:可能有元素遗漏,请检查。",
"chemical_code.message.incorrect.remind5": "提示:元素顺序不正确,请检查。",
"chemical_code.message.running": "目前有一局遊戲正在進行。",
"chemical_code.message.running": "目前有遊戲正在進行。",
"chemical_code.message.timeup": "回答逾時,正確答案是 ${answer}。",
"chemical_code.stop.help": "停止目前的遊戲。",
"chemical_code.stop.message": "已停止,正確答案是 ${answer}。",

View file

@ -92,16 +92,16 @@ play_state = {}
@tf.command('{{twenty_four.help}}')
async def _(msg: Bot.MessageSession):
if msg.target.targetId in play_state and play_state[msg.target.targetId]['24']['active']:
if msg.target.targetId in play_state and play_state[msg.target.targetId]['active']:
await msg.finish(msg.locale.t('twenty_four.message.running'))
play_state.update({msg.target.targetId: {'24': {'active': True}}})
play_state.update({msg.target.targetId: {'game': '24', 'active': True}})
numbers = [random.randint(1, 13) for _ in range(4)]
has_solution_flag = has_solution(numbers)
answer = await msg.waitNextMessage(msg.locale.t('twenty_four.message', numbers=numbers))
expression = answer.asDisplay(text_only=True)
if play_state[msg.target.targetId]['24']['active']:
if play_state[msg.target.targetId]['active']:
if expression.lower() in ['无解', '無解', 'none', 'n/a']:
if has_solution_flag:
await answer.sendMessage(msg.locale.t('twenty_four.message.incorrect.have_solution'))
@ -115,13 +115,13 @@ async def _(msg: Bot.MessageSession):
await answer.sendMessage(msg.locale.t('twenty_four.message.incorrect'))
else:
await answer.sendMessage(msg.locale.t('twenty_four.message.incorrect.error'))
play_state[msg.target.targetId]['24']['active'] = False
play_state[msg.target.targetId]['active'] = False
@tf.command('stop {{twenty_four.stop.help}}')
async def s(msg: Bot.MessageSession):
state = play_state.get(msg.target.targetId, {}).get('24', False)
state = play_state.get(msg.target.targetId, False)
if state:
if state['active']:
play_state[msg.target.targetId]['24']['active'] = False

View file

@ -9,5 +9,6 @@
"twenty_four.message.running": "当前有一局游戏正在进行中。",
"twenty_four.stop.help": "停止当前的游戏。",
"twenty_four.stop.message": "已停止。",
"twenty_four.stop.message.none": "当前没有游戏在进行中。"
"twenty_four.stop.message.failed": "当前此游戏未在进行,可能有其他游戏正在进行。",
"twenty_four.stop.message.none": "当前没有游戏正在进行。"
}