Add color changing

This commit is contained in:
Hri7566 2023-09-10 18:04:54 -04:00
parent ca434a4ca5
commit 78fb73a25a
5 changed files with 53 additions and 4 deletions

View File

@ -1,3 +1,4 @@
defaultName: "Anonymous"
defaultFlags:
volume: 100
enableColorChanging: false

View File

@ -38,7 +38,7 @@ export async function readUser(_id: string) {
export async function updateUser(
_id: string,
data: Omit<User, "id"> & { _id: string }
data: Partial<Omit<User, "id"> & { _id: string }>
) {
return await prisma.user.update({
where: { id: _id },

View File

@ -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<UsersConfig>("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
}
]);
}
}
}

View File

@ -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);
}
};

View File

@ -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);