import EventTarget from "./EventTarget"; import MWSE from "./index"; import Peer from "./Peer"; import { RoomInfo } from "./RoomInfo"; 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; autoFetchInfo?: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; public peers : Map = new Map(); public info : RoomInfo; constructor(wsts:MWSE){ super(); this.mwse = wsts; this.info = new RoomInfo(this); } public setRoomOptions(options : IRoomOptions | string) { if(typeof options == "string") { this.roomId = options; }else{ let defaultOptions = { joinType: "free", ifexistsJoin: true, accessType: "private", notifyActionInvite: true, notifyActionJoined: true, notifyActionEjected: true, autoFetchInfo: true }; Object.assign(defaultOptions,options); this.options = defaultOptions as IRoomOptions; } } 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" && this.options.ifexistsJoin) { return this.join(); } throw new Error(result.message || result.messages); }else{ this.options = { ...this.options, ...result.room }; this.roomId = result.room.id; this.mwse.rooms.set(this.roomId as string, this); } } async join(){ let result = await this.mwse.EventPooling.request({ type:'joinroom', name: this.options.name, credential: this.options.credential, autoFetchInfo: this.options.autoFetchInfo || false }); if(result.status == 'fail') { throw new Error(result.message); }else{ this.options = { ...this.options, ...result.room }; if(result.info) { this.info.info = result.info; }; this.roomId = result.room.id; this.mwse.rooms.set(this.roomId as string, this); } } async eject(){ let {type} = await this.mwse.EventPooling.request({ type:'ejectroom', roomId: this.roomId }); this.peers.clear(); if(type == 'success') { this.mwse.rooms.delete(this.roomId as string); } } async send(pack: any, wom:boolean = false){ if(!this.mwse.writable){ return console.warn("Socket is not writable"); } await this.mwse.EventPooling.request({ type:'pack/room', pack, to: this.roomId, wom }); } async fetchPeers(filter?:{[key:string]:any}, onlyNumber:boolean = false) : Promise { if(onlyNumber) { let {count} = await this.mwse.EventPooling.request({ type:'room/peer-count', roomId: this.roomId, filter: filter || {} }) as {count:Number}; return count; }else{ let {status, peers} = await this.mwse.EventPooling.request({ type:'room-peers', roomId: this.roomId, filter: filter || {} }) as {status:"success"|"fail", peers: string[]}; let cup : Peer[] = []; if(status == 'fail') { throw new Error("Cant using peers on room") }else if(status == 'success'){ for (const peerid of peers) { let peer = this.mwse.peer(peerid,true); cup.push(peer); this.peers.set(peerid, peer); } }; return cup; } } }