txlyre 23 hours ago
parent
commit
335afb6e7a
2 changed files with 31 additions and 9 deletions
  1. 14 2
      chess0.py
  2. 17 7
      commands.py

+ 14 - 2
chess0.py

@@ -81,7 +81,7 @@ class ChessSession:
             raise self.game_over
 
     async def move(self, move=None):
-        self.check_game_over(self)
+        self.check_game_over()
 
         if move is not None:
             move = self.parse_move(move)
@@ -126,7 +126,7 @@ class ChessSession:
             self.check_game_over()
 
     def skip(self):
-        self.check_game_over(self)
+        self.check_game_over()
 
         self.board.push(chess.Move.null())
         self.move_ts = time.time()
@@ -184,6 +184,11 @@ class ChessManager:
 
         session.board.pop()
 
+        session.game_over = None
+        session.checkmate = False
+
+        session.check_game_over()
+
     def is_check(self, id):
         session = self.sessions.get(id)
         if not session:
@@ -252,6 +257,13 @@ class ChessManager:
 
         return session.board.turn
 
+    def game_over(self, id):
+        session = self.sessions.get(id)
+        if not session:
+            raise KeyError(id)
+
+        return session.game_over
+
     def animate(self, id, count=None, size=256, shallow=False):
         session = self.sessions.get(id)
         if not session:

+ 17 - 7
commands.py

@@ -663,12 +663,6 @@ def chess_render(chess, id, as_list=True):
     return board
 
 
-async def chess_start_handler(chess, id):
-    await chess.begin(id)
-
-    return chess_render(chess, id)
-
-
 def chess_game_stats(chess, id):
     if not chess.has_moves(id):
         return "Ходов ещё не сделано."
@@ -691,6 +685,12 @@ def chess_game_over(chess, id, e):
     ]
 
 
+async def chess_start_handler(chess, id):
+    await chess.begin(id)
+
+    return chess_render(chess, id)
+
+
 async def chess_from_handler(chess, id, moves):
     try:
         await chess.begin(id, moves, strict=not isinstance(id, str))
@@ -763,6 +763,8 @@ async def chess_undo_handler(chess, id):
         return ["Нет активной игры."]
     except IndexError:
         return ["Нечего отменять."]
+    except GameOver as e:
+        return chess_game_over(chess, id, e)
 
     return ["Последний ход отменён.", svg2png(chess.svg(id))]
 
@@ -809,8 +811,12 @@ async def chess_pass_handler(chess, id):
     return reply
 
 
-async def chess_board_handler(chess, id):
+async def chess_board_handler(chess, id):     
     try:
+        e = chess.game_over(id)
+        if e is not None:
+            return chess_game_over(chess, id, e)
+
         reply = chess_render(chess, id)
     except KeyError:
         return ["Нет активной игры."]
@@ -826,6 +832,10 @@ async def chess_board_handler(chess, id):
 
 async def chess_moves_handler(chess, id):
     try:
+        e = chess.game_over(id)
+        if e is not None:
+            return chess_game_over(chess, id, e)
+
         text = chess_game_stats(chess, id)
     except KeyError:
         return ["Нет активной игры."]