Finishing game
This commit is contained in:
34
app.py
34
app.py
@@ -192,6 +192,20 @@ class Room:
|
|||||||
rooms: dict[str, 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("/")
|
@app.GET("/")
|
||||||
async def home(request):
|
async def home(request):
|
||||||
return render(request, "index.html")
|
return render(request, "index.html")
|
||||||
@@ -405,6 +419,8 @@ async def move(request: Request, room_id):
|
|||||||
room = rooms[room_id]
|
room = rooms[room_id]
|
||||||
if len(room.players) != 2:
|
if len(room.players) != 2:
|
||||||
return 400, {"code": "PRES", "error": "Game has not been started"}
|
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
|
board = room.board
|
||||||
uid = req["uid"]
|
uid = req["uid"]
|
||||||
src = req["from"].lower()
|
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":
|
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(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 {
|
return {
|
||||||
"code": "MOVD",
|
"code": "MOVD",
|
||||||
"color": color.value,
|
"color": color.value,
|
||||||
"turn": room.turn.value,
|
"turn": room.turn.value,
|
||||||
|
"state": room.state.value,
|
||||||
"board": board.serialize(),
|
"board": board.serialize(),
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user