Skip to content

Commit

Permalink
consolidate plyToTurn function and put it in ui/chess
Browse files Browse the repository at this point in the history
  • Loading branch information
allanjoseph98 committed Nov 27, 2024
1 parent e9b0814 commit 29c97d4
Show file tree
Hide file tree
Showing 15 changed files with 22 additions and 15 deletions.
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ui/analyse/src/pgnExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type AnalyseCtrl from './ctrl';
import { h } from 'snabbdom';
import { initialFen, fixCrazySan } from 'chess';
import { type MaybeVNodes } from 'common/snabbdom';
import { plyToTurn } from './util';
import { plyToTurn } from 'chess';

interface PgnNode {
ply: Ply;
Expand Down
2 changes: 1 addition & 1 deletion ui/analyse/src/plugins/analyse.nvui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { makeSan } from 'chessops/san';
import { opposite, parseUci } from 'chessops/util';
import { parseFen } from 'chessops/fen';
import { setupPosition } from 'chessops/variant';
import { plyToTurn } from '../util';
import { plyToTurn } from 'chess';
import { Chessground as makeChessground } from 'chessground';
import { pubsub } from 'common/pubsub';

Expand Down
2 changes: 0 additions & 2 deletions ui/analyse/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export function readOnlyProp<A>(value: A): () => A {
};
}

export const plyToTurn = (ply: number): number => Math.floor((ply - 1) / 2) + 1;

export function treeReconstruct(parts: Tree.Node[], sidelines?: Tree.Node[][]): Tree.Node {
const root = parts[0],
nb = parts.length;
Expand Down
2 changes: 1 addition & 1 deletion ui/analyse/src/view/moveView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { h, type VNode } from 'snabbdom';
import { fixCrazySan } from 'chess';
import { defined } from 'common';
import { view as cevalView, renderEval as normalizeEval } from 'ceval';
import { plyToTurn } from '../util';
import { plyToTurn } from 'chess';

export interface Ctx {
withDots?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion ui/analyse/src/view/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fixCrazySan } from 'chess';
import { attributesModule, classModule, eventListenersModule, init, h, VNodeData } from 'snabbdom';
import { plyToTurn } from '../util';
import { plyToTurn } from 'chess';

export const patch = init([classModule, attributesModule, eventListenersModule]);

Expand Down
1 change: 1 addition & 0 deletions ui/chart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"chartjs-adapter-dayjs-4": "^1.0.4",
"chartjs-plugin-datalabels": "^2.2.0",
"chartjs-plugin-zoom": "^2.0.1",
"chess": "workspace:*",
"common": "workspace:*",
"dayjs": "^1.11.13",
"nouislider": "^15.8.1"
Expand Down
3 changes: 2 additions & 1 deletion ui/chart/src/acpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import division from './division';
import type { AcplChart, AnalyseData, Player } from './interface';
import ChartDataLabels from 'chartjs-plugin-datalabels';
import { pubsub } from 'common/pubsub';
import { plyToTurn } from 'chess';

resizePolyfill();
Chart.register(LineController, LinearScale, PointElement, LineElement, Tooltip, Filler, ChartDataLabels);
Expand Down Expand Up @@ -69,7 +70,7 @@ export default async function (
else if (node.san?.includes('#')) cp = isWhite ? Infinity : -Infinity;
if (cp && d.game.variant.key === 'antichess' && node.san?.includes('#')) cp = -cp;
else if (node.eval?.cp) cp = node.eval.cp;
const turn = Math.floor((node.ply - 1) / 2) + 1;
const turn = plyToTurn(node.ply);
const dots = isWhite ? '.' : '...';
const winchance = winningChances.povChances('white', { cp: cp });
// Plot winchance because logarithmic but display the corresponding cp.eval from AnalyseData in the tooltip
Expand Down
6 changes: 5 additions & 1 deletion ui/chart/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"extends": "../tsconfig.base.json",
"references": [{ "path": "../common/tsconfig.json" }, { "path": "../ceval/tsconfig.json" }]
"references": [
{ "path": "../common/tsconfig.json" },
{ "path": "../ceval/tsconfig.json" },
{ "path": "../chess/tsconfig.json" }
]
}
2 changes: 2 additions & 0 deletions ui/chess/src/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ export function readDrops(line?: string | null): Key[] | null {
if (typeof line === 'undefined' || line === null) return null;
return (line.match(/.{2}/g) as Key[]) || [];
}

export const plyToTurn = (ply: number): number => Math.floor((ply - 1) / 2) + 1;
4 changes: 1 addition & 3 deletions ui/nvui/src/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { chessgroundDests } from 'chessops/compat';
import { type SquareName, RULES, type Rules } from 'chessops/types';
import { setupPosition } from 'chessops/variant';
import { parseUci } from 'chessops/util';
import { type SanToUci, sanWriter } from 'chess';
import { plyToTurn, type SanToUci, sanWriter } from 'chess';
import { storage } from 'common/storage';

export type Style = 'uci' | 'san' | 'literate' | 'nato' | 'anna';
Expand Down Expand Up @@ -722,8 +722,6 @@ export function renderMainline(
return res;
}

const plyToTurn = (ply: Ply): number => Math.floor((ply - 1) / 2) + 1;

export function renderComments(node: Tree.Node, style: Style): string {
if (!node.comments) return '';
return (node.comments || []).map(c => renderComment(c, style)).join('. ');
Expand Down
2 changes: 1 addition & 1 deletion ui/puzzle/src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { winningChances } from 'ceval';
import * as licon from 'common/licon';
import { type StoredProp, storedIntProp } from 'common/storage';
import { domDialog } from 'common/dialog';
import { plyToTurn } from './util';
import { plyToTurn } from 'chess';

export default class Report {
// if local eval suspect multiple solutions, report the puzzle, once at most
Expand Down
1 change: 0 additions & 1 deletion ui/puzzle/src/util.ts

This file was deleted.

2 changes: 1 addition & 1 deletion ui/puzzle/src/view/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { renderEval as normalizeEval } from 'ceval';
import { path as treePath } from 'tree';
import { type MaybeVNode, type LooseVNodes, looseH as h } from 'common/snabbdom';
import type PuzzleCtrl from '../ctrl';
import { plyToTurn } from '../util';
import { plyToTurn } from 'chess';

interface Ctx {
ctrl: PuzzleCtrl;
Expand Down
3 changes: 2 additions & 1 deletion ui/round/src/ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { defined, type Toggle, toggle, requestIdleCallback } from 'common';
import { storage, once, type LichessBooleanStorage } from 'common/storage';
import { pubsub } from 'common/pubsub';
import { readFen, almostSanOf, speakable } from 'chess/sanWriter';
import { plyToTurn } from 'chess';

interface GoneBerserk {
white?: boolean;
Expand Down Expand Up @@ -378,7 +379,7 @@ export default class RoundController implements MoveRootCtrl {
if (this.ply < 1) txt = `${joined}\n${txt}`;
else {
let move = d.steps[d.steps.length - 1].san;
const turn = Math.floor((this.ply - 1) / 2) + 1;
const turn = plyToTurn(this.ply);
move = `${turn}${this.ply % 2 === 1 ? '.' : '...'} ${move}`;
txt = `${opponent}\nplayed ${move}.\n${txt}`;
}
Expand Down

0 comments on commit 29c97d4

Please sign in to comment.