txlyre 1 week ago
parent
commit
7213267fb0
2 changed files with 22 additions and 6 deletions
  1. 17 6
      chess0.py
  2. 5 0
      commands.py

+ 17 - 6
chess0.py

@@ -1,5 +1,6 @@
 import re
 import time
+import random
 
 import chess
 import chess.svg
@@ -106,13 +107,16 @@ class ChessSession:
 
         moves = moves.strip()
         if re.match(r"^[0-9]{1,3}$", moves):
-            try:
-                moves = int(moves)
-            except ValueError:
-                raise IllegalMove(moves)
+            if moves == "960":
+                moves = random.randint(0, 959)
+            else:
+                try:
+                    moves = int(moves)
+                except ValueError:
+                    raise IllegalMove(moves)
 
-            if moves < 0 or moves > 959:
-                raise IllegalMove(moves)
+                if moves < 0 or moves > 959:
+                    raise IllegalMove(moves)
 
             self.board.set_chess960_pos(moves)
 
@@ -335,6 +339,13 @@ class ChessManager:
 
         return session.game_over
 
+    def fen(self, id):
+        session = self.sessions.get(id)
+        if not session:
+            raise KeyError(id)
+
+        return session.board.fen()
+
     def animate(self, id, count=None, size=256, shallow=False):
         session = self.sessions.get(id)
         if not session:

+ 5 - 0
commands.py

@@ -935,6 +935,9 @@ async def chess_anim_handler(chess, id, arg):
 
     return [buffer]
 
+async def chess_fen_handler(chess, id):
+  return [chess.fen(id)]
+
 CHESS_COMMANDS = {
     "start": (chess_start_handler, "Начать новую игру", 0),
     "from": (chess_from_handler, "Начать новую игру с доской в указанном состоянии", 1, True),
@@ -950,6 +953,8 @@ CHESS_COMMANDS = {
     "create": (chess_start_handler, "Создать общую доску", 0),
     "createfrom": (chess_from_handler, "Создать общую доску в указанном состоянии", 1, True),
 
+    "fen": (chess_fen_handler, "Получить представление доски в FEN", 0),
+
     "anim": (chess_anim_handler, "Создать анимацию последних N ходов", 0, True),
 }