Commit 586e1c59 authored by 神楽坂玲奈's avatar 神楽坂玲奈 Committed by Ma

websocket room list

parent a032e47f
...@@ -4,6 +4,7 @@ _.mixin(_.str.exports()); ...@@ -4,6 +4,7 @@ _.mixin(_.str.exports());
spawn = require('child_process').spawn spawn = require('child_process').spawn
spawnSync = require('child_process').spawnSync spawnSync = require('child_process').spawnSync
ygopro = require './ygopro.js' ygopro = require './ygopro.js'
roomlist = require './roomlist'
bunyan = require 'bunyan' bunyan = require 'bunyan'
moment = require 'moment' moment = require 'moment'
#redis = require 'redis' #redis = require 'redis'
...@@ -134,7 +135,7 @@ class Room ...@@ -134,7 +135,7 @@ class Room
room_pass = room_name_and_pass[1] room_pass = room_name_and_pass[1]
client_name == room_name and client_pass != room_pass client_name == room_name and client_pass != room_pass
constructor: (name) -> constructor: (name, @hostinfo) ->
@name = name @name = name
@alive = true @alive = true
@players = [] @players = []
...@@ -148,7 +149,7 @@ class Room ...@@ -148,7 +149,7 @@ class Room
@welcome = '' @welcome = ''
Room.all.push this Room.all.push this
@hostinfo = @hostinfo ||=
lflist: 0 lflist: 0
rule: if settings.modules.enable_TCG_as_default then 2 else 0 rule: if settings.modules.enable_TCG_as_default then 2 else 0
mode: 0 mode: 0
...@@ -308,6 +309,7 @@ class Room ...@@ -308,6 +309,7 @@ class Room
@process.stdout.setEncoding('utf8') @process.stdout.setEncoding('utf8')
@process.stdout.once 'data', (data)=> @process.stdout.once 'data', (data)=>
@established = true @established = true
roomlist.create(this)
@port = parseInt data @port = parseInt data
_.each @players, (player)=> _.each @players, (player)=>
player.server.connect @port, '127.0.0.1',=> player.server.connect @port, '127.0.0.1',=>
...@@ -350,6 +352,7 @@ class Room ...@@ -350,6 +352,7 @@ class Room
index = _.indexOf(Room.all, this) index = _.indexOf(Room.all, this)
#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
roomlist.delete @name if !@private and !@started and @established
return return
get_playing_player: -> get_playing_player: ->
...@@ -380,6 +383,7 @@ class Room ...@@ -380,6 +383,7 @@ class Room
Room.players_oppentlist[client.remoteAddress] = null Room.players_oppentlist[client.remoteAddress] = null
if @established if @established
roomlist.update(this) unless @started
client.server.connect @port, '127.0.0.1', -> client.server.connect @port, '127.0.0.1', ->
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
...@@ -401,6 +405,7 @@ class Room ...@@ -401,6 +405,7 @@ class Room
Room.ban_player(client.name, client.ip, "强退") Room.ban_player(client.name, client.ip, "强退")
if @players.length if @players.length
ygopro.stoc_send_chat_to_room this, "#{client.name} #{'离开了游戏'}#{if error then ": #{error}" else ''}" ygopro.stoc_send_chat_to_room this, "#{client.name} #{'离开了游戏'}#{if error then ": #{error}" else ''}"
roomlist.update(this) unless @started
#client.room = null #client.room = null
else else
@process.kill() @process.kill()
......
// Generated by CoffeeScript 1.10.0 // Generated by CoffeeScript 1.9.3
(function() { (function() {
var Room, _, bunyan, get_memory_usage, log, moment, settings, spawn, spawnSync, ygopro; var Room, _, bunyan, get_memory_usage, log, moment, roomlist, settings, spawn, spawnSync, ygopro;
_ = require('underscore'); _ = require('underscore');
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
ygopro = require('./ygopro.js'); ygopro = require('./ygopro.js');
roomlist = require('./roomlist');
bunyan = require('bunyan'); bunyan = require('bunyan');
moment = require('moment'); moment = require('moment');
...@@ -184,8 +186,9 @@ ...@@ -184,8 +186,9 @@
}); });
}; };
function Room(name) { function Room(name, hostinfo) {
var draw_count, error1, lflist, param, rule, start_hand, start_lp, time_limit; var draw_count, lflist, param, rule, start_hand, start_lp, time_limit;
this.hostinfo = hostinfo;
this.name = name; this.name = name;
this.alive = true; this.alive = true;
this.players = []; this.players = [];
...@@ -198,7 +201,7 @@ ...@@ -198,7 +201,7 @@
this.random_type = ''; this.random_type = '';
this.welcome = ''; this.welcome = '';
Room.all.push(this); Room.all.push(this);
this.hostinfo = { this.hostinfo || (this.hostinfo = {
lflist: 0, lflist: 0,
rule: settings.modules.enable_TCG_as_default ? 2 : 0, rule: settings.modules.enable_TCG_as_default ? 2 : 0,
mode: 0, mode: 0,
...@@ -209,7 +212,7 @@ ...@@ -209,7 +212,7 @@
start_hand: 5, start_hand: 5,
draw_count: 1, draw_count: 1,
time_limit: 180 time_limit: 180
}; });
if (name.slice(0, 2) === 'M#') { if (name.slice(0, 2) === 'M#') {
this.hostinfo.mode = 1; this.hostinfo.mode = 1;
} else if (name.slice(0, 2) === 'T#') { } else if (name.slice(0, 2) === 'T#') {
...@@ -391,6 +394,7 @@ ...@@ -391,6 +394,7 @@
this.process.stdout.once('data', (function(_this) { this.process.stdout.once('data', (function(_this) {
return function(data) { return function(data) {
_this.established = true; _this.established = true;
roomlist.create(_this);
_this.port = parseInt(data); _this.port = parseInt(data);
_.each(_this.players, function(player) { _.each(_this.players, function(player) {
player.server.connect(_this.port, '127.0.0.1', function() { player.server.connect(_this.port, '127.0.0.1', function() {
...@@ -406,7 +410,7 @@ ...@@ -406,7 +410,7 @@
}); });
}; };
})(this)); })(this));
} catch (error1) { } catch (_error) {
this.error = "建立房间失败,请重试"; this.error = "建立房间失败,请重试";
} }
} }
...@@ -447,6 +451,9 @@ ...@@ -447,6 +451,9 @@
if (index !== -1) { if (index !== -1) {
Room.all.splice(index, 1); Room.all.splice(index, 1);
} }
if (!this["private"] && !this.started && this.established) {
roomlist["delete"](this.name);
}
}; };
Room.prototype.get_playing_player = function() { Room.prototype.get_playing_player = function() {
...@@ -489,6 +496,9 @@ ...@@ -489,6 +496,9 @@
} }
} }
if (this.established) { if (this.established) {
if (!this.started) {
roomlist.update(this);
}
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;
...@@ -520,6 +530,9 @@ ...@@ -520,6 +530,9 @@
} }
if (this.players.length) { if (this.players.length) {
ygopro.stoc_send_chat_to_room(this, client.name + " " + '离开了游戏' + (error ? ": " + error : '')); ygopro.stoc_send_chat_to_room(this, client.name + " " + '离开了游戏' + (error ? ": " + error : ''));
if (!this.started) {
roomlist.update(this);
}
} else { } else {
this.process.kill(); this.process.kill();
this["delete"](); this["delete"]();
......
WebSocketServer = require('ws').Server;
server = null
room_data = (room)->
id: room.name,
title: room.title,
user: {username: room.username}
users: ({username: client.name, position: client.pos} for client in room.players),
options: room.hostinfo
init = (http_server, Room)->
server = new WebSocketServer
server: http_server
server.on 'connection', (connection) ->
connection.send JSON.stringify
event: 'init'
data: room_data(room) for room in Room.all when room.established and !room.private and !room.started
create = (room)->
broadcast('create', room_data(room))
update = (room)->
broadcast('update', room_data(room))
_delete = (room_id)->
broadcast('delete', room_id)
broadcast = (event, data)->
return if !server
message = JSON.stringify
event: event
data: data
for connection in server.clients
try
connection.send message
module.exports =
init: init
create: create
update: update
delete: _delete
\ No newline at end of file
// Generated by CoffeeScript 1.9.3
(function() {
var WebSocketServer, _delete, broadcast, create, init, room_data, server, update;
WebSocketServer = require('ws').Server;
server = null;
room_data = function(room) {
var client;
return {
id: room.name,
title: room.title,
user: {
username: room.username
},
users: (function() {
var i, len, ref, results;
ref = room.players;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
client = ref[i];
results.push({
username: client.name,
position: client.pos
});
}
return results;
})(),
options: room.hostinfo
};
};
init = function(http_server, Room) {
server = new WebSocketServer({
server: http_server
});
return server.on('connection', function(connection) {
var room;
return connection.send(JSON.stringify({
event: 'init',
data: (function() {
var i, len, ref, results;
ref = Room.all;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
room = ref[i];
if (room.established && !room["private"] && !room.started) {
results.push(room_data(room));
}
}
return results;
})()
}));
});
};
create = function(room) {
return broadcast('create', room_data(room));
};
update = function(room) {
return broadcast('update', room_data(room));
};
_delete = function(room_id) {
return broadcast('delete', room_id);
};
broadcast = function(event, data) {
var connection, i, len, message, ref, results;
if (!server) {
return;
}
message = JSON.stringify({
event: event,
data: data
});
ref = server.clients;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
connection = ref[i];
try {
results.push(connection.send(message));
} catch (_error) {}
}
return results;
};
module.exports = {
init: init,
create: create,
update: update,
"delete": _delete
};
}).call(this);
...@@ -34,7 +34,7 @@ settings.version = parseInt(fs.readFileSync('ygopro/gframe/game.cpp', 'utf8').ma ...@@ -34,7 +34,7 @@ settings.version = parseInt(fs.readFileSync('ygopro/gframe/game.cpp', 'utf8').ma
#组件 #组件
ygopro = require './ygopro.js' ygopro = require './ygopro.js'
Room = require './room.js' Room = require './room.js'
roomlist = require './roomlist.js'
#debug模式 端口号+1 #debug模式 端口号+1
debug = false debug = false
...@@ -398,8 +398,6 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)-> ...@@ -398,8 +398,6 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
options = { options = {
lflist: 0 lflist: 0
time_limit: 180 time_limit: 180
title: info.pass.slice(8)
private: action == 2
rule: (opt1 >> 5) & 3 rule: (opt1 >> 5) & 3
mode: (opt1 >> 3) & 3 mode: (opt1 >> 3) & 3
enable_priority: !!((opt1 >> 2) & 1) enable_priority: !!((opt1 >> 2) & 1)
...@@ -410,6 +408,8 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)-> ...@@ -410,6 +408,8 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
draw_count: opt3 & 0xF draw_count: opt3 & 0xF
} }
room = new Room(name, options) room = new Room(name, options)
room.title = info.pass.slice(8)
room.private = action == 2
when 3 when 3
name = info.pass.slice(8) name = info.pass.slice(8)
room = Room.find_by_name(name) room = Room.find_by_name(name)
...@@ -697,6 +697,7 @@ ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)-> ...@@ -697,6 +697,7 @@ ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)->
return unless client.room return unless client.room
unless client.room.started #first start unless client.room.started #first start
client.room.started = true client.room.started = true
roomlist.delete room.name unless room.private
#client.room.duels = [] #client.room.duels = []
client.room.dueling_players = [] client.room.dueling_players = []
for player in client.room.players when player.pos != 7 for player in client.room.players when player.pos != 7
...@@ -869,10 +870,5 @@ if settings.modules.http ...@@ -869,10 +870,5 @@ if settings.modules.http
cert: fs.readFileSync(settings.modules.http.ssl.cert) cert: fs.readFileSync(settings.modules.http.ssl.cert)
key: fs.readFileSync(settings.modules.http.ssl.key) key: fs.readFileSync(settings.modules.http.ssl.key)
https_server = https.createServer(options, requestListener) https_server = https.createServer(options, requestListener)
WebSocketServer = require('ws').Server roomlist.init https_server, Room
websocket_server = new WebSocketServer
server: https_server
websocket_server.on 'connection', (connection) ->
console.log(room for room in Room.all when room.established)
https_server.listen settings.modules.http.ssl.port https_server.listen settings.modules.http.ssl.port
\ No newline at end of file
// Generated by CoffeeScript 1.9.3 // Generated by CoffeeScript 1.9.3
(function() { (function() {
var Graveyard, Room, WebSocketServer, _, bunyan, crypto, debug, dialogues, execFile, fs, http, http_server, https, https_server, log, moment, net, options, os, path, request, requestListener, settings, tips, tribute, url, wait_room_start, websocket_server, ygopro; var Graveyard, Room, _, bunyan, crypto, debug, dialogues, execFile, fs, http, http_server, https, https_server, log, moment, net, options, os, path, request, requestListener, roomlist, settings, tips, tribute, url, wait_room_start, ygopro;
net = require('net'); net = require('net');
...@@ -44,6 +44,8 @@ ...@@ -44,6 +44,8 @@
Room = require('./room.js'); Room = require('./room.js');
roomlist = require('./roomlist.js');
debug = false; debug = false;
log = null; log = null;
...@@ -425,8 +427,6 @@ ...@@ -425,8 +427,6 @@
options = { options = {
lflist: 0, lflist: 0,
time_limit: 180, time_limit: 180,
title: info.pass.slice(8),
"private": action === 2,
rule: (opt1 >> 5) & 3, rule: (opt1 >> 5) & 3,
mode: (opt1 >> 3) & 3, mode: (opt1 >> 3) & 3,
enable_priority: !!((opt1 >> 2) & 1), enable_priority: !!((opt1 >> 2) & 1),
...@@ -437,6 +437,8 @@ ...@@ -437,6 +437,8 @@
draw_count: opt3 & 0xF draw_count: opt3 & 0xF
}; };
room = new Room(name, options); room = new Room(name, options);
room.title = info.pass.slice(8);
room["private"] = action === 2;
break; break;
case 3: case 3:
name = info.pass.slice(8); name = info.pass.slice(8);
...@@ -786,6 +788,9 @@ ...@@ -786,6 +788,9 @@
} }
if (!client.room.started) { if (!client.room.started) {
client.room.started = true; client.room.started = true;
if (!room["private"]) {
roomlist["delete"](room.name);
}
client.room.dueling_players = []; client.room.dueling_players = [];
ref = client.room.players; ref = client.room.players;
for (k = 0, len = ref.length; k < len; k++) { for (k = 0, len = ref.length; k < len; k++) {
...@@ -1034,25 +1039,7 @@ ...@@ -1034,25 +1039,7 @@
key: fs.readFileSync(settings.modules.http.ssl.key) key: fs.readFileSync(settings.modules.http.ssl.key)
}; };
https_server = https.createServer(options, requestListener); https_server = https.createServer(options, requestListener);
WebSocketServer = require('ws').Server; roomlist.init(https_server, Room);
websocket_server = new WebSocketServer({
server: https_server
});
websocket_server.on('connection', function(connection) {
var room;
return console.log((function() {
var k, len, ref, results;
ref = Room.all;
results = [];
for (k = 0, len = ref.length; k < len; k++) {
room = ref[k];
if (room.established) {
results.push(room);
}
}
return results;
})());
});
https_server.listen(settings.modules.http.ssl.port); https_server.listen(settings.modules.http.ssl.port);
} }
} }
......
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