
/*=============================================================================================
    author:ZhaoJun
    version:1.2
    email:2732810281@qq.com
=============================================================================================*/


//严格模式
/*-------------------------------------------------------------------------------------------*/	
    'use strict';
/*-------------------------------------------------------------------------------------------*/
    

const filters= $Sys.model('/fun/filter')

// 用户管理操作
/*-------------------------------------------------------------------------------------------*/	
    class userMrg
    {
        static getUserName(con,type='webrtc')
        {
            type=type?type+'_':''
            if(!con.$saveName)return
            for (let index = 0; index < con.$saveName.length; index++) 
            {
                let cur=con.$saveName[index]
                if(cur.indexOf(type)===0)
                {
                    return cur.slice(type.length)
                }
            }
        }
 
        static add(user,type,con)
        {
            let saveName=type+'_'+user
            if(!con.$saveName)con.$saveName=[saveName]
            else if(con.$saveName.length<$ExtendNet.twUsers) con.$saveName.push(saveName)
            else 
            {
                console.warn('TCP/WS客户端连接上登陆用户过多...'+saveName);
                con.json(filters.echo($Code.sys.conLimit),{$end:true})
                return
            }
            
            if(!$ExtendNet.twType.includes(type))
            {
                con.json(filters.echo($Code.sys.deny),{$end:true})
                return
            }
            let curCOn=$ExtendNet.tw.has(saveName)
            if(curCOn)
            {
                $ExtendNet.tw.get(saveName).json(filters.echo($Code.code.otherSign),{$end:true})
                userMrg.remove(user,type)
            }
            con.once
            (
                'close',()=>
                {
                    console.info('TCP/WS客户端关闭...'+saveName);
                    if($ExtendNet.tw.get(saveName)==con)
                    {
                        userMrg.remove(user,type)
                    }
                }
            )
            return $ExtendNet.tw.set(saveName,con)
        }
        
        static send(user,type,data)
        {
            let saveName=type+'_'+user
            if($ExtendNet.tw.has(saveName))
            {
                return $ExtendNet.tw.get(saveName).json(data)
            }
            else
            {
                return false;
            }
        }
        
        static sendAll(user,data)
        {
            for (const iterator of $ExtendNet.twType) 
            {
                let saveName=iterator+'_'+user
                if($ExtendNet.tw.has(saveName))
                {
                    $ExtendNet.tw.get(saveName).json(data);
                }
            }
        }

        static sendHot(data)
        {
            for (const iterator of $ExtendNet.tw.keys()) 
            {
                if(iterator.indexOf('asyncWeb_$root')==0)
                {
                    $ExtendNet.tw.get(iterator).json(data);
                }  
            } 
        }
        
        static remove(user,type)
        { 
            return $ExtendNet.tw.delete(type+'_'+user)
        }
        
        static removeAll(user)
        {
            for (const iterator of $ExtendNet.twType) 
            {
                let saveName=iterator+'_'+user
                if($ExtendNet.tw.has(saveName))
                {
                    $ExtendNet.tw.delete(saveName)
                }
            }
        }
  }
/*-------------------------------------------------------------------------------------------*/    
module.exports=userMrg;