txlyre 2 days ago
parent
commit
167adc1d77
2 changed files with 22 additions and 13 deletions
  1. 19 11
      chess0.py
  2. 3 2
      openkriemy.py

+ 19 - 11
chess0.py

@@ -26,14 +26,8 @@ class ChessSession:
         move = move.strip()
 
         try:
-            if move.startswith(":"):
-                move = self.board.parse_san(move[1:])
-            else:
-                move = self.board.parse_uci(move)
-        except (chess.InvalidMoveError, chess.IllegalMoveError):
-            raise IllegalMove(move)
-
-        if move != chess.Move.null() and move not in self.board.legal_moves:
+            move = self.board.parse_san(move)
+        except (chess.InvalidMoveError, chess.IllegalMoveError, chess.AmbiguousMoveError):
             raise IllegalMove(move)
 
         return move
@@ -65,9 +59,23 @@ class ChessSession:
     def from_moves(self, moves, strict=True):
         self.board.reset()
 
-        moves = moves.strip().split(" ")
-        if len(moves) > 600 or (strict and len(moves) % 2 != 0):
-            raise IllegalMove
+        moves = moves.strip()
+        if moves.startswith("1."):
+            text = moves
+            moves = []
+
+            while text and len(moves) < 600:
+                match = re.match(r"^(?:\d+\. ?([^.\s]+) ([^.\s]+))", text)
+                if not match:
+                    raise IllegalMove
+
+                moves.extend((match.group(1), match.group(2)))           
+
+                text = text[match.end() - match.start():].strip()
+        else:
+            moves = moves.split(" ")
+            if len(moves) > 600 or (strict and len(moves) % 2 != 0):
+                raise IllegalMove
 
         for i, move in zip(range(len(moves)), moves):
             move = self.parse_move(move)

+ 3 - 2
openkriemy.py

@@ -24,7 +24,7 @@ from actions import (
     markov_say,
     run,
 )
-from commands import COMMANDS
+from commands import COMMANDS, CHESS_COMMANDS, CHESS_ALIASES
 from markov import Markov
 from chess0 import ChessManager
 
@@ -82,8 +82,9 @@ async def on_message(event):
             reply = await event.get_reply_message()
             if reply and get_peer_id(reply.from_id) == await bot.get_peer_id("me") and reply.media:
                 text = text.strip()
+                cmd = text.lower().split(" ")[0]
 
-                if text.startswith(":") or re.match(r"^[a-hA-H][1-8][a-hA-H][1-8]$", text):
+                if cmd not in CHESS_COMMANDS and cmd not in CHESS_ALIASES:
                     text = f"/chess m {text}"
                 else:
                     text = f"/chess {text}"