179 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
			
		
		
	
	
			179 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
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 config! : 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<string,Peer> = 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.config = defaultOptions as IRoomOptions;
 | 
						|
        }
 | 
						|
    }
 | 
						|
    
 | 
						|
    setRoomId(uuid: string){
 | 
						|
        this.roomId = uuid;
 | 
						|
    }
 | 
						|
    async createRoom(roomOptions : IRoomOptions){
 | 
						|
        let config = this.config ||  roomOptions;
 | 
						|
        let result = await this.mwse.EventPooling.request({
 | 
						|
            type:'create-room',
 | 
						|
            ...config
 | 
						|
        });
 | 
						|
        if(result.status == 'fail')
 | 
						|
        {
 | 
						|
            if(result.message == "ALREADY-EXISTS" && this.config.ifexistsJoin)
 | 
						|
            {
 | 
						|
                return this.join();
 | 
						|
            }
 | 
						|
            throw new Error(result.message || result.messages);
 | 
						|
        }else{
 | 
						|
            this.options = {
 | 
						|
                ...this.config,
 | 
						|
                ...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.config.name,
 | 
						|
            credential: this.config.credential,
 | 
						|
            autoFetchInfo: this.config.autoFetchInfo || false
 | 
						|
        });
 | 
						|
        if(result.status == 'fail')
 | 
						|
        {
 | 
						|
            throw new Error(result.message);
 | 
						|
        }else{
 | 
						|
            this.options = {
 | 
						|
                ...this.config,
 | 
						|
                ...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, handshake = false){
 | 
						|
        if(!this.mwse.writable){
 | 
						|
            return console.warn("Socket is not writable");
 | 
						|
        }
 | 
						|
        if(handshake)
 | 
						|
        {
 | 
						|
            let {type} = await this.mwse.EventPooling.request({
 | 
						|
                type:'pack/room',
 | 
						|
                pack,
 | 
						|
                to: this.roomId,
 | 
						|
                wom,
 | 
						|
                handshake
 | 
						|
            }) as {
 | 
						|
                type:"success"|"fail"
 | 
						|
            };
 | 
						|
            if(type == "fail"){
 | 
						|
                throw new Error("Cant send message to room")
 | 
						|
            }
 | 
						|
        }else{
 | 
						|
            await this.mwse.EventPooling.request({
 | 
						|
                type:'pack/room',
 | 
						|
                pack,
 | 
						|
                to: this.roomId,
 | 
						|
                wom,
 | 
						|
                handshake
 | 
						|
            })
 | 
						|
        }
 | 
						|
    }
 | 
						|
    async fetchPeers(filter?:{[key:string]:any}, onlyNumber:boolean = false) : Promise<Number | Peer[]>
 | 
						|
    {
 | 
						|
        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;
 | 
						|
        }
 | 
						|
    }
 | 
						|
} |