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
This diff is collapsed.
...@@ -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