MWSE/frontend/Room.ts

93 lines
2.3 KiB
TypeScript

import EventTarget from "./EventTarget";
import MWSE from "./index";
export interface IRoomOptions
{
name: string;
description?:string;
joinType: "free"|"invite"|"password"|"lock";
credential?: string;
ifexistsJoin?: boolean;
accessType?: "public"|"private";
notifyActionInvite?: boolean;
notifyActionJoined?: boolean;
notifyActionEjected?: boolean;
}
export default class Room extends EventTarget
{
public mwse : MWSE;
public options! : IRoomOptions;
public roomId? : string;
public accessType? : "public"|"private";
public description? : string;
public joinType? : "free"|"invite"|"password"|"lock";
public name? : string;
public owner? : string;
constructor(wsts:MWSE){
super();
this.mwse = wsts;
}
public setRoomOptions(options : IRoomOptions | string)
{
if(typeof options == "string")
{
this.roomId = options;
}else{
this.options = options;
}
}
setRoomId(uuid: string){
this.roomId = uuid;
}
async createRoom(roomOptions : IRoomOptions){
let options = this.options || roomOptions;
let result = await this.mwse.EventPooling.request({
type:'create-room',
...options
});
if(result.status == 'fail')
{
if(result.message == "ALREADY-EXISTS")
{
return this.join();
}
throw new Error(result.message || result.messages);
}else{
this.options = {
...this.options,
...result.room
};
this.roomId = result.room.id;
}
}
async join(){
let result = await this.mwse.EventPooling.request({
type:'joinroom',
name: this.options.name,
credential: this.options.credential
});
if(result.status == 'fail')
{
throw new Error(result.message);
}else{
this.options = {
...this.options,
...result.room
};
this.roomId = result.room.id;
}
}
async send(pack: any, wom:boolean = false){
await this.mwse.EventPooling.request({
type:'pack/room',
pack,
to: this.roomId,
wom
});
}
}