${yours ? 'Challenge a friend or share a game link. Your games and invitations will appear here.' : 'Games appear here while your opponent is thinking.'}
${yours ? '' : ''}
`; }
function renderGames ()
{
if (!state) return;
clearTimeout(moveHoldTimer);
let relocating = false;
const now = performance.now();
for (const [id, hold] of moveHolds)
{
const g = state.games.find(g => g.id === id);
if (hold.user !== state.user.id || !g || g.status !== 'active' || (g.version > hold.version && g.yourTurn))
{
moveHolds.delete(id); continue;
}
// Start the pause when the confirmed board position is first displayed.
if (hold.until === null && g.version > hold.version) hold.until = now + 3000;
if (hold.until !== null && hold.until <= now && !drag) { moveHolds.delete(id); relocating = true; }
}
const deadlines = [...moveHolds.values()].filter(h => h.until !== null).map(h => h.until);
if (deadlines.length) moveHoldTimer = setTimeout(renderGames, Math.max(drag ? 100 : 1, Math.min(...deadlines) - now));
const selector = '.game-card, .waiting-heading, #main > footer';
const key = el => el.dataset.game || el.tagName;
const animate = !matchMedia('(prefers-reduced-motion: reduce)').matches &&
(relocating || [...document.querySelectorAll(selector)].some(el => el.getAnimations().length));
const before = new Map(animate ? [...document.querySelectorAll(selector)].map(el => [key(el), el.getBoundingClientRect()]) : []);
const yours = state.games.filter(g => g.yourTurn || g.status === 'ended' || g.status === 'pending' && g.incoming || moveHolds.has(g.id));
const waiting = state.games.filter(g => !yours.includes(g));
$('#your-count').textContent = yours.length; $('#waiting-count').textContent = waiting.length;
// Continue from the visible fill even when polling/selection recreates the cards mid-animation.
const materialBefore = new Map([...document.querySelectorAll('.material-bar')].map(el =>
{
const fill = $('.material-white', el), animation = fill.getAnimations()[0];
return [el.closest('[data-game]').dataset.game, { share: new DOMMatrixReadOnly(getComputedStyle(fill).transform).a, target: el.dataset.share, remaining: animation ? Math.max(0, 650 - Number(animation.currentTime)) : 650 }];
}));
$('#your-games').innerHTML = yours.map(card).join('') || empty(true);
$('#waiting-games').innerHTML = waiting.map(card).join('') || empty(false);
const reduced = matchMedia('(prefers-reduced-motion: reduce)').matches;
for (const el of document.querySelectorAll('.material-bar'))
{
const fill = $('.material-white', el), share = Number(el.dataset.share), old = materialBefore.get(el.closest('[data-game]').dataset.game);
fill.style.transform = `scaleX(${share})`;
if (!reduced && old && Math.abs(old.share - share) > .0001) fill.animate([{ transform: `scaleX(${old.share})` }, { transform: `scaleX(${share})` }], { duration: old.target === el.dataset.share ? old.remaining : 650, easing: 'linear' });
}
renderMoveFeedback(now, reduced);
if (animate)
{
for (const el of document.querySelectorAll(selector))
{
const old = before.get(key(el));
el.getAnimations().forEach(animation => animation.cancel());
if (!old) continue;
const rect = el.getBoundingClientRect(), dx = old.left - rect.left, dy = old.top - rect.top;
if (Math.abs(dx) < 1 && Math.abs(dy) < 1) continue;
el.animate([
{ transform: `translate(${dx}px, ${dy}px)`, zIndex: 2 },
{ transform: 'translate(0, 0)', zIndex: 2 }
], { duration: 650, easing: 'cubic-bezier(.22, 1, .36, 1)' });
}
}
}
function card (g)
{
let content = '';
if (g.status === 'pending') content = `
♘
${g.incoming ? 'A little challenge?' : 'Challenge sent.'}
${g.incoming ? `${esc(g.opponent)} invited you to play. Your board is waiting.` : `Waiting for ${esc(g.opponent)} to accept your challenge.`}
${g.incoming ? `` : 'The next move is theirs.'}
`;
else if (g.status === 'ended') content = `
${board(g)}
${g.outcome === 'win' ? 'You won!' : g.outcome === 'loss' ? 'You lost' : 'A game well matched.'}
${esc(g.reason)}${g.opponentRematch && g.canRematch ? `${esc(g.opponent)} wants a rematch.` : ''}
Your friends will appear here. Send an invitation to get started.
';
$('#account-summary').innerHTML = `${avatarMarkup(state.user.name, state.user.avatar, true)}${esc(state.user.name)}${state.local ? 'Local test account' : 'Ready for your next move'}`;
}
function options (g) { showModal(`
Your game with ${esc(g.opponent)}
${g.check ? 'You are in check. Find a move that protects your king.' : 'Take your time. The board will be here.'}
${kind === 'friend' ? 'Invite a friend.' : 'Send a little challenge.'}
Share this link. ${kind === 'friend' ? 'You’ll become friends when they accept.' : 'The first person to accept starts a game with you.'} It expires in one month.
`); }); }
if ($('#match-link')) $('#match-link').onclick = () => createLink('match');
if ($('#friend-link')) $('#friend-link').onclick = () => createLink('friend');
$('#settings-button').onclick = () => { if (!state) return; showModal(`
`); };
if ($('#rules-button')) $('#rules-button').onclick = () => showModal(`MICROCHESS
Small board. Real chess.
The usual rules of chess, on a 4 × 5 board.
Each side starts with a king, rook, bishop, knight and one pawn. White moves first.
Pawns move one square forward, capture diagonally and never move two squares. There is no castling or en passant.
Reach the last rank to promote to a queen, rook, bishop or knight.
Protect your king. Checkmate wins; stalemate and dead positions draw.
Claim a draw after three repetitions or 50 moves by each side without a pawn move or capture. You may also claim with an intended move, via the game menu. Five repetitions or 75 moves draw automatically.
Click a piece to see legal destinations, then click a dot. Or drag it to a destination.
The bar below your opponent’s name compares remaining material: White on the left, Black on the right. Pawn 1, knight/bishop 3, rook 5, queen 9; kings are not counted.
After your move, a blue pulse marks a clear estimated gain; red marks a likely material loss or mating threat. This quick tactical check can miss deeper ideas. Quiet or uncertain moves get no pulse.