Binato/server/objects/Channel.ts

76 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-11-19 01:06:03 +00:00
import { DataStream } from "./DataStream";
2022-11-19 15:06:03 +00:00
import { User } from "./User";
const osu = require("osu-packet");
2022-11-19 01:06:03 +00:00
export class Channel {
public name:string;
public description:string;
2022-11-19 15:06:03 +00:00
public stream:DataStream;
private isLocked:boolean = false;
2022-11-20 23:37:39 +00:00
private _isSpecial:boolean = false;
2022-11-19 01:06:03 +00:00
2022-11-20 23:37:39 +00:00
public constructor(name:string, description:string, stream:DataStream, isSpecial:boolean = false) {
2022-11-19 01:06:03 +00:00
this.name = name;
this.description = description;
this.stream = stream;
2022-11-20 23:37:39 +00:00
this._isSpecial = isSpecial;
}
public get isSpecial() {
return this._isSpecial;
2022-11-19 01:06:03 +00:00
}
2022-11-19 15:06:03 +00:00
public get userCount() {
return this.stream.userCount;
}
public SendMessage(sender:User, message:string) {
const isBotCommand = message[0] === "!";
if (this.isLocked && !isBotCommand) {
return this.SendSystemMessage("This channel is currently locked", sender);
}
if (isBotCommand) {
if (message.split(" ")[0] === "!lock") {
this.isLocked = true;
}
}
const osuPacketWriter = new osu.Bancho.Writer;
osuPacketWriter.SendMessage({
sendingClient: sender.username,
message: message,
target: this.name,
senderId: sender.id
});
this.stream.SendWithExclusion(osuPacketWriter.toBuffer, sender);
}
public SendSystemMessage(message:string, sendTo?:User) {
const osuPacketWriter = new osu.Bancho.Writer;
osuPacketWriter.SendMessage({
sendingClient: "System",
message: message,
target: this.name,
senderId: 1
});
if (sendTo instanceof User) {
sendTo.addActionToQueue(osuPacketWriter.toBuffer);
} else {
this.stream.Send(osuPacketWriter.toBuffer);
}
}
public Join(user:User) {
this.stream.AddUser(user);
const osuPacketWriter = new osu.Bancho.Writer;
osuPacketWriter.ChannelJoinSuccess(this.name);
user.addActionToQueue(osuPacketWriter.toBuffer);
}
2022-11-19 01:06:03 +00:00
2022-11-19 15:06:03 +00:00
public Leave(user:User) {
this.stream.RemoveUser(user);
2022-11-19 01:06:03 +00:00
}
}