From 78fb73a25ac41d6c0bd8a914d4ccf9b3a1452376 Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Sun, 10 Sep 2023 18:04:54 -0400 Subject: [PATCH] Add color changing --- config/users.yml | 1 + src/data/user.ts | 2 +- src/ws/Socket.ts | 43 ++++++++++++++++++++++++-- src/ws/events/user/handlers/userset.ts | 9 ++++++ src/ws/events/user/index.ts | 2 ++ 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 src/ws/events/user/handlers/userset.ts diff --git a/config/users.yml b/config/users.yml index a820af3..9fa72ec 100644 --- a/config/users.yml +++ b/config/users.yml @@ -1,3 +1,4 @@ defaultName: "Anonymous" defaultFlags: volume: 100 +enableColorChanging: false diff --git a/src/data/user.ts b/src/data/user.ts index 86ff644..c932664 100644 --- a/src/data/user.ts +++ b/src/data/user.ts @@ -38,7 +38,7 @@ export async function readUser(_id: string) { export async function updateUser( _id: string, - data: Omit & { _id: string } + data: Partial & { _id: string }> ) { return await prisma.user.update({ where: { id: _id }, diff --git a/src/ws/Socket.ts b/src/ws/Socket.ts index 2733550..1fe6c84 100644 --- a/src/ws/Socket.ts +++ b/src/ws/Socket.ts @@ -9,7 +9,7 @@ import { UserFlags } from "../util/types"; import { User } from "@prisma/client"; -import { createUser, readUser } from "../data/user"; +import { createUser, readUser, updateUser } from "../data/user"; import { eventGroups } from "./events"; import { loadConfig } from "../util/config"; import { Gateway } from "./Gateway"; @@ -21,13 +21,15 @@ import { Logger } from "../util/Logger"; interface UsersConfig { defaultName: string; defaultFlags: UserFlags; + enableColorChanging: boolean; } const usersConfig = loadConfig("config/users.yml", { defaultName: "Anonymous", defaultFlags: { volume: 100 - } + }, + enableColorChanging: false }); const logger = new Logger("Sockets"); @@ -114,7 +116,7 @@ export class Socket extends EventEmitter { } } - logger.debug("Found channel:", channel); + // logger.debug("Found channel:", channel); // Does channel exist? if (channel) { @@ -293,4 +295,39 @@ export class Socket extends EventEmitter { } ]); } + + public async userset(name?: string, color?: string) { + let isColor = false; + + if (color && usersConfig.enableColorChanging) { + isColor = + typeof color === "string" && !!color.match(/^#[0-9a-f]{6}$/i); + } + + await updateUser(this._id, { + name: typeof name == "string" ? name : undefined, + color: color ? (isColor ? color : undefined) : undefined + }); + + await this.loadUser(); + + const ch = this.getCurrentChannel(); + + if (ch) { + let part = this.getParticipant() as Participant; + let cursorPos = this.getCursorPos(); + + ch.sendArray([ + { + m: "p", + _id: part._id, + color: part.color, + id: part.id, + name: part.name, + x: cursorPos.x, + y: cursorPos.y + } + ]); + } + } } diff --git a/src/ws/events/user/handlers/userset.ts b/src/ws/events/user/handlers/userset.ts new file mode 100644 index 0000000..d6760dd --- /dev/null +++ b/src/ws/events/user/handlers/userset.ts @@ -0,0 +1,9 @@ +import { ServerEventListener } from "../../../../util/types"; + +export const userset: ServerEventListener<"userset"> = { + id: "userset", + callback: (msg, socket) => { + // Change username/color + socket.userset(msg.set.name, msg.set.color); + } +}; diff --git a/src/ws/events/user/index.ts b/src/ws/events/user/index.ts index 0dfd214..4ae03e6 100644 --- a/src/ws/events/user/index.ts +++ b/src/ws/events/user/index.ts @@ -7,11 +7,13 @@ import { devices } from "./handlers/devices"; import { ch } from "./handlers/ch"; import { m } from "./handlers/m"; import { a } from "./handlers/a"; +import { userset } from "./handlers/userset"; EVENTGROUP_USER.add(hi); EVENTGROUP_USER.add(devices); EVENTGROUP_USER.add(ch); EVENTGROUP_USER.add(m); EVENTGROUP_USER.add(a); +EVENTGROUP_USER.add(userset); eventGroups.push(EVENTGROUP_USER);