txlyre 1 day ago
parent
commit
fe59206913
2 changed files with 28 additions and 0 deletions
  1. 20 0
      chess0.py
  2. 8 0
      commands.py

+ 20 - 0
chess0.py

@@ -41,6 +41,7 @@ class ChessSession:
 
         self.game_over = None
         self.checkmate = False
+        self.draw = False
 
     def parse_move(self, move):
         move = move.strip()
@@ -63,6 +64,8 @@ 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
 
             raise self.game_over
         elif self.board.status() & chess.STATUS_NO_WHITE_KING:
@@ -222,6 +225,15 @@ class ChessManager:
 
         await session.move(move)
 
+    def clear(self, id):
+        session = self.sessions.get(id)
+        if not session:
+            raise KeyError(id)
+
+        session.game_over = None
+        session.checkmate = False
+        session.draw = False    
+
     def undo(self, id):
         session = self.sessions.get(id)
         if not session:
@@ -231,6 +243,7 @@ class ChessManager:
 
         session.game_over = None
         session.checkmate = False
+        session.draw = False
 
         session.check_game_over()
 
@@ -248,6 +261,13 @@ class ChessManager:
 
         return session.checkmate
 
+    def is_draw(self, id):
+        session = self.sessions.get(id)
+        if not session:
+            raise KeyError(id)
+
+        return session.draw
+
     async def skip(self, id):
         session = self.sessions.get(id)
         if not session:

+ 8 - 0
commands.py

@@ -678,6 +678,14 @@ def chess_game_stats(chess, id):
 
 
 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)}",