Commit 1275e258 authored by mercury233's avatar mercury233

test3

parent a481f165
......@@ -85,6 +85,7 @@ class Room
#log.info 'room-exit', this.name, this.port, code
@disconnector = 'server' unless @disconnector
this.delete()
return
@process.stdout.setEncoding('utf8')
@process.stdout.once 'data', (data)=>
@established = true
......@@ -94,7 +95,9 @@ class Room
player.server.write buffer for buffer in player.pre_establish_buffers
player.established = true
player.pre_establish_buffers = null
return
return
return
delete: ->
#积分
return if @deleted
......@@ -104,6 +107,7 @@ class Room
#Room.all[index] = null unless index == -1
Room.all.splice(index, 1) unless index == -1
@deleted = true
return
connect: (client)->
@players.push client
......@@ -113,6 +117,8 @@ class Room
client.server.write buffer for buffer in client.pre_establish_buffers
client.established = true
client.pre_establish_buffers = []
return
return
disconnect: (client, error)->
if client.is_post_watcher
......@@ -127,5 +133,6 @@ class Room
else
@process.kill()
this.delete()
return
module.exports = Room
\ No newline at end of file
......@@ -102,7 +102,7 @@
if (!_this.disconnector) {
_this.disconnector = 'server';
}
return _this["delete"]();
_this["delete"]();
};
})(this));
this.process.stdout.setEncoding('utf8');
......@@ -110,8 +110,8 @@
return function(data) {
_this.established = true;
_this.port = parseInt(data);
return _.each(_this.players, function(player) {
return player.server.connect(_this.port, '127.0.0.1', function() {
_.each(_this.players, function(player) {
player.server.connect(_this.port, '127.0.0.1', function() {
var buffer, i, len, ref;
ref = player.pre_establish_buffers;
for (i = 0, len = ref.length; i < len; i++) {
......@@ -119,7 +119,7 @@
player.server.write(buffer);
}
player.established = true;
return player.pre_establish_buffers = null;
player.pre_establish_buffers = null;
});
});
};
......@@ -136,13 +136,13 @@
if (index !== -1) {
Room.all.splice(index, 1);
}
return this.deleted = true;
this.deleted = true;
};
Room.prototype.connect = function(client) {
this.players.push(client);
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;
ref = client.pre_establish_buffers;
for (i = 0, len = ref.length; i < len; i++) {
......@@ -150,7 +150,7 @@
client.server.write(buffer);
}
client.established = true;
return client.pre_establish_buffers = [];
client.pre_establish_buffers = [];
});
}
};
......@@ -161,7 +161,7 @@
ygopro.stoc_send_chat_to_room(this, client.name + " " + '退出了观战' + (error ? ": " + error : ''));
index = _.indexOf(this.watchers, client);
if (index !== -1) {
return this.watchers.splice(index, 1);
this.watchers.splice(index, 1);
}
} else {
index = _.indexOf(this.players, client);
......@@ -169,10 +169,10 @@
this.players.splice(index, 1);
}
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 {
this.process.kill();
return this["delete"]();
this["delete"]();
}
}
};
......
......@@ -65,6 +65,7 @@ net.createServer (client) ->
client.closed = true
client.room.disconnect(client) if client.room
server.end()
return
client.on 'error', (error)->
#log.info "client error", client.name, error
......@@ -73,9 +74,11 @@ net.createServer (client) ->
client.closed = error
client.room.disconnect(client, error) if client.room
server.end()
return
client.on 'timeout', ()->
server.end()
return
server.on 'close', (had_error) ->
#log.info "server closed", client.name, had_error
......@@ -84,6 +87,7 @@ net.createServer (client) ->
unless client.closed
ygopro.stoc_send_chat(client, "服务器关闭了连接")
client.end()
return
server.on 'error', (error)->
#log.info "server error", client.name, error
......@@ -92,6 +96,7 @@ net.createServer (client) ->
unless client.closed
ygopro.stoc_send_chat(client, "服务器错误: #{error}")
client.end()
return
#需要重构
#客户端到服务端(ctos)协议分析
......@@ -139,6 +144,7 @@ net.createServer (client) ->
ctos_proto = 0
else
break
return
#服务端到客户端(stoc)
stoc_buffer = new Buffer(0)
......@@ -179,14 +185,17 @@ net.createServer (client) ->
stoc_proto = 0
else
break
return 0
return
return
.listen settings.port, ->
log.info "server started", settings.ip, settings.port
return
#功能模块
ygopro.ctos_follow 'PLAYER_INFO', true, (buffer, info, client, server)->
client.name = info.name #在创建room之前暂存
return
ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
#log.info info
......@@ -259,6 +268,7 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
client.end()
else
client.room.connect(client)
return
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: ""
}
ygopro.ctos_send watcher, 'HS_TOOBSERVER'
return
watcher.on 'data', (data)->
client.room.watcher_buffers.push data
for w in client.room.watchers
w.write data if w #a WTF fix
return
watcher.on 'error', (error)->
#log.error "watcher error", error
return
return
#登场台词
if settings.modules.dialogues
......@@ -303,6 +316,7 @@ if settings.modules.dialogues
else
#log.info "dialogues loaded", _.size body
dialogues = body
return
ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)->
msg = buffer.readInt8(0)
......@@ -366,7 +380,7 @@ ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)->
if dialogues[card]
for line in _.lines dialogues[card][Math.floor(Math.random() * dialogues[card].length)]
ygopro.stoc_send_chat client, line
return
#房间管理
......@@ -377,10 +391,12 @@ ygopro.stoc_follow 'TYPE_CHANGE', false, (buffer, info, client, server)->
client.is_host = is_host
client.pos = selftype
#console.log "TYPE_CHANGE to #{client.name}:", info, selftype, is_host
return
#tip
ygopro.stoc_send_random_tip = (client)->
ygopro.stoc_send_chat client, "Tip: " + tips[Math.floor(Math.random() * tips.length)] if tips
return
tips = null
if settings.modules.tips
......@@ -390,6 +406,7 @@ if settings.modules.tips
, (error, response, body)->
tips = body
#log.info "tips loaded", tips.length
return
ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)->
unless client.room.started #first start
......@@ -400,6 +417,7 @@ ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)->
client.room.dueling_players[player.pos] = player
if settings.modules.tips
ygopro.stoc_send_random_tip(client)
return
ygopro.ctos_follow 'CHAT', false, (buffer, info, client, server)->
switch _.trim(info.msg)
......@@ -414,6 +432,7 @@ ygopro.ctos_follow 'CHAT', false, (buffer, info, client, server)->
else
#log.warn 'ping', stdout
ygopro.stoc_send_chat_to_room client.room, stdout
return
when '/help'
ygopro.stoc_send_chat(client,"YGOSrv233 指令帮助")
......@@ -422,6 +441,7 @@ ygopro.ctos_follow 'CHAT', false, (buffer, info, client, server)->
#ygopro.stoc_send_chat(client,"/senddeck 发送自己的卡组")
when '/tip'
ygopro.stoc_send_random_tip(client) if settings.modules.tips
return
ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)->
#log.info info
......@@ -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])
client.main = main
client.side = side
return
###
if settings.modules.skip_empty_side
......@@ -491,4 +512,5 @@ if settings.modules.http
else
response.writeHead(404);
response.end();
return
http_server.listen settings.modules.http.port
This diff is collapsed.
......@@ -43,6 +43,7 @@ for name, declaration of structs_declaration
break
throw "unknown proto" if !@constants.STOC[proto]
@stoc_follows[proto] = {callback: callback, synchronous: synchronous}
return
@ctos_follow = (proto, synchronous, callback)->
if typeof proto == 'string'
for key, value of @constants.CTOS
......@@ -51,6 +52,7 @@ for name, declaration of structs_declaration
break
throw "unknown proto" if !@constants.CTOS[proto]
@ctos_follows[proto] = {callback: callback, synchronous: synchronous}
return
#消息发送函数,至少要把俩合起来....
......@@ -78,6 +80,7 @@ for name, declaration of structs_declaration
header.writeUInt8 proto, 2
socket.write header
socket.write buffer if buffer.length
return
@ctos_send = (socket, proto, info)->
#console.log proto, proto_structs.CTOS[proto], structs[proto_structs.CTOS[proto]]
......@@ -103,6 +106,7 @@ for name, declaration of structs_declaration
header.writeUInt8 proto, 2
socket.write header
socket.write buffer if buffer.length
return
#util
@stoc_send_chat = (client, msg, player = 8)->
......@@ -111,9 +115,11 @@ for name, declaration of structs_declaration
player: player
msg: line
}
return
@stoc_send_chat_to_room = (room, msg, player = 8)->
for client in room.players
@stoc_send_chat(client, msg, player) if client
for client in room.watchers
@stoc_send_chat(client, msg, player) if client
return
\ No newline at end of file
......@@ -71,7 +71,7 @@
throw "unknown proto";
}
}
return this.stoc_follows[proto] = {
this.stoc_follows[proto] = {
callback: callback,
synchronous: synchronous
};
......@@ -92,7 +92,7 @@
throw "unknown proto";
}
}
return this.ctos_follows[proto] = {
this.ctos_follows[proto] = {
callback: callback,
synchronous: synchronous
};
......@@ -128,7 +128,7 @@
header.writeUInt8(proto, 2);
socket.write(header);
if (buffer.length) {
return socket.write(buffer);
socket.write(buffer);
}
};
......@@ -162,29 +162,27 @@
header.writeUInt8(proto, 2);
socket.write(header);
if (buffer.length) {
return socket.write(buffer);
socket.write(buffer);
}
};
this.stoc_send_chat = function(client, msg, player) {
var j, len1, line, ref, results;
var j, len1, line, ref;
if (player == null) {
player = 8;
}
ref = _.lines(msg);
results = [];
for (j = 0, len1 = ref.length; j < len1; j++) {
line = ref[j];
results.push(this.stoc_send(client, 'CHAT', {
this.stoc_send(client, 'CHAT', {
player: player,
msg: line
}));
});
}
return results;
};
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) {
player = 8;
}
......@@ -196,16 +194,12 @@
}
}
ref1 = room.watchers;
results = [];
for (k = 0, len2 = ref1.length; k < len2; k++) {
client = ref1[k];
if (client) {
results.push(this.stoc_send_chat(client, msg, player));
} else {
results.push(void 0);
this.stoc_send_chat(client, msg, player);
}
}
return results;
};
}).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