'use strict'; /** * Player avatars, same idea as Allianties: a coloured initials circle, * with the auth profile picture on top when it loads. A failed picture * is dropped; the initials stay. */ 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(); }, 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; }, 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]; }, 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'; }, 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 = 'player-avatar' + (player.alive === false ? ' dead' : ''); el.setAttribute('role', 'img'); el.setAttribute('aria-label', player.name || '?'); el.title = player.name || ''; el.style.borderColor = color; el.style.backgroundColor = color; el.style.color = Avatar.textColor(color); if (opts.size) { var size = opts.size; el.style.width = size + 'px'; el.style.height = size + 'px'; el.style.fontSize = Math.max(10, Math.round(size * 0.4)) + 'px'; } var txt = doc.createElement('span'); txt.className = 'player-avatar-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 = 'player-avatar-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; } else if (typeof globalThis !== 'undefined') { globalThis.Avatar = Avatar; }