Skip to content

Commit

Permalink
Merge pull request lichess-org#14257 from lichess-org/puzzle-ctrl-cla…
Browse files Browse the repository at this point in the history
…ss-again

Puzzle ctrl class again
  • Loading branch information
ornicar authored Dec 20, 2023
2 parents bcdbf4f + 901914b commit f80a888
Show file tree
Hide file tree
Showing 17 changed files with 590 additions and 749 deletions.
10 changes: 5 additions & 5 deletions ui/common/src/snabbdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ export type LooseVNodes = (MaybeVNode | boolean)[];
type LooseVNode = VNodeChildElement | boolean;
type VNodeKids = LooseVNode | LooseVNode[];

function filterKids(children: VNodeKids): VNodeChildElement[] {
return (Array.isArray(children) ? children : [children]).filter(
x => (x && x !== true) || x === '', // '' may be falsy but it's a valid VNode
) as VNodeChildElement[];
}
// '' may be falsy but it's a valid VNode
const kidFilter = (x: any): boolean => (x && x !== true) || x === '';

const filterKids = (children: VNodeKids): VNodeChildElement[] =>
(Array.isArray(children) ? children : [children]).filter(kidFilter) as VNodeChildElement[];

/* obviate need for some ternary expressions in renders. Allows
looseH('div', [ kids && h('div', 'kid') ])
Expand Down
18 changes: 9 additions & 9 deletions ui/puzzle/src/autoShape.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { winningChances, CevalCtrl } from 'ceval';
import { DrawModifiers, DrawShape } from 'chessground/draw';
import { Vm } from './interfaces';
import { Api as CgApi } from 'chessground/api';
import { opposite, parseUci, makeSquare } from 'chessops/util';
import { NormalMove } from 'chessops/types';

interface Opts {
vm: Vm;
node: Tree.Node;
showComputer(): boolean;
ceval: CevalCtrl;
ground: CgApi;
nextNodeBest?: Uci;
threatMode: boolean;
nextNodeBest(): Uci | undefined;
threatMode(): boolean;
}

function makeAutoShapesFromUci(
Expand All @@ -30,24 +30,24 @@ function makeAutoShapesFromUci(
}

export default function (opts: Opts): DrawShape[] {
const n = opts.vm.node,
const n = opts.node,
hovering = opts.ceval.hovering(),
color = n.fen.includes(' w ') ? 'white' : 'black';
let shapes: DrawShape[] = [];
if (hovering && hovering.fen === n.fen)
shapes = shapes.concat(makeAutoShapesFromUci(color, hovering.uci, 'paleBlue'));
if (opts.vm.showAutoShapes() && opts.vm.showComputer()) {
if (opts.showComputer()) {
if (n.eval) shapes = shapes.concat(makeAutoShapesFromUci(color, n.eval.best!, 'paleGreen'));
if (!hovering) {
let nextBest: Uci | undefined = opts.nextNodeBest;
let nextBest: Uci | undefined = opts.nextNodeBest();
if (!nextBest && opts.ceval.enabled() && n.ceval) nextBest = n.ceval.pvs[0].moves[0];
if (nextBest) shapes = shapes.concat(makeAutoShapesFromUci(color, nextBest, 'paleBlue'));
if (
opts.ceval.enabled() &&
n.ceval &&
n.ceval.pvs &&
n.ceval.pvs[1] &&
!(opts.threatMode && n.threat && n.threat.pvs[2])
!(opts.threatMode() && n.threat && n.threat.pvs[2])
) {
n.ceval.pvs.forEach(function (pv) {
if (pv.moves[0] === nextBest) return;
Expand All @@ -62,7 +62,7 @@ export default function (opts: Opts): DrawShape[] {
}
}
}
if (opts.ceval.enabled() && opts.threatMode && n.threat) {
if (opts.ceval.enabled() && opts.threatMode() && n.threat) {
if (n.threat.pvs[1]) {
shapes = shapes.concat(makeAutoShapesFromUci(opposite(color), n.threat.pvs[0].moves[0], 'paleRed'));
n.threat.pvs.slice(1).forEach(function (pv) {
Expand Down
28 changes: 14 additions & 14 deletions ui/puzzle/src/control.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { path as treePath } from 'tree';
import { KeyboardController } from './interfaces';
import PuzzleCtrl from './ctrl';

export function canGoForward(ctrl: KeyboardController): boolean {
return ctrl.vm.node.children.length > 0;
export function canGoForward(ctrl: PuzzleCtrl): boolean {
return ctrl.node.children.length > 0;
}

export function next(ctrl: KeyboardController): void {
const child = ctrl.vm.node.children[0];
export function next(ctrl: PuzzleCtrl): void {
const child = ctrl.node.children[0];
if (!child) return;
ctrl.userJump(ctrl.vm.path + child.id);
ctrl.userJump(ctrl.path + child.id);
}

export function prev(ctrl: KeyboardController): void {
ctrl.userJump(treePath.init(ctrl.vm.path));
export function prev(ctrl: PuzzleCtrl): void {
ctrl.userJump(treePath.init(ctrl.path));
}

export function last(ctrl: KeyboardController): void {
const toInit = !treePath.contains(ctrl.vm.path, ctrl.vm.initialPath);
ctrl.userJump(toInit ? ctrl.vm.initialPath : treePath.fromNodeList(ctrl.vm.mainline));
export function last(ctrl: PuzzleCtrl): void {
const toInit = !treePath.contains(ctrl.path, ctrl.initialPath);
ctrl.userJump(toInit ? ctrl.initialPath : treePath.fromNodeList(ctrl.mainline));
}

export function first(ctrl: KeyboardController): void {
const toInit = ctrl.vm.path !== ctrl.vm.initialPath && treePath.contains(ctrl.vm.path, ctrl.vm.initialPath);
ctrl.userJump(toInit ? ctrl.vm.initialPath : treePath.root);
export function first(ctrl: PuzzleCtrl): void {
const toInit = ctrl.path !== ctrl.initialPath && treePath.contains(ctrl.path, ctrl.initialPath);
ctrl.userJump(toInit ? ctrl.initialPath : treePath.root);
}
Loading

0 comments on commit f80a888

Please sign in to comment.