Files
client/app/renderer/js/badges-model.js
T

25 lines
1.2 KiB
JavaScript

// Client badge string parsing (pure). The core passes `client_badges` through unparsed (snapshot `badges`,
// docs/bridge.md); legacy TeamSpeak clients publish it as `key=value` pairs separated by ':', e.g.
// "Overwolf=0:badges=<uuid>,<uuid>". Only the `badges` value is used: up to MAX_SHOWN_BADGES distinct UUIDs
// (official account settings max_active_badges = 3). Signed TS6 badges (client_signed_badges) are not part of
// the snapshot and are never synthesised here.
export const MAX_SHOWN_BADGES = 3;
export const MAX_BADGE_STRING = 1024;
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
export function parseClientBadges(raw) {
if (typeof raw !== 'string' || raw.length === 0 || raw.length > MAX_BADGE_STRING) return [];
const uuids = [];
for (const pair of raw.split(':')) {
const eq = pair.indexOf('=');
if (eq < 0 || pair.slice(0, eq).trim().toLowerCase() !== 'badges') continue;
for (const part of pair.slice(eq + 1).split(',')) {
const uuid = part.trim().toLowerCase();
if (UUID.test(uuid) && !uuids.includes(uuid)) uuids.push(uuid);
if (uuids.length === MAX_SHOWN_BADGES) return uuids;
}
}
return uuids;
}