shogiground
Version:
lishogi.org shogi ui
29 lines • 1.92 kB
JavaScript
import { samePiece } from './util.js';
export function addToHand(s, piece, cnt = 1) {
const hand = s.hands.handMap.get(piece.color), role = (s.hands.roles.includes(piece.role) ? piece.role : s.promotion.unpromotesTo(piece.role)) ||
piece.role;
if (hand && s.hands.roles.includes(role))
hand.set(role, (hand.get(role) || 0) + cnt);
}
export function removeFromHand(s, piece, cnt = 1) {
const hand = s.hands.handMap.get(piece.color), role = (s.hands.roles.includes(piece.role) ? piece.role : s.promotion.unpromotesTo(piece.role)) ||
piece.role, num = hand === null || hand === void 0 ? void 0 : hand.get(role);
if (hand && num)
hand.set(role, Math.max(num - cnt, 0));
}
export function renderHand(s, handEl) {
var _a;
handEl.classList.toggle('promotion', !!s.promotion.current);
let wrapEl = handEl.firstElementChild;
while (wrapEl) {
const pieceEl = wrapEl.firstElementChild, piece = { role: pieceEl.sgRole, color: pieceEl.sgColor }, num = ((_a = s.hands.handMap.get(piece.color)) === null || _a === void 0 ? void 0 : _a.get(piece.role)) || 0, isSelected = !!s.selectedPiece && samePiece(piece, s.selectedPiece) && !s.droppable.spare;
wrapEl.classList.toggle('selected', (s.activeColor === 'both' || s.activeColor === s.turnColor) && isSelected);
wrapEl.classList.toggle('preselected', s.activeColor !== 'both' && s.activeColor !== s.turnColor && isSelected);
wrapEl.classList.toggle('last-dest', s.highlight.lastDests && !!s.lastPiece && samePiece(piece, s.lastPiece));
wrapEl.classList.toggle('drawing', !!s.drawable.piece && samePiece(s.drawable.piece, piece));
wrapEl.classList.toggle('current-pre', !!s.predroppable.current && samePiece(s.predroppable.current.piece, piece));
wrapEl.dataset.nb = num.toString();
wrapEl = wrapEl.nextElementSibling;
}
}
//# sourceMappingURL=hands.js.map