Commit bba69ea7 authored by mercury233's avatar mercury233

memory leak test4, synchronous ctos_follow, hide password, add http stop server

parent 5df13214
...@@ -122,10 +122,12 @@ class Room ...@@ -122,10 +122,12 @@ class Room
return if @deleted return if @deleted
#log.info 'room-delete', this.name, Room.all.length #log.info 'room-delete', this.name, Room.all.length
@watcher_buffers = [] @watcher_buffers = []
@players = []
@watcher.end() if @watcher
@deleted = true
index = _.indexOf(Room.all, this) index = _.indexOf(Room.all, this)
#Room.all[index] = null unless index == -1 #Room.all[index] = null unless index == -1
Room.all.splice(index, 1) unless index == -1 Room.all.splice(index, 1) unless index == -1
@deleted = true
return return
connect: (client)-> connect: (client)->
...@@ -144,13 +146,16 @@ class Room ...@@ -144,13 +146,16 @@ class Room
ygopro.stoc_send_chat_to_room this, "#{client.name} #{'退出了观战'}#{if error then ": #{error}" else ''}" ygopro.stoc_send_chat_to_room this, "#{client.name} #{'退出了观战'}#{if error then ": #{error}" else ''}"
index = _.indexOf(@watchers, client) index = _.indexOf(@watchers, client)
@watchers.splice(index, 1) unless index == -1 @watchers.splice(index, 1) unless index == -1
client.room = null
else else
index = _.indexOf(@players, client) index = _.indexOf(@players, client)
@players.splice(index, 1) unless index == -1 @players.splice(index, 1) unless index == -1
if @players.length if @players.length
ygopro.stoc_send_chat_to_room this, "#{client.name} #{'离开了游戏'}#{if error then ": #{error}" else ''}" ygopro.stoc_send_chat_to_room this, "#{client.name} #{'离开了游戏'}#{if error then ": #{error}" else ''}"
client.room = null
else else
@process.kill() @process.kill()
client.room = null
this.delete() this.delete()
return return
......
...@@ -154,11 +154,15 @@ ...@@ -154,11 +154,15 @@
return; return;
} }
this.watcher_buffers = []; this.watcher_buffers = [];
this.players = [];
if (this.watcher) {
this.watcher.end();
}
this.deleted = true;
index = _.indexOf(Room.all, this); index = _.indexOf(Room.all, this);
if (index !== -1) { if (index !== -1) {
Room.all.splice(index, 1); Room.all.splice(index, 1);
} }
this.deleted = true;
}; };
Room.prototype.connect = function(client) { Room.prototype.connect = function(client) {
...@@ -185,6 +189,7 @@ ...@@ -185,6 +189,7 @@
if (index !== -1) { if (index !== -1) {
this.watchers.splice(index, 1); this.watchers.splice(index, 1);
} }
client.room = null;
} else { } else {
index = _.indexOf(this.players, client); index = _.indexOf(this.players, client);
if (index !== -1) { if (index !== -1) {
...@@ -192,8 +197,10 @@ ...@@ -192,8 +197,10 @@
} }
if (this.players.length) { if (this.players.length) {
ygopro.stoc_send_chat_to_room(this, client.name + " " + '离开了游戏' + (error ? ": " + error : '')); ygopro.stoc_send_chat_to_room(this, client.name + " " + '离开了游戏' + (error ? ": " + error : ''));
client.room = null;
} else { } else {
this.process.kill(); this.process.kill();
client.room = null;
this["delete"](); this["delete"]();
} }
} }
......
...@@ -35,21 +35,24 @@ if process.argv[2] == '--debug' ...@@ -35,21 +35,24 @@ if process.argv[2] == '--debug'
log = bunyan.createLogger name: "mycard-debug" log = bunyan.createLogger name: "mycard-debug"
else else
log = bunyan.createLogger name: "mycard" log = bunyan.createLogger name: "mycard"
###
#定时清理关闭的连接 #定时清理关闭的连接
Graveyard = [] Graveyard = []
send_to_graveyard = (socket) ->
unless _.indexOf(Graveyard, socket)
Graveyard.push(socket)
tribute = (socket) -> tribute = (socket) ->
setTimeout send_to_graveyard(socket), 30000 setTimeout ((socket)-> Graveyard.push(socket);return)(socket), 3000
return
setInterval ()-> setInterval ()->
log.info Graveyard for fuck,i in Graveyard
, 30000 Graveyard[i].destroy() if Graveyard[i]
### for you,j in Graveyard[i]
Graveyard[i][j] = null
Graveyard[i] = null
Graveyard = []
return
, 3000
#网络连接 #网络连接
net.createServer (client) -> net.createServer (client) ->
server = new net.Socket() server = new net.Socket()
...@@ -60,7 +63,7 @@ net.createServer (client) -> ...@@ -60,7 +63,7 @@ net.createServer (client) ->
#释放处理 #释放处理
client.on 'close', (had_error) -> client.on 'close', (had_error) ->
#log.info "client closed", client.name, had_error #log.info "client closed", client.name, had_error
#tribute(client) tribute(client)
unless client.closed unless client.closed
client.closed = true client.closed = true
client.room.disconnect(client) if client.room client.room.disconnect(client) if client.room
...@@ -69,7 +72,7 @@ net.createServer (client) -> ...@@ -69,7 +72,7 @@ net.createServer (client) ->
client.on 'error', (error)-> client.on 'error', (error)->
#log.info "client error", client.name, error #log.info "client error", client.name, error
#tribute(client) tribute(client)
unless client.closed unless client.closed
client.closed = error client.closed = error
client.room.disconnect(client, error) if client.room client.room.disconnect(client, error) if client.room
...@@ -82,7 +85,7 @@ net.createServer (client) -> ...@@ -82,7 +85,7 @@ net.createServer (client) ->
server.on 'close', (had_error) -> server.on 'close', (had_error) ->
#log.info "server closed", client.name, had_error #log.info "server closed", client.name, had_error
#tribute(server) tribute(server)
server.closed = true unless server.closed server.closed = true unless server.closed
unless client.closed unless client.closed
ygopro.stoc_send_chat(client, "服务器关闭了连接") ygopro.stoc_send_chat(client, "服务器关闭了连接")
...@@ -91,7 +94,7 @@ net.createServer (client) -> ...@@ -91,7 +94,7 @@ net.createServer (client) ->
server.on 'error', (error)-> server.on 'error', (error)->
#log.info "server error", client.name, error #log.info "server error", client.name, error
#tribute(server) tribute(server)
server.closed = error server.closed = error
unless client.closed unless client.closed
ygopro.stoc_send_chat(client, "服务器错误: #{error}") ygopro.stoc_send_chat(client, "服务器错误: #{error}")
...@@ -111,11 +114,8 @@ net.createServer (client) -> ...@@ -111,11 +114,8 @@ net.createServer (client) ->
client.room.watcher.write data client.room.watcher.write data
else else
ctos_buffer = Buffer.concat([ctos_buffer, data], ctos_buffer.length + data.length) #buffer的错误使用方式,好孩子不要学 ctos_buffer = Buffer.concat([ctos_buffer, data], ctos_buffer.length + data.length) #buffer的错误使用方式,好孩子不要学
if client.established datas = []
server.write data
else
client.pre_establish_buffers.push data
while true while true
if ctos_message_length == 0 if ctos_message_length == 0
...@@ -131,19 +131,28 @@ net.createServer (client) -> ...@@ -131,19 +131,28 @@ net.createServer (client) ->
else else
if ctos_buffer.length >= 2 + ctos_message_length if ctos_buffer.length >= 2 + ctos_message_length
#console.log "CTOS", ygopro.constants.CTOS[ctos_proto] #console.log "CTOS", ygopro.constants.CTOS[ctos_proto]
cancel = false
if ygopro.ctos_follows[ctos_proto] if ygopro.ctos_follows[ctos_proto]
b = ctos_buffer.slice(3, ctos_message_length-1+3) b = ctos_buffer.slice(3, ctos_message_length-1+3)
if struct = ygopro.structs[ygopro.proto_structs.CTOS[ygopro.constants.CTOS[ctos_proto]]] if struct = ygopro.structs[ygopro.proto_structs.CTOS[ygopro.constants.CTOS[ctos_proto]]]
struct._setBuff(b) struct._setBuff(b)
ygopro.ctos_follows[ctos_proto].callback b, _.clone(struct.fields), client, server if ygopro.ctos_follows[ctos_proto].synchronous
cancel = ygopro.ctos_follows[ctos_proto].callback b, _.clone(struct.fields), client, server
else
ygopro.ctos_follows[ctos_proto].callback b, _.clone(struct.fields), client, server
else else
ygopro.ctos_follows[ctos_proto].callback b, null, client, server ygopro.ctos_follows[ctos_proto].callback b, null, client, server
datas.push ctos_buffer.slice(0, 2 + ctos_message_length) unless cancel
ctos_buffer = ctos_buffer.slice(2 + ctos_message_length) ctos_buffer = ctos_buffer.slice(2 + ctos_message_length)
ctos_message_length = 0 ctos_message_length = 0
ctos_proto = 0 ctos_proto = 0
else else
break break
if client.established
server.write buffer for buffer in datas
else
client.pre_establish_buffers.push buffer for buffer in datas
return return
#服务端到客户端(stoc) #服务端到客户端(stoc)
...@@ -194,8 +203,13 @@ net.createServer (client) -> ...@@ -194,8 +203,13 @@ net.createServer (client) ->
#功能模块 #功能模块
ygopro.ctos_follow 'PLAYER_INFO', true, (buffer, info, client, server)-> ygopro.ctos_follow 'PLAYER_INFO', true, (buffer, info, client, server)->
client.name = info.name #在创建room之前暂存 name=info.name.split("$")[0];
return struct = ygopro.structs["CTOS_PlayerInfo"]
struct._setBuff(buffer)
struct.set("name",name)
buffer = struct.buffer
client.name = name
return false
ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)-> ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
#log.info info #log.info info
...@@ -272,6 +286,7 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)-> ...@@ -272,6 +286,7 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)-> ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)->
#欢迎信息 #欢迎信息
return unless client.room
if settings.modules.welcome if settings.modules.welcome
ygopro.stoc_send_chat client, settings.modules.welcome ygopro.stoc_send_chat client, settings.modules.welcome
if (os.freemem() / os.totalmem())<=0.1 if (os.freemem() / os.totalmem())<=0.1
...@@ -292,6 +307,7 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)-> ...@@ -292,6 +307,7 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)->
return return
watcher.on 'data', (data)-> watcher.on 'data', (data)->
return unless client.room
client.room.watcher_buffers.push data client.room.watcher_buffers.push data
for w in client.room.watchers for w in client.room.watchers
w.write data if w #a WTF fix w.write data if w #a WTF fix
...@@ -327,20 +343,21 @@ ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)-> ...@@ -327,20 +343,21 @@ ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)->
client.lp = client.room.hostinfo.start_lp client.lp = client.room.hostinfo.start_lp
#ygopro.stoc_send_chat_to_room(client.room, "LP跟踪调试信息: #{client.name} 初始LP #{client.lp}") #ygopro.stoc_send_chat_to_room(client.room, "LP跟踪调试信息: #{client.name} 初始LP #{client.lp}")
###
if ygopro.constants.MSG[msg] == 'WIN' and _.startsWith(client.room.name, 'M#') and client.is_host if ygopro.constants.MSG[msg] == 'WIN' and _.startsWith(client.room.name, 'M#') and client.is_host
pos = buffer.readUInt8(1) pos = buffer.readUInt8(1)
pos = 1 - pos unless client.is_first or pos == 2 pos = 1 - pos unless client.is_first or pos == 2
reason = buffer.readUInt8(2) reason = buffer.readUInt8(2)
#log.info {winner: pos, reason: reason} #log.info {winner: pos, reason: reason}
client.room.duels.push {winner: pos, reason: reason} client.room.duels.push {winner: pos, reason: reason}
###
#lp跟踪 #lp跟踪
if ygopro.constants.MSG[msg] == 'DAMAGE' and client.is_host if ygopro.constants.MSG[msg] == 'DAMAGE' and client.is_host
pos = buffer.readUInt8(1) pos = buffer.readUInt8(1)
pos = 1 - pos unless client.is_first pos = 1 - pos unless client.is_first
val = buffer.readInt32LE(2) val = buffer.readInt32LE(2)
client.room.dueling_players[pos].lp -= val client.room.dueling_players[pos].lp -= val
#ygopro.stoc_send_chat_to_room(client.room, "LP跟踪调试信息: #{client.room.dueling_players[pos].name} 受到伤害 #{val},现在的LP为 #{client.room.dueling_players[pos].lp}")
if 0 < client.room.dueling_players[pos].lp <= 100 if 0 < client.room.dueling_players[pos].lp <= 100
ygopro.stoc_send_chat_to_room(client.room, "你的生命已经如风中残烛了!") ygopro.stoc_send_chat_to_room(client.room, "你的生命已经如风中残烛了!")
...@@ -350,29 +367,20 @@ ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)-> ...@@ -350,29 +367,20 @@ ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)->
val = buffer.readInt32LE(2) val = buffer.readInt32LE(2)
client.room.dueling_players[pos].lp += val client.room.dueling_players[pos].lp += val
#ygopro.stoc_send_chat_to_room(client.room, "LP跟踪调试信息: #{client.room.dueling_players[pos].name} 回复 #{val},现在的LP为 #{client.room.dueling_players[pos].lp}")
if ygopro.constants.MSG[msg] == 'LPUPDATE' and client.is_host if ygopro.constants.MSG[msg] == 'LPUPDATE' and client.is_host
pos = buffer.readUInt8(1) pos = buffer.readUInt8(1)
pos = 1 - pos unless client.is_first pos = 1 - pos unless client.is_first
val = buffer.readInt32LE(2) val = buffer.readInt32LE(2)
client.room.dueling_players[pos].lp = val client.room.dueling_players[pos].lp = val
#ygopro.stoc_send_chat_to_room(client.room, "LP跟踪调试信息: #{client.room.dueling_players[pos].name} 的LP变成 #{client.room.dueling_players[pos].lp}")
if ygopro.constants.MSG[msg] == 'PAY_LPCOST' and client.is_host if ygopro.constants.MSG[msg] == 'PAY_LPCOST' and client.is_host
pos = buffer.readUInt8(1) pos = buffer.readUInt8(1)
pos = 1 - pos unless client.is_first pos = 1 - pos unless client.is_first
val = buffer.readInt32LE(2) val = buffer.readInt32LE(2)
client.room.dueling_players[pos].lp -= val client.room.dueling_players[pos].lp -= val
#ygopro.stoc_send_chat_to_room(client.room, "LP跟踪调试信息: #{client.room.dueling_players[pos].name} 支付 #{val},现在的LP为 #{client.room.dueling_players[pos].lp}")
if 0 < client.room.dueling_players[pos].lp <= 100 if 0 < client.room.dueling_players[pos].lp <= 100
ygopro.stoc_send_chat_to_room(client.room, "背水一战!") ygopro.stoc_send_chat_to_room(client.room, "背水一战!")
#登场台词 #登场台词
if settings.modules.dialogues if settings.modules.dialogues
if ygopro.constants.MSG[msg] == 'SUMMONING' or ygopro.constants.MSG[msg] == 'SPSUMMONING' if ygopro.constants.MSG[msg] == 'SUMMONING' or ygopro.constants.MSG[msg] == 'SPSUMMONING'
...@@ -383,8 +391,6 @@ ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)-> ...@@ -383,8 +391,6 @@ ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)->
return return
#房间管理 #房间管理
ygopro.stoc_follow 'TYPE_CHANGE', false, (buffer, info, client, server)-> ygopro.stoc_follow 'TYPE_CHANGE', false, (buffer, info, client, server)->
selftype = info.type & 0xf; selftype = info.type & 0xf;
is_host = ((info.type >> 4) & 0xf) != 0; is_host = ((info.type >> 4) & 0xf) != 0;
...@@ -409,9 +415,10 @@ if settings.modules.tips ...@@ -409,9 +415,10 @@ if settings.modules.tips
return return
ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)-> ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)->
return unless client.room
unless client.room.started #first start unless client.room.started #first start
client.room.started = true client.room.started = true
client.room.duels = [] #client.room.duels = []
client.room.dueling_players = [] client.room.dueling_players = []
for player in client.room.players when player.pos != 7 for player in client.room.players when player.pos != 7
client.room.dueling_players[player.pos] = player client.room.dueling_players[player.pos] = player
...@@ -419,7 +426,8 @@ ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)-> ...@@ -419,7 +426,8 @@ ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)->
ygopro.stoc_send_random_tip(client) ygopro.stoc_send_random_tip(client)
return return
ygopro.ctos_follow 'CHAT', false, (buffer, info, client, server)-> ygopro.ctos_follow 'CHAT', true, (buffer, info, client, server)->
cancel = _.startsWith(_.trim(info.msg),"/")
switch _.trim(info.msg) switch _.trim(info.msg)
when '/ping' when '/ping'
execFile 'ss', ['-it', "dst #{client.remoteAddress}:#{client.remotePort}"], (error, stdout, stderr)-> execFile 'ss', ['-it', "dst #{client.remoteAddress}:#{client.remotePort}"], (error, stdout, stderr)->
...@@ -433,15 +441,15 @@ ygopro.ctos_follow 'CHAT', false, (buffer, info, client, server)-> ...@@ -433,15 +441,15 @@ ygopro.ctos_follow 'CHAT', false, (buffer, info, client, server)->
#log.warn 'ping', stdout #log.warn 'ping', stdout
ygopro.stoc_send_chat_to_room client.room, stdout ygopro.stoc_send_chat_to_room client.room, stdout
return return
when '/help' when '/help'
ygopro.stoc_send_chat(client,"YGOSrv233 指令帮助") ygopro.stoc_send_chat(client,"YGOSrv233 指令帮助")
ygopro.stoc_send_chat(client,"/help 显示这个帮助信息") ygopro.stoc_send_chat(client,"/help 显示这个帮助信息")
ygopro.stoc_send_chat(client,"/tip 显示一条提示") if settings.modules.tips ygopro.stoc_send_chat(client,"/tip 显示一条提示") if settings.modules.tips
#ygopro.stoc_send_chat(client,"/senddeck 发送自己的卡组")
when '/tip' when '/tip'
ygopro.stoc_send_random_tip(client) if settings.modules.tips ygopro.stoc_send_random_tip(client) if settings.modules.tips
return return cancel
ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)-> ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)->
#log.info info #log.info info
...@@ -451,22 +459,9 @@ ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)-> ...@@ -451,22 +459,9 @@ ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)->
client.side = side client.side = side
return return
###
if settings.modules.skip_empty_side
ygopro.stoc_follow 'CHANGE_SIDE', false, (buffer, info, client, server)->
if client.side
ygopro.ctos_send server, 'UPDATE_DECK', {
mainc: client.main.length,
sidec: 0,
deckbuf: client.main
}
ygopro.stoc_send_chat client, '等待更换副卡组中...'
###
#http #http
if settings.modules.http if settings.modules.http
http_server = http.createServer (request, response)-> http_server = http.createServer (request, response)->
#http://122.0.65.70:7922/?operation=getroomjson
u = url.parse(request.url,1) u = url.parse(request.url,1)
#log.info u #log.info u
if u.pathname == '/count.json' if u.pathname == '/count.json'
...@@ -506,8 +501,18 @@ if settings.modules.http ...@@ -506,8 +501,18 @@ if settings.modules.http
else if u.query.pass == settings.modules.http.password && u.query.shout else if u.query.pass == settings.modules.http.password && u.query.shout
for room in Room.all for room in Room.all
ygopro.stoc_send_chat_to_room(room, u.query.shout) ygopro.stoc_send_chat_to_room(room, u.query.shout)
response.writeHead(200); response.writeHead(200)
response.end("shout " + u.query.shout + " ok"); response.end("shout " + u.query.shout + " ok")
else if u.query.pass == settings.modules.http.password && u.query.stop
settings.modules.stop = u.query.stop
response.writeHead(200)
response.end("stop " + u.query.shout + " ok")
else if u.query.pass == settings.modules.http.password && u.query.welcome
settings.modules.welcome = u.query.welcome
response.writeHead(200)
response.end("welcome " + u.query.shout + " ok")
else else
response.writeHead(404); response.writeHead(404);
......
// Generated by CoffeeScript 1.9.3 // Generated by CoffeeScript 1.9.3
(function() { (function() {
var Room, _, bunyan, debug, dialogues, execFile, fs, http, http_server, log, net, os, path, request, settings, tips, url, ygopro; var Graveyard, Room, _, bunyan, debug, dialogues, execFile, fs, http, http_server, log, net, os, path, request, settings, tips, tribute, url, ygopro;
net = require('net'); net = require('net');
...@@ -50,22 +50,30 @@ ...@@ -50,22 +50,30 @@
}); });
} }
Graveyard = [];
/* tribute = function(socket) {
#定时清理关闭的连接 setTimeout((function(socket) {
Graveyard = [] Graveyard.push(socket);
})(socket), 3000);
send_to_graveyard = (socket) -> };
unless _.indexOf(Graveyard, socket)
Graveyard.push(socket) setInterval(function() {
var fuck, i, j, k, l, len, len1, ref, you;
tribute = (socket) -> for (i = k = 0, len = Graveyard.length; k < len; i = ++k) {
setTimeout send_to_graveyard(socket), 30000 fuck = Graveyard[i];
if (Graveyard[i]) {
setInterval ()-> Graveyard[i].destroy();
log.info Graveyard }
, 30000 ref = Graveyard[i];
*/ for (j = l = 0, len1 = ref.length; l < len1; j = ++l) {
you = ref[j];
Graveyard[i][j] = null;
}
Graveyard[i] = null;
}
Graveyard = [];
}, 3000);
net.createServer(function(client) { net.createServer(function(client) {
var ctos_buffer, ctos_message_length, ctos_proto, server, stoc_buffer, stoc_message_length, stoc_proto; var ctos_buffer, ctos_message_length, ctos_proto, server, stoc_buffer, stoc_message_length, stoc_proto;
...@@ -73,6 +81,7 @@ ...@@ -73,6 +81,7 @@
client.server = server; client.server = server;
client.setTimeout(300000); client.setTimeout(300000);
client.on('close', function(had_error) { client.on('close', function(had_error) {
tribute(client);
if (!client.closed) { if (!client.closed) {
client.closed = true; client.closed = true;
if (client.room) { if (client.room) {
...@@ -82,6 +91,7 @@ ...@@ -82,6 +91,7 @@
server.end(); server.end();
}); });
client.on('error', function(error) { client.on('error', function(error) {
tribute(client);
if (!client.closed) { if (!client.closed) {
client.closed = error; client.closed = error;
if (client.room) { if (client.room) {
...@@ -94,6 +104,7 @@ ...@@ -94,6 +104,7 @@
server.end(); server.end();
}); });
server.on('close', function(had_error) { server.on('close', function(had_error) {
tribute(server);
if (!server.closed) { if (!server.closed) {
server.closed = true; server.closed = true;
} }
...@@ -103,6 +114,7 @@ ...@@ -103,6 +114,7 @@
} }
}); });
server.on('error', function(error) { server.on('error', function(error) {
tribute(server);
server.closed = error; server.closed = error;
if (!client.closed) { if (!client.closed) {
ygopro.stoc_send_chat(client, "服务器错误: " + error); ygopro.stoc_send_chat(client, "服务器错误: " + error);
...@@ -114,16 +126,12 @@ ...@@ -114,16 +126,12 @@
ctos_proto = 0; ctos_proto = 0;
client.pre_establish_buffers = new Array(); client.pre_establish_buffers = new Array();
client.on('data', function(data) { client.on('data', function(data) {
var b, struct; var b, buffer, cancel, datas, k, l, len, len1, struct;
if (client.is_post_watcher) { if (client.is_post_watcher) {
client.room.watcher.write(data); client.room.watcher.write(data);
} else { } else {
ctos_buffer = Buffer.concat([ctos_buffer, data], ctos_buffer.length + data.length); ctos_buffer = Buffer.concat([ctos_buffer, data], ctos_buffer.length + data.length);
if (client.established) { datas = [];
server.write(data);
} else {
client.pre_establish_buffers.push(data);
}
while (true) { while (true) {
if (ctos_message_length === 0) { if (ctos_message_length === 0) {
if (ctos_buffer.length >= 2) { if (ctos_buffer.length >= 2) {
...@@ -139,15 +147,23 @@ ...@@ -139,15 +147,23 @@
} }
} else { } else {
if (ctos_buffer.length >= 2 + ctos_message_length) { if (ctos_buffer.length >= 2 + ctos_message_length) {
cancel = false;
if (ygopro.ctos_follows[ctos_proto]) { if (ygopro.ctos_follows[ctos_proto]) {
b = ctos_buffer.slice(3, ctos_message_length - 1 + 3); b = ctos_buffer.slice(3, ctos_message_length - 1 + 3);
if (struct = ygopro.structs[ygopro.proto_structs.CTOS[ygopro.constants.CTOS[ctos_proto]]]) { if (struct = ygopro.structs[ygopro.proto_structs.CTOS[ygopro.constants.CTOS[ctos_proto]]]) {
struct._setBuff(b); struct._setBuff(b);
ygopro.ctos_follows[ctos_proto].callback(b, _.clone(struct.fields), client, server); if (ygopro.ctos_follows[ctos_proto].synchronous) {
cancel = ygopro.ctos_follows[ctos_proto].callback(b, _.clone(struct.fields), client, server);
} else {
ygopro.ctos_follows[ctos_proto].callback(b, _.clone(struct.fields), client, server);
}
} else { } else {
ygopro.ctos_follows[ctos_proto].callback(b, null, client, server); ygopro.ctos_follows[ctos_proto].callback(b, null, client, server);
} }
} }
if (!cancel) {
datas.push(ctos_buffer.slice(0, 2 + ctos_message_length));
}
ctos_buffer = ctos_buffer.slice(2 + ctos_message_length); ctos_buffer = ctos_buffer.slice(2 + ctos_message_length);
ctos_message_length = 0; ctos_message_length = 0;
ctos_proto = 0; ctos_proto = 0;
...@@ -156,6 +172,17 @@ ...@@ -156,6 +172,17 @@
} }
} }
} }
if (client.established) {
for (k = 0, len = datas.length; k < len; k++) {
buffer = datas[k];
server.write(buffer);
}
} else {
for (l = 0, len1 = datas.length; l < len1; l++) {
buffer = datas[l];
client.pre_establish_buffers.push(buffer);
}
}
} }
}); });
stoc_buffer = new Buffer(0); stoc_buffer = new Buffer(0);
...@@ -204,11 +231,18 @@ ...@@ -204,11 +231,18 @@
}); });
ygopro.ctos_follow('PLAYER_INFO', true, function(buffer, info, client, server) { ygopro.ctos_follow('PLAYER_INFO', true, function(buffer, info, client, server) {
client.name = info.name; var name, struct;
name = info.name.split("$")[0];
struct = ygopro.structs["CTOS_PlayerInfo"];
struct._setBuff(buffer);
struct.set("name", name);
buffer = struct.buffer;
client.name = name;
return false;
}); });
ygopro.ctos_follow('JOIN_GAME', false, function(buffer, info, client, server) { ygopro.ctos_follow('JOIN_GAME', false, function(buffer, info, client, server) {
var j, len, ref; var k, len, ref;
if (settings.modules.stop) { if (settings.modules.stop) {
ygopro.stoc_send_chat(client, settings.modules.stop); ygopro.stoc_send_chat(client, settings.modules.stop);
ygopro.stoc_send(client, 'ERROR_MSG', { ygopro.stoc_send(client, 'ERROR_MSG', {
...@@ -257,8 +291,8 @@ ...@@ -257,8 +291,8 @@
ygopro.stoc_send_chat_to_room(client.room, client.name + " 加入了观战"); ygopro.stoc_send_chat_to_room(client.room, client.name + " 加入了观战");
client.room.watchers.push(client); client.room.watchers.push(client);
ref = client.room.watcher_buffers; ref = client.room.watcher_buffers;
for (j = 0, len = ref.length; j < len; j++) { for (k = 0, len = ref.length; k < len; k++) {
buffer = ref[j]; buffer = ref[k];
client.write(buffer); client.write(buffer);
} }
ygopro.stoc_send_chat(client, "观战中."); ygopro.stoc_send_chat(client, "观战中.");
...@@ -278,6 +312,9 @@ ...@@ -278,6 +312,9 @@
ygopro.stoc_follow('JOIN_GAME', false, function(buffer, info, client, server) { ygopro.stoc_follow('JOIN_GAME', false, function(buffer, info, client, server) {
var watcher; var watcher;
if (!client.room) {
return;
}
if (settings.modules.welcome) { if (settings.modules.welcome) {
ygopro.stoc_send_chat(client, settings.modules.welcome); ygopro.stoc_send_chat(client, settings.modules.welcome);
} }
...@@ -298,11 +335,14 @@ ...@@ -298,11 +335,14 @@
ygopro.ctos_send(watcher, 'HS_TOOBSERVER'); ygopro.ctos_send(watcher, 'HS_TOOBSERVER');
}); });
watcher.on('data', function(data) { watcher.on('data', function(data) {
var j, len, ref, w; var k, len, ref, w;
if (!client.room) {
return;
}
client.room.watcher_buffers.push(data); client.room.watcher_buffers.push(data);
ref = client.room.watchers; ref = client.room.watchers;
for (j = 0, len = ref.length; j < len; j++) { for (k = 0, len = ref.length; k < len; k++) {
w = ref[j]; w = ref[k];
if (w) { if (w) {
w.write(data); w.write(data);
} }
...@@ -329,24 +369,22 @@ ...@@ -329,24 +369,22 @@
} }
ygopro.stoc_follow('GAME_MSG', false, function(buffer, info, client, server) { ygopro.stoc_follow('GAME_MSG', false, function(buffer, info, client, server) {
var card, j, len, line, msg, playertype, pos, reason, ref, ref1, ref2, val; var card, k, len, line, msg, playertype, pos, ref, ref1, ref2, val;
msg = buffer.readInt8(0); msg = buffer.readInt8(0);
if (ygopro.constants.MSG[msg] === 'START') { if (ygopro.constants.MSG[msg] === 'START') {
playertype = buffer.readUInt8(1); playertype = buffer.readUInt8(1);
client.is_first = !(playertype & 0xf); client.is_first = !(playertype & 0xf);
client.lp = client.room.hostinfo.start_lp; client.lp = client.room.hostinfo.start_lp;
} }
if (ygopro.constants.MSG[msg] === 'WIN' && _.startsWith(client.room.name, 'M#') && client.is_host) {
pos = buffer.readUInt8(1); /*
if (!(client.is_first || pos === 2)) { if ygopro.constants.MSG[msg] == 'WIN' and _.startsWith(client.room.name, 'M#') and client.is_host
pos = 1 - pos; pos = buffer.readUInt8(1)
} pos = 1 - pos unless client.is_first or pos == 2
reason = buffer.readUInt8(2); reason = buffer.readUInt8(2)
client.room.duels.push({ #log.info {winner: pos, reason: reason}
winner: pos, client.room.duels.push {winner: pos, reason: reason}
reason: reason */
});
}
if (ygopro.constants.MSG[msg] === 'DAMAGE' && client.is_host) { if (ygopro.constants.MSG[msg] === 'DAMAGE' && client.is_host) {
pos = buffer.readUInt8(1); pos = buffer.readUInt8(1);
if (!client.is_first) { if (!client.is_first) {
...@@ -390,8 +428,8 @@ ...@@ -390,8 +428,8 @@
card = buffer.readUInt32LE(1); card = buffer.readUInt32LE(1);
if (dialogues[card]) { if (dialogues[card]) {
ref2 = _.lines(dialogues[card][Math.floor(Math.random() * dialogues[card].length)]); ref2 = _.lines(dialogues[card][Math.floor(Math.random() * dialogues[card].length)]);
for (j = 0, len = ref2.length; j < len; j++) { for (k = 0, len = ref2.length; k < len; k++) {
line = ref2[j]; line = ref2[k];
ygopro.stoc_send_chat(client, line); ygopro.stoc_send_chat(client, line);
} }
} }
...@@ -425,14 +463,16 @@ ...@@ -425,14 +463,16 @@
} }
ygopro.stoc_follow('DUEL_START', false, function(buffer, info, client, server) { ygopro.stoc_follow('DUEL_START', false, function(buffer, info, client, server) {
var j, len, player, ref; var k, len, player, ref;
if (!client.room) {
return;
}
if (!client.room.started) { if (!client.room.started) {
client.room.started = true; client.room.started = true;
client.room.duels = [];
client.room.dueling_players = []; client.room.dueling_players = [];
ref = client.room.players; ref = client.room.players;
for (j = 0, len = ref.length; j < len; j++) { for (k = 0, len = ref.length; k < len; k++) {
player = ref[j]; player = ref[k];
if (player.pos !== 7) { if (player.pos !== 7) {
client.room.dueling_players[player.pos] = player; client.room.dueling_players[player.pos] = player;
} }
...@@ -443,7 +483,9 @@ ...@@ -443,7 +483,9 @@
} }
}); });
ygopro.ctos_follow('CHAT', false, function(buffer, info, client, server) { ygopro.ctos_follow('CHAT', true, function(buffer, info, client, server) {
var cancel;
cancel = _.startsWith(_.trim(info.msg), "/");
switch (_.trim(info.msg)) { switch (_.trim(info.msg)) {
case '/ping': case '/ping':
execFile('ss', ['-it', "dst " + client.remoteAddress + ":" + client.remotePort], function(error, stdout, stderr) { execFile('ss', ['-it', "dst " + client.remoteAddress + ":" + client.remotePort], function(error, stdout, stderr) {
...@@ -472,22 +514,23 @@ ...@@ -472,22 +514,23 @@
ygopro.stoc_send_random_tip(client); ygopro.stoc_send_random_tip(client);
} }
} }
return cancel;
}); });
ygopro.ctos_follow('UPDATE_DECK', false, function(buffer, info, client, server) { ygopro.ctos_follow('UPDATE_DECK', false, function(buffer, info, client, server) {
var i, main, side; var i, main, side;
main = (function() { main = (function() {
var j, ref, results; var k, ref, results;
results = []; results = [];
for (i = j = 0, ref = info.mainc; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) { for (i = k = 0, ref = info.mainc; 0 <= ref ? k < ref : k > ref; i = 0 <= ref ? ++k : --k) {
results.push(info.deckbuf[i]); results.push(info.deckbuf[i]);
} }
return results; return results;
})(); })();
side = (function() { side = (function() {
var j, ref, ref1, results; var k, ref, ref1, results;
results = []; results = [];
for (i = j = ref = info.mainc, ref1 = info.mainc + info.sidec; ref <= ref1 ? j < ref1 : j > ref1; i = ref <= ref1 ? ++j : --j) { for (i = k = ref = info.mainc, ref1 = info.mainc + info.sidec; ref <= ref1 ? k < ref1 : k > ref1; i = ref <= ref1 ? ++k : --k) {
results.push(info.deckbuf[i]); results.push(info.deckbuf[i]);
} }
return results; return results;
...@@ -496,22 +539,9 @@ ...@@ -496,22 +539,9 @@
client.side = side; client.side = side;
}); });
/*
if settings.modules.skip_empty_side
ygopro.stoc_follow 'CHANGE_SIDE', false, (buffer, info, client, server)->
if client.side
ygopro.ctos_send server, 'UPDATE_DECK', {
mainc: client.main.length,
sidec: 0,
deckbuf: client.main
}
ygopro.stoc_send_chat client, '等待更换副卡组中...'
*/
if (settings.modules.http) { if (settings.modules.http) {
http_server = http.createServer(function(request, response) { http_server = http.createServer(function(request, response) {
var j, len, player, ref, room, roomsjson, u; var k, len, player, ref, room, roomsjson, u;
u = url.parse(request.url, 1); u = url.parse(request.url, 1);
if (u.pathname === '/count.json') { if (u.pathname === '/count.json') {
response.writeHead(200); response.writeHead(200);
...@@ -520,22 +550,22 @@ ...@@ -520,22 +550,22 @@
response.writeHead(200); response.writeHead(200);
roomsjson = JSON.stringify({ roomsjson = JSON.stringify({
rooms: (function() { rooms: (function() {
var j, len, ref, results; var k, len, ref, results;
ref = Room.all; ref = Room.all;
results = []; results = [];
for (j = 0, len = ref.length; j < len; j++) { for (k = 0, len = ref.length; k < len; k++) {
room = ref[j]; room = ref[k];
if (room.established) { if (room.established) {
results.push({ results.push({
roomid: room.port.toString(), roomid: room.port.toString(),
roomname: room.name.split('$', 2)[0], roomname: room.name.split('$', 2)[0],
needpass: (room.name.indexOf('$') !== -1).toString(), needpass: (room.name.indexOf('$') !== -1).toString(),
users: (function() { users: (function() {
var k, len1, ref1, results1; var l, len1, ref1, results1;
ref1 = room.players; ref1 = room.players;
results1 = []; results1 = [];
for (k = 0, len1 = ref1.length; k < len1; k++) { for (l = 0, len1 = ref1.length; l < len1; l++) {
player = ref1[k]; player = ref1[l];
if (player.pos != null) { if (player.pos != null) {
results1.push({ results1.push({
id: (-1).toString(), id: (-1).toString(),
...@@ -558,22 +588,22 @@ ...@@ -558,22 +588,22 @@
response.writeHead(200); response.writeHead(200);
response.end(JSON.stringify({ response.end(JSON.stringify({
rooms: (function() { rooms: (function() {
var j, len, ref, results; var k, len, ref, results;
ref = Room.all; ref = Room.all;
results = []; results = [];
for (j = 0, len = ref.length; j < len; j++) { for (k = 0, len = ref.length; k < len; k++) {
room = ref[j]; room = ref[k];
if (room.established) { if (room.established) {
results.push({ results.push({
roomid: room.port.toString(), roomid: room.port.toString(),
roomname: room.name.split('$', 2)[0], roomname: room.name.split('$', 2)[0],
needpass: (room.name.indexOf('$') !== -1).toString(), needpass: (room.name.indexOf('$') !== -1).toString(),
users: (function() { users: (function() {
var k, len1, ref1, results1; var l, len1, ref1, results1;
ref1 = room.players; ref1 = room.players;
results1 = []; results1 = [];
for (k = 0, len1 = ref1.length; k < len1; k++) { for (l = 0, len1 = ref1.length; l < len1; l++) {
player = ref1[k]; player = ref1[l];
if (player.pos != null) { if (player.pos != null) {
results1.push({ results1.push({
id: (-1).toString(), id: (-1).toString(),
...@@ -593,12 +623,20 @@ ...@@ -593,12 +623,20 @@
})); }));
} else if (u.query.pass === settings.modules.http.password && u.query.shout) { } else if (u.query.pass === settings.modules.http.password && u.query.shout) {
ref = Room.all; ref = Room.all;
for (j = 0, len = ref.length; j < len; j++) { for (k = 0, len = ref.length; k < len; k++) {
room = ref[j]; room = ref[k];
ygopro.stoc_send_chat_to_room(room, u.query.shout); ygopro.stoc_send_chat_to_room(room, u.query.shout);
} }
response.writeHead(200); response.writeHead(200);
response.end("shout " + u.query.shout + " ok"); response.end("shout " + u.query.shout + " ok");
} else if (u.query.pass === settings.modules.http.password && u.query.stop) {
settings.modules.stop = u.query.stop;
response.writeHead(200);
response.end("stop " + u.query.shout + " ok");
} else if (u.query.pass === settings.modules.http.password && u.query.welcome) {
settings.modules.welcome = u.query.welcome;
response.writeHead(200);
response.end("welcome " + u.query.shout + " ok");
} else { } else {
response.writeHead(404); response.writeHead(404);
response.end(); response.end();
......
...@@ -110,6 +110,9 @@ for name, declaration of structs_declaration ...@@ -110,6 +110,9 @@ for name, declaration of structs_declaration
#util #util
@stoc_send_chat = (client, msg, player = 8)-> @stoc_send_chat = (client, msg, player = 8)->
if !client
console.log "err stoc_send_chat"
return
for line in _.lines(msg) for line in _.lines(msg)
@stoc_send client, 'CHAT', { @stoc_send client, 'CHAT', {
player: player player: player
...@@ -118,6 +121,9 @@ for name, declaration of structs_declaration ...@@ -118,6 +121,9 @@ for name, declaration of structs_declaration
return return
@stoc_send_chat_to_room = (room, msg, player = 8)-> @stoc_send_chat_to_room = (room, msg, player = 8)->
if !room
console.log "err stoc_send_chat_to_room"
return
for client in room.players for client in room.players
@stoc_send_chat(client, msg, player) if client @stoc_send_chat(client, msg, player) if client
for client in room.watchers for client in room.watchers
......
...@@ -171,6 +171,10 @@ ...@@ -171,6 +171,10 @@
if (player == null) { if (player == null) {
player = 8; player = 8;
} }
if (!client) {
console.log("err stoc_send_chat");
return;
}
ref = _.lines(msg); ref = _.lines(msg);
for (j = 0, len1 = ref.length; j < len1; j++) { for (j = 0, len1 = ref.length; j < len1; j++) {
line = ref[j]; line = ref[j];
...@@ -186,6 +190,10 @@ ...@@ -186,6 +190,10 @@
if (player == null) { if (player == null) {
player = 8; player = 8;
} }
if (!room) {
console.log("err stoc_send_chat_to_room");
return;
}
ref = room.players; ref = room.players;
for (j = 0, len1 = ref.length; j < len1; j++) { for (j = 0, len1 = ref.length; j < len1; j++) {
client = ref[j]; client = ref[j];
......
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