Skip to content

Commit

Permalink
sanWriter.sanOf doesn't return a SAN
Browse files Browse the repository at this point in the history
But something that looks like a SAN, yet misses check flag and proper
disambiguation. I'm calling it an `AlmostSan`.

`speakable(san: San)` won't work properly if passed an `AlmostSan`.

I wish types could help us differentiate the two.
  • Loading branch information
ornicar committed Nov 27, 2024
1 parent 57f589c commit 84180ba
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions ui/@types/lichess/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ type Perf = Exclude<VariantKey, 'standard'> | Speed;

type Uci = string;
type San = string;
type AlmostSan = string;
type Ply = number;
type Seconds = number;
type Centis = number;
Expand Down
10 changes: 7 additions & 3 deletions ui/chess/src/sanWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ function slidingMovesTo(s: number, deltas: number[], board: Board): number[] {
return result;
}

export function sanOf(board: Board, uci: string): San {
/* Produces a string that resembles a SAN,
* but lacks the check/checkmate flag,
* and probably has incomplete disambiguation.
* But it's quick. */
export function almostSanOf(board: Board, uci: string): AlmostSan {
if (uci.includes('@')) return fixCrazySan(uci);

const move = decomposeUci(uci);
Expand All @@ -96,7 +100,7 @@ export function sanOf(board: Board, uci: string): San {

// pawn moves
if (pt === 'p') {
let san: string;
let san: AlmostSan;
if (uci[0] === uci[2]) san = move[1];
else san = uci[0] + 'x' + move[1];
if (move[2]) san += '=' + move[2].toUpperCase();
Expand Down Expand Up @@ -140,7 +144,7 @@ export function sanWriter(fen: string, ucis: string[]): SanToUci {
const board = readFen(fen);
const sans: SanToUci = {};
ucis.forEach(function (uci) {
const san = sanOf(board, uci);
const san = almostSanOf(board, uci);
sans[san] = uci;
if (san.includes('x')) sans[san.replace('x', '')] = uci;
});
Expand Down
4 changes: 2 additions & 2 deletions ui/round/src/ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import type {
import { defined, type Toggle, toggle, requestIdleCallback } from 'common';
import { storage, once, type LichessBooleanStorage } from 'common/storage';
import { pubsub } from 'common/pubsub';
import { readFen, sanOf, speakable } from 'chess/sanWriter';
import { readFen, almostSanOf, speakable } from 'chess/sanWriter';

interface GoneBerserk {
white?: boolean;
Expand Down Expand Up @@ -342,7 +342,7 @@ export default class RoundController implements MoveRootCtrl {

if (!meta.preConfirmed && this.confirmMoveToggle() && !meta.premove) {
if (site.sound.speech()) {
const spoken = `${speakable(sanOf(readFen(this.stepAt(this.ply).fen), move.u))}. confirm?`;
const spoken = `${speakable(almostSanOf(readFen(this.stepAt(this.ply).fen), move.u))}. confirm?`;
site.sound.say(spoken, false, true);
}
this.toSubmit = move;
Expand Down

0 comments on commit 84180ba

Please sign in to comment.