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
......@@ -81,7 +81,7 @@
client.room.disconnect(client);
}
}
return server.end();
server.end();
});
client.on('error', function(error) {
if (!client.closed) {
......@@ -90,10 +90,10 @@
client.room.disconnect(client, error);
}
}
return server.end();
server.end();
});
client.on('timeout', function() {
return server.end();
server.end();
});
server.on('close', function(had_error) {
if (!server.closed) {
......@@ -101,14 +101,14 @@
}
if (!client.closed) {
ygopro.stoc_send_chat(client, "服务器关闭了连接");
return client.end();
client.end();
}
});
server.on('error', function(error) {
server.closed = error;
if (!client.closed) {
ygopro.stoc_send_chat(client, "服务器错误: " + error);
return client.end();
client.end();
}
});
ctos_buffer = new Buffer(0);
......@@ -116,9 +116,9 @@
ctos_proto = 0;
client.pre_establish_buffers = new Array();
client.on('data', function(data) {
var b, results, struct;
var b, struct;
if (client.is_post_watcher) {
return client.room.watcher.write(data);
client.room.watcher.write(data);
} else {
ctos_buffer = Buffer.concat([ctos_buffer, data], ctos_buffer.length + data.length);
if (client.established) {
......@@ -126,17 +126,16 @@
} else {
client.pre_establish_buffers.push(data);
}
results = [];
while (true) {
if (ctos_message_length === 0) {
if (ctos_buffer.length >= 2) {
results.push(ctos_message_length = ctos_buffer.readUInt16LE(0));
ctos_message_length = ctos_buffer.readUInt16LE(0);
} else {
break;
}
} else if (ctos_proto === 0) {
if (ctos_buffer.length >= 3) {
results.push(ctos_proto = ctos_buffer.readUInt8(2));
ctos_proto = ctos_buffer.readUInt8(2);
} else {
break;
}
......@@ -153,33 +152,31 @@
}
ctos_buffer = ctos_buffer.slice(2 + ctos_message_length);
ctos_message_length = 0;
results.push(ctos_proto = 0);
ctos_proto = 0;
} else {
break;
}
}
}
return results;
}
});
stoc_buffer = new Buffer(0);
stoc_message_length = 0;
stoc_proto = 0;
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);
client.write(data);
results = [];
while (true) {
if (stoc_message_length === 0) {
if (stoc_buffer.length >= 2) {
results.push(stoc_message_length = stoc_buffer.readUInt16LE(0));
stoc_message_length = stoc_buffer.readUInt16LE(0);
} else {
break;
}
} else if (stoc_proto === 0) {
if (stoc_buffer.length >= 3) {
results.push(stoc_proto = stoc_buffer.readUInt8(2));
stoc_proto = stoc_buffer.readUInt8(2);
} else {
break;
}
......@@ -197,21 +194,19 @@
}
stoc_buffer = stoc_buffer.slice(2 + stoc_message_length);
stoc_message_length = 0;
results.push(stoc_proto = 0);
stoc_proto = 0;
} else {
break;
}
}
}
return results;
});
return 0;
}).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) {
return client.name = info.name;
client.name = info.name;
});
ygopro.ctos_follow('JOIN_GAME', false, function(buffer, info, client, server) {
......@@ -221,41 +216,41 @@
msg: 4,
code: settings.version
});
return client.end();
client.end();
} else if (settings.modules.stop) {
ygopro.stoc_send_chat(client, settings.modules.stop);
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
});
return client.end();
client.end();
} else if (!info.pass.length) {
ygopro.stoc_send_chat(client, "房间为空,请修改房间名");
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
});
return client.end();
client.end();
} else if ((os.freemem() / os.totalmem()) <= 0.1) {
ygopro.stoc_send_chat(client, "服务器已经爆满,请稍候再试");
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
});
return client.end();
client.end();
} else if (!Room.validate(info.pass)) {
ygopro.stoc_send_chat(client, "房间密码不正确");
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
});
return client.end();
client.end();
} else if (client.name === '[INCORRECT]') {
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
});
return client.end();
client.end();
} else {
client.room = Room.find_or_create_by_name(info.pass);
if (client.room.started) {
......@@ -268,17 +263,17 @@
buffer = ref[j];
client.write(buffer);
}
return ygopro.stoc_send_chat(client, "观战中.");
ygopro.stoc_send_chat(client, "观战中.");
} else {
ygopro.stoc_send_chat(client, "决斗已开始");
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
});
return client.end();
client.end();
}
} else {
return client.room.connect(client);
client.room.connect(client);
}
}
});
......@@ -302,24 +297,20 @@
some_unknown_mysterious_fucking_thing: 0,
pass: ""
});
return ygopro.ctos_send(watcher, 'HS_TOOBSERVER');
ygopro.ctos_send(watcher, 'HS_TOOBSERVER');
});
watcher.on('data', function(data) {
var j, len, ref, results, w;
var j, len, ref, w;
client.room.watcher_buffers.push(data);
ref = client.room.watchers;
results = [];
for (j = 0, len = ref.length; j < len; j++) {
w = ref[j];
if (w) {
results.push(w.write(data));
} else {
results.push(void 0);
w.write(data);
}
}
return results;
});
return watcher.on('error', function(error) {});
watcher.on('error', function(error) {});
}
});
......@@ -330,17 +321,17 @@
json: true
}, function(error, response, body) {
if (_.isString(body)) {
return log.warn("dialogues bad json", body);
log.warn("dialogues bad json", body);
} else if (error || !body) {
return log.warn('dialogues error', error, response);
log.warn('dialogues error', error, response);
} else {
return dialogues = body;
dialogues = body;
}
});
}
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);
if (ygopro.constants.MSG[msg] === 'START') {
playertype = buffer.readUInt8(1);
......@@ -401,12 +392,10 @@
card = buffer.readUInt32LE(1);
if (dialogues[card]) {
ref2 = _.lines(dialogues[card][Math.floor(Math.random() * dialogues[card].length)]);
results = [];
for (j = 0, len = ref2.length; j < len; j++) {
line = ref2[j];
results.push(ygopro.stoc_send_chat(client, line));
ygopro.stoc_send_chat(client, line);
}
return results;
}
}
}
......@@ -417,12 +406,12 @@
selftype = info.type & 0xf;
is_host = ((info.type >> 4) & 0xf) !== 0;
client.is_host = is_host;
return client.pos = selftype;
client.pos = selftype;
});
ygopro.stoc_send_random_tip = function(client) {
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 @@
url: settings.modules.tips,
json: true
}, function(error, response, body) {
return tips = body;
tips = body;
});
}
......@@ -452,36 +441,37 @@
}
}
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) {
switch (_.trim(info.msg)) {
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;
if (error) {
return ygopro.stoc_send_chat_to_room(client.room, error);
ygopro.stoc_send_chat_to_room(client.room, error);
} else {
line = _.lines(stdout)[2];
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 {
return ygopro.stoc_send_chat_to_room(client.room, stdout);
ygopro.stoc_send_chat_to_room(client.room, stdout);
}
}
});
break;
case '/help':
ygopro.stoc_send_chat(client, "YGOSrv233 指令帮助");
ygopro.stoc_send_chat(client, "/help 显示这个帮助信息");
if (settings.modules.tips) {
return ygopro.stoc_send_chat(client, "/tip 显示一条提示");
ygopro.stoc_send_chat(client, "/tip 显示一条提示");
}
break;
case '/tip':
if (settings.modules.tips) {
return ygopro.stoc_send_random_tip(client);
ygopro.stoc_send_random_tip(client);
}
}
});
......@@ -505,7 +495,7 @@
return results;
})();
client.main = main;
return client.side = side;
client.side = side;
});
......@@ -527,7 +517,7 @@
u = url.parse(request.url, 1);
if (u.pathname === '/count.json') {
response.writeHead(200);
return response.end(Room.all.length.toString());
response.end(Room.all.length.toString());
} else if (u.pathname === '/rooms.js') {
response.writeHead(200);
roomsjson = JSON.stringify({
......@@ -565,10 +555,10 @@
return results;
})()
});
return response.end("loadroom( " + roomsjson + " );");
response.end("loadroom( " + roomsjson + " );");
} else if (u.query.operation === 'getroomjson') {
response.writeHead(200);
return response.end(JSON.stringify({
response.end(JSON.stringify({
rooms: (function() {
var j, len, ref, results;
ref = Room.all;
......@@ -610,10 +600,10 @@
ygopro.stoc_send_chat_to_room(room, u.query.shout);
}
response.writeHead(200);
return response.end("shout " + u.query.shout + " ok");
response.end("shout " + u.query.shout + " ok");
} else {
response.writeHead(404);
return response.end();
response.end();
}
});
http_server.listen(settings.modules.http.port);
......
......@@ -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