txlyre 17 hours ago
parent
commit
9a0b2f3ce6
2 changed files with 9 additions and 7 deletions
  1. 5 5
      chess0.py
  2. 4 2
      commands.py

+ 5 - 5
chess0.py

@@ -62,17 +62,17 @@ class ChessSession:
 
 
         self.check_game_over()
         self.check_game_over()
 
 
-    def from_moves(self, moves, san=False):
+    def from_moves(self, moves, strict=True):
         self.board.reset()
         self.board.reset()
 
 
         moves = moves.strip().split(" ")
         moves = moves.strip().split(" ")
-        if len(moves) > 600 or len(moves) % 2 != 0:
+        if len(moves) > 600 or (strict and len(moves) % 2 != 0):
             raise IllegalMove
             raise IllegalMove
 
 
         for i, move in zip(range(len(moves)), moves):
         for i, move in zip(range(len(moves)), moves):
             move = self.parse_move(move)
             move = self.parse_move(move)
  
  
-            if move == chess.Move.null() and i % 2 != 0:
+            if strict and move == chess.Move.null() and i % 2 != 0:
                 raise IllegalMove(move)
                 raise IllegalMove(move)
 
 
             self.board.push(move)
             self.board.push(move)
@@ -101,7 +101,7 @@ class ChessManager:
             ):
             ):
                 await self.end(id)
                 await self.end(id)
 
 
-    async def begin(self, id, moves=None):
+    async def begin(self, id, moves=None, strict=True):
         if id in self.sessions:
         if id in self.sessions:
             self.end(id)
             self.end(id)
 
 
@@ -111,7 +111,7 @@ class ChessManager:
         self.sessions[id] = ChessSession(self.engine)
         self.sessions[id] = ChessSession(self.engine)
 
 
         if moves is not None:
         if moves is not None:
-            self.sessions[id].from_moves(moves)
+            self.sessions[id].from_moves(moves, strict=strict)
 
 
     def end(self, id):
     def end(self, id):
         session = self.sessions.get(id)
         session = self.sessions.get(id)

+ 4 - 2
commands.py

@@ -685,7 +685,7 @@ def chess_game_over(chess, id, e):
 
 
 async def chess_from_handler(chess, id, moves):
 async def chess_from_handler(chess, id, moves):
     try:
     try:
-        await chess.begin(id, moves)
+        await chess.begin(id, moves, strict=not isinstance(id, str))
     except GameOver as e:
     except GameOver as e:
         return chess_game_over(chess, id, e)
         return chess_game_over(chess, id, e)
     except IllegalMove as e:
     except IllegalMove as e:
@@ -839,6 +839,7 @@ CHESS_COMMANDS = {
     "moves": (chess_moves_handler, "Показать историю ходов и состояние игры", 0),
     "moves": (chess_moves_handler, "Показать историю ходов и состояние игры", 0),
 
 
     "create": (chess_start_handler, "Создать общую доску", 0),
     "create": (chess_start_handler, "Создать общую доску", 0),
+    "createfrom": (chess_from_handler, "Создать общую доску в указанном состоянии", 1, True),
 }
 }
 
 
 CHESS_ALIASES = {
 CHESS_ALIASES = {
@@ -852,6 +853,7 @@ CHESS_ALIASES = {
     "ms": "moves",
     "ms": "moves",
     "i": "moves",
     "i": "moves",
     "c": "create",
     "c": "create",
+    "cf": "createfrom",
 }
 }
 
 
 
 
@@ -900,7 +902,7 @@ async def chess_handler(bot, event, command):
         args = command.args[1:]
         args = command.args[1:]
 
 
     peer_id = str(get_peer_id(event.peer_id))
     peer_id = str(get_peer_id(event.peer_id))
-    result = await subcommand[0](bot.chess, peer_id if name == "create" or (peer_id in bot.chess.sessions and name not in ("start", "from")) else event.sender.id, *args)
+    result = await subcommand[0](bot.chess, peer_id if name in ("create", "createfrom") or (peer_id in bot.chess.sessions and name not in ("start", "from")) else event.sender.id, *args)
 
 
     for reply in result:
     for reply in result:
         if isinstance(reply, bytes):
         if isinstance(reply, bytes):