Commit 1275e258 authored by mercury233's avatar mercury233

test3

parent a481f165
...@@ -85,6 +85,7 @@ class Room ...@@ -85,6 +85,7 @@ class Room
#log.info 'room-exit', this.name, this.port, code #log.info 'room-exit', this.name, this.port, code
@disconnector = 'server' unless @disconnector @disconnector = 'server' unless @disconnector
this.delete() this.delete()
return
@process.stdout.setEncoding('utf8') @process.stdout.setEncoding('utf8')
@process.stdout.once 'data', (data)=> @process.stdout.once 'data', (data)=>
@established = true @established = true
...@@ -94,7 +95,9 @@ class Room ...@@ -94,7 +95,9 @@ class Room
player.server.write buffer for buffer in player.pre_establish_buffers player.server.write buffer for buffer in player.pre_establish_buffers
player.established = true player.established = true
player.pre_establish_buffers = null player.pre_establish_buffers = null
return
return
return
delete: -> delete: ->
#积分 #积分
return if @deleted return if @deleted
...@@ -104,6 +107,7 @@ class Room ...@@ -104,6 +107,7 @@ class Room
#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 @deleted = true
return
connect: (client)-> connect: (client)->
@players.push client @players.push client
...@@ -113,6 +117,8 @@ class Room ...@@ -113,6 +117,8 @@ class Room
client.server.write buffer for buffer in client.pre_establish_buffers client.server.write buffer for buffer in client.pre_establish_buffers
client.established = true client.established = true
client.pre_establish_buffers = [] client.pre_establish_buffers = []
return
return
disconnect: (client, error)-> disconnect: (client, error)->
if client.is_post_watcher if client.is_post_watcher
...@@ -127,5 +133,6 @@ class Room ...@@ -127,5 +133,6 @@ class Room
else else
@process.kill() @process.kill()
this.delete() this.delete()
return
module.exports = Room module.exports = Room
\ No newline at end of file
...@@ -102,7 +102,7 @@ ...@@ -102,7 +102,7 @@
if (!_this.disconnector) { if (!_this.disconnector) {
_this.disconnector = 'server'; _this.disconnector = 'server';
} }
return _this["delete"](); _this["delete"]();
}; };
})(this)); })(this));
this.process.stdout.setEncoding('utf8'); this.process.stdout.setEncoding('utf8');
...@@ -110,8 +110,8 @@ ...@@ -110,8 +110,8 @@
return function(data) { return function(data) {
_this.established = true; _this.established = true;
_this.port = parseInt(data); _this.port = parseInt(data);
return _.each(_this.players, function(player) { _.each(_this.players, function(player) {
return player.server.connect(_this.port, '127.0.0.1', function() { player.server.connect(_this.port, '127.0.0.1', function() {
var buffer, i, len, ref; var buffer, i, len, ref;
ref = player.pre_establish_buffers; ref = player.pre_establish_buffers;
for (i = 0, len = ref.length; i < len; i++) { for (i = 0, len = ref.length; i < len; i++) {
...@@ -119,7 +119,7 @@ ...@@ -119,7 +119,7 @@
player.server.write(buffer); player.server.write(buffer);
} }
player.established = true; player.established = true;
return player.pre_establish_buffers = null; player.pre_establish_buffers = null;
}); });
}); });
}; };
...@@ -136,13 +136,13 @@ ...@@ -136,13 +136,13 @@
if (index !== -1) { if (index !== -1) {
Room.all.splice(index, 1); Room.all.splice(index, 1);
} }
return this.deleted = true; this.deleted = true;
}; };
Room.prototype.connect = function(client) { Room.prototype.connect = function(client) {
this.players.push(client); this.players.push(client);
if (this.established) { if (this.established) {
return client.server.connect(this.port, '127.0.0.1', function() { client.server.connect(this.port, '127.0.0.1', function() {
var buffer, i, len, ref; var buffer, i, len, ref;
ref = client.pre_establish_buffers; ref = client.pre_establish_buffers;
for (i = 0, len = ref.length; i < len; i++) { for (i = 0, len = ref.length; i < len; i++) {
...@@ -150,7 +150,7 @@ ...@@ -150,7 +150,7 @@
client.server.write(buffer); client.server.write(buffer);
} }
client.established = true; client.established = true;
return client.pre_establish_buffers = []; client.pre_establish_buffers = [];
}); });
} }
}; };
...@@ -161,7 +161,7 @@ ...@@ -161,7 +161,7 @@
ygopro.stoc_send_chat_to_room(this, client.name + " " + '退出了观战' + (error ? ": " + error : '')); ygopro.stoc_send_chat_to_room(this, client.name + " " + '退出了观战' + (error ? ": " + error : ''));
index = _.indexOf(this.watchers, client); index = _.indexOf(this.watchers, client);
if (index !== -1) { if (index !== -1) {
return this.watchers.splice(index, 1); this.watchers.splice(index, 1);
} }
} else { } else {
index = _.indexOf(this.players, client); index = _.indexOf(this.players, client);
...@@ -169,10 +169,10 @@ ...@@ -169,10 +169,10 @@
this.players.splice(index, 1); this.players.splice(index, 1);
} }
if (this.players.length) { if (this.players.length) {
return ygopro.stoc_send_chat_to_room(this, client.name + " " + '离开了游戏' + (error ? ": " + error : '')); ygopro.stoc_send_chat_to_room(this, client.name + " " + '离开了游戏' + (error ? ": " + error : ''));
} else { } else {
this.process.kill(); this.process.kill();
return this["delete"](); this["delete"]();
} }
} }
}; };
......
...@@ -65,6 +65,7 @@ net.createServer (client) -> ...@@ -65,6 +65,7 @@ net.createServer (client) ->
client.closed = true client.closed = true
client.room.disconnect(client) if client.room client.room.disconnect(client) if client.room
server.end() server.end()
return
client.on 'error', (error)-> client.on 'error', (error)->
#log.info "client error", client.name, error #log.info "client error", client.name, error
...@@ -73,9 +74,11 @@ net.createServer (client) -> ...@@ -73,9 +74,11 @@ net.createServer (client) ->
client.closed = error client.closed = error
client.room.disconnect(client, error) if client.room client.room.disconnect(client, error) if client.room
server.end() server.end()
return
client.on 'timeout', ()-> client.on 'timeout', ()->
server.end() server.end()
return
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
...@@ -84,6 +87,7 @@ net.createServer (client) -> ...@@ -84,6 +87,7 @@ net.createServer (client) ->
unless client.closed unless client.closed
ygopro.stoc_send_chat(client, "服务器关闭了连接") ygopro.stoc_send_chat(client, "服务器关闭了连接")
client.end() client.end()
return
server.on 'error', (error)-> server.on 'error', (error)->
#log.info "server error", client.name, error #log.info "server error", client.name, error
...@@ -92,6 +96,7 @@ net.createServer (client) -> ...@@ -92,6 +96,7 @@ net.createServer (client) ->
unless client.closed unless client.closed
ygopro.stoc_send_chat(client, "服务器错误: #{error}") ygopro.stoc_send_chat(client, "服务器错误: #{error}")
client.end() client.end()
return
#需要重构 #需要重构
#客户端到服务端(ctos)协议分析 #客户端到服务端(ctos)协议分析
...@@ -139,6 +144,7 @@ net.createServer (client) -> ...@@ -139,6 +144,7 @@ net.createServer (client) ->
ctos_proto = 0 ctos_proto = 0
else else
break break
return
#服务端到客户端(stoc) #服务端到客户端(stoc)
stoc_buffer = new Buffer(0) stoc_buffer = new Buffer(0)
...@@ -179,14 +185,17 @@ net.createServer (client) -> ...@@ -179,14 +185,17 @@ net.createServer (client) ->
stoc_proto = 0 stoc_proto = 0
else else
break break
return 0 return
return
.listen settings.port, -> .listen settings.port, ->
log.info "server started", settings.ip, settings.port log.info "server started", settings.ip, settings.port
return
#功能模块 #功能模块
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之前暂存 client.name = info.name #在创建room之前暂存
return
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
...@@ -259,6 +268,7 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)-> ...@@ -259,6 +268,7 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
client.end() client.end()
else else
client.room.connect(client) client.room.connect(client)
return
ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)-> ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)->
#欢迎信息 #欢迎信息
...@@ -279,15 +289,18 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)-> ...@@ -279,15 +289,18 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)->
pass: "" pass: ""
} }
ygopro.ctos_send watcher, 'HS_TOOBSERVER' ygopro.ctos_send watcher, 'HS_TOOBSERVER'
return
watcher.on 'data', (data)-> watcher.on 'data', (data)->
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
return
watcher.on 'error', (error)-> watcher.on 'error', (error)->
#log.error "watcher error", error #log.error "watcher error", error
return
return
#登场台词 #登场台词
if settings.modules.dialogues if settings.modules.dialogues
...@@ -303,6 +316,7 @@ if settings.modules.dialogues ...@@ -303,6 +316,7 @@ if settings.modules.dialogues
else else
#log.info "dialogues loaded", _.size body #log.info "dialogues loaded", _.size body
dialogues = body dialogues = body
return
ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)-> ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)->
msg = buffer.readInt8(0) msg = buffer.readInt8(0)
...@@ -366,7 +380,7 @@ ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)-> ...@@ -366,7 +380,7 @@ ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)->
if dialogues[card] if dialogues[card]
for line in _.lines dialogues[card][Math.floor(Math.random() * dialogues[card].length)] for line in _.lines dialogues[card][Math.floor(Math.random() * dialogues[card].length)]
ygopro.stoc_send_chat client, line ygopro.stoc_send_chat client, line
return
#房间管理 #房间管理
...@@ -377,10 +391,12 @@ ygopro.stoc_follow 'TYPE_CHANGE', false, (buffer, info, client, server)-> ...@@ -377,10 +391,12 @@ ygopro.stoc_follow 'TYPE_CHANGE', false, (buffer, info, client, server)->
client.is_host = is_host client.is_host = is_host
client.pos = selftype client.pos = selftype
#console.log "TYPE_CHANGE to #{client.name}:", info, selftype, is_host #console.log "TYPE_CHANGE to #{client.name}:", info, selftype, is_host
return
#tip #tip
ygopro.stoc_send_random_tip = (client)-> ygopro.stoc_send_random_tip = (client)->
ygopro.stoc_send_chat client, "Tip: " + tips[Math.floor(Math.random() * tips.length)] if tips ygopro.stoc_send_chat client, "Tip: " + tips[Math.floor(Math.random() * tips.length)] if tips
return
tips = null tips = null
if settings.modules.tips if settings.modules.tips
...@@ -390,6 +406,7 @@ if settings.modules.tips ...@@ -390,6 +406,7 @@ if settings.modules.tips
, (error, response, body)-> , (error, response, body)->
tips = body tips = body
#log.info "tips loaded", tips.length #log.info "tips loaded", tips.length
return
ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)-> ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)->
unless client.room.started #first start unless client.room.started #first start
...@@ -400,6 +417,7 @@ ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)-> ...@@ -400,6 +417,7 @@ ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)->
client.room.dueling_players[player.pos] = player client.room.dueling_players[player.pos] = player
if settings.modules.tips if settings.modules.tips
ygopro.stoc_send_random_tip(client) ygopro.stoc_send_random_tip(client)
return
ygopro.ctos_follow 'CHAT', false, (buffer, info, client, server)-> ygopro.ctos_follow 'CHAT', false, (buffer, info, client, server)->
switch _.trim(info.msg) switch _.trim(info.msg)
...@@ -414,6 +432,7 @@ ygopro.ctos_follow 'CHAT', false, (buffer, info, client, server)-> ...@@ -414,6 +432,7 @@ ygopro.ctos_follow 'CHAT', false, (buffer, info, client, server)->
else else
#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
when '/help' when '/help'
ygopro.stoc_send_chat(client,"YGOSrv233 指令帮助") ygopro.stoc_send_chat(client,"YGOSrv233 指令帮助")
...@@ -422,6 +441,7 @@ ygopro.ctos_follow 'CHAT', false, (buffer, info, client, server)-> ...@@ -422,6 +441,7 @@ ygopro.ctos_follow 'CHAT', false, (buffer, info, client, server)->
#ygopro.stoc_send_chat(client,"/senddeck 发送自己的卡组") #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
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
...@@ -429,6 +449,7 @@ ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)-> ...@@ -429,6 +449,7 @@ ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)->
side = (info.deckbuf[i] for i in [info.mainc...info.mainc+info.sidec]) side = (info.deckbuf[i] for i in [info.mainc...info.mainc+info.sidec])
client.main = main client.main = main
client.side = side client.side = side
return
### ###
if settings.modules.skip_empty_side if settings.modules.skip_empty_side
...@@ -491,4 +512,5 @@ if settings.modules.http ...@@ -491,4 +512,5 @@ if settings.modules.http
else else
response.writeHead(404); response.writeHead(404);
response.end(); response.end();
return
http_server.listen settings.modules.http.port http_server.listen settings.modules.http.port
...@@ -81,7 +81,7 @@ ...@@ -81,7 +81,7 @@
client.room.disconnect(client); client.room.disconnect(client);
} }
} }
return server.end(); server.end();
}); });
client.on('error', function(error) { client.on('error', function(error) {
if (!client.closed) { if (!client.closed) {
...@@ -90,10 +90,10 @@ ...@@ -90,10 +90,10 @@
client.room.disconnect(client, error); client.room.disconnect(client, error);
} }
} }
return server.end(); server.end();
}); });
client.on('timeout', function() { client.on('timeout', function() {
return server.end(); server.end();
}); });
server.on('close', function(had_error) { server.on('close', function(had_error) {
if (!server.closed) { if (!server.closed) {
...@@ -101,14 +101,14 @@ ...@@ -101,14 +101,14 @@
} }
if (!client.closed) { if (!client.closed) {
ygopro.stoc_send_chat(client, "服务器关闭了连接"); ygopro.stoc_send_chat(client, "服务器关闭了连接");
return client.end(); client.end();
} }
}); });
server.on('error', function(error) { server.on('error', function(error) {
server.closed = error; server.closed = error;
if (!client.closed) { if (!client.closed) {
ygopro.stoc_send_chat(client, "服务器错误: " + error); ygopro.stoc_send_chat(client, "服务器错误: " + error);
return client.end(); client.end();
} }
}); });
ctos_buffer = new Buffer(0); ctos_buffer = new Buffer(0);
...@@ -116,9 +116,9 @@ ...@@ -116,9 +116,9 @@
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, results, struct; var b, struct;
if (client.is_post_watcher) { if (client.is_post_watcher) {
return 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) { if (client.established) {
...@@ -126,17 +126,16 @@ ...@@ -126,17 +126,16 @@
} else { } else {
client.pre_establish_buffers.push(data); client.pre_establish_buffers.push(data);
} }
results = [];
while (true) { while (true) {
if (ctos_message_length === 0) { if (ctos_message_length === 0) {
if (ctos_buffer.length >= 2) { if (ctos_buffer.length >= 2) {
results.push(ctos_message_length = ctos_buffer.readUInt16LE(0)); ctos_message_length = ctos_buffer.readUInt16LE(0);
} else { } else {
break; break;
} }
} else if (ctos_proto === 0) { } else if (ctos_proto === 0) {
if (ctos_buffer.length >= 3) { if (ctos_buffer.length >= 3) {
results.push(ctos_proto = ctos_buffer.readUInt8(2)); ctos_proto = ctos_buffer.readUInt8(2);
} else { } else {
break; break;
} }
...@@ -153,33 +152,31 @@ ...@@ -153,33 +152,31 @@
} }
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;
results.push(ctos_proto = 0); ctos_proto = 0;
} else { } else {
break; break;
} }
} }
} }
return results;
} }
}); });
stoc_buffer = new Buffer(0); stoc_buffer = new Buffer(0);
stoc_message_length = 0; stoc_message_length = 0;
stoc_proto = 0; stoc_proto = 0;
server.on('data', function(data) { server.on('data', function(data) {
var b, results, stanzas, struct; var b, stanzas, struct;
stoc_buffer = Buffer.concat([stoc_buffer, data], stoc_buffer.length + data.length); stoc_buffer = Buffer.concat([stoc_buffer, data], stoc_buffer.length + data.length);
client.write(data); client.write(data);
results = [];
while (true) { while (true) {
if (stoc_message_length === 0) { if (stoc_message_length === 0) {
if (stoc_buffer.length >= 2) { if (stoc_buffer.length >= 2) {
results.push(stoc_message_length = stoc_buffer.readUInt16LE(0)); stoc_message_length = stoc_buffer.readUInt16LE(0);
} else { } else {
break; break;
} }
} else if (stoc_proto === 0) { } else if (stoc_proto === 0) {
if (stoc_buffer.length >= 3) { if (stoc_buffer.length >= 3) {
results.push(stoc_proto = stoc_buffer.readUInt8(2)); stoc_proto = stoc_buffer.readUInt8(2);
} else { } else {
break; break;
} }
...@@ -197,21 +194,19 @@ ...@@ -197,21 +194,19 @@
} }
stoc_buffer = stoc_buffer.slice(2 + stoc_message_length); stoc_buffer = stoc_buffer.slice(2 + stoc_message_length);
stoc_message_length = 0; stoc_message_length = 0;
results.push(stoc_proto = 0); stoc_proto = 0;
} else { } else {
break; break;
} }
} }
} }
return results;
}); });
return 0;
}).listen(settings.port, function() { }).listen(settings.port, function() {
return log.info("server started", settings.ip, settings.port); log.info("server started", settings.ip, settings.port);
}); });
ygopro.ctos_follow('PLAYER_INFO', true, function(buffer, info, client, server) { ygopro.ctos_follow('PLAYER_INFO', true, function(buffer, info, client, server) {
return client.name = info.name; client.name = info.name;
}); });
ygopro.ctos_follow('JOIN_GAME', false, function(buffer, info, client, server) { ygopro.ctos_follow('JOIN_GAME', false, function(buffer, info, client, server) {
...@@ -221,41 +216,41 @@ ...@@ -221,41 +216,41 @@
msg: 4, msg: 4,
code: settings.version code: settings.version
}); });
return client.end(); client.end();
} else if (settings.modules.stop) { } else 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', {
msg: 1, msg: 1,
code: 2 code: 2
}); });
return client.end(); client.end();
} else if (!info.pass.length) { } else if (!info.pass.length) {
ygopro.stoc_send_chat(client, "房间为空,请修改房间名"); ygopro.stoc_send_chat(client, "房间为空,请修改房间名");
ygopro.stoc_send(client, 'ERROR_MSG', { ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1, msg: 1,
code: 2 code: 2
}); });
return client.end(); client.end();
} else if ((os.freemem() / os.totalmem()) <= 0.1) { } else if ((os.freemem() / os.totalmem()) <= 0.1) {
ygopro.stoc_send_chat(client, "服务器已经爆满,请稍候再试"); ygopro.stoc_send_chat(client, "服务器已经爆满,请稍候再试");
ygopro.stoc_send(client, 'ERROR_MSG', { ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1, msg: 1,
code: 2 code: 2
}); });
return client.end(); client.end();
} else if (!Room.validate(info.pass)) { } else if (!Room.validate(info.pass)) {
ygopro.stoc_send_chat(client, "房间密码不正确"); ygopro.stoc_send_chat(client, "房间密码不正确");
ygopro.stoc_send(client, 'ERROR_MSG', { ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1, msg: 1,
code: 2 code: 2
}); });
return client.end(); client.end();
} else if (client.name === '[INCORRECT]') { } else if (client.name === '[INCORRECT]') {
ygopro.stoc_send(client, 'ERROR_MSG', { ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1, msg: 1,
code: 2 code: 2
}); });
return client.end(); client.end();
} else { } else {
client.room = Room.find_or_create_by_name(info.pass); client.room = Room.find_or_create_by_name(info.pass);
if (client.room.started) { if (client.room.started) {
...@@ -268,17 +263,17 @@ ...@@ -268,17 +263,17 @@
buffer = ref[j]; buffer = ref[j];
client.write(buffer); client.write(buffer);
} }
return ygopro.stoc_send_chat(client, "观战中."); ygopro.stoc_send_chat(client, "观战中.");
} else { } else {
ygopro.stoc_send_chat(client, "决斗已开始"); ygopro.stoc_send_chat(client, "决斗已开始");
ygopro.stoc_send(client, 'ERROR_MSG', { ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1, msg: 1,
code: 2 code: 2
}); });
return client.end(); client.end();
} }
} else { } else {
return client.room.connect(client); client.room.connect(client);
} }
} }
}); });
...@@ -302,24 +297,20 @@ ...@@ -302,24 +297,20 @@
some_unknown_mysterious_fucking_thing: 0, some_unknown_mysterious_fucking_thing: 0,
pass: "" pass: ""
}); });
return 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, results, w; var j, len, ref, w;
client.room.watcher_buffers.push(data); client.room.watcher_buffers.push(data);
ref = client.room.watchers; ref = client.room.watchers;
results = [];
for (j = 0, len = ref.length; j < len; j++) { for (j = 0, len = ref.length; j < len; j++) {
w = ref[j]; w = ref[j];
if (w) { if (w) {
results.push(w.write(data)); w.write(data);
} else {
results.push(void 0);
} }
} }
return results;
}); });
return watcher.on('error', function(error) {}); watcher.on('error', function(error) {});
} }
}); });
...@@ -330,17 +321,17 @@ ...@@ -330,17 +321,17 @@
json: true json: true
}, function(error, response, body) { }, function(error, response, body) {
if (_.isString(body)) { if (_.isString(body)) {
return log.warn("dialogues bad json", body); log.warn("dialogues bad json", body);
} else if (error || !body) { } else if (error || !body) {
return log.warn('dialogues error', error, response); log.warn('dialogues error', error, response);
} else { } else {
return dialogues = body; dialogues = body;
} }
}); });
} }
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, results, val; var card, j, len, line, msg, playertype, pos, reason, 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);
...@@ -401,12 +392,10 @@ ...@@ -401,12 +392,10 @@
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)]);
results = [];
for (j = 0, len = ref2.length; j < len; j++) { for (j = 0, len = ref2.length; j < len; j++) {
line = ref2[j]; line = ref2[j];
results.push(ygopro.stoc_send_chat(client, line)); ygopro.stoc_send_chat(client, line);
} }
return results;
} }
} }
} }
...@@ -417,12 +406,12 @@ ...@@ -417,12 +406,12 @@
selftype = info.type & 0xf; selftype = info.type & 0xf;
is_host = ((info.type >> 4) & 0xf) !== 0; is_host = ((info.type >> 4) & 0xf) !== 0;
client.is_host = is_host; client.is_host = is_host;
return client.pos = selftype; client.pos = selftype;
}); });
ygopro.stoc_send_random_tip = function(client) { ygopro.stoc_send_random_tip = function(client) {
if (tips) { if (tips) {
return ygopro.stoc_send_chat(client, "Tip: " + tips[Math.floor(Math.random() * tips.length)]); ygopro.stoc_send_chat(client, "Tip: " + tips[Math.floor(Math.random() * tips.length)]);
} }
}; };
...@@ -433,7 +422,7 @@ ...@@ -433,7 +422,7 @@
url: settings.modules.tips, url: settings.modules.tips,
json: true json: true
}, function(error, response, body) { }, function(error, response, body) {
return tips = body; tips = body;
}); });
} }
...@@ -452,36 +441,37 @@ ...@@ -452,36 +441,37 @@
} }
} }
if (settings.modules.tips) { if (settings.modules.tips) {
return ygopro.stoc_send_random_tip(client); ygopro.stoc_send_random_tip(client);
} }
}); });
ygopro.ctos_follow('CHAT', false, function(buffer, info, client, server) { ygopro.ctos_follow('CHAT', false, function(buffer, info, client, server) {
switch (_.trim(info.msg)) { switch (_.trim(info.msg)) {
case '/ping': case '/ping':
return execFile('ss', ['-it', "dst " + client.remoteAddress + ":" + client.remotePort], function(error, stdout, stderr) { execFile('ss', ['-it', "dst " + client.remoteAddress + ":" + client.remotePort], function(error, stdout, stderr) {
var line; var line;
if (error) { if (error) {
return ygopro.stoc_send_chat_to_room(client.room, error); ygopro.stoc_send_chat_to_room(client.room, error);
} else { } else {
line = _.lines(stdout)[2]; line = _.lines(stdout)[2];
if (line.indexOf('rtt') !== -1) { if (line.indexOf('rtt') !== -1) {
return ygopro.stoc_send_chat_to_room(client.room, line); ygopro.stoc_send_chat_to_room(client.room, line);
} else { } else {
return ygopro.stoc_send_chat_to_room(client.room, stdout); ygopro.stoc_send_chat_to_room(client.room, stdout);
} }
} }
}); });
break;
case '/help': case '/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 显示这个帮助信息");
if (settings.modules.tips) { if (settings.modules.tips) {
return ygopro.stoc_send_chat(client, "/tip 显示一条提示"); ygopro.stoc_send_chat(client, "/tip 显示一条提示");
} }
break; break;
case '/tip': case '/tip':
if (settings.modules.tips) { if (settings.modules.tips) {
return ygopro.stoc_send_random_tip(client); ygopro.stoc_send_random_tip(client);
} }
} }
}); });
...@@ -505,7 +495,7 @@ ...@@ -505,7 +495,7 @@
return results; return results;
})(); })();
client.main = main; client.main = main;
return client.side = side; client.side = side;
}); });
...@@ -527,7 +517,7 @@ ...@@ -527,7 +517,7 @@
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);
return response.end(Room.all.length.toString()); response.end(Room.all.length.toString());
} else if (u.pathname === '/rooms.js') { } else if (u.pathname === '/rooms.js') {
response.writeHead(200); response.writeHead(200);
roomsjson = JSON.stringify({ roomsjson = JSON.stringify({
...@@ -565,10 +555,10 @@ ...@@ -565,10 +555,10 @@
return results; return results;
})() })()
}); });
return response.end("loadroom( " + roomsjson + " );"); response.end("loadroom( " + roomsjson + " );");
} else if (u.query.operation === 'getroomjson') { } else if (u.query.operation === 'getroomjson') {
response.writeHead(200); response.writeHead(200);
return response.end(JSON.stringify({ response.end(JSON.stringify({
rooms: (function() { rooms: (function() {
var j, len, ref, results; var j, len, ref, results;
ref = Room.all; ref = Room.all;
...@@ -610,10 +600,10 @@ ...@@ -610,10 +600,10 @@
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);
return response.end("shout " + u.query.shout + " ok"); response.end("shout " + u.query.shout + " ok");
} else { } else {
response.writeHead(404); response.writeHead(404);
return response.end(); response.end();
} }
}); });
http_server.listen(settings.modules.http.port); http_server.listen(settings.modules.http.port);
......
...@@ -43,6 +43,7 @@ for name, declaration of structs_declaration ...@@ -43,6 +43,7 @@ for name, declaration of structs_declaration
break break
throw "unknown proto" if !@constants.STOC[proto] throw "unknown proto" if !@constants.STOC[proto]
@stoc_follows[proto] = {callback: callback, synchronous: synchronous} @stoc_follows[proto] = {callback: callback, synchronous: synchronous}
return
@ctos_follow = (proto, synchronous, callback)-> @ctos_follow = (proto, synchronous, callback)->
if typeof proto == 'string' if typeof proto == 'string'
for key, value of @constants.CTOS for key, value of @constants.CTOS
...@@ -51,6 +52,7 @@ for name, declaration of structs_declaration ...@@ -51,6 +52,7 @@ for name, declaration of structs_declaration
break break
throw "unknown proto" if !@constants.CTOS[proto] throw "unknown proto" if !@constants.CTOS[proto]
@ctos_follows[proto] = {callback: callback, synchronous: synchronous} @ctos_follows[proto] = {callback: callback, synchronous: synchronous}
return
#消息发送函数,至少要把俩合起来.... #消息发送函数,至少要把俩合起来....
...@@ -78,6 +80,7 @@ for name, declaration of structs_declaration ...@@ -78,6 +80,7 @@ for name, declaration of structs_declaration
header.writeUInt8 proto, 2 header.writeUInt8 proto, 2
socket.write header socket.write header
socket.write buffer if buffer.length socket.write buffer if buffer.length
return
@ctos_send = (socket, proto, info)-> @ctos_send = (socket, proto, info)->
#console.log proto, proto_structs.CTOS[proto], structs[proto_structs.CTOS[proto]] #console.log proto, proto_structs.CTOS[proto], structs[proto_structs.CTOS[proto]]
...@@ -103,6 +106,7 @@ for name, declaration of structs_declaration ...@@ -103,6 +106,7 @@ for name, declaration of structs_declaration
header.writeUInt8 proto, 2 header.writeUInt8 proto, 2
socket.write header socket.write header
socket.write buffer if buffer.length socket.write buffer if buffer.length
return
#util #util
@stoc_send_chat = (client, msg, player = 8)-> @stoc_send_chat = (client, msg, player = 8)->
...@@ -111,9 +115,11 @@ for name, declaration of structs_declaration ...@@ -111,9 +115,11 @@ for name, declaration of structs_declaration
player: player player: player
msg: line msg: line
} }
return
@stoc_send_chat_to_room = (room, msg, player = 8)-> @stoc_send_chat_to_room = (room, msg, player = 8)->
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
@stoc_send_chat(client, msg, player) if client @stoc_send_chat(client, msg, player) if client
return
\ No newline at end of file
...@@ -71,7 +71,7 @@ ...@@ -71,7 +71,7 @@
throw "unknown proto"; throw "unknown proto";
} }
} }
return this.stoc_follows[proto] = { this.stoc_follows[proto] = {
callback: callback, callback: callback,
synchronous: synchronous synchronous: synchronous
}; };
...@@ -92,7 +92,7 @@ ...@@ -92,7 +92,7 @@
throw "unknown proto"; throw "unknown proto";
} }
} }
return this.ctos_follows[proto] = { this.ctos_follows[proto] = {
callback: callback, callback: callback,
synchronous: synchronous synchronous: synchronous
}; };
...@@ -128,7 +128,7 @@ ...@@ -128,7 +128,7 @@
header.writeUInt8(proto, 2); header.writeUInt8(proto, 2);
socket.write(header); socket.write(header);
if (buffer.length) { if (buffer.length) {
return socket.write(buffer); socket.write(buffer);
} }
}; };
...@@ -162,29 +162,27 @@ ...@@ -162,29 +162,27 @@
header.writeUInt8(proto, 2); header.writeUInt8(proto, 2);
socket.write(header); socket.write(header);
if (buffer.length) { if (buffer.length) {
return socket.write(buffer); socket.write(buffer);
} }
}; };
this.stoc_send_chat = function(client, msg, player) { this.stoc_send_chat = function(client, msg, player) {
var j, len1, line, ref, results; var j, len1, line, ref;
if (player == null) { if (player == null) {
player = 8; player = 8;
} }
ref = _.lines(msg); ref = _.lines(msg);
results = [];
for (j = 0, len1 = ref.length; j < len1; j++) { for (j = 0, len1 = ref.length; j < len1; j++) {
line = ref[j]; line = ref[j];
results.push(this.stoc_send(client, 'CHAT', { this.stoc_send(client, 'CHAT', {
player: player, player: player,
msg: line msg: line
})); });
} }
return results;
}; };
this.stoc_send_chat_to_room = function(room, msg, player) { this.stoc_send_chat_to_room = function(room, msg, player) {
var client, j, k, len1, len2, ref, ref1, results; var client, j, k, len1, len2, ref, ref1;
if (player == null) { if (player == null) {
player = 8; player = 8;
} }
...@@ -196,16 +194,12 @@ ...@@ -196,16 +194,12 @@
} }
} }
ref1 = room.watchers; ref1 = room.watchers;
results = [];
for (k = 0, len2 = ref1.length; k < len2; k++) { for (k = 0, len2 = ref1.length; k < len2; k++) {
client = ref1[k]; client = ref1[k];
if (client) { if (client) {
results.push(this.stoc_send_chat(client, msg, player)); this.stoc_send_chat(client, msg, player);
} else {
results.push(void 0);
} }
} }
return results;
}; };
}).call(this); }).call(this);
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