txlyre 1 day ago
parent
commit
ffcc5059b3
2 changed files with 34 additions and 4 deletions
  1. 7 0
      chess0.py
  2. 27 4
      commands.py

+ 7 - 0
chess0.py

@@ -104,6 +104,13 @@ class ChessManager:
 
         session.board.pop()
 
+    def is_check(self, id):
+        session = self.sessions.get(id)
+        if not session:
+            raise KeyError(id)
+
+        return session.board.is_check()
+
     async def skip(self, id):
         session = self.sessions.get(id)
         if not session:

+ 27 - 4
commands.py

@@ -683,7 +683,12 @@ async def chess_move_handler(chess, id, move):
     except IllegalMove:
         return ["Некорректный ход."]
 
-    return [svg2png(chess.svg(id))]
+    reply = [svg2png(chess.svg(id))]
+
+    if chess.is_check(id):
+        reply.append("Шах!")
+
+    return reply
 
 
 async def chess_undo_handler(chess, id):
@@ -713,7 +718,12 @@ async def chess_skip_handler(chess, id):
             f"Конец игры: {str(e)}",
         ]
 
-    return [svg2png(chess.svg(id))]
+    reply = [svg2png(chess.svg(id))]
+
+    if chess.is_check(id):
+        reply.append("Шах!")
+
+    return reply
 
 
 async def chess_pass_handler(chess, id):
@@ -732,7 +742,12 @@ async def chess_pass_handler(chess, id):
             f"Конец игры: {str(e)}",
         ]
 
-    return [svg2png(chess.svg(id))]
+    reply = [svg2png(chess.svg(id))]
+
+    if chess.is_check(id):
+        reply.append("Шах!")
+
+    return reply
 
 
 async def chess_board_handler(chess, id):
@@ -741,12 +756,20 @@ async def chess_board_handler(chess, id):
     except KeyError:
         return ["Нет активной игры."]
 
-    return [board]
+    reply = [board]
+
+    if chess.is_check(id):
+        reply.append("Шах!")
+
+    return reply
 
 
 async def chess_moves_handler(chess, id):
     try:
         moves = f"Последние два хода: {chess.moves(id, 2)}\nВсе ходы (первый ход - игрока): {chess.moves(id)}"
+
+        if chess.is_check(id):
+            moves = f"Шах.\n{moves}"
     except KeyError:
         return ["Нет активной игры."]