Finishing game

This commit is contained in:
0880
2026-01-18 19:07:25 +03:30
parent 460255db9e
commit 83b570214a

34
app.py
View File

@@ -192,6 +192,20 @@ class Room:
rooms: dict[str, Room] = {}
favicon = Path("favicon.ico").read_bytes()
favicon_update = Path("favicon_update.ico").read_bytes()
@app.GET("/favicon.ico")
async def favicon_s(request):
return HTTPResponse(request, favicon, content_type="image/x-icon")
@app.GET("/favicon_update.ico")
async def favicon_update_s(request):
return HTTPResponse(request, favicon_update, content_type="image/x-icon")
@app.GET("/")
async def home(request):
return render(request, "index.html")
@@ -405,6 +419,8 @@ async def move(request: Request, room_id):
room = rooms[room_id]
if len(room.players) != 2:
return 400, {"code": "PRES", "error": "Game has not been started"}
if room.state != State.NOT_FINISHED:
return 400, {"code": "FINI", "error": "Game has not finished."}
board = room.board
uid = req["uid"]
src = req["from"].lower()
@@ -442,11 +458,27 @@ async def move(request: Request, room_id):
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()
room.last_move = datetime.now(timezone.utc)
opp_checkmate = True
for i in range(8):
for j in range(8):
P = board.index_xy(i, j)
if P != "E" and P.value.isupper() != is_white:
moves = generate_valid_moves(
P.value.lower(), board, not is_white, xy_to_pos_safe(i, j)
)
if len(moves) > 0:
opp_checkmate = False
break
if not opp_checkmate:
break
if opp_checkmate:
room.state = State.BLACK_WIN if is_white else State.WHITE_WIN
return {
"code": "MOVD",
"color": color.value,
"turn": room.turn.value,
"state": room.state.value,
"board": board.serialize(),
}
else: