txlyre 17 hours ago
parent
commit
f20d457347
2 changed files with 19 additions and 12 deletions
  1. 3 3
      chess0.py
  2. 16 9
      commands.py

+ 3 - 3
chess0.py

@@ -150,16 +150,16 @@ class ChessManager:
 
 
         session.skip()
         session.skip()
 
 
-    def svg(self, id, size=256):
+    def svg(self, id, 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)
 
 
         arrows = []
         arrows = []
-        if session.board.move_stack:
+        if session.board.move_stack and session.board.move_stack[-1] != chess.Move.null():
             arrows.append((session.board.move_stack[-1].from_square, session.board.move_stack[-1].to_square))
             arrows.append((session.board.move_stack[-1].from_square, session.board.move_stack[-1].to_square))
 
 
-            if len(session.board.move_stack) > 1 and session.board.move_stack[-2] != chess.Move.null():
+            if not shallow and len(session.board.move_stack) > 1 and session.board.move_stack[-2] != chess.Move.null():
                 arrows.append((session.board.move_stack[-2].from_square, session.board.move_stack[-2].to_square))
                 arrows.append((session.board.move_stack[-2].from_square, session.board.move_stack[-2].to_square))
           
           
         if session.board.is_check():
         if session.board.is_check():

+ 16 - 9
commands.py

@@ -652,10 +652,19 @@ async def roll_handler(bot, event, command):
     )
     )
 
 
 
 
+def chess_render(chess, id, as_list=True):
+    board = svg2png(chess.svg(id, shallow=isinstance(id, str)))
+
+    if as_list:
+        return [board]
+
+    return board
+
+
 async def chess_start_handler(chess, id):
 async def chess_start_handler(chess, id):
     await chess.begin(id)
     await chess.begin(id)
 
 
-    return [svg2png(chess.svg(id))]
+    return chess_render(chess, id)
 
 
 
 
 def chess_game_stats(chess, id):
 def chess_game_stats(chess, id):
@@ -671,7 +680,7 @@ def chess_game_stats(chess, id):
 
 
 
 
 def chess_game_over(chess, id, e):
 def chess_game_over(chess, id, e):
-    board = svg2png(chess.svg(id))
+    board = chess_render(chess, id, False)
     stats = chess_game_stats(chess, id)
     stats = chess_game_stats(chess, id)
 
 
     chess.end(id)
     chess.end(id)
@@ -698,7 +707,7 @@ async def chess_from_handler(chess, id, moves):
 
 
         return ["Некорректная последовательность ходов."]
         return ["Некорректная последовательность ходов."]
 
 
-    reply = [svg2png(chess.svg(id))]
+    reply = chess_render(chess, id)
 
 
     if chess.is_check(id):
     if chess.is_check(id):
         reply.append("Шах!")
         reply.append("Шах!")
@@ -732,7 +741,7 @@ async def chess_move_handler(chess, id, move):
     except IllegalMove:
     except IllegalMove:
         return ["Некорректный ход."]
         return ["Некорректный ход."]
 
 
-    reply = [svg2png(chess.svg(id))]
+    reply = chess_render(chess, id)
 
 
     if chess.is_check(id):
     if chess.is_check(id):
         reply.append("Шах!")
         reply.append("Шах!")
@@ -769,7 +778,7 @@ async def chess_skip_handler(chess, id):
     except GameOver as e:
     except GameOver as e:
         return chess_game_over(chess, id, e)
         return chess_game_over(chess, id, e)
 
 
-    reply = [svg2png(chess.svg(id))]
+    reply = chess_render(chess, id)
 
 
     if chess.is_check(id):
     if chess.is_check(id):
         reply.append("Шах!")
         reply.append("Шах!")
@@ -790,7 +799,7 @@ async def chess_pass_handler(chess, id):
     except GameOver as e:
     except GameOver as e:
         return chess_game_over(chess, id, e)
         return chess_game_over(chess, id, e)
 
 
-    reply = [svg2png(chess.svg(id))]
+    reply = chess_render(chess, id)
 
 
     if chess.is_check(id):
     if chess.is_check(id):
         reply.append("Шах!")
         reply.append("Шах!")
@@ -800,12 +809,10 @@ async def chess_pass_handler(chess, id):
 
 
 async def chess_board_handler(chess, id):
 async def chess_board_handler(chess, id):
     try:
     try:
-        board = svg2png(chess.svg(id))
+        reply = chess_render(chess, id)
     except KeyError:
     except KeyError:
         return ["Нет активной игры."]
         return ["Нет активной игры."]
 
 
-    reply = [board]
-
     if chess.is_check(id):
     if chess.is_check(id):
         reply.append("Шах!")
         reply.append("Шах!")