txlyre 14 hours ago
parent
commit
665cffff92
2 changed files with 26 additions and 15 deletions
  1. 12 6
      chess0.py
  2. 14 9
      commands.py

+ 12 - 6
chess0.py

@@ -14,7 +14,10 @@ class IllegalMove(Exception):
     pass
     pass
 
 
 
 
-def board2svg(board, size=256, shallow=False):
+def board2svg(board, size=256, shallow=False, orientation=None):
+    if orientation is None:
+        orientation = board.turn
+
     arrows = []
     arrows = []
     if board.move_stack and board.move_stack[-1] != chess.Move.null():
     if board.move_stack and board.move_stack[-1] != chess.Move.null():
         arrows.append((board.move_stack[-1].from_square, board.move_stack[-1].to_square))
         arrows.append((board.move_stack[-1].from_square, board.move_stack[-1].to_square))
@@ -23,9 +26,9 @@ def board2svg(board, size=256, shallow=False):
             arrows.append((board.move_stack[-2].from_square, board.move_stack[-2].to_square))
             arrows.append((board.move_stack[-2].from_square, board.move_stack[-2].to_square))
           
           
     if board.is_check():
     if board.is_check():
-        return chess.svg.board(board, orientation=board.turn, size=size, arrows=arrows, fill={checker: "#cc0000cc" for checker in board.checkers()}, check=board.king(board.turn))
+        return chess.svg.board(board, orientation=orientation, size=size, arrows=arrows, fill={checker: "#cc0000cc" for checker in board.checkers()}, check=board.king(board.turn))
 
 
-    return chess.svg.board(board, orientation=board.turn, size=size, arrows=arrows)
+    return chess.svg.board(board, orientation=orientation, size=size, arrows=arrows)
 
 
 
 
 class ChessSession:
 class ChessSession:
@@ -219,7 +222,7 @@ class ChessManager:
 
 
         return session.board.turn
         return session.board.turn
 
 
-    def animate(self, id, count=2, size=256, shallow=False):
+    def animate(self, id, count=None, size=256, shallow=False):
         session = self.sessions.get(id)
         session = self.sessions.get(id)
         if not session:
         if not session:
             raise KeyError(id)
             raise KeyError(id)
@@ -227,11 +230,14 @@ class ChessManager:
         board = chess.Board()
         board = chess.Board()
         frames = []
         frames = []
 
 
-        frames.append(board2svg(board, size=size, shallow=shallow))
+        frames.append(board2svg(board, size=size, shallow=shallow, orientation=chess.WHITE))
 
 
         for move in session.board.move_stack:
         for move in session.board.move_stack:
             board.push(move)
             board.push(move)
 
 
-            frames.append(board2svg(board, size=size, shallow=shallow))
+            frames.append(board2svg(board, size=size, shallow=shallow, orientation=chess.WHITE))
+
+            if count is not None and len(frames) >= count:
+                break
 
 
         return frames
         return frames

+ 14 - 9
commands.py

@@ -839,17 +839,22 @@ async def chess_moves_handler(chess, id):
     return [text]
     return [text]
 
 
 
 
-async def chess_anim_handler(chess, id, count):
-    try:
-        count = int(count)
+async def chess_anim_handler(chess, id, *args):
+    count = None
 
 
-        if count < 2:
-            raise ValueError
-    except Exception:
-        return ["Некорректное значение аргумента."]
+    if len(args) >= 1:
+        count = args[0]
+
+        try:
+            count = int(count)
+
+            if count < 2:
+                raise ValueError
+        except Exception:
+            return ["Некорректное значение аргумента."]
 
 
     try:
     try:
-        frames = chess.animate(id, shallow=isinstance(id, str))
+        frames = chess.animate(id, count=count, shallow=isinstance(id, str))
     except KeyError:
     except KeyError:
         return ["Нет активной игры."]
         return ["Нет активной игры."]
 
 
@@ -879,7 +884,7 @@ CHESS_COMMANDS = {
     "create": (chess_start_handler, "Создать общую доску", 0),
     "create": (chess_start_handler, "Создать общую доску", 0),
     "createfrom": (chess_from_handler, "Создать общую доску в указанном состоянии", 1, True),
     "createfrom": (chess_from_handler, "Создать общую доску в указанном состоянии", 1, True),
 
 
-    "anim": (chess_anim_handler, "Создать анимацию последних N ходов", 1),
+    "anim": (chess_anim_handler, "Создать анимацию последних N ходов", 0, True),
 }
 }
 
 
 CHESS_ALIASES = {
 CHESS_ALIASES = {