(function(){ var WebSocketServer=require('ws').Server; var wss=new WebSocketServer({port:8080}); var rooms=[{},{},{},{},{},{}]; var events=[]; var clients={}; var messages={ enter:function(index,nickname,avatar,config,mode){ this.nickname=nickname; this.avatar=avatar; var room=rooms[index]; if(!room){ index=0; room=rooms[0]; } this.room=room; delete this.status; if(room.owner){ if(room.servermode&&!room.owner._onconfig&&config&&mode){ room.owner.sendl('createroom',index,config,mode); room.owner._onconfig=this; room.owner.nickname=nickname; room.owner.avatar=avatar; } else if(!room.config){ this.sendl('enterroomfailed'); } else{ this.owner=room.owner; this.owner.sendl('onconnection',this.wsid); } util.updaterooms(); } else{ room.owner=this; this.sendl('createroom',index); } }, changeAvatar:function(nickname,avatar){ this.nickname=nickname; this.avatar=avatar; util.updateclients(); }, server:function(cfg){ if(cfg){ this.servermode=true; var room=rooms[cfg[0]]; if(!room||room.owner){ this.sendl('reloadroom',true); } else{ room.owner=this; this.room=room; this.nickname=cfg[1]; this.avatar=cfg[2]; this.sendl('createroom',cfg[0],{},'auto') } } else{ for(var i=0;i=20){ this.sendl('eventsdenied','total'); } else if(cfg.utc<=time){ this.sendl('eventsdenied','time'); } else{ cfg.nickname=cfg.nickname||'无名玩家'; cfg.avatar=cfg.nickname||'caocao'; cfg.creator=id; cfg.id=util.getid(); cfg.members=[id]; events.unshift(cfg); changed=true; } } } if(changed){ util.updateevents(); } }, config:function(config){ var room=this.room; if(room&&room.owner==this){ if(room.servermode){ room.servermode=false; if(this._onconfig){ if(clients[this._onconfig.wsid]){ this._onconfig.owner=this; this.sendl('onconnection',this._onconfig.wsid); } delete this._onconfig; } } room.config=config; } util.updaterooms(); }, status:function(str){ if(typeof str=='string'){ this.status=str; } else{ delete this.status; } util.updateclients(); }, send:function(id,message){ if(clients[id]&&clients[id].owner==this){ try{ clients[id].send(message); } catch(e){ clients[id].close(); } } }, close:function(id){ if(clients[id]&&clients[id].owner==this){ clients[id].close(); } }, }; var util={ sendl:function(){ var args=[]; for(var i=0;i