remember ceval state in session storage

ceval-session
Niklas Fiekas 2020-12-14 17:40:39 +01:00
parent 2361f9fe3c
commit 9044b67891
4 changed files with 25 additions and 9 deletions

View File

@ -46,6 +46,12 @@ function median(values: number[]): number {
return values.length % 2 ? values[half] : (values[half - 1] + values[half]) / 2.0; return values.length % 2 ? values[half] : (values[half - 1] + values[half]) / 2.0;
} }
function enabledAfterDisable() {
const enabledAfter = lichess.tempStorage.get('ceval.enabled-after');
const disable = lichess.storage.get('ceval.disable');
return !disable || enabledAfter === disable;
}
export default function(opts: CevalOpts): CevalCtrl { export default function(opts: CevalOpts): CevalCtrl {
const storageKey = (k: string) => { const storageKey = (k: string) => {
return opts.storageKeyPrefix ? `${opts.storageKeyPrefix}.${k}` : k; return opts.storageKeyPrefix ? `${opts.storageKeyPrefix}.${k}` : k;
@ -81,7 +87,7 @@ export default function(opts: CevalOpts): CevalCtrl {
const infinite = storedProp('ceval.infinite', false); const infinite = storedProp('ceval.infinite', false);
let curEval: Tree.ClientEval | null = null; let curEval: Tree.ClientEval | null = null;
const allowed = prop(true); const allowed = prop(true);
const enabled = prop(false); const enabled = prop(opts.possible && allowed() && enabledAfterDisable());
let started: Started | false = false; let started: Started | false = false;
let lastStarted: Started | false = false; // last started object (for going deeper even if stopped) let lastStarted: Started | false = false; // last started object (for going deeper even if stopped)
const hovering = prop<Hovering | null>(null); const hovering = prop<Hovering | null>(null);
@ -135,7 +141,7 @@ export default function(opts: CevalOpts): CevalCtrl {
npsRecorder(ev); npsRecorder(ev);
curEval = ev; curEval = ev;
opts.emit(ev, work); opts.emit(ev, work);
if (ev.fen !== lastEmitFen) { if (ev.fen !== lastEmitFen && enabledAfterDisable()) { // amnesty while auto disable not processed
lastEmitFen = ev.fen; lastEmitFen = ev.fen;
lichess.storage.fire('ceval.fen', ev.fen); lichess.storage.fire('ceval.fen', ev.fen);
} }
@ -150,7 +156,7 @@ export default function(opts: CevalOpts): CevalCtrl {
const start = (path: Tree.Path, steps: Step[], threatMode: boolean, deeper: boolean) => { const start = (path: Tree.Path, steps: Step[], threatMode: boolean, deeper: boolean) => {
if (!enabled() || !opts.possible) return; if (!enabled() || !opts.possible || !enabledAfterDisable()) return;
isDeeper(deeper); isDeeper(deeper);
const maxD = effectiveMaxDepth(); const maxD = effectiveMaxDepth();
@ -190,6 +196,10 @@ export default function(opts: CevalOpts): CevalCtrl {
} }
} }
// Notify all other tabs to disable ceval.
lichess.storage.fire('ceval.disable');
lichess.tempStorage.set('ceval.enabled-after', lichess.storage.get('ceval.disable')!);
pool.start(work); pool.start(work);
started = { started = {
@ -238,7 +248,14 @@ export default function(opts: CevalOpts): CevalCtrl {
toggle() { toggle() {
if (!opts.possible || !allowed()) return; if (!opts.possible || !allowed()) return;
stop(); stop();
enabled(!enabled()); if (!enabled() && !document.hidden) {
const disable = lichess.storage.get('ceval.disable');
if (disable) lichess.tempStorage.set('ceval.enabled-after', disable);
enabled(true);
} else {
lichess.tempStorage.set('ceval.enabled-after', '');
enabled(false);
}
}, },
curDepth: () => curEval ? curEval.depth : 0, curDepth: () => curEval ? curEval.depth : 0,
effectiveMaxDepth, effectiveMaxDepth,

View File

@ -9,7 +9,7 @@ export { ctrl, view, winningChances };
// stop when another tab starts. Listen only once here, // stop when another tab starts. Listen only once here,
// as the ctrl can be instantiated several times. // as the ctrl can be instantiated several times.
// gotta do the click on the toggle to have it visually change. // gotta do the click on the toggle to have it visually change.
lichess.storage.make('ceval.disable').listen(_ => { lichess.storage.make('ceval.disable').listen(() => {
const toggle = document.getElementById('analyse-toggle-ceval') as HTMLInputElement | undefined; const toggle = document.getElementById('analyse-toggle-ceval') as HTMLInputElement | undefined;
if (toggle?.checked) toggle.click(); if (toggle?.checked) toggle.click();
}); });

View File

@ -144,7 +144,6 @@ export class Pool {
} }
start(work: Work): void { start(work: Work): void {
lichess.storage.fire('ceval.disable'); // disable on all other tabs
this.getWorker().then(function(worker) { this.getWorker().then(function(worker) {
worker.start(work); worker.start(work);
}).catch(function(error) { }).catch(function(error) {

View File

@ -17,9 +17,9 @@ export function subscribe(ctrl: RoundController): void {
// bots can cheat alright // bots can cheat alright
if (ctrl.data.player.user && ctrl.data.player.user.title === 'BOT') return; if (ctrl.data.player.user && ctrl.data.player.user.title === 'BOT') return;
// Disable ceval. Unless this game is loaded directly on a position being // Notify tabs to disable ceval. Unless this game is loaded directly on a
// analysed, there is plenty of time (7 moves, in most cases) for this to // position being analysed, there is plenty of time (7 moves, in most cases)
// take effect. // for this to take effect.
lichess.storage.fire('ceval.disable'); lichess.storage.fire('ceval.disable');
lichess.storage.make('ceval.fen').listen(e => { lichess.storage.make('ceval.fen').listen(e => {