beginning of transition
This commit is contained in:
+15
-12
@@ -5,7 +5,8 @@ from dataclasses import dataclass, field
|
||||
from threading import Lock
|
||||
from typing import Optional
|
||||
|
||||
import chess #todo: replace with own chess logic implementation to remove dependency
|
||||
import chess
|
||||
from chess_sim.game_board import ChessBoard, Color, Outcome
|
||||
from flask_login import current_user
|
||||
from flask import request
|
||||
from flask_socketio import emit, join_room, leave_room
|
||||
@@ -23,7 +24,7 @@ class GameRoom:
|
||||
p2_sid: Optional[str] = None
|
||||
p2_name: Optional[str] = None
|
||||
ready: dict[str, bool] = field(default_factory=dict)
|
||||
board: chess.Board = field(default_factory=chess.Board)
|
||||
board: ChessBoard = ChessBoard.init_default()
|
||||
color_by_sid: dict[str, str] = field(default_factory=dict)
|
||||
initial_ms: int = 600000
|
||||
increment_ms: int = 0
|
||||
@@ -81,14 +82,14 @@ def _sid_for_color(room: GameRoom, color: str) -> Optional[str]:
|
||||
|
||||
|
||||
def _turn_sid(room: GameRoom) -> Optional[str]:
|
||||
return _sid_for_color(room, "w" if room.board.turn == chess.WHITE else "b")
|
||||
return _sid_for_color(room, "w" if room.board.turn == Color.WHITE else "b")
|
||||
|
||||
|
||||
def _clock_payload(room: GameRoom) -> dict:
|
||||
return {
|
||||
"white_time_left_ms": max(0, room.white_ms),
|
||||
"black_time_left_ms": max(0, room.black_ms),
|
||||
"turn": "w" if room.board.turn == chess.WHITE else "b",
|
||||
"turn": "w" if room.board.turn == Color.WHITE else "b",
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +102,7 @@ def _apply_elapsed(room: GameRoom) -> Optional[str]:
|
||||
if elapsed_ms <= 0:
|
||||
return None
|
||||
|
||||
active_color = "w" if room.board.turn == chess.WHITE else "b"
|
||||
active_color = "w" if room.board.turn == Color.WHITE else "b"
|
||||
if active_color == "w":
|
||||
room.white_ms = max(0, room.white_ms - elapsed_ms)
|
||||
else:
|
||||
@@ -125,13 +126,14 @@ def _cleanup_room(code: str) -> None:
|
||||
code_by_sid.pop(room.p2_sid, None)
|
||||
|
||||
|
||||
def _game_over_reason(board: chess.Board) -> str:
|
||||
def _game_over_reason(board: ChessBoard) -> str:
|
||||
if board.is_checkmate():
|
||||
return "checkmate"
|
||||
if board.is_stalemate():
|
||||
return "stalemate"
|
||||
if board.is_insufficient_material():
|
||||
return "insufficient material"
|
||||
# todo introduce game over reason insufficient material
|
||||
# if board.is_insufficient_material():
|
||||
# return "insufficient material"
|
||||
if board.is_seventyfive_moves():
|
||||
return "75-move rule"
|
||||
if board.is_fivefold_repetition():
|
||||
@@ -143,10 +145,11 @@ def _emit_game_over(room: GameRoom, reason: str) -> None:
|
||||
room.completed = True
|
||||
room.game_active = False
|
||||
|
||||
outcome = room.board.outcome(claim_draw=True)
|
||||
outcome = room.board.outcome()
|
||||
p1_result = "draw"
|
||||
p2_result = "draw"
|
||||
|
||||
|
||||
if outcome and outcome.winner is not None:
|
||||
winner_color = "w" if outcome.winner == chess.WHITE else "b"
|
||||
if room.color_by_sid.get(room.p1_sid) == winner_color:
|
||||
@@ -181,7 +184,7 @@ def _start_game_if_ready(room: GameRoom) -> None:
|
||||
if not room.ready.get(room.p1_sid) or not room.ready.get(room.p2_sid):
|
||||
return
|
||||
|
||||
room.board = chess.Board()
|
||||
room.board = ChessBoard.init_default()
|
||||
if room.p1_pref == "w":
|
||||
p1_color = "w"
|
||||
elif room.p1_pref == "b":
|
||||
@@ -202,14 +205,14 @@ def _start_game_if_ready(room: GameRoom) -> None:
|
||||
p1_payload = {
|
||||
"play_as": p1_color,
|
||||
"time_left_ms": room.white_ms if p1_color == "w" else room.black_ms,
|
||||
"fen": room.board.fen(),
|
||||
"fen": room.board.to_fen(),
|
||||
"opponent": room.p2_name,
|
||||
**_clock_payload(room),
|
||||
}
|
||||
p2_payload = {
|
||||
"play_as": p2_color,
|
||||
"time_left_ms": room.white_ms if p2_color == "w" else room.black_ms,
|
||||
"fen": room.board.fen(),
|
||||
"fen": room.board.to_fen(),
|
||||
"opponent": room.p1_name,
|
||||
**_clock_payload(room),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user