noname/game/server.js

144 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-04-11 01:51:47 +00:00
(function(){
var WebSocketServer=require('ws').Server;
var wss=new WebSocketServer({port:8080});
var rooms=[{},{},{}];
var clients={};
var messages={
enter:function(index,nickname,avatar){
this.nickname=nickname;
this.avatar=avatar;
var room=rooms[index];
if(!room){
index=0;
room=rooms[0];
}
this.room=room;
if(room.owner){
if(!room.config){
this.sendl('enterroomfailed');
}
else{
this.owner=room.owner;
2016-04-11 06:13:35 +00:00
this.owner.sendl('onconnection',this.wsid);
2016-04-11 01:51:47 +00:00
}
2016-04-11 06:13:35 +00:00
util.updaterooms();
2016-04-11 01:51:47 +00:00
}
else{
room.owner=this;
this.sendl('createroom');
}
},
config:function(config){
var room=this.room;
if(room&&room.owner==this){
room.config=config;
}
2016-04-11 06:13:35 +00:00
util.updaterooms();
2016-04-11 01:51:47 +00:00
},
2016-04-11 06:13:35 +00:00
send:function(id,message){
if(clients[id]&&clients[id].owner==this){
clients[id].send(message);
}
},
close:function(id){
if(clients[id]&&clients[id].owner==this){
clients[id].close();
2016-04-11 01:51:47 +00:00
}
}
};
var util={
sendl:function(){
2016-04-11 06:34:50 +00:00
var args=[];
for(var i=0;i<arguments.length;i++){
args.push(arguments[i]);
}
this.send(JSON.stringify(args));
2016-04-11 01:51:47 +00:00
},
getid:function(){
return (Math.floor(1000000000+9000000000*Math.random())).toString();
},
getroomlist:function(){
var roomlist=[];
2016-04-11 06:13:35 +00:00
for(var i=0;i<3;i++){
rooms[i]._num=0;
}
for(var i in clients){
if(clients[i].room){
clients[i].room._num++;
}
}
2016-04-11 01:51:47 +00:00
for(var i=0;i<3;i++){
if(rooms[i].owner&&rooms[i].config){
2016-04-11 06:13:35 +00:00
roomlist[i]=[rooms[i].owner.nickname,rooms[i].owner.avatar,
rooms[i].config,rooms[i]._num];
2016-04-11 01:51:47 +00:00
}
else{
roomlist[i]=null;
}
2016-04-11 06:13:35 +00:00
delete rooms[i]._num;
2016-04-11 01:51:47 +00:00
}
return roomlist;
2016-04-11 06:13:35 +00:00
},
updaterooms:function(){
var roomlist=util.getroomlist();
for(var i in clients){
if(!clients[i].room){
clients[i].sendl('updaterooms',roomlist);
}
}
},
2016-04-11 01:51:47 +00:00
};
wss.on('connection',function(ws){
ws.sendl=util.sendl;
ws.wsid=util.getid();
clients[ws.wsid]=ws;
ws.sendl('roomlist',util.getroomlist());
ws.on('message',function(message){
2016-04-11 06:13:35 +00:00
if(this.owner){
this.owner.sendl('onmessage',this.wsid,message);
2016-04-11 01:51:47 +00:00
}
2016-04-11 06:13:35 +00:00
else{
var arr;
try{
arr=JSON.parse(message);
if(!Array.isArray(arr)){
throw('err');
}
}
catch(e){
this.sendl('denied','banned');
return;
}
if(arr.shift()=='server'){
var type=arr.shift();
if(messages[type]){
messages[type].apply(this,arr);
}
2016-04-11 01:51:47 +00:00
}
}
});
ws.on('close',function(){
2016-04-11 06:13:35 +00:00
if(!clients[this.wsid]) return;
2016-04-11 01:51:47 +00:00
if(this.owner){
2016-04-11 06:13:35 +00:00
this.owner.sendl('onclose',this.wsid);
2016-04-11 01:51:47 +00:00
}
else{
var room=this.room;
if(room&&room.owner==this){
room.owner=null;
room.config=null;
for(var i in clients){
if(clients[i].room==room&&clients[i]!=this){
clients[i].close();
2016-04-11 06:13:35 +00:00
delete clients[i];
2016-04-11 01:51:47 +00:00
}
}
}
delete clients[this.wsid];
}
2016-04-11 06:13:35 +00:00
util.updaterooms();
2016-04-11 01:51:47 +00:00
});
});
}());