display newly promoted piece on board

This commit is contained in:
2026-06-21 16:07:39 +02:00
parent b1b76522b8
commit 60bab173be
+28 -11
View File
@@ -13,7 +13,7 @@ export const ChessBoard = {
dests: this.getValidDests(), dests: this.getValidDests(),
}, },
events: { events: {
move: (orig, dest, _metadata) => { move: (orig, dest, _capturedPiece) => {
this.handleMove(orig, dest); this.handleMove(orig, dest);
}, },
}, },
@@ -41,27 +41,43 @@ export const ChessBoard = {
}, },
getValidDests() { getValidDests() {
// Convert chess.js moves into the Map format Chessground expects
const dests = new Map(); const dests = new Map();
this.chess.moves({ verbose: true }).forEach((m) => { this.chess.moves({ verbose: true }).forEach((m) => {
if (!dests.has(m.from)) dests.set(m.from, []); if (!dests.has(m.from)) dests.set(m.from, []);
dests.get(m.from).push(m.to); dests.get(m.from).push(m.to);
}); });
return dests; return dests;
}, },
handleMove(orig, dest) { handleMove(orig, dest) {
const move = this.chess.move({ from: orig, to: dest, promotion: "q" }); const { role, color } = this.ground.state.pieces.get(dest);
const destRank = dest.split("").pop()
const isPromotion = this.isPromotion(role, destRank)
if (isPromotion) {
console.log("PROMOTION LOGIC")
this.executeMove(orig, dest, "q");
return
}
this.executeMove(orig, dest, "q");
},
isPromotion(piece, rank) {
return piece === "pawn" && (rank === "1" || rank === "8");
},
showPromotionDialog() {
console.log("trigger promotion dialog");
},
executeMove(orig, dest, promotion) {
const move = this.chess.move({ from: orig, to: dest, promotion });
if (move) { if (move) {
const piece = this.chess.get(orig);
if (piece && piece.type === "p" && (dest[1] === "8" || dest[1] === "1")) {
this.pendingPromotion = { orig, dest };
this.showPromotionDialog();
return;
}
this.pushEvent("move_played", { this.pushEvent("move_played", {
from: orig, from: orig,
to: dest, to: dest,
@@ -73,6 +89,7 @@ export const ChessBoard = {
this.ground.set({ this.ground.set({
turnColor: nextTurnColor, turnColor: nextTurnColor,
fen: this.chess.fen(),
movable: { movable: {
color: nextTurnColor, color: nextTurnColor,
dests: this.getValidDests(), dests: this.getValidDests(),