Skip to content

Commit f3e5bde

Browse files
Apply time increments for players whose moves were auto-applied (forced moves or premoves)
1 parent 73c1884 commit f3e5bde

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

api/abstractplay.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3535,6 +3535,7 @@ async function submitMove(userid: string, pars: { id: string, move: string, draw
35353535
const simultaneous = flags !== undefined && flags.includes('simultaneous');
35363536
const lastMoveTime = (new Date(engine.stack[engine.stack.length - 1]._timestamp)).getTime();
35373537
let autoMoves = 0;
3538+
let autoMovesPerPlayer: number[] = [];
35383539
const list: Promise<any>[] = [];
35393540

35403541
try {
@@ -3549,6 +3550,7 @@ async function submitMove(userid: string, pars: { id: string, move: string, draw
35493550
} else {
35503551
const result = applyMove(userid, pars.move, currentMoveNumber, engine, game, flags, opponentExploration, pars.exploration);
35513552
autoMoves = result.autoMoves;
3553+
autoMovesPerPlayer = result.autoMovesPerPlayer;
35523554
for (const workItem of result.work) {
35533555
list.push(workItem);
35543556
}
@@ -3578,6 +3580,13 @@ async function submitMove(userid: string, pars: { id: string, move: string, draw
35783580
else
35793581
player.time = player.time! - timeUsed + game.clockInc * 3600000;
35803582
if (player.time > game.clockMax * 3600000) player.time = game.clockMax * 3600000;
3583+
// Apply time increments for players whose moves were auto-applied (forced moves or premoves)
3584+
for (let i = 0; i < autoMovesPerPlayer.length; i++) {
3585+
if (autoMovesPerPlayer[i] > 0) {
3586+
game.players[i].time = (game.players[i].time || 0) + autoMovesPerPlayer[i] * game.clockInc * 3600000;
3587+
if (game.players[i].time! > game.clockMax * 3600000) game.players[i].time = game.clockMax * 3600000;
3588+
}
3589+
}
35813590
// console.log("players", game.players);
35823591
const playerIDs = game.players.map((p: { id: any; }) => p.id);
35833592
// TODO: We are updating players and their games. This should be put in some kind of critical section!
@@ -4418,7 +4427,7 @@ function applyMove(
44184427
flags: string[],
44194428
opponentExploration: Exploration[] | null = null,
44204429
myExploration: Exploration[] | null = null
4421-
): {autoMoves: number, work: Promise<any>[]} {
4430+
): {autoMoves: number, autoMovesPerPlayer: number[], work: Promise<any>[]} {
44224431
// non simultaneous move game.
44234432
if (game.players[parseInt(game.toMove as string)].id !== userid) {
44244433
throw new Error('It is not your turn!');
@@ -4434,6 +4443,7 @@ function applyMove(
44344443
explorations[opponentPlayerIndex] = opponentExploration;
44354444
console.log(`My explorations: ${JSON.stringify(explorations[myPlayerIndex])}, Opponent explorations: ${JSON.stringify(explorations[opponentPlayerIndex])}`);
44364445
let autoMoves = 0;
4446+
const autoMovesPerPlayer: number[] = new Array(game.players.length).fill(0);
44374447

44384448
// Apply the initial submitted move
44394449
console.log(`Applying submitted move: ${move}`);
@@ -4459,9 +4469,12 @@ function applyMove(
44594469
}
44604470
}
44614471
if (move) {
4472+
// @ts-ignore
4473+
const movedPlayerIndex = engine.currplayer - 1;
44624474
try {
44634475
engine.move(move);
44644476
autoMoves += 1;
4477+
autoMovesPerPlayer[movedPlayerIndex] += 1;
44654478
} catch (e) {
44664479
console.log(`Premove ${move} is invalid!: ${e}`);
44674480
break;
@@ -4512,7 +4525,7 @@ function applyMove(
45124525
}
45134526
game.toMove = `${engine.currplayer - 1}`;
45144527
}
4515-
return {autoMoves, work};
4528+
return {autoMoves, autoMovesPerPlayer, work};
45164529
}
45174530

45184531
function isInterestingComment(comment: string): boolean {

0 commit comments

Comments
 (0)