Fix a bunch of bugs

This commit is contained in:
0880
2026-01-17 19:29:13 +03:30
parent dd91a8723b
commit 539c739c76

33
app.py
View File

@@ -27,7 +27,7 @@ class Coord:
return Coord(self.x, self.y) return Coord(self.x, self.y)
def __str__(self): def __str__(self):
return coord_to_pos_safe(self) return str(coord_to_pos(self))
def __eq__(self, other): def __eq__(self, other):
if isinstance(other, Coord): if isinstance(other, Coord):
@@ -159,6 +159,7 @@ class Room:
turn: Color turn: Color
players: list[str] players: list[str]
last_move: datetime last_move: datetime
game_start: datetime | None
state: State state: State
def __init__(self): def __init__(self):
@@ -166,10 +167,12 @@ class Room:
self.board = Board() self.board = Board()
self.players = [] self.players = []
self.last_move = datetime.now() self.last_move = datetime.now()
self.game_start = None
self.state = State.NOT_FINISHED self.state = State.NOT_FINISHED
def start(self): def start(self):
self.last_move = datetime.now() self.last_move = datetime.now()
self.game_start = datetime.now()
def add_player(self) -> None | tuple[str, Color]: def add_player(self) -> None | tuple[str, Color]:
np: int = len(self.players) np: int = len(self.players)
@@ -243,15 +246,15 @@ def get_piece_moves(piece_kind, board: Board, is_white, src: str) -> list[Coord]
if ( if (
board.index_xy(x + 1, y + dir) board.index_xy(x + 1, y + dir)
not in [Piece.EMPTY, Piece.NONE, Piece.BLACK_KING, Piece.WHITE_KING] 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 ( if (
board.index_xy(x - 1, y + dir) board.index_xy(x - 1, y + dir)
not in [Piece.EMPTY, Piece.NONE, Piece.BLACK_KING, Piece.WHITE_KING] 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": elif piece_kind == "b":
x, y = pos_to_coord(src) x, y = pos_to_coord(src)
comb = product([1, -1], repeat=2) 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: if dir[0] ** 2 + dir[1] ** 2 == 0:
continue # x=0 y=0 cannot move => invalid continue # x=0 y=0 cannot move => invalid
target: Coord = Coord(x=x + dir[0], y=y + dir[1]) target: Coord = Coord(x=x + dir[0], y=y + dir[1])
if ( if (p := board.index_coord(target)) != Piece.NONE and (
p := board.index_coord(target) p.value.isupper() != is_white or p.value == "E"
) != Piece.NONE and p.value.isupper() != is_white: ):
valids.append(target.copy()) valids.append(target.copy())
elif piece_kind == "r": elif piece_kind == "r":
x, y = pos_to_coord(src) x, y = pos_to_coord(src)
moves = [(2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (-1, 2), (1, -2), (-1, -2)] moves = [(2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (-1, 2), (1, -2), (-1, -2)]
for m in moves: for m in moves:
target: Coord = Coord(x=x + m[0], y=y + m[1]) target: Coord = Coord(x=x + m[0], y=y + m[1])
if ( if (p := board.index_coord(target)) != Piece.NONE and (
p := board.index_coord(target) p.value.isupper() != is_white or p.value == "E"
) != Piece.NONE and p.value.isupper() != is_white: ):
valids.append(target.copy()) valids.append(target.copy())
return valids return valids
@@ -423,7 +426,7 @@ async def move(request: Request, room_id):
board.grid[c.y][c.x] = srcp board.grid[c.y][c.x] = srcp
sx, sy = pos_to_coord(src) sx, sy = pos_to_coord(src)
board.grid[sy][sx] = Piece.EMPTY 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 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.turn = Color.BLACK if color == Color.WHITE else Color.WHITE
room.last_move = datetime.now() room.last_move = datetime.now()
@@ -444,6 +447,12 @@ async def poll(request, room_id):
room = rooms[key] room = rooms[key]
if (datetime.now() - room.last_move) >= timedelta(hours=24): if (datetime.now() - room.last_move) >= timedelta(hours=24):
del rooms[key] 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: if room_id not in rooms:
return 400, {"code": "NOEX", "error": "Room does not exist"} return 400, {"code": "NOEX", "error": "Room does not exist"}
room = rooms[room_id] room = rooms[room_id]