From 539c739c763d4431f54f1f592db19406202ef7a2 Mon Sep 17 00:00:00 2001 From: 0880 <98263509+0880880@users.noreply.github.com> Date: Sat, 17 Jan 2026 19:29:13 +0330 Subject: [PATCH] Fix a bunch of bugs --- app.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/app.py b/app.py index 3b109e9..5043db5 100644 --- a/app.py +++ b/app.py @@ -27,7 +27,7 @@ class Coord: return Coord(self.x, self.y) def __str__(self): - return coord_to_pos_safe(self) + return str(coord_to_pos(self)) def __eq__(self, other): if isinstance(other, Coord): @@ -159,6 +159,7 @@ class Room: turn: Color players: list[str] last_move: datetime + game_start: datetime | None state: State def __init__(self): @@ -166,10 +167,12 @@ class Room: self.board = Board() self.players = [] self.last_move = datetime.now() + self.game_start = None self.state = State.NOT_FINISHED def start(self): self.last_move = datetime.now() + self.game_start = datetime.now() def add_player(self) -> None | tuple[str, Color]: np: int = len(self.players) @@ -243,15 +246,15 @@ def get_piece_moves(piece_kind, board: Board, is_white, src: str) -> list[Coord] if ( board.index_xy(x + 1, y + dir) not in [Piece.EMPTY, Piece.NONE, Piece.BLACK_KING, Piece.WHITE_KING] - and board.index_xy(x + 1, y + dir).value.upper() != is_white + and board.index_xy(x + 1, y + dir).value.isupper() != is_white ): - valids.append(Coord(x=x + 1, y=y + 1)) + valids.append(Coord(x=x + 1, y=y + dir)) if ( board.index_xy(x - 1, y + dir) not in [Piece.EMPTY, Piece.NONE, Piece.BLACK_KING, Piece.WHITE_KING] - and board.index_xy(x - 1, y + dir).value.upper() != is_white + and board.index_xy(x - 1, y + dir).value.isupper() != is_white ): - valids.append(Coord(x=x - 1, y=y + 1)) + valids.append(Coord(x=x - 1, y=y + dir)) elif piece_kind == "b": x, y = pos_to_coord(src) comb = product([1, -1], repeat=2) @@ -309,18 +312,18 @@ def get_piece_moves(piece_kind, board: Board, is_white, src: str) -> list[Coord] if dir[0] ** 2 + dir[1] ** 2 == 0: continue # x=0 y=0 cannot move => invalid target: Coord = Coord(x=x + dir[0], y=y + dir[1]) - if ( - p := board.index_coord(target) - ) != Piece.NONE and p.value.isupper() != is_white: + if (p := board.index_coord(target)) != Piece.NONE and ( + p.value.isupper() != is_white or p.value == "E" + ): valids.append(target.copy()) elif piece_kind == "r": x, y = pos_to_coord(src) moves = [(2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (-1, 2), (1, -2), (-1, -2)] for m in moves: target: Coord = Coord(x=x + m[0], y=y + m[1]) - if ( - p := board.index_coord(target) - ) != Piece.NONE and p.value.isupper() != is_white: + if (p := board.index_coord(target)) != Piece.NONE and ( + p.value.isupper() != is_white or p.value == "E" + ): valids.append(target.copy()) return valids @@ -423,7 +426,7 @@ async def move(request: Request, room_id): board.grid[c.y][c.x] = srcp sx, sy = pos_to_coord(src) board.grid[sy][sx] = Piece.EMPTY - if c.y == 0 or c.y == 7 and srcp in [Piece.BLACK_PAWN, Piece.WHITE_PAWN]: + if c.y == 0 or c.y == 7 and piece_kind == "p": board.grid[c.y][c.x] = Piece.WHITE_QUEEN if is_white else Piece.BLACK_QUEEN room.turn = Color.BLACK if color == Color.WHITE else Color.WHITE room.last_move = datetime.now() @@ -444,6 +447,12 @@ async def poll(request, room_id): room = rooms[key] if (datetime.now() - room.last_move) >= timedelta(hours=24): del rooms[key] + if ( + room.game_start + and (datetime.now() - room.game_start) >= timedelta(minutes=30) + and room.state == State.NOT_FINISHED + ): + room.state = State.TIE if room_id not in rooms: return 400, {"code": "NOEX", "error": "Room does not exist"} room = rooms[room_id]