This commit is contained in:
libccy 2016-04-11 09:51:47 +08:00
parent 084a8195dc
commit 6feeb753df
3 changed files with 141 additions and 4 deletions

View File

@ -6960,6 +6960,7 @@
this.node.name.innerHTML=get.verticalStr(name);
this.nickname=name;
this.avatar=character;
this.node.nameol.innerHTML='';
},
uninitOL:function(){
this.node.avatar.hide();
@ -11793,6 +11794,7 @@
},
createroom:function(){
game.online=false;
game.onlineroom=true;
lib.node={};
for(var i=0;i<ui.rooms.length;i++){
ui.rooms[i].delete();
@ -11800,6 +11802,10 @@
delete ui.rooms;
game.switchMode('identity');
},
enterroomfailed:function(){
alert('请稍后再试');
_status.enteringroom=false;
},
roomlist:function(list){
game.online=true;
lib.config.recentIP.remove(_status.ip);
@ -11839,13 +11845,13 @@
player.dataset.position='c'+i;
player.classList.add('connect');
player.roomindex=i;
if(!list[i]||!list[i].config){
if(!list[i]){
player.initOL('空房间',list2[i])
}
else{
var config=list[i].config;
player.initOL(get.cnNumber(parseInt(config.number))+'人'+get.translation(config.mode),list[i].owner[1]);
player.setNickname(list[i].owner[0]);
var config=list[i][2];
player.initOL(get.cnNumber(parseInt(config.number))+'人'+get.translation(config.mode),list[i][1]);
player.setNickname(list[i][0]);
}
ui.rooms.push(player);
}
@ -11857,6 +11863,23 @@
proceed();
}
},
updaterooms:function(list){
if(ui.rooms){
var list2=['re_caocao','liubei','sunquan'];
for(var i=0;i<ui.rooms.length;i++){
var player=ui.rooms[i];
if(!list[i]){
player.initOL('空房间',list2[i])
}
else{
var config=list[i][2];
player.initOL(get.cnNumber(parseInt(config.number))+'人'+get.translation(config.mode),list[i][1]);
player.setNickname(list[i][0]);
}
ui.rooms.push(player);
}
}
},
init:function(id,config,ip){
game.online=true;
game.onlineID=id;

113
game/server.js Normal file
View File

@ -0,0 +1,113 @@
(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;
this.owner.send('connection');
this.sendl('enterroom');
}
messages.updaterooms();
}
else{
room.owner=this;
this.sendl('createroom');
}
},
config:function(config){
var room=this.room;
if(room&&room.owner==this){
room.config=config;
}
messages.updaterooms();
},
updaterooms:function(){
var roomlist=util.getroomlist();
for(var i in clients){
if(!clients[i].room){
clients[i].sendl('updaterooms',roomlist);
}
}
}
};
var util={
sendl:function(){
this.send(JSON.stringify(Array.from(arguments)));
},
getid:function(){
return (Math.floor(1000000000+9000000000*Math.random())).toString();
},
getroomlist:function(){
var roomlist=[];
for(var i=0;i<3;i++){
if(rooms[i].owner&&rooms[i].config){
roomlist[i]=[rooms[i].owner.nickname,rooms[i].owner.avatar,rooms[i].config];
}
else{
roomlist[i]=null;
}
}
return roomlist;
}
};
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){
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);
}
}
});
ws.on('close',function(){
if(this.owner){
}
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();
}
}
}
delete clients[this.wsid];
}
messages.updaterooms();
});
});
}());

View File

@ -34,6 +34,7 @@ window.noname_source_list=[
'game/game.js',
'game/jszip.js',
'game/package.js',
'game/server.js',
'game/source.js',
'game/update.js',
'layout/default/blur.css',