| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * Player tokens / avatars. |
| 5 | * |
| 6 | * A token is ALWAYS a visible circle in the player's colour with their |
| 7 | * initials. A profile picture, when present and loadable, is layered on |
| 8 | * top. If the picture fails to load (404, empty URL, blocked), the <img> |
| 9 | * is removed and the initials circle underneath stays — the token itself |
| 10 | * is never replaced or detached, so it survives image errors and board |
| 11 | * re-renders. Failed URLs are remembered so later renders (every 3 s |
| 12 | * poll) skip them instead of flashing a broken image. |
| 13 | */ |
| 14 | var Avatar = { |
| 15 | failed: {}, |
| 16 | loaded: {}, |
| 17 | |
| 18 | initials: function (name) |
| 19 | { |
| 20 | if (!name) { return '?'; } |
| 21 | var parts = String(name).trim().split(/[\s._-]+/).filter(function (s) { return s.length > 0; }); |
| 22 | if (!parts.length) { return '?'; } |
| 23 | var first = Array.from(parts[0]); |
| 24 | var s = first[0]; |
| 25 | if (parts.length > 1) { s += Array.from(parts[parts.length - 1])[0]; } |
| 26 | else if (first.length > 1 && /[a-z]/i.test(first[1])) { s += first[1]; } |
| 27 | return s.toUpperCase(); |
| 28 | }, |
| 29 | |
| 30 | /** Usable picture URL, or null (empty, whitespace, javascript:, already failed). */ |
| 31 | usableUrl: function (url) |
| 32 | { |
| 33 | if (typeof url !== 'string') { return null; } |
| 34 | url = url.trim(); |
| 35 | if (!url) { return null; } |
| 36 | if (!/^(https?:\/\/|data:image\/|\/|\.{0,2}\/?[\w-])/i.test(url)) { return null; } |
| 37 | if (/^(javascript|vbscript):/i.test(url)) { return null; } |
| 38 | if (Avatar.failed[url]) { return null; } |
| 39 | return url; |
| 40 | }, |
| 41 | |
| 42 | /** Deterministic fallback colour for a player without one (e.g. the menu). */ |
| 43 | colorFor: function (id) |
| 44 | { |
| 45 | var palette = ['#e85d5d', '#4fa3e8', '#5dd39e', '#e8c547', '#b07ce8', '#e8873a', '#3ad1d1', '#e86fb8']; |
| 46 | var h = 0; |
| 47 | String(id || '').split('').forEach(function (c) { h = (h * 31 + c.charCodeAt(0)) >>> 0; }); |
| 48 | return palette[h % palette.length]; |
| 49 | }, |
| 50 | |
| 51 | /** Black or white text, whichever reads better on the given #rgb/#rrggbb colour. */ |
| 52 | textColor: function (hex) |
| 53 | { |
| 54 | var m = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(String(hex || '')); |
| 55 | if (!m) { return '#fff'; } |
| 56 | var v = m[1]; |
| 57 | if (v.length === 3) { v = v[0] + v[0] + v[1] + v[1] + v[2] + v[2]; } |
| 58 | var r = parseInt(v.slice(0, 2), 16), g = parseInt(v.slice(2, 4), 16), b = parseInt(v.slice(4, 6), 16); |
| 59 | var lum = (0.299 * r + 0.587 * g + 0.114 * b) / 255; |
| 60 | return lum > 0.6 ? '#111' : '#fff'; |
| 61 | }, |
| 62 | |
| 63 | /** |
| 64 | * Build a token element (span.piece). opts: { size, fixed, doc }. |
| 65 | * The element itself is the token; append it anywhere. |
| 66 | */ |
| 67 | piece: function (player, opts) |
| 68 | { |
| 69 | opts = opts || {}; |
| 70 | var doc = opts.doc || document; |
| 71 | var color = player.color || Avatar.colorFor(player.id || player.name); |
| 72 | var el = doc.createElement('span'); |
| 73 | el.className = 'piece piece-token' + (player.alive === false ? ' dead' : ''); |
| 74 | el.setAttribute('role', 'img'); |
| 75 | el.setAttribute('aria-label', player.name || '?'); |
| 76 | el.title = player.name || ''; |
| 77 | el.style.setProperty('--c', color); |
| 78 | el.style.borderColor = color; |
| 79 | el.style.backgroundColor = color; |
| 80 | el.style.color = Avatar.textColor(color); |
| 81 | if (opts.fixed) |
| 82 | { |
| 83 | var size = opts.size || 40; |
| 84 | el.classList.add('piece-fixed'); |
| 85 | el.style.setProperty('--sz', size + 'px'); |
| 86 | el.style.width = size + 'px'; |
| 87 | el.style.height = size + 'px'; |
| 88 | } |
| 89 | var txt = doc.createElement('span'); |
| 90 | txt.className = 'piece-initials'; |
| 91 | txt.textContent = Avatar.initials(player.name); |
| 92 | el.appendChild(txt); |
| 93 | var url = Avatar.usableUrl(player.avatar); |
| 94 | if (url) |
| 95 | { |
| 96 | var img = doc.createElement('img'); |
| 97 | img.alt = ''; |
| 98 | img.className = 'piece-img'; |
| 99 | img.decoding = 'async'; |
| 100 | img.referrerPolicy = 'no-referrer'; |
| 101 | img.addEventListener('error', function () |
| 102 | { |
| 103 | Avatar.failed[url] = true; |
| 104 | el.classList.remove('has-img'); |
| 105 | if (img.parentNode) { img.parentNode.removeChild(img); } |
| 106 | }); |
| 107 | img.addEventListener('load', function () |
| 108 | { |
| 109 | if (!img.naturalWidth) { Avatar.failed[url] = true; if (img.parentNode) { img.parentNode.removeChild(img); } return; } |
| 110 | Avatar.loaded[url] = true; |
| 111 | el.classList.add('has-img'); |
| 112 | }); |
| 113 | if (Avatar.loaded[url]) { el.classList.add('has-img'); } |
| 114 | img.src = url; |
| 115 | el.appendChild(img); |
| 116 | } |
| 117 | return el; |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | if (typeof module !== 'undefined' && module.exports) |
| 122 | { module.exports = Avatar; } |
projects-timfalken-com allianties