From dc841cb25e2efd456811f8e01cdc60d825c0f2b4 Mon Sep 17 00:00:00 2001 From: marzavec Date: Thu, 11 Jan 2024 11:14:15 -0800 Subject: [PATCH] added socket.flare MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each of the top level are given their own flair emoji which is applied to the socket on join or other events. May also add a module allowing users to set their own utf emoji, such as: /flair 🍆 --- client/client.js | 4 +- commands/channels/claimchannel.js | 4 ++ commands/channels/setlevel.js | 4 ++ commands/core/chat.js | 1 + commands/core/join.js | 8 +++- commands/utility/_UAC.js | 68 +++++++++++++++++++++++++++++++ 6 files changed, 86 insertions(+), 3 deletions(-) diff --git a/client/client.js b/client/client.js index 944a644..4f4d119 100644 --- a/client/client.js +++ b/client/client.js @@ -566,8 +566,8 @@ function pushMessage(args) { if (args.trip) { var tripEl = document.createElement('span'); - if (args.mod) { - tripEl.textContent = String.fromCodePoint(11088) + " " + args.trip + " "; + if (args.flair) { + tripEl.textContent = args.flair + " " + args.trip + " "; } else { tripEl.textContent = args.trip + " "; } diff --git a/commands/channels/claimchannel.js b/commands/channels/claimchannel.js index c2d74a5..587cd8b 100644 --- a/commands/channels/claimchannel.js +++ b/commands/channels/claimchannel.js @@ -11,6 +11,7 @@ import { isModerator, getUserDetails, levels, + getAppearance, } from '../utility/_UAC.js'; import { Errors, @@ -140,6 +141,9 @@ export function chatHook({ channel: socket.channel, }, { channel: socket.channel }); + const { color, flair } = getAppearance(levels.channelOwner); + socket.color = color; + socket.flair = flair; socket.level = levels.channelOwner; const updateNotice = { diff --git a/commands/channels/setlevel.js b/commands/channels/setlevel.js index 44a7381..9f89807 100644 --- a/commands/channels/setlevel.js +++ b/commands/channels/setlevel.js @@ -87,7 +87,11 @@ export async function run({ trip: payload.trip, }); + const { color, flair } = getAppearance(newLevel); + for (let i = 0, j = targetClients.length; i < j; i += 1) { + targetClients[i].color = color; + targetClients[i].flair = flair; targetClients[i].level = newLevel; server.broadcast({ diff --git a/commands/core/chat.js b/commands/core/chat.js index 5fc579a..587f406 100644 --- a/commands/core/chat.js +++ b/commands/core/chat.js @@ -121,6 +121,7 @@ export async function run({ channel: socket.channel, text, level: socket.level, + flair: socket.flair, customId, }; diff --git a/commands/core/join.js b/commands/core/join.js index ded6c8f..5665157 100644 --- a/commands/core/join.js +++ b/commands/core/join.js @@ -30,6 +30,7 @@ import { getUserPerms, getUserDetails, isModerator, + getAppearance, } from '../utility/_UAC.js'; /** @@ -106,6 +107,10 @@ export async function run({ }, socket); } + const { color, flair } = getAppearance(level); + socket.color = color; + socket.flair = flair; + // store the user values const userInfo = { nick, @@ -115,7 +120,8 @@ export async function run({ level, userid: socket.userid, isBot: socket.isBot, - color: socket.color, + color, + flair, channel, }; diff --git a/commands/utility/_UAC.js b/commands/utility/_UAC.js index f5e9ff9..46c3a63 100644 --- a/commands/utility/_UAC.js +++ b/commands/utility/_UAC.js @@ -16,6 +16,26 @@ import { getChannelSettings, } from './_Channels.js'; +/** + * Returns random rgb code + * @return {string} + */ +const randomRGB = () => { + const saturation = 0.80; + const lightness = 0.65; + const hue = Math.floor(Math.random() * 360); + + const k = (n) => (n + hue / 30) % 12; + const a = saturation * Math.min(lightness, 1 - lightness); + const f = (n) => lightness - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1))); + + const r = `${Math.floor(255 * f(0)).toString(16)}`; + const g = `${Math.floor(255 * f(8)).toString(16)}`; + const b = `${Math.floor(255 * f(4)).toString(16)}`; + + return `${r}${g}${b}`; +}; + /** * Object defining labels for default permission ranges * @typedef {Object} levels @@ -37,6 +57,35 @@ export const levels = { trustedUser: 500, default: 100, + bot: 99, +}; + +/** + * Object defining labels for default permission ranges + * @typedef {Object} levelAppearance + * @property {number} admin Global administrator range + */ +export const levelAppearance = { + [levels.admin]: { + color: 'd73737', + flair: String.fromCodePoint(127775), // 🌟 + }, + [levels.moderator]: { + color: '1fad83', + flair: String.fromCodePoint(11088), // ⭐ + }, + [levels.channelOwner]: { + color: 'dd8800', + flair: String.fromCodePoint(128081), // 👑 + }, + [levels.channelModerator]: { + color: '2fa1ee', + flair: String.fromCodePoint(128171), // 💫 + }, + [levels.bot]: { + color: '2fa1ee', + flair: String.fromCodePoint(129302), // 🤖 + }, }; /** @@ -115,6 +164,7 @@ export function getUserDetails(socket) { userid: socket.userid, isBot: socket.isBot, color: socket.color, + flair: socket.flair, online: true, }; } @@ -131,6 +181,24 @@ export function verifyNickname(nick) { return /^[a-zA-Z0-9_]{1,24}$/.test(nick); } +/** + * Returns an object with 'color' and 'flair' properties + * associated by level + * @public + * @param {number} level Provided level + * @return {object} + */ +export function getAppearance(level) { + if (typeof levelAppearance[level] !== 'undefined') { + return levelAppearance[level]; + } + + return { + color: randomRGB(), + flair: false, + }; +} + /** * Hashes a user's password, returning a trip code * or a blank string