2022-11-23 00:48:28 +00:00
|
|
|
import { SharedContent } from "../interfaces/SharedContent";
|
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-23 00:48:28 +00:00
|
|
|
private readonly botUser:User;
|
|
|
|
|
|
|
|
public constructor(sharedContent:SharedContent, 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;
|
2022-11-23 00:48:28 +00:00
|
|
|
|
|
|
|
const bot = sharedContent.users.getByKey("bot");
|
|
|
|
if (!(bot instanceof User)) {
|
|
|
|
throw "Something has gone horribly wrong, the bot user doesn't exist!";
|
|
|
|
}
|
|
|
|
this.botUser = bot;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get self() {
|
|
|
|
return this;
|
2022-11-20 23:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-11-23 00:48:28 +00:00
|
|
|
|
|
|
|
if (message === "!mp start") {
|
|
|
|
this.SendBotMessage("glhf!");
|
|
|
|
sender.match?.startMatch();
|
|
|
|
}
|
2022-11-19 15:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-11-23 00:48:28 +00:00
|
|
|
public SendBotMessage(message:string, sendTo?:User) {
|
|
|
|
const osuPacketWriter = new osu.Bancho.Writer;
|
|
|
|
osuPacketWriter.SendMessage({
|
|
|
|
sendingClient: this.botUser.username,
|
|
|
|
message: message,
|
|
|
|
target: this.name,
|
|
|
|
senderId: this.botUser.id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (sendTo instanceof User) {
|
|
|
|
sendTo.addActionToQueue(osuPacketWriter.toBuffer);
|
|
|
|
} else {
|
|
|
|
this.stream.Send(osuPacketWriter.toBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 15:06:03 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|