From b2534dad5055a9ddf2f3bd72a072a3218a3e904e Mon Sep 17 00:00:00 2001 From: liuwei Date: Tue, 3 Jun 2025 11:53:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=8C=9C=E6=AD=8C=E5=90=8D?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E4=BB=8EAPI=E4=B8=AD=E6=8F=90?= =?UTF-8?q?=E5=8F=96=E6=AD=8C=E6=9B=B2=E5=89=8D=E5=A5=8F=EF=BC=8C=E7=84=B6?= =?UTF-8?q?=E5=90=8E=E7=8C=9C=E6=AD=8C=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/guess_song/main.py | 42 ++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/plugins/guess_song/main.py b/plugins/guess_song/main.py index 1d34cb9..ef62cc2 100644 --- a/plugins/guess_song/main.py +++ b/plugins/guess_song/main.py @@ -180,9 +180,13 @@ class GuessSongPlugin(MessagePluginInterface): if self.redis_db: current_game = self.redis_db.get_game_session(session_id) - # 简化逻辑:如果有内容,且游戏进行中,则视为答案 - if current_game and current_game.get("status") == "playing" and content: - return await self._check_answer(bot, session_id, sender, content, current_game) + # 如果游戏进行中,且有内容,则视为答案 + if current_game and current_game.get("status") == "playing": + if content: # 有内容,视为答案 + return await self._check_answer(bot, session_id, sender, content, current_game) + else: # 没有内容,提示已有游戏在进行中 + await bot.send_text_message(session_id, f"⚠️ 当前已有猜歌游戏在进行中,请直接回复歌名进行猜测", sender) + return True, "已有游戏进行中" # 否则开始新游戏(可以指定歌手或随机) return await self._start_new_game(bot, session_id, sender, content if content else None) @@ -225,23 +229,36 @@ class GuessSongPlugin(MessagePluginInterface): return False, f"处理出错: {e}" @points_reward_decorator(5, "game", "猜歌名游戏", Feature.GUESS_MUSIC) - async def _check_answer(self, bot: WechatAPIClient, session_id: str, sender: str, answer: str, - game_data: Dict[str, Any]) -> Tuple[bool, str]: + async def _check_answer(self, message: Dict[str, Any]) -> Tuple[bool, str]: """检查答案""" try: + # 从message中提取所需参数 + sender = message.get("sender", "") + session_id = message.get("roomid", "") or sender # 如果没有roomid,使用sender作为session_id + answer = message.get("content", "").strip() # 从消息内容中获取答案 + bot = message.get("bot") + + # 从Redis获取游戏数据 + game_data = None + if self.redis_db: + game_data = self.redis_db.get_game_session(session_id) + + if not game_data: + return False, "没有进行中的游戏" + correct_answer = game_data.get("song_name", "") singer_name = game_data.get("singer_name", "") - + # 检查答案是否正确(简单比较,可以改进为模糊匹配) if answer.lower() == correct_answer.lower(): # 游戏结束,发送成功消息 await bot.send_text_message(session_id, f"🎉 恭喜你猜对了!\n歌曲:{correct_answer}\n歌手:{singer_name}", sender) - + # 删除游戏会话 if self.redis_db: self.redis_db.delete_game_session(session_id) - + return True, "猜对了" else: # 答案错误 @@ -249,22 +266,25 @@ class GuessSongPlugin(MessagePluginInterface): current_time = time.time() start_time = game_data.get("start_time", 0) hint_given = game_data.get("hint_given", False) - + if not hint_given and (current_time - start_time) > 30: # 给出提示(显示歌名的第一个字) hint = correct_answer[0] + "*" * (len(correct_answer) - 1) await bot.send_text_message(session_id, f"💡 提示:歌名以 '{correct_answer[0]}' 开头") - + # 更新游戏会话,标记已给出提示 game_data["hint_given"] = True if self.redis_db: self.redis_db.save_game_session(session_id, game_data) - + # 告知用户答案错误 await bot.send_text_message(session_id, f"❌ 答案错误,请继续猜测!") return False, "答案错误" except Exception as e: self.LOG.error(f"检查答案出错: {e}") + session_id = message.get("roomid", "") or message.get("sender", "") + sender = message.get("sender", "") + bot = message.get("bot") await bot.send_text_message(session_id, f"❌检查答案出错,请稍后重试", sender) return False, f"处理出错: {e}"