old changes

This commit is contained in:
Hri7566 2024-01-06 04:15:27 -05:00
parent 9de2717930
commit b330432541
9 changed files with 115 additions and 46 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}

BIN
bun.lockb

Binary file not shown.

View File

@ -12,23 +12,23 @@
"author": "Hri7566", "author": "Hri7566",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@prisma/client": "5.2.0", "@prisma/client": "5.7.0",
"@t3-oss/env-core": "^0.6.1", "@t3-oss/env-core": "^0.6.1",
"bun": "^1.0.0", "bun": "^1.0.0",
"bun-types": "^1.0.1", "bun-types": "^1.0.1",
"commander": "^11.1.0",
"date-holidays": "^3.21.5", "date-holidays": "^3.21.5",
"dotenv": "^8.6.0", "dotenv": "^8.6.0",
"events": "^3.3.0", "events": "^3.3.0",
"fancy-text-converter": "^1.0.9", "fancy-text-converter": "^1.0.9",
"keccak": "^2.1.0", "keccak": "^2.1.0",
"mppclone-client": "^1.1.3",
"unique-names-generator": "^4.7.1", "unique-names-generator": "^4.7.1",
"yaml": "^2.3.2", "yaml": "^2.3.2",
"zod": "^3.22.2" "zod": "^3.22.2"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^20.5.9", "@types/node": "^20.5.9",
"prisma": "^5.2.0", "prisma": "5.7.0",
"typescript": "^5.2.2" "typescript": "^5.2.2"
} }
} }

View File

@ -3,20 +3,21 @@ import { Logger } from "../util/Logger";
import { loadConfig } from "../util/config"; import { loadConfig } from "../util/config";
import { import {
ChannelSettingValue, ChannelSettingValue,
ChannelSettings, IChannelSettings,
ClientEvents, ClientEvents,
Participant, Participant,
ServerEvents ServerEvents,
IChannelInfo
} from "../util/types"; } from "../util/types";
import { Socket } from "../ws/Socket"; import { Socket } from "../ws/Socket";
import { validateChannelSettings } from "./settings"; import { validateChannelSettings } from "./settings";
import { socketsBySocketID } from "../ws/server"; import { findSocketByPartID, socketsBySocketID } from "../ws/server";
import Crown from "./Crown"; import Crown from "./Crown";
interface ChannelConfig { interface ChannelConfig {
forceLoad: string[]; forceLoad: string[];
lobbySettings: Partial<ChannelSettings>; lobbySettings: Partial<IChannelSettings>;
defaultSettings: Partial<ChannelSettings>; defaultSettings: Partial<IChannelSettings>;
lobbyRegexes: string[]; lobbyRegexes: string[];
lobbyBackdoor: string; lobbyBackdoor: string;
fullChannel: string; fullChannel: string;
@ -48,7 +49,7 @@ export const config = loadConfig<ChannelConfig>("config/channels.yml", {
export const channelList = new Array<Channel>(); export const channelList = new Array<Channel>();
export class Channel extends EventEmitter { export class Channel extends EventEmitter {
private settings: Partial<ChannelSettings> = config.defaultSettings; private settings: Partial<IChannelSettings> = config.defaultSettings;
private ppl = new Array<Participant>(); private ppl = new Array<Participant>();
public logger: Logger; public logger: Logger;
@ -59,7 +60,7 @@ export class Channel extends EventEmitter {
constructor( constructor(
private _id: string, private _id: string,
set?: Partial<ChannelSettings>, set?: Partial<IChannelSettings>,
creator?: Socket, creator?: Socket,
owner_id?: string owner_id?: string
) { ) {
@ -193,7 +194,7 @@ export class Channel extends EventEmitter {
* @returns undefined * @returns undefined
*/ */
public changeSettings( public changeSettings(
set: Partial<ChannelSettings>, set: Partial<IChannelSettings>,
admin: boolean = false admin: boolean = false
) { ) {
if (this.isDestroyed()) return; if (this.isDestroyed()) return;
@ -223,7 +224,7 @@ export class Channel extends EventEmitter {
* @param setting Channel setting to get * @param setting Channel setting to get
* @returns Value of setting * @returns Value of setting
*/ */
public getSetting(setting: keyof ChannelSettings) { public getSetting(setting: keyof IChannelSettings) {
return this.settings[setting]; return this.settings[setting];
} }
@ -369,8 +370,10 @@ export class Channel extends EventEmitter {
id: this.getID(), id: this.getID(),
count: this.ppl.length, count: this.ppl.length,
settings: this.settings, settings: this.settings,
crown: JSON.parse(JSON.stringify(this.crown)) crown: this.crown
}; ? JSON.parse(JSON.stringify(this.crown))
: undefined
} as IChannelInfo;
} }
/** /**
@ -520,8 +523,34 @@ export class Channel extends EventEmitter {
*/ */
public dropCrown() { public dropCrown() {
if (this.crown) { if (this.crown) {
delete this.crown.participantId;
this.crown.time = Date.now(); this.crown.time = Date.now();
let socket;
if (this.crown.participantId)
socket = findSocketByPartID(this.crown.participantId);
let x = Math.random() * 100;
let y1 = Math.random() * 100;
let y2 = y1 + Math.random() * (100 - y1);
if (socket) {
const cursorPos = socket.getCursorPos();
let cursorX = cursorPos.x;
if (typeof cursorPos.x == "string")
cursorX = parseInt(cursorPos.x);
let cursorY = cursorPos.y;
if (typeof cursorPos.y == "string")
cursorY = parseInt(cursorPos.y);
}
// Screen positions
this.crown.startPos = { x, y: y1 };
this.crown.endPos = { x, y: y2 };
delete this.crown.participantId;
this.emit("update"); this.emit("update");
} }
} }

View File

@ -1,4 +1,4 @@
import { Participant } from "../util/types"; import { Participant, Vector2 } from "../util/types";
import { Socket } from "../ws/Socket"; import { Socket } from "../ws/Socket";
export class Crown { export class Crown {
@ -6,6 +6,16 @@ export class Crown {
public participantId: string | undefined; public participantId: string | undefined;
public time: number = Date.now(); public time: number = Date.now();
public startPos: Vector2 = {
x: 50,
y: 50
};
public endPos: Vector2 = {
x: 50,
y: 50
};
public canBeSetBy(socket: Socket) { public canBeSetBy(socket: Socket) {
// can claim, drop, or give if... // can claim, drop, or give if...
const flags = socket.getUserFlags(); const flags = socket.getUserFlags();

44
src/util/types.d.ts vendored
View File

@ -29,7 +29,7 @@ declare interface Participant extends User {
id: string; // participant id (same as user id on mppclone) id: string; // participant id (same as user id on mppclone)
} }
declare type ChannelSettings = { declare type IChannelSettings = {
color: string; color: string;
crownsolo: boolean; crownsolo: boolean;
chat: boolean; chat: boolean;
@ -87,14 +87,8 @@ declare interface Crown {
userId: string; userId: string;
partcipantId?: string; partcipantId?: string;
time: number; time: number;
startPos: { startPos: Vector2;
x: number; endPos: Vector2;
y: number;
};
endPos: {
x: number;
y: number;
};
} }
declare interface ChannelInfo { declare interface ChannelInfo {
@ -103,7 +97,7 @@ declare interface ChannelInfo {
id: string; id: string;
_id: string; _id: string;
crown?: Crown; crown?: Crown;
settings: Partial<ChannelSettings>; settings: Partial<IChannelSettings>;
} }
// Events copied from Hri7566/mppclone-client typedefs // Events copied from Hri7566/mppclone-client typedefs
@ -120,7 +114,7 @@ declare interface ServerEvents {
ch: { ch: {
m: "ch"; m: "ch";
_id: string; _id: string;
set: ChannelSettings; set: IChannelSettings;
}; };
chown: { chown: {
@ -130,7 +124,7 @@ declare interface ServerEvents {
chset: { chset: {
m: "chset"; m: "chset";
set: ChannelSettings; set: IChannelSettings;
}; };
custom: { custom: {
@ -328,3 +322,29 @@ declare type ServerEventListener<EventID extends keyof ServerEvents> = {
id: EventID; id: EventID;
callback: (msg: ServerEvents[EventID], socket: Socket) => void; callback: (msg: ServerEvents[EventID], socket: Socket) => void;
}; };
declare type Vector2<T = number> = {
x: T;
y: T;
};
declare interface ICrown {
// User who had the crown (remove participantId if there is none, no userId if there hasn't been one)
userId?: string;
participantId?: string;
// Crown position when dropped (beginning and end of slide animation)
startPos: Vector2;
endPos: Vector2;
// Timestamp from the latest crown update
time: number;
}
declare interface IChannelInfo {
_id: string;
id: string;
count: number;
settings: Partial<IChannelSettings>;
crown?: ICrown;
}

View File

@ -1,6 +1,6 @@
/** /**
* Socket connection module * Socket connection module
* *
* Represents user connections * Represents user connections
*/ */
@ -8,17 +8,18 @@ import { createColor, createID, createUserID } from "../util/id";
import EventEmitter from "events"; import EventEmitter from "events";
import { import {
ChannelInfo, ChannelInfo,
ChannelSettings, IChannelSettings,
ClientEvents, ClientEvents,
Participant, Participant,
ServerEvents, ServerEvents,
UserFlags UserFlags,
Vector2
} from "../util/types"; } from "../util/types";
import { User } from "@prisma/client"; import { User } from "@prisma/client";
import { createUser, readUser, updateUser } from "../data/user"; import { createUser, readUser, updateUser } from "../data/user";
import { eventGroups } from "./events"; import { eventGroups } from "./events";
import { Gateway } from "./Gateway"; import { Gateway } from "./Gateway";
import { Channel, channelList } from "../channel/Channel"; import { channelList, Channel } from "../channel/Channel";
import { ServerWebSocket } from "bun"; import { ServerWebSocket } from "bun";
import { socketsBySocketID } from "./server"; import { socketsBySocketID } from "./server";
import { Logger } from "../util/Logger"; import { Logger } from "../util/Logger";
@ -30,6 +31,8 @@ import { config } from "./usersConfig";
const logger = new Logger("Sockets"); const logger = new Logger("Sockets");
type CursorValue = string | number;
export class Socket extends EventEmitter { export class Socket extends EventEmitter {
private id: string; private id: string;
private _id: string; private _id: string;
@ -43,19 +46,14 @@ export class Socket extends EventEmitter {
public desiredChannel: { public desiredChannel: {
_id: string | undefined; _id: string | undefined;
set: Partial<ChannelSettings> | undefined; set: Partial<IChannelSettings> | undefined;
} = { } = {
_id: undefined, _id: undefined,
set: {} set: {}
}; };
public currentChannelID: string | undefined; public currentChannelID: string | undefined;
private cursorPos: private cursorPos: Vector2<CursorValue> = { x: 200, y: 100 };
| {
x: string | number | undefined;
y: string | number | undefined;
}
| undefined;
constructor(private ws: ServerWebSocket<unknown>, public socketID: string) { constructor(private ws: ServerWebSocket<unknown>, public socketID: string) {
super(); super();
@ -110,7 +108,7 @@ export class Socket extends EventEmitter {
return this.id; return this.id;
} }
public setChannel(_id: string, set?: Partial<ChannelSettings>) { public setChannel(_id: string, set?: Partial<IChannelSettings>) {
if (this.isDestroyed()) return; if (this.isDestroyed()) return;
this.desiredChannel._id = _id; this.desiredChannel._id = _id;
@ -136,7 +134,8 @@ export class Socket extends EventEmitter {
// Doesn't exist, create // Doesn't exist, create
channel = new Channel( channel = new Channel(
this.desiredChannel._id, this.desiredChannel._id,
this.desiredChannel.set this.desiredChannel.set,
this
); );
channel.join(this); channel.join(this);
@ -255,13 +254,13 @@ export class Socket extends EventEmitter {
public getCursorPos() { public getCursorPos() {
if (!this.cursorPos) if (!this.cursorPos)
this.cursorPos = { this.cursorPos = {
x: undefined, x: "-10.00",
y: undefined y: "-10.00"
}; };
return this.cursorPos; return this.cursorPos;
} }
public setCursorPos(x: number | string, y: number | string) { public setCursorPos(x: CursorValue, y: CursorValue) {
if (typeof x == "number") { if (typeof x == "number") {
x = x.toFixed(2); x = x.toFixed(2);
} }

View File

@ -1,3 +1,4 @@
// Bun hoists import, but not require? // Bun hoists import so we are kinda forced to use require here...
// Maybe bun should have a setting for that :/
require("./events/user"); require("./events/user");
require("./events/admin"); require("./events/admin");

View File

@ -6,6 +6,13 @@ export const m: ServerEventListener<"m"> = {
// Cursor movement // Cursor movement
if (!socket.rateLimits?.normal.m.attempt()) return; if (!socket.rateLimits?.normal.m.attempt()) return;
if (!msg.x || !msg.y) return; if (!msg.x || !msg.y) return;
socket.setCursorPos(msg.x, msg.y);
let x = msg.x;
let y = msg.y;
if (typeof msg.x == "string") x = parseFloat(msg.x);
if (typeof msg.y == "string") y = parseFloat(msg.y);
socket.setCursorPos(x, y);
} }
}; };