'use strict'; /** * Player tokens / avatars. * * A token is ALWAYS a visible circle in the player's colour with their * initials. A profile picture, when present and loadable, is layered on * top. If the picture fails to load (404, empty URL, blocked), the * is removed and the initials circle underneath stays — the token itself * is never replaced or detached, so it survives image errors and board * re-renders. Failed URLs are remembered so later renders (every 3 s * poll) skip them instead of flashing a broken image. */ var Avatar = { failed: {}, loaded: {}, initials: function (name) { if (!name) { return '?'; } var parts = String(name).trim().split(/[\s._-]+/).filter(function (s) { return s.length > 0; }); if (!parts.length) { return '?'; } var first = Array.from(parts[0]); var s = first[0]; if (parts.length > 1) { s += Array.from(parts[parts.length - 1])[0]; } else if (first.length > 1 && /[a-z]/i.test(first[1])) { s += first[1]; } return s.toUpperCase(); }, /** Usable picture URL, or null (empty, whitespace, javascript:, already failed). */ usableUrl: function (url) { if (typeof url !== 'string') { return null; } url = url.trim(); if (!url) { return null; } if (!/^(https?:\/\/|data:image\/|\/|\.{0,2}\/?[\w-])/i.test(url)) { return null; } if (/^(javascript|vbscript):/i.test(url)) { return null; } if (Avatar.failed[url]) { return null; } return url; }, /** Deterministic fallback colour for a player without one (e.g. the menu). */ colorFor: function (id) { var palette = ['#e85d5d', '#4fa3e8', '#5dd39e', '#e8c547', '#b07ce8', '#e8873a', '#3ad1d1', '#e86fb8']; var h = 0; String(id || '').split('').forEach(function (c) { h = (h * 31 + c.charCodeAt(0)) >>> 0; }); return palette[h % palette.length]; }, /** Black or white text, whichever reads better on the given #rgb/#rrggbb colour. */ textColor: function (hex) { var m = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(String(hex || '')); if (!m) { return '#fff'; } var v = m[1]; if (v.length === 3) { v = v[0] + v[0] + v[1] + v[1] + v[2] + v[2]; } var r = parseInt(v.slice(0, 2), 16), g = parseInt(v.slice(2, 4), 16), b = parseInt(v.slice(4, 6), 16); var lum = (0.299 * r + 0.587 * g + 0.114 * b) / 255; return lum > 0.6 ? '#111' : '#fff'; }, /** * Build a token element (span.piece). opts: { size, fixed, doc }. * The element itself is the token; append it anywhere. */ piece: function (player, opts) { opts = opts || {}; var doc = opts.doc || document; var color = player.color || Avatar.colorFor(player.id || player.name); var el = doc.createElement('span'); el.className = 'piece piece-token' + (player.alive === false ? ' dead' : ''); el.setAttribute('role', 'img'); el.setAttribute('aria-label', player.name || '?'); el.title = player.name || ''; el.style.setProperty('--c', color); el.style.borderColor = color; el.style.backgroundColor = color; el.style.color = Avatar.textColor(color); if (opts.fixed) { var size = opts.size || 40; el.classList.add('piece-fixed'); el.style.setProperty('--sz', size + 'px'); el.style.width = size + 'px'; el.style.height = size + 'px'; } var txt = doc.createElement('span'); txt.className = 'piece-initials'; txt.textContent = Avatar.initials(player.name); el.appendChild(txt); var url = Avatar.usableUrl(player.avatar); if (url) { var img = doc.createElement('img'); img.alt = ''; img.className = 'piece-img'; img.decoding = 'async'; img.referrerPolicy = 'no-referrer'; img.addEventListener('error', function () { Avatar.failed[url] = true; el.classList.remove('has-img'); if (img.parentNode) { img.parentNode.removeChild(img); } }); img.addEventListener('load', function () { if (!img.naturalWidth) { Avatar.failed[url] = true; if (img.parentNode) { img.parentNode.removeChild(img); } return; } Avatar.loaded[url] = true; el.classList.add('has-img'); }); if (Avatar.loaded[url]) { el.classList.add('has-img'); } img.src = url; el.appendChild(img); } return el; } }; if (typeof module !== 'undefined' && module.exports) { module.exports = Avatar; }