Commit 39ee483e authored by mercury233's avatar mercury233

add

parent 5a2989b7
......@@ -31,8 +31,11 @@ class Room
#alive
@all = []
@players_oppentlist = {}
@find_or_create_by_name: (name)->
@find_or_create_by_name: (name, player_name)->
if name == '' or name.toUpperCase() == 'S' or name.toUpperCase() == 'M'
return @find_or_create_random(name.toUpperCase(), player_name)
if room = @find_by_name(name)
return room
else if get_memory_usage()>=90
......@@ -40,11 +43,30 @@ class Room
else
return new Room(name)
@find_or_create_random: (type, player_name)->
if type == ''
result = _.find @all, (room)->
room.random_type and room.players.length == 1 and room.players[0].name != Room.players_oppentlist[player_name]
else
result = _.find @all, (room)->
room.random_type == type and room.players.length == 1 and room.players[0].name != Room.players_oppentlist[player_name]
if result
result.welcome = '对手已经在等你了,开始决斗吧!'
log.info 'found room', player_name
else
type = if type then type else 'S'
name = type + ',RANDOM#' + Math.floor(Math.random()*100000)
result = new Room(name)
result.random_type = type
result.welcome = '已建立随机对战房间,正在等待对手!'
log.info 'create room', player_name, name
return result
@find_by_name: (name)->
result = _.find @all, (room)->
room.name == name
#log.info 'find_by_name', name, result
result
return result
@find_by_port: (port)->
_.find @all, (room)->
......@@ -68,6 +90,8 @@ class Room
@established = false
@watcher_buffers = []
@watchers = []
@random_type = ''
@welcome = ''
Room.all.push this
@hostinfo =
......@@ -250,6 +274,9 @@ class Room
connect: (client)->
@players.push client
if @random_type
Room.players_oppentlist[@players[0].name] = if @players[1] then @players[1].name else null
if @players[1] then Room.players_oppentlist[@players[1].name] = @players[0].name
if @established
client.server.connect @port, '127.0.0.1', ->
......
......@@ -39,8 +39,13 @@
Room = (function() {
Room.all = [];
Room.find_or_create_by_name = function(name) {
Room.players_oppentlist = {};
Room.find_or_create_by_name = function(name, player_name) {
var room;
if (name === '' || name.toUpperCase() === 'S' || name.toUpperCase() === 'M') {
return this.find_or_create_random(name.toUpperCase(), player_name);
}
if (room = this.find_by_name(name)) {
return room;
} else if (get_memory_usage() >= 90) {
......@@ -50,6 +55,31 @@
}
};
Room.find_or_create_random = function(type, player_name) {
var name, result;
if (type === '') {
result = _.find(this.all, function(room) {
return room.random_type && room.players.length === 1 && room.players[0].name !== Room.players_oppentlist[player_name];
});
} else {
result = _.find(this.all, function(room) {
return room.random_type === type && room.players.length === 1 && room.players[0].name !== Room.players_oppentlist[player_name];
});
}
if (result) {
result.welcome = '对手已经在等你了,开始决斗吧!';
log.info('found room', player_name);
} else {
type = type ? type : 'S';
name = type + ',RANDOM#' + Math.floor(Math.random() * 100000);
result = new Room(name);
result.random_type = type;
result.welcome = '已建立随机对战房间,正在等待对手!';
log.info('create room', player_name, name);
}
return result;
};
Room.find_by_name = function(name) {
var result;
result = _.find(this.all, function(room) {
......@@ -87,6 +117,8 @@
this.established = false;
this.watcher_buffers = [];
this.watchers = [];
this.random_type = '';
this.welcome = '';
Room.all.push(this);
this.hostinfo = {
lflist: 0,
......@@ -313,6 +345,12 @@
Room.prototype.connect = function(client) {
this.players.push(client);
if (this.random_type) {
Room.players_oppentlist[this.players[0].name] = this.players[1] ? this.players[1].name : null;
if (this.players[1]) {
Room.players_oppentlist[this.players[1].name] = this.players[0].name;
}
}
if (this.established) {
client.server.connect(this.port, '127.0.0.1', function() {
var buffer, i, len, ref;
......
......@@ -228,13 +228,13 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
}
client.end()
else if !info.pass.length
ygopro.stoc_send_chat(client,"房间为空,请修改房间名")
ygopro.stoc_send client, 'ERROR_MSG',{
msg: 1
code: 2
}
client.end()
#else if !info.pass.length
# ygopro.stoc_send_chat(client,"房间为空,请修改房间名")
# ygopro.stoc_send client, 'ERROR_MSG',{
# msg: 1
# code: 2
# }
# client.end()
else if !Room.validate(info.pass)
#ygopro.stoc_send client, 'ERROR_MSG',{
......@@ -257,7 +257,7 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
else
#log.info 'join_game',info.pass, client.name
client.room = Room.find_or_create_by_name(info.pass)
client.room = Room.find_or_create_by_name(info.pass, client.name)
if !client.room
ygopro.stoc_send_chat(client,"服务器已经爆满,请稍候再试")
ygopro.stoc_send client, 'ERROR_MSG',{
......@@ -289,8 +289,8 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)->
return unless client.room
if settings.modules.welcome
ygopro.stoc_send_chat client, settings.modules.welcome
##if (os.freemem() / os.totalmem())<=0.1
## ygopro.stoc_send_chat client, "服务器已经爆满,随时存在崩溃风险!"
if client.room.welcome
ygopro.stoc_send_chat client, client.room.welcome
if settings.modules.post_start_watching and !client.room.watcher
client.room.watcher = watcher = net.connect client.room.port, ->
......@@ -449,6 +449,9 @@ ygopro.ctos_follow 'CHAT', true, (buffer, info, client, server)->
when '/tip'
ygopro.stoc_send_random_tip(client) if settings.modules.tips
when '/test'
log.info Room.players_oppentlist
return cancel
ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)->
......
......@@ -256,13 +256,6 @@
code: settings.version
});
client.end();
} else if (!info.pass.length) {
ygopro.stoc_send_chat(client, "房间为空,请修改房间名");
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
});
client.end();
} else if (!Room.validate(info.pass)) {
ygopro.stoc_send_chat(client, "房间密码不正确");
ygopro.stoc_send(client, 'ERROR_MSG', {
......@@ -277,7 +270,7 @@
});
client.end();
} else {
client.room = Room.find_or_create_by_name(info.pass);
client.room = Room.find_or_create_by_name(info.pass, client.name);
if (!client.room) {
ygopro.stoc_send_chat(client, "服务器已经爆满,请稍候再试");
ygopro.stoc_send(client, 'ERROR_MSG', {
......@@ -318,6 +311,9 @@
if (settings.modules.welcome) {
ygopro.stoc_send_chat(client, settings.modules.welcome);
}
if (client.room.welcome) {
ygopro.stoc_send_chat(client, client.room.welcome);
}
if (settings.modules.post_start_watching && !client.room.watcher) {
client.room.watcher = watcher = net.connect(client.room.port, function() {
ygopro.ctos_send(watcher, 'PLAYER_INFO', {
......@@ -510,6 +506,9 @@
if (settings.modules.tips) {
ygopro.stoc_send_random_tip(client);
}
break;
case '/test':
log.info(Room.players_oppentlist);
}
return cancel;
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment