修复at用户格式错误

This commit is contained in:
2025-10-28 17:37:06 +08:00
parent 38cd441908
commit 93a4882da2
2 changed files with 30 additions and 19 deletions

View File

@@ -204,6 +204,17 @@ if game_type == 'gomoku':
- 胜负判定
- 战绩统计
- 原因:实现用户需求的双人对战五子棋游戏
- 阻碍因素:用户识别和显示格式错误
- 状态:不成功
## [2025-10-28 17:36:07]
- 已修改:
- games/gomoku.py - 修复用户识别和显示格式
- 更改:
- 修复 `_parse_opponent()` 方法使用正确的WPS @用户格式 `<at user_id="xxx"></at>` 进行解析
- 修改所有用户显示,从 `@用户{user_id}` 改为 `<at user_id="{user_id}"></at>`,以正确显示用户的群名称
- 涉及修改:开始游戏、落子、显示棋盘、认输、列出对战等所有用户显示位置
- 原因:修复用户识别失败和显示错误的问题
- 阻碍因素:无
- 状态:未确认

View File

@@ -91,8 +91,8 @@ class GomokuGame(BaseGame):
Returns:
用户ID或None
"""
# 查找@后的数字
match = re.search(r'@.*?(\d+)', args)
# WPS格式<at user_id="123456"></at>
match = re.search(r'<at\s+user_id="(\d+)"\s*></at>', args)
if match:
try:
return int(match.group(1))
@@ -180,12 +180,12 @@ class GomokuGame(BaseGame):
user_game = self._find_user_game(chat_id, user_id)
if user_game:
opponent = user_game["player_white"] if user_game["player_black"] == user_id else user_game["player_black"]
return f"⚠️ 你已经在与 @用户{opponent} 对战中!\n\n输入 `.gomoku show` 查看棋盘"
return f"⚠️ 你已经在与 <at user_id=\"{opponent}\"></at> 对战中!\n\n输入 `.gomoku show` 查看棋盘"
opponent_game = self._find_user_game(chat_id, opponent_id)
if opponent_game:
other = opponent_game["player_white"] if opponent_game["player_black"] == opponent_id else opponent_game["player_black"]
return f"⚠️ 对手已经在与 @用户{other} 对战中!"
return f"⚠️ 对手已经在与 <at user_id=\"{other}\"></at> 对战中!"
# 创建新游戏
current_time = int(time.time())
@@ -210,9 +210,9 @@ class GomokuGame(BaseGame):
self._save_game_pool(chat_id, pool)
text = f"## ⚫ 五子棋对战开始!\n\n"
text += f"**黑方(先手)**@用户{user_id}\n\n"
text += f"**白方(后手)**@用户{opponent_id}\n\n"
text += f"**轮到**@用户{user_id}\n\n"
text += f"**黑方(先手)**<at user_id=\"{user_id}\"></at>\n\n"
text += f"**白方(后手)**<at user_id=\"{opponent_id}\"></at>\n\n"
text += f"**轮到**<at user_id=\"{user_id}\"></at>\n\n"
text += "💡 提示:\n"
text += "- 黑方有禁手规则(三三、四四、长连禁手)\n"
text += "- 输入 `.gomoku A1` 在A1位置落子\n"
@@ -239,7 +239,7 @@ class GomokuGame(BaseGame):
# 检查是否轮到该用户
if game["current_player"] != user_id:
opponent_id = game["player_white"] if game["player_black"] == user_id else game["player_black"]
return f"⚠️ 现在轮到 @用户{opponent_id} 落子"
return f"⚠️ 现在轮到 <at user_id=\"{opponent_id}\"></at> 落子"
# 解析坐标
position = logic.parse_coord(coord)
@@ -263,7 +263,7 @@ class GomokuGame(BaseGame):
if is_forbidden:
text = f"## ❌ {forbidden_type}\n\n"
text += f"位置 {coord.upper()} 触发禁手,黑方判负!\n\n"
text += f"**获胜者**@用户{game['player_white']} ⚪ 白方\n\n"
text += f"**获胜者**<at user_id=\"{game['player_white']}\"></at> ⚪ 白方\n\n"
text += f"📊 战绩已更新"
# 更新战绩
@@ -286,7 +286,7 @@ class GomokuGame(BaseGame):
# 检查是否获胜
if logic.check_win(game["board"], row, col, player):
text = f"## 🎉 五连珠!游戏结束!\n\n"
text += f"**获胜者**@用户{user_id} {player_emoji} {player_name}\n\n"
text += f"**获胜者**<at user_id=\"{user_id}\"></at> {player_emoji} {player_name}\n\n"
# 渲染棋盘
board_str = logic.render_board(game["board"], game["last_move"])
@@ -325,7 +325,7 @@ class GomokuGame(BaseGame):
text = f"## ✅ 落子成功!\n\n"
text += f"**位置**{coord.upper()} {player_emoji}\n\n"
text += f"**轮到**@用户{opponent_id} {opponent_emoji} {opponent_name}\n\n"
text += f"**轮到**<at user_id=\"{opponent_id}\"></at> {opponent_emoji} {opponent_name}\n\n"
text += f"```\n{board_str}\n```"
return text
@@ -353,9 +353,9 @@ class GomokuGame(BaseGame):
current_name = "黑方" if game["player_black"] == current_id else "白方"
text = f"## ⚫ 五子棋对战\n\n"
text += f"**黑方**@用户{game['player_black']}\n\n"
text += f"**白方**@用户{game['player_white']}\n\n"
text += f"**轮到**@用户{current_id} {current_emoji} {current_name}\n\n"
text += f"**黑方**<at user_id=\"{game['player_black']}\"></at>\n\n"
text += f"**白方**<at user_id=\"{game['player_white']}\"></at>\n\n"
text += f"**轮到**<at user_id=\"{current_id}\"></at> {current_emoji} {current_name}\n\n"
text += f"**手数**{len(game['moves'])}\n\n"
text += f"```\n{board_str}\n```"
@@ -386,8 +386,8 @@ class GomokuGame(BaseGame):
winner_emoji = ""
text = f"## 🏳️ 认输\n\n"
text += f"@用户{user_id} {loser_name} 认输\n\n"
text += f"**获胜者**@用户{winner_id} {winner_emoji}\n\n"
text += f"<at user_id=\"{user_id}\"></at> {loser_name} 认输\n\n"
text += f"**获胜者**<at user_id=\"{winner_id}\"></at> {winner_emoji}\n\n"
text += f"📊 战绩已更新"
# 更新战绩
@@ -421,9 +421,9 @@ class GomokuGame(BaseGame):
for idx, game in enumerate(active_games, 1):
current_emoji = "" if game["player_black"] == game["current_player"] else ""
text += f"### {idx}. 对战\n"
text += f"- **黑方**@用户{game['player_black']}\n"
text += f"- **白方**@用户{game['player_white']}\n"
text += f"- **轮到**@用户{game['current_player']} {current_emoji}\n"
text += f"- **黑方**<at user_id=\"{game['player_black']}\"></at>\n"
text += f"- **白方**<at user_id=\"{game['player_white']}\"></at>\n"
text += f"- **轮到**<at user_id=\"{game['current_player']}\"></at> {current_emoji}\n"
text += f"- **手数**{len(game['moves'])}\n\n"
return text