Skip to content

Commit

Permalink
add time controls in play chess
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Feb 9, 2024
1 parent 089e8ff commit 9e99e5e
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 63 deletions.
21 changes: 21 additions & 0 deletions src-tauri/src/chess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ impl EngineProcess {
GoMode::Depth(depth) => format!("go depth {}\n", depth),
GoMode::Time(time) => format!("go movetime {}\n", time),
GoMode::Nodes(nodes) => format!("go nodes {}\n", nodes),
GoMode::PlayersTime(PlayersTime {
white,
black,
winc,
binc,
}) => {
format!(
"go wtime {} btime {} winc {} binc {}\n",
white, black, winc, binc
)
}
GoMode::Infinite => "go infinite\n".to_string(),
};
self.stdin.write_all(msg.as_bytes()).await?;
Expand Down Expand Up @@ -357,12 +368,21 @@ pub struct EngineOption {
#[derive(Deserialize, Debug, Clone, Type, PartialEq, Eq)]
#[serde(tag = "t", content = "c")]
pub enum GoMode {
PlayersTime(PlayersTime),
Depth(u32),
Time(u32),
Nodes(u32),
Infinite,
}

#[derive(Deserialize, Debug, Clone, Type, PartialEq, Eq)]
pub struct PlayersTime {
white: u32,
black: u32,
winc: u32,
binc: u32,
}

#[tauri::command]
#[specta::specta]
pub async fn kill_engines(tab: String, state: tauri::State<'_, AppState>) -> Result<(), Error> {
Expand Down Expand Up @@ -488,6 +508,7 @@ pub async fn get_best_moves(
GoMode::Nodes(nodes) => {
(cur_nodes as f64 / nodes as f64) * 100.0
}
GoMode::PlayersTime(_) => 99.99,
GoMode::Infinite => 99.99,
};
BestMovesPayload {
Expand Down
3 changes: 2 additions & 1 deletion src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ export type DatabaseProgress = { id: string; progress: number }
export type DownloadProgress = { progress: number; id: bigint; finished: boolean }
export type EngineOption = { name: string; value: string }
export type EngineOptions = { fen: string; moves: string[]; extraOptions: EngineOption[] }
export type GoMode = { t: "Depth"; c: number } | { t: "Time"; c: number } | { t: "Nodes"; c: number } | { t: "Infinite" }
export type GoMode = { t: "PlayersTime"; c: PlayersTime } | { t: "Depth"; c: number } | { t: "Time"; c: number } | { t: "Nodes"; c: number } | { t: "Infinite" }
export type MonthData = { count: number; avg_elo: number }
export type PlayersTime = { white: number; black: number; winc: number; binc: number }
export type ReportProgress = { progress: number; id: bigint; finished: boolean }
export type Results = { won: number; lost: number; draw: number }
export type Score = { type: "cp"; value: number } | { type: "mate"; value: number }
Expand Down
50 changes: 45 additions & 5 deletions src/components/boards/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { chessboard } from "@/styles/Chessboard.css";
import {
ANNOTATION_INFO,
Annotation,
TimeControlField,
getMaterialDiff,
makeClk,
moveToKey,
Expand Down Expand Up @@ -83,6 +84,10 @@ interface ChessboardProps {
boardRef: React.MutableRefObject<HTMLDivElement | null>;
saveFile?: () => void;
addGame?: () => void;
whiteTime?: number;
blackTime?: number;
whiteTc?: TimeControlField;
blackTc?: TimeControlField;
}

function Board({
Expand All @@ -100,6 +105,10 @@ function Board({
addGame,
root,
position,
whiteTime,
blackTime,
whiteTc,
blackTc,
}: ChessboardProps) {
const dispatch = useContext(TreeDispatchContext);

Expand Down Expand Up @@ -326,7 +335,7 @@ function Board({
});
if (position.length <= 1 && timeControl) {
if (timeControl.length > 0) {
const seconds = timeControl[0].seconds;
const seconds = timeControl[0].seconds / 1000;
if (!whiteSeconds) {
whiteSeconds = seconds;
}
Expand All @@ -335,13 +344,26 @@ function Board({
}
}
}
if (whiteTime) {
whiteSeconds = whiteTime / 1000;
}
if (blackTime) {
blackSeconds = blackTime / 1000;
}

const topClock = orientation === "black" ? whiteSeconds : blackSeconds;
const topTc = orientation === "black" ? whiteTc : blackTc;
const topProgress =
timeControl && topClock ? topClock / timeControl[0].seconds : 0;
(topTc || timeControl) && topClock
? topClock / ((topTc?.seconds ?? timeControl![0].seconds) / 1000)
: 0;

const bottomClock = orientation === "black" ? blackSeconds : whiteSeconds;
const bottomTc = orientation === "black" ? blackTc : whiteTc;
const bottomProgress =
timeControl && bottomClock ? bottomClock / timeControl[0].seconds : 0;
(topTc || timeControl) && bottomClock
? bottomClock / ((bottomTc?.seconds ?? timeControl![0].seconds) / 1000)
: 0;

const [boardFen, setBoardFen] = useState<string | null>(null);

Expand Down Expand Up @@ -496,7 +518,7 @@ function Board({
}}
>
<Text fz="lg" fw="bold" px="xs">
{makeClk(topClock)}
{formatClock(topClock)}
</Text>
<Progress
size="xs"
Expand Down Expand Up @@ -537,7 +559,7 @@ function Board({
}}
>
<Text fz="lg" fw="bold" px="xs">
{makeClk(bottomClock)}
{formatClock(bottomClock)}
</Text>
<Progress
size="xs"
Expand Down Expand Up @@ -727,4 +749,22 @@ const glyphToSvg = {
),
} as const;

function formatClock(seconds: number) {
let s = Math.max(0, seconds);
const hours = Math.floor(s / 3600);
const minutes = Math.floor((s % 3600) / 60);
s = (s % 3600) % 60;

let timeString = `${minutes.toString().padStart(2, "0")}`;
if (hours > 0) {
timeString = `${hours}:${timeString}`;
}
if (seconds < 60) {
timeString += `:${s.toFixed(1).padStart(4, "0")}`;
} else {
timeString += `:${Math.floor(s).toString().padStart(2, "0")}`;
}
return timeString;
}

export default memo(Board);
Loading

0 comments on commit 9e99e5e

Please sign in to comment.