Skip to content

Commit

Permalink
do not use cloud eval for repetitions (fixes lichess-org#5472)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Jun 18, 2020
1 parent 138aabd commit 0938908
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
29 changes: 20 additions & 9 deletions ui/analyse/src/ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { storedProp, StoredBooleanProp } from 'common/storage';
import { make as makeSocket, Socket } from './socket';
import { ForecastCtrl } from './forecast/interfaces';
import { make as makeForecast } from './forecast/forecastCtrl';
import { ctrl as cevalCtrl, isEvalBetter, CevalCtrl, Work as CevalWork, CevalOpts } from 'ceval';
import { ctrl as cevalCtrl, isEvalBetter, sanIrreversible, CevalCtrl, Work as CevalWork, CevalOpts } from 'ceval';
import explorerCtrl from './explorer/explorerCtrl';
import { ExplorerCtrl } from './explorer/interfaces';
import * as game from 'game';
Expand Down Expand Up @@ -777,18 +777,29 @@ export default class AnalyseCtrl {
if (uci) this.playUci(uci);
}

canEvalGet = (node: Tree.Node): boolean => this.opts.study || node.ply < 15;
canEvalGet(): boolean {
if (this.node.ply >= 15 && !this.opts.study) return false;

// cloud eval does not support threefold repetition
const fens = new Set();
for (let i = this.nodeList.length - 1; i >= 0; i--) {
const node = this.nodeList[i];
const fen = node.fen.split(' ').slice(0, 4).join(' ');
if (fens.has(fen)) return false;
if (node.san && sanIrreversible(this.data.game.variant.key, node.san)) return true;
fens.add(fen);
}
return true;
}

instanciateEvalCache() {
this.evalCache = makeEvalCache({
variant: this.data.game.variant.key,
canGet: this.canEvalGet,
canPut: (node: Tree.Node) => {
return this.data.evalPut && this.canEvalGet(node) && (
// if not in study, only put decent opening moves
this.opts.study || (!node.ceval!.mate && Math.abs(node.ceval!.cp!) < 99)
);
},
canGet: () => this.canEvalGet(),
canPut: () => this.data.evalPut && this.canEvalGet() && (
// if not in study, only put decent opening moves
this.opts.study || (!this.node.ceval!.mate && Math.abs(this.node.ceval!.cp!) < 99)
),
getNode: () => this.node,
send: this.opts.socketSend,
receive: this.onNewCeval
Expand Down
4 changes: 2 additions & 2 deletions ui/analyse/src/evalCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ export function make(opts): EvalCache {
return {
onCeval: throttle(500, function() {
const node = opts.getNode(), ev = node.ceval;
if (ev && !ev.cloud && node.fen in fetchedByFen && qualityCheck(ev) && opts.canPut(node)) {
if (ev && !ev.cloud && node.fen in fetchedByFen && qualityCheck(ev) && opts.canPut()) {
opts.send('evalPut', toPutData(opts.variant, ev));
}
}),
fetch(path: Tree.Path, multiPv: number): void {
const node = opts.getNode();
if ((node.ceval && node.ceval.cloud) || !opts.canGet(node)) return;
if ((node.ceval && node.ceval.cloud) || !opts.canGet()) return;
const serverEval = fetchedByFen[node.fen];
if (serverEval) return opts.receive(toCeval(serverEval), path);
else if (node.fen in fetchedByFen) return; // waiting for response
Expand Down
9 changes: 1 addition & 8 deletions ui/ceval/src/ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@ import { prop } from 'common';
import { storedProp } from 'common/storage';
import throttle from 'common/throttle';
import { povChances } from './winningChances';
import { sanIrreversible } from './util';

const li = window.lichess;

function sanIrreversible(variant: VariantKey, san: string): boolean {
if (san.startsWith('O-O')) return true;
if (variant === 'crazyhouse') return false;
if (san.includes('x')) return true; // capture
if (san.toLowerCase() === san) return true; // pawn move
return variant === 'threeCheck' && san.includes('+');
}

function officialStockfish(variant: VariantKey): boolean {
return variant === 'standard' || variant === 'chess960';
}
Expand Down
2 changes: 1 addition & 1 deletion ui/ceval/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as view from './view';
import * as winningChances from './winningChances';

export { CevalCtrl, NodeEvals, Eval, Work, CevalOpts } from './types';
export { isEvalBetter, renderEval } from './util';
export { isEvalBetter, renderEval, sanIrreversible } from './util';
export { ctrl, view, winningChances };

// stop when another tab starts. Listen only once here,
Expand Down
8 changes: 8 additions & 0 deletions ui/ceval/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ export function renderEval(e: number): string {
e = Math.max(Math.min(Math.round(e / 10) / 10, 99), -99);
return (e > 0 ? '+' : '') + e.toFixed(1);
}

export function sanIrreversible(variant: VariantKey, san: string): boolean {
if (san.startsWith('O-O')) return true;
if (variant === 'crazyhouse') return false;
if (san.includes('x')) return true; // capture
if (san.toLowerCase() === san) return true; // pawn move
return variant === 'threeCheck' && san.includes('+');
}

0 comments on commit 0938908

Please sign in to comment.