| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * Player avatars, same idea as Allianties: a coloured initials circle, |
| 5 | * with the auth profile picture on top when it loads. A failed picture |
| 6 | * is dropped; the initials stay. |
| 7 | */ |
| 8 | var Avatar = { |
| 9 | failed: {}, |
| 10 | loaded: {}, |
| 11 | |
| 12 | initials: function (name) |
| 13 | { |
| 14 | if (!name) { return '?'; } |
| 15 | var parts = String(name).trim().split(/[\s._-]+/).filter(function (s) { return s.length > 0; }); |
| 16 | if (!parts.length) { return '?'; } |
| 17 | var first = Array.from(parts[0]); |
| 18 | var s = first[0]; |
| 19 | if (parts.length > 1) { s += Array.from(parts[parts.length - 1])[0]; } |
| 20 | else if (first.length > 1 && /[a-z]/i.test(first[1])) { s += first[1]; } |
| 21 | return s.toUpperCase(); |
| 22 | }, |
| 23 | |
| 24 | usableUrl: function (url) |
| 25 | { |
| 26 | if (typeof url !== 'string') { return null; } |
| 27 | url = url.trim(); |
| 28 | if (!url) { return null; } |
| 29 | if (!/^(https?:\/\/|data:image\/|\/|\.{0,2}\/?[\w-])/i.test(url)) { return null; } |
| 30 | if (/^(javascript|vbscript):/i.test(url)) { return null; } |
| 31 | if (Avatar.failed[url]) { return null; } |
| 32 | return url; |
| 33 | }, |
| 34 | |
| 35 | colorFor: function (id) |
| 36 | { |
| 37 | var palette = ['#e85d5d', '#4fa3e8', '#5dd39e', '#e8c547', '#b07ce8', '#e8873a', '#3ad1d1', '#e86fb8']; |
| 38 | var h = 0; |
| 39 | String(id || '').split('').forEach(function (c) { h = (h * 31 + c.charCodeAt(0)) >>> 0; }); |
| 40 | return palette[h % palette.length]; |
| 41 | }, |
| 42 | |
| 43 | textColor: function (hex) |
| 44 | { |
| 45 | var m = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(String(hex || '')); |
| 46 | if (!m) { return '#fff'; } |
| 47 | var v = m[1]; |
| 48 | if (v.length === 3) { v = v[0] + v[0] + v[1] + v[1] + v[2] + v[2]; } |
| 49 | var r = parseInt(v.slice(0, 2), 16), g = parseInt(v.slice(2, 4), 16), b = parseInt(v.slice(4, 6), 16); |
| 50 | var lum = (0.299 * r + 0.587 * g + 0.114 * b) / 255; |
| 51 | return lum > 0.6 ? '#111' : '#fff'; |
| 52 | }, |
| 53 | |
| 54 | piece: function (player, opts) |
| 55 | { |
| 56 | opts = opts || {}; |
| 57 | var doc = opts.doc || document; |
| 58 | var color = player.color || Avatar.colorFor(player.id || player.name); |
| 59 | var el = doc.createElement('span'); |
| 60 | el.className = 'player-avatar' + (player.alive === false ? ' dead' : ''); |
| 61 | el.setAttribute('role', 'img'); |
| 62 | el.setAttribute('aria-label', player.name || '?'); |
| 63 | el.title = player.name || ''; |
| 64 | el.style.borderColor = color; |
| 65 | el.style.backgroundColor = color; |
| 66 | el.style.color = Avatar.textColor(color); |
| 67 | if (opts.size) |
| 68 | { |
| 69 | var size = opts.size; |
| 70 | el.style.width = size + 'px'; |
| 71 | el.style.height = size + 'px'; |
| 72 | el.style.fontSize = Math.max(10, Math.round(size * 0.4)) + 'px'; |
| 73 | } |
| 74 | var txt = doc.createElement('span'); |
| 75 | txt.className = 'player-avatar-initials'; |
| 76 | txt.textContent = Avatar.initials(player.name); |
| 77 | el.appendChild(txt); |
| 78 | var url = Avatar.usableUrl(player.avatar); |
| 79 | if (url) |
| 80 | { |
| 81 | var img = doc.createElement('img'); |
| 82 | img.alt = ''; |
| 83 | img.className = 'player-avatar-img'; |
| 84 | img.decoding = 'async'; |
| 85 | img.referrerPolicy = 'no-referrer'; |
| 86 | img.addEventListener('error', function () |
| 87 | { |
| 88 | Avatar.failed[url] = true; |
| 89 | el.classList.remove('has-img'); |
| 90 | if (img.parentNode) { img.parentNode.removeChild(img); } |
| 91 | }); |
| 92 | img.addEventListener('load', function () |
| 93 | { |
| 94 | if (!img.naturalWidth) |
| 95 | { |
| 96 | Avatar.failed[url] = true; |
| 97 | if (img.parentNode) { img.parentNode.removeChild(img); } |
| 98 | return; |
| 99 | } |
| 100 | Avatar.loaded[url] = true; |
| 101 | el.classList.add('has-img'); |
| 102 | }); |
| 103 | if (Avatar.loaded[url]) { el.classList.add('has-img'); } |
| 104 | img.src = url; |
| 105 | el.appendChild(img); |
| 106 | } |
| 107 | return el; |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | if (typeof module !== 'undefined' && module.exports) |
| 112 | { module.exports = Avatar; } |
| 113 | else if (typeof globalThis !== 'undefined') |
| 114 | { globalThis.Avatar = Avatar; } |
projects-timfalken-com meteors