Skip to content

Commit 82de550

Browse files
committed
Merge branch 'develop' of https://github.com/AbstractPlay/node-backend into develop
2 parents cfad1ce + f3e5bde commit 82de550

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
@@ -3540,6 +3540,7 @@ async function submitMove(userid: string, pars: { id: string, move: string, draw
35403540
const simultaneous = flags !== undefined && flags.includes('simultaneous');
35413541
const lastMoveTime = (new Date(engine.stack[engine.stack.length - 1]._timestamp)).getTime();
35423542
let autoMoves = 0;
3543+
let autoMovesPerPlayer: number[] = [];
35433544
const list: Promise<any>[] = [];
35443545

35453546
try {
@@ -3554,6 +3555,7 @@ async function submitMove(userid: string, pars: { id: string, move: string, draw
35543555
} else {
35553556
const result = applyMove(userid, pars.move, currentMoveNumber, engine, game, flags, opponentExploration, pars.exploration);
35563557
autoMoves = result.autoMoves;
3558+
autoMovesPerPlayer = result.autoMovesPerPlayer;
35573559
for (const workItem of result.work) {
35583560
list.push(workItem);
35593561
}
@@ -3583,6 +3585,13 @@ async function submitMove(userid: string, pars: { id: string, move: string, draw
35833585
else
35843586
player.time = player.time! - timeUsed + game.clockInc * 3600000;
35853587
if (player.time > game.clockMax * 3600000) player.time = game.clockMax * 3600000;
3588+
// Apply time increments for players whose moves were auto-applied (forced moves or premoves)
3589+
for (let i = 0; i < autoMovesPerPlayer.length; i++) {
3590+
if (autoMovesPerPlayer[i] > 0) {
3591+
game.players[i].time = (game.players[i].time || 0) + autoMovesPerPlayer[i] * game.clockInc * 3600000;
3592+
if (game.players[i].time! > game.clockMax * 3600000) game.players[i].time = game.clockMax * 3600000;
3593+
}
3594+
}
35863595
// console.log("players", game.players);
35873596
const playerIDs = game.players.map((p: { id: any; }) => p.id);
35883597
// TODO: We are updating players and their games. This should be put in some kind of critical section!
@@ -4423,7 +4432,7 @@ function applyMove(
44234432
flags: string[],
44244433
opponentExploration: Exploration[] | null = null,
44254434
myExploration: Exploration[] | null = null
4426-
): {autoMoves: number, work: Promise<any>[]} {
4435+
): {autoMoves: number, autoMovesPerPlayer: number[], work: Promise<any>[]} {
44274436
// non simultaneous move game.
44284437
if (game.players[parseInt(game.toMove as string)].id !== userid) {
44294438
throw new Error('It is not your turn!');
@@ -4439,6 +4448,7 @@ function applyMove(
44394448
explorations[opponentPlayerIndex] = opponentExploration;
44404449
console.log(`My explorations: ${JSON.stringify(explorations[myPlayerIndex])}, Opponent explorations: ${JSON.stringify(explorations[opponentPlayerIndex])}`);
44414450
let autoMoves = 0;
4451+
const autoMovesPerPlayer: number[] = new Array(game.players.length).fill(0);
44424452

44434453
// Apply the initial submitted move
44444454
console.log(`Applying submitted move: ${move}`);
@@ -4464,9 +4474,12 @@ function applyMove(
44644474
}
44654475
}
44664476
if (move) {
4477+
// @ts-ignore
4478+
const movedPlayerIndex = engine.currplayer - 1;
44674479
try {
44684480
engine.move(move);
44694481
autoMoves += 1;
4482+
autoMovesPerPlayer[movedPlayerIndex] += 1;
44704483
} catch (e) {
44714484
console.log(`Premove ${move} is invalid!: ${e}`);
44724485
break;
@@ -4517,7 +4530,7 @@ function applyMove(
45174530
}
45184531
game.toMove = `${engine.currplayer - 1}`;
45194532
}
4520-
return {autoMoves, work};
4533+
return {autoMoves, autoMovesPerPlayer, work};
45214534
}
45224535

45234536
function isInterestingComment(comment: string): boolean {

0 commit comments

Comments
 (0)