txlyre 1 day ago
parent
commit
8b3b6cb22e
2 changed files with 49 additions and 15 deletions
  1. 12 6
      chess0.py
  2. 37 9
      commands.py

+ 12 - 6
chess0.py

@@ -41,7 +41,7 @@ class ChessSession:
 
         self.game_over = None
         self.checkmate = False
-        self.draw = False
+        self.draw = None
 
     def parse_move(self, move):
         move = move.strip()
@@ -65,7 +65,10 @@ class ChessSession:
             if outcome.termination == chess.Termination.CHECKMATE:
                 self.checkmate = True
             elif outcome.termination in (chess.Termination.THREEFOLD_REPETITION, chess.Termination.FIFTY_MOVES):
-                self.draw = True
+                self.draw = self.game_over
+                self.game_over = None
+
+                return
 
             raise self.game_over
         elif self.board.status() & chess.STATUS_NO_WHITE_KING:
@@ -232,7 +235,7 @@ class ChessManager:
 
         session.game_over = None
         session.checkmate = False
-        session.draw = False    
+        session.draw = None    
 
     def undo(self, id):
         session = self.sessions.get(id)
@@ -243,7 +246,7 @@ class ChessManager:
 
         session.game_over = None
         session.checkmate = False
-        session.draw = False
+        session.draw = None
 
         session.check_game_over()
 
@@ -261,12 +264,15 @@ class ChessManager:
 
         return session.checkmate
 
-    def is_draw(self, id):
+    def draw(self, id):
         session = self.sessions.get(id)
         if not session:
             raise KeyError(id)
 
-        return session.draw
+        draw = session.draw
+        session.draw = None
+
+        return draw
 
     async def skip(self, id):
         session = self.sessions.get(id)

+ 37 - 9
commands.py

@@ -674,18 +674,14 @@ def chess_game_stats(chess, id):
     elif chess.is_check(id):
         text = f"Шах!\n{text}"
 
+    draw = chess.draw(id)
+    if draw:
+        text = f"Ничья: {str(e)}\n{text}"
+
     return text
 
 
 def chess_game_over(chess, id, e):
-    if chess.is_draw(id):
-        chess.clear(id)
-
-        return [
-            chess_render(chess, id, False),
-            f"Ничья: {str(e)}",
-        ]
-
     return [
       chess_render(chess, id, False),
       f"Конец игры: {str(e)}",
@@ -719,6 +715,10 @@ async def chess_from_handler(chess, id, moves):
     if chess.is_check(id):
         reply.append("Шах!")
 
+    draw = chess.draw(id)
+    if draw:
+        reply.append(f"Ничья: {str(draw)}")
+
     if isinstance(id, str):
         reply.append(f"Ход {'белых' if chess.turn(id) else 'чёрных'}.")
 
@@ -756,6 +756,10 @@ async def chess_move_handler(chess, id, move):
     if chess.is_check(id):
         reply.append("Шах!")
 
+    draw = chess.draw(id)
+    if draw:
+        reply.append(f"Ничья: {str(draw)}")   
+
     return reply
 
 
@@ -774,8 +778,16 @@ async def chess_undo_handler(chess, id):
     except GameOver as e:
         return chess_game_over(chess, id, e)
 
-    return ["Последний ход отменён.", svg2png(chess.svg(id))]
+    reply = ["Последний ход отменён.", svg2png(chess.svg(id))]
+
+    if chess.is_check(id):
+        reply.append("Шах!")
+
+    draw = chess.draw(id)
+    if draw:
+        reply.append(f"Ничья: {str(draw)}")
 
+    return reply
 
 async def chess_skip_handler(chess, id):
     is_group = isinstance(id, str)
@@ -795,6 +807,10 @@ async def chess_skip_handler(chess, id):
     if chess.is_check(id):
         reply.append("Шах!")
 
+    draw = chess.draw(id)
+    if draw:
+        reply.append(f"Ничья: {str(e)}")
+
     return reply
 
 
@@ -816,6 +832,10 @@ async def chess_pass_handler(chess, id):
     if chess.is_check(id):
         reply.append("Шах!")
 
+    draw = chess.draw(id)
+    if draw:
+        reply.append(f"Ничья: {str(e)}")
+
     return reply
 
 
@@ -832,6 +852,10 @@ async def chess_board_handler(chess, id):
     if chess.is_check(id):
         reply.append("Шах!")
 
+    draw = chess.draw(id)
+    if draw:
+        reply.append(f"Ничья: {str(e)}")
+
     if isinstance(id, str):
         reply.append(f"Ход {'белых' if chess.turn(id) else 'чёрных'}.")
 
@@ -872,6 +896,10 @@ async def chess_set_handler(chess, id, moves):
     if chess.is_check(id):
         reply.append("Шах!")
 
+    draw = chess.draw(id)
+    if draw:
+        reply.append(f"Ничья: {str(e)}")
+
     if isinstance(id, str):
         reply.append(f"Ход {'белых' if chess.turn(id) else 'чёрных'}.")