Upstream lichess-org/lila state of the touched files before f8fadacc65
This commit is contained in:
1 parent
8e8500f7ac
commit
818e05beaf
2 files changed
+14
-129
No files matched your search
+14
-18
@@ -1,11 +1,10 @@
|
||||
import type { DrawShape } from '@lichess-org/chessground/draw';
|
||||
import type { Outcome, Position } from 'chessops';
|
||||
import type { Outcome } from 'chessops';
|
||||
import { parseFen } from 'chessops/fen';
|
||||
import { makeSquare, opposite } from 'chessops/util';
|
||||
|
||||
import type { PositionResult } from '@/tree/types';
|
||||
|
||||
import { endgameGlyphs } from './glyphs';
|
||||
import { fenColor } from './chess';
|
||||
import { endgameGlyphs, type EndgameGlyph } from './glyphs';
|
||||
import type { StatusName } from './status';
|
||||
|
||||
export interface EndgameResult {
|
||||
@@ -15,33 +14,33 @@ export interface EndgameResult {
|
||||
|
||||
type EndgameNode = {
|
||||
fen: FEN;
|
||||
pos: () => PositionResult;
|
||||
outcome: () => Outcome | undefined;
|
||||
check: () => boolean;
|
||||
dests: () => Dests;
|
||||
};
|
||||
|
||||
const terminalStatus = (pos: Position, outcome: Outcome): StatusName =>
|
||||
outcome.winner ? (pos.isCheckmate() ? 'mate' : 'variantEnd') : pos.isStalemate() ? 'stalemate' : 'draw';
|
||||
|
||||
const endgameResult = (
|
||||
node: EndgameNode,
|
||||
isLast: boolean,
|
||||
gameWinner: Color | undefined,
|
||||
gameStatus: StatusName,
|
||||
): EndgameResult => {
|
||||
const outcome = node.dests().size === 0 ? node.outcome() : undefined;
|
||||
if (outcome) return { winner: outcome.winner, status: terminalStatus(node.pos().unwrap(), outcome) };
|
||||
if (!isLast) return {};
|
||||
return { winner: node.outcome()?.winner ?? gameWinner, status: gameStatus };
|
||||
const outcome = node.outcome();
|
||||
const isTerminal = node.dests().size === 0;
|
||||
const isMate = isTerminal && node.check();
|
||||
const isStalemate = isTerminal && !node.check() && outcome && !outcome.winner;
|
||||
|
||||
return {
|
||||
winner: isMate ? opposite(fenColor(node.fen)) : isStalemate ? undefined : (outcome?.winner ?? gameWinner),
|
||||
status: isMate ? 'mate' : isStalemate ? 'stalemate' : gameStatus,
|
||||
};
|
||||
};
|
||||
|
||||
export function endgameShapesForNode(
|
||||
node: EndgameNode,
|
||||
isLast: boolean,
|
||||
gameWinner: Color | undefined,
|
||||
gameStatus: StatusName,
|
||||
): DrawShape[] {
|
||||
const result = endgameResult(node, isLast, gameWinner, gameStatus);
|
||||
const result = endgameResult(node, gameWinner, gameStatus);
|
||||
return endgameShapes(node.fen, result.winner, result.status);
|
||||
}
|
||||
|
||||
@@ -53,8 +52,6 @@ export function findKingSquare(fen: FEN, color: Color): Key | undefined {
|
||||
return king === undefined ? undefined : makeSquare(king);
|
||||
}
|
||||
|
||||
type EndgameGlyph = keyof typeof endgameGlyphs;
|
||||
|
||||
const loserGlyph = (status: StatusName | undefined): EndgameGlyph =>
|
||||
status === 'mate'
|
||||
? 'mate'
|
||||
@@ -95,7 +92,6 @@ export function endgameShapes(
|
||||
};
|
||||
|
||||
if (winner) {
|
||||
add(winner, 'win');
|
||||
add(opposite(winner), loserGlyph(status));
|
||||
} else {
|
||||
const glyph = drawGlyph(status);
|
||||
|
||||
Reference in new issue
Block a user