bot freezing solved and difficulty levels reduced to the original three

This commit is contained in:
2026-10-15 14:08:13 +03:30
parent d8adef472e
commit 8ff113cfba
7 changed files with 148 additions and 126 deletions
+18 -4
View File
@@ -27,6 +27,7 @@ export interface State {
timer_id: string | null;
deadline: number | null;
bot_timer: string | null;
bot_n: number;
moves_played: number;
captures: number;
// Castling rights decay as kings/rooks move or corner rooks are captured.
@@ -234,7 +235,7 @@ export function isValidMove(state: State, from: number, to: number, seat: number
}
// Geometry + king safety, without any turn/phase/seat checks.
function isMoveLegalFor(s: State, from: number, to: number, moverColor: Color): boolean {
export function isMoveLegalFor(s: State, from: number, to: number, moverColor: Color): boolean {
if (!pieceCanMove(s, from, to, moverColor)) return false;
return !wouldLeaveInCheck(s, from, to, moverColor);
}
@@ -297,10 +298,19 @@ function setDeadline(s: State, t: { now: number; setTimer(id: string, due: numbe
t.setTimer(s.timer_id, s.deadline);
}
const pendingBot = (s: State) => s.phase === "play" && !!s.players[s.turn]?.bot && !s.players[s.turn].left;
function scheduleBot(s: State, t: { now: number; setTimer(id: string, due: number): void; clearTimer?(id: string): void }) {
const p = s.players[s.turn];
if (s.phase !== "play" || !p.bot || s.bot_timer) return;
s.bot_timer = `bot-${s.timer_n}`;
if (!pendingBot(s)) {
if (s.bot_timer) {
t.clearTimer?.(s.bot_timer);
s.bot_timer = null;
}
return;
}
if (s.bot_timer) return;
s.bot_n += 1;
s.bot_timer = `bot-${s.bot_n}`;
t.setTimer(s.bot_timer, t.now + 600);
}
@@ -365,6 +375,8 @@ function finish(s: State, ctx: Ctx, winner: number | null, reason: string = "com
s.deadline = null;
if (s.timer_id) ctx.clearTimer(s.timer_id);
s.timer_id = null;
if (s.bot_timer) ctx.clearTimer(s.bot_timer);
s.bot_timer = null;
ctx.finish({
reason: reason === "abandoned" ? "abandoned" : "completed",
standings: s.players.map((p) => ({
@@ -410,6 +422,7 @@ export const chess: GameDef<State> = {
timer_id: null,
deadline: null,
bot_timer: null,
bot_n: 0,
moves_played: 0,
captures: 0,
rights: { wk: true, wq: true, bk: true, bq: true },
@@ -439,6 +452,7 @@ export const chess: GameDef<State> = {
p.user_id = String(input.payload.bot_user_id);
p.name = String(input.payload.display?.name ?? "🤖");
p.bot = true;
if (input.payload.bot_level) p.level = String(input.payload.bot_level);
scheduleBot(s, ctx);
}
return;