From: Benjamin Auder <benjamin.auder@somewhere>
Date: Mon, 10 Dec 2018 15:58:42 +0000 (+0100)
Subject: Fix thinking time cap for computer (5 seconds)
X-Git-Url: https://git.auder.net/variants/Chakart/doc/html/pieces/common.css?a=commitdiff_plain;h=e82cd97976a6c7707467a384f7d6ca1daa9a0772;p=vchess.git

Fix thinking time cap for computer (5 seconds)
---

diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js
index 5c1ef97c..a8f24893 100644
--- a/public/javascripts/base_rules.js
+++ b/public/javascripts/base_rules.js
@@ -824,7 +824,6 @@ class ChessRules
 	// NOTE: works also for extinction chess because depth is 3...
 	getComputerMove()
 	{
-		this.shouldReturn = false;
 		const maxeval = VariantRules.INFINITY;
 		const color = this.turn;
 		// Some variants may show a bigger moves list to the human (Switching),
@@ -889,14 +888,17 @@ class ChessRules
 			candidates.push(j);
 		let currentBest = moves1[_.sample(candidates, 1)];
 
+		// From here, depth >= 3: may take a while, so we control time
+		const timeStart = Date.now();
+
 		// Skip depth 3+ if we found a checkmate (or if we are checkmated in 1...)
 		if (VariantRules.SEARCH_DEPTH >= 3
 			&& Math.abs(moves1[0].eval) < VariantRules.THRESHOLD_MATE)
 		{
 			for (let i=0; i<moves1.length; i++)
 			{
-				if (this.shouldReturn)
-					return currentBest; //depth-2, minimum
+				if (Date.now()-timeStart >= 5000) //more than 5 seconds
+					return currentBest; //depth 2 at least
 				this.play(moves1[i]);
 				// 0.1 * oldEval : heuristic to avoid some bad moves (not all...)
 				moves1[i].eval = 0.1*moves1[i].eval +
diff --git a/public/javascripts/components/game.js b/public/javascripts/components/game.js
index 7b238f5c..44786ff9 100644
--- a/public/javascripts/components/game.js
+++ b/public/javascripts/components/game.js
@@ -18,7 +18,6 @@ Vue.component('my-game', {
 			incheck: [],
 			pgnTxt: "",
 			expert: (getCookie("expert") === "1" ? true : false),
-			gameId: "", //used to limit computer moves' time
 		};
 	},
 	render(h) {
@@ -855,7 +854,6 @@ Vue.component('my-game', {
 				}
 				return;
 			}
-			this.gameId = getRandString();
 			this.vr = new VariantRules(fen, moves || []);
 			this.score = "*";
 			this.pgnTxt = ""; //redundant with this.score = "*", but cleaner
@@ -895,17 +893,6 @@ Vue.component('my-game', {
 		},
 		playComputerMove: function() {
 			const timeStart = Date.now();
-			// We use moves' count to know if search finished:
-			const nbMoves = this.vr.moves.length;
-			const gameId = this.gameId; //to know if game was reset before timer end
-			setTimeout(
-				() => {
-					if (gameId != this.gameId)
-						return; //game stopped
-					const L = this.vr.moves.length;
-					if (nbMoves == L || !this.vr.moves[L-1].notation) //move search didn't finish
-						this.vr.shouldReturn = true;
-				}, 5000);
 			const compMove = this.vr.getComputerMove();
 			// (first move) HACK: avoid selecting elements before they appear on page:
 			const delay = Math.max(500-(Date.now()-timeStart), 0);