Commit 85a01308 authored by 神楽坂玲奈's avatar 神楽坂玲奈

class Room

parent 06e55585
{
"name": "ygopro-server",
"version": "2.0.0",
"description": "a server for ygopro",
"repository": {
"type": "git",
"url": "https://github.com/mycard/ygopro-server.git"
},
"keywords": [
"mycard",
"ygopro",
"server"
],
"author": "zh99998 <zh99998@gmail.com>",
"dependencies": {
"underscore": "*",
"freeport": "*",
"struct": "*",
"inotify": "*"
},
"license": "GPLv3",
"main": "ygopro-server.js",
"scripts": {
"start": "forever start mycard-server-match.js",
"build": "coffee -c mycard-server-match.coffee"
},
"engines": {
"node": "*"
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
// Generated by CoffeeScript 1.6.3 // Generated by CoffeeScript 1.6.3
(function() { (function() {
var Inotify, Struct, constants, ctos_follow, ctos_follows, ctos_send, declaration, field, freeport, fs, http, inotify, listener, name, net, path, proto_structs, result, rooms, rooms_port_process, server_listener, settings, spawn, stoc_follow, stoc_follows, stoc_send, structs, structs_declaration, taici, type, typedefs, url, _, _i, _len; var Inotify, Room, Struct, constants, ctos_follow, ctos_follows, ctos_send, debug, declaration, field, freeport, fs, http, inotify, listener, name, net, path, proto_structs, result, server_listener, settings, spawn, stoc_follow, stoc_follows, stoc_send, structs, structs_declaration, taici, type, typedefs, url, _, _i, _len;
net = require('net'); net = require('net');
...@@ -8,6 +8,10 @@ ...@@ -8,6 +8,10 @@
url = require('url'); url = require('url');
path = require('path');
fs = require('fs');
spawn = require('child_process').spawn; spawn = require('child_process').spawn;
freeport = require('freeport'); freeport = require('freeport');
...@@ -16,6 +20,8 @@ ...@@ -16,6 +20,8 @@
_ = require('underscore'); _ = require('underscore');
Inotify = require('inotify').Inotify;
structs_declaration = require('./structs.json'); structs_declaration = require('./structs.json');
typedefs = require('./typedefs.json'); typedefs = require('./typedefs.json');
...@@ -26,9 +32,59 @@ ...@@ -26,9 +32,59 @@
settings = require('./config.json'); settings = require('./config.json');
Room = (function() {
Room.all = [];
function Room(name, port, client) {
this.name = name;
this.port = port;
this.alive = true;
this.players = [];
this.add_client(client);
Room.all.push(this);
}
Room.prototype["delete"] = function(room) {
return delete Room.all[_.indexOf(Room.all, room)];
};
Room.prototype.add_client = function(client) {
return this.players.push({
client: client,
name: client.player
});
};
Room.find_by_name = function(name) {
return _.find(this.all, function(room) {
return room.name === name;
});
};
Room.find_by_port = function(port) {
return _.find(this.all, function(room) {
return room.name === port;
});
};
Room.find_by_client = function(client) {
return _.find(this.all, function(room) {
return _.some(room.players, function(player) {
return player.client === client;
});
});
};
return Room;
})();
debug = false;
if (process.argv[2] === '--debug') { if (process.argv[2] === '--debug') {
settings.port++; settings.port++;
settings.http_port++; settings.http_port++;
debug = true;
} }
structs = {}; structs = {};
...@@ -143,7 +199,9 @@ ...@@ -143,7 +199,9 @@
if (buffer.length) { if (buffer.length) {
socket.write(buffer); socket.write(buffer);
} }
return console.log('stoc_sent:', buffer); if (debug) {
return console.log('stoc_sent:', buffer);
}
}; };
ctos_send = function(socket, proto, info) { ctos_send = function(socket, proto, info) {
...@@ -178,7 +236,9 @@ ...@@ -178,7 +236,9 @@
if (buffer.length) { if (buffer.length) {
socket.write(buffer); socket.write(buffer);
} }
return console.log('ctos_sent:', buffer); if (debug) {
return console.log('ctos_sent:', buffer);
}
}; };
server_listener = function(port, client, server) { server_listener = function(port, client, server) {
...@@ -195,7 +255,9 @@ ...@@ -195,7 +255,9 @@
} }
server.on("data", function(data) { server.on("data", function(data) {
var b, struct; var b, struct;
console.log('server: ', data); if (debug) {
console.log('server: ', data);
}
stoc_buffer = Buffer.concat([stoc_buffer, data], stoc_buffer.length + data.length); stoc_buffer = Buffer.concat([stoc_buffer, data], stoc_buffer.length + data.length);
while (true) { while (true) {
if (stoc_message_length === 0) { if (stoc_message_length === 0) {
...@@ -212,7 +274,9 @@ ...@@ -212,7 +274,9 @@
} }
} else { } else {
if (stoc_buffer.length >= 2 + stoc_message_length) { if (stoc_buffer.length >= 2 + stoc_message_length) {
console.log(constants.STOC[stoc_proto]); if (debug) {
console.log(constants.STOC[stoc_proto]);
}
if (stoc_follows[stoc_proto]) { if (stoc_follows[stoc_proto]) {
b = stoc_buffer.slice(3, stoc_message_length - 1 + 3); b = stoc_buffer.slice(3, stoc_message_length - 1 + 3);
if (struct = structs[proto_structs.STOC[constants.STOC[stoc_proto]]]) { if (struct = structs[proto_structs.STOC[constants.STOC[stoc_proto]]]) {
...@@ -244,10 +308,6 @@ ...@@ -244,10 +308,6 @@
}); });
}; };
rooms = {};
rooms_port_process = {};
listener = net.createServer(function(client) { listener = net.createServer(function(client) {
var ctos_buffer, ctos_message_length, ctos_proto, server; var ctos_buffer, ctos_message_length, ctos_proto, server;
client.connected = false; client.connected = false;
...@@ -261,7 +321,9 @@ ...@@ -261,7 +321,9 @@
}); });
client.on("data", function(data) { client.on("data", function(data) {
var b, struct; var b, struct;
console.log('client: ', data); if (debug) {
console.log('client: ', data);
}
ctos_buffer = Buffer.concat([ctos_buffer, data], ctos_buffer.length + data.length); ctos_buffer = Buffer.concat([ctos_buffer, data], ctos_buffer.length + data.length);
while (true) { while (true) {
if (ctos_message_length === 0) { if (ctos_message_length === 0) {
...@@ -278,7 +340,9 @@ ...@@ -278,7 +340,9 @@
} }
} else { } else {
if (ctos_buffer.length >= 2 + ctos_message_length) { if (ctos_buffer.length >= 2 + ctos_message_length) {
console.log(constants.CTOS[ctos_proto]); if (debug) {
console.log(constants.CTOS[ctos_proto]);
}
if (ctos_follows[ctos_proto]) { if (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 = structs[proto_structs.CTOS[constants.CTOS[ctos_proto]]]) { if (struct = structs[proto_structs.CTOS[constants.CTOS[ctos_proto]]]) {
...@@ -321,7 +385,7 @@ ...@@ -321,7 +385,7 @@
}); });
ctos_follow('JOIN_GAME', false, function(buffer, info, client, server) { ctos_follow('JOIN_GAME', false, function(buffer, info, client, server) {
var room_name; var room, room_name;
room_name = info.pass; room_name = info.pass;
if (info.version !== settings.version) { if (info.version !== settings.version) {
stoc_send(client, 'ERROR_MSG', { stoc_send(client, 'ERROR_MSG', {
...@@ -343,24 +407,24 @@ ...@@ -343,24 +407,24 @@
return client.end(); return client.end();
} else { } else {
if (client.player !== '[INCORRECT]') { if (client.player !== '[INCORRECT]') {
if (rooms[room_name]) { room = Room.find_by_name(room_name);
if (typeof rooms[room_name] === 'number') { if (room) {
return server.connect(rooms[room_name], '127.0.0.1', function() { room.add_client(client);
return server_listener(rooms[room_name], client, server); if (room.established) {
return server.connect(room.port, '127.0.0.1', function() {
return server_listener(room.port, client, server);
}); });
} else {
return rooms[room_name].push(client);
} }
} else { } else {
return freeport(function(err, port) { return freeport(function(err, port) {
var param, room; var param, process;
if (rooms[room_name]) { room = Room.find_by_name(room_name);
if (typeof rooms[room_name] === 'number') { if (room) {
return server.connect(rooms[room_name], '127.0.0.1', function() { room.add_client(client);
return server_listener(rooms[room_name], client, server); if (room.established) {
return server.connect(room.port, '127.0.0.1', function() {
return server_listener(room.port, client, server);
}); });
} else {
return rooms[room_name].push(client);
} }
} else { } else {
if (err) { if (err) {
...@@ -370,7 +434,7 @@ ...@@ -370,7 +434,7 @@
}); });
return client.end(); return client.end();
} else { } else {
rooms[room_name] = [client]; room = new Room(room_name, port, client);
if (room_name.slice(0, 2) === 'M#') { if (room_name.slice(0, 2) === 'M#') {
param = [0, 0, 1, 'F', 'F', 'F', 8000, 5, 1]; param = [0, 0, 1, 'F', 'F', 'F', 8000, 5, 1];
} else if (room_name.slice(0, 2) === 'T#') { } else if (room_name.slice(0, 2) === 'T#') {
...@@ -382,26 +446,20 @@ ...@@ -382,26 +446,20 @@
param = [0, 0, 0, 'F', 'F', 'F', 8000, 5, 1]; param = [0, 0, 0, 'F', 'F', 'F', 8000, 5, 1];
} }
param.unshift(port); param.unshift(port);
room = spawn('./ygopro', param, { process = spawn('./ygopro', param, {
cwd: 'ygocore' cwd: 'ygocore'
}); });
room.alive = true; room.process = process;
rooms_port_process[port] = room; process.on('exit', function(code) {
room.on('exit', function(code) { return room["delete"]();
delete rooms[room_name];
return delete rooms_port_process[port];
}); });
return room.stdout.once('data', function(data) { return process.stdout.once('data', function(data) {
if (rooms[room_name]) { room.established = true;
rooms[room_name].forEach(function(c) { return _.each(room.players, function(player) {
return server.connect(port, '127.0.0.1', function() { return server.connect(port, '127.0.0.1', function() {
return server_listener(port, c, server); return server_listener(port, player.client, server);
});
}); });
return rooms[room_name] = port; });
} else {
return room.kill();
}
}); });
} }
} }
...@@ -453,19 +511,13 @@ ...@@ -453,19 +511,13 @@
http.createServer(function(request, response) { http.createServer(function(request, response) {
if (url.parse(request.url).pathname === '/count.json') { if (url.parse(request.url).pathname === '/count.json') {
response.writeHead(200); response.writeHead(200);
return response.end(Object.keys(rooms).length.toString()); return response.end(rooms.length.toString());
} else { } else {
response.writeHead(404); response.writeHead(404);
return response.end(); return response.end();
} }
}).listen(settings.http_port); }).listen(settings.http_port);
path = require('path');
fs = require('fs');
Inotify = require('inotify').Inotify;
inotify = new Inotify(); inotify = new Inotify();
inotify.addWatch({ inotify.addWatch({
...@@ -476,7 +528,7 @@ ...@@ -476,7 +528,7 @@
mask = event.mask; mask = event.mask;
if (event.name) { if (event.name) {
port = parseInt(path.basename(event.name, '.yrp')); port = parseInt(path.basename(event.name, '.yrp'));
room = rooms_port_process[port]; room = Room.find_by_port(port);
if (room) { if (room) {
if (mask & Inotify.IN_CREATE) { if (mask & Inotify.IN_CREATE) {
...@@ -493,15 +545,15 @@ ...@@ -493,15 +545,15 @@
}); });
setInterval(function() { setInterval(function() {
var port, room, _results; var room, _j, _len1, _results;
_results = []; _results = [];
for (port in rooms_port_process) { for (_j = 0, _len1 = rooms.length; _j < _len1; _j++) {
room = rooms_port_process[port]; room = rooms[_j];
if (room.alive) { if (room.alive) {
_results.push(room.alive = false); _results.push(room.alive = false);
} else { } else {
console.log("killed " + port + " " + room); console.log("kill " + port + " " + room);
_results.push(room.kill()); _results.push(room.process.kill());
} }
} }
return _results; return _results;
......
This diff is collapsed.
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