Commit eb0a701c authored by mercury233's avatar mercury233

观战列表

parent 7b9afc11
WebSocketServer = require('ws').Server WebSocketServer = require('ws').Server
url = require('url')
settings = global.settings
server = null server = null
...@@ -8,32 +9,41 @@ room_data = (room)-> ...@@ -8,32 +9,41 @@ room_data = (room)->
title: room.title, title: room.title,
user: {username: room.username} user: {username: room.username}
users: ({username: client.name, position: client.pos} for client in room.players), users: ({username: client.name, position: client.pos} for client in room.players),
options: room.hostinfo options: room.hostinfo,
arena: settings.modules.arena_mode.enabled && room.arena && settings.modules.arena_mode.mode
init = (http_server, ROOM_all)-> init = (http_server, ROOM_all)->
server = new WebSocketServer server = new WebSocketServer
server: http_server server: http_server
server.on 'connection', (connection) -> server.on 'connection', (connection) ->
connection.filter = url.parse(connection.upgradeReq.url, true).query.filter || 'waiting'
connection.send JSON.stringify connection.send JSON.stringify
event: 'init' event: 'init'
data: room_data(room) for room in ROOM_all when room and room.established and !room.private and !room.started data: room_data(room) for room in ROOM_all when room and room.established and !room.private and (room.started == (connection.filter == 'started'))
create = (room)-> create = (room)->
broadcast('create', room_data(room)) broadcast('create', room_data(room), 'waiting')
update = (room)-> update = (room)->
broadcast('update', room_data(room)) broadcast('update', room_data(room), 'waiting')
start = (room)->
broadcast('delete', room_data(room), 'waiting')
broadcast('create', room_data(room), 'started')
_delete = (room_id)-> _delete = (room)->
broadcast('delete', room_id) if(room.started)
broadcast('delete', room.name, 'started')
else
broadcast('delete', room.name, 'waiting')
broadcast = (event, data)-> broadcast = (event, data, filter)->
return if !server return if !server
message = JSON.stringify message = JSON.stringify
event: event event: event
data: data data: data
for connection in server.clients for connection in server.clients when connection.filter == filter
try try
connection.send message connection.send message
...@@ -41,4 +51,6 @@ module.exports = ...@@ -41,4 +51,6 @@ module.exports =
init: init init: init
create: create create: create
update: update update: update
delete: _delete start: start
\ No newline at end of file delete: _delete
// Generated by CoffeeScript 1.12.2 // Generated by CoffeeScript 1.12.2
(function() { (function() {
var WebSocketServer, _delete, broadcast, create, init, room_data, server, update; var WebSocketServer, _delete, broadcast, create, init, room_data, server, settings, start, update, url;
WebSocketServer = require('ws').Server; WebSocketServer = require('ws').Server;
url = require('url');
settings = global.settings;
server = null; server = null;
room_data = function(room) { room_data = function(room) {
...@@ -27,7 +31,8 @@ ...@@ -27,7 +31,8 @@
} }
return results; return results;
})(), })(),
options: room.hostinfo options: room.hostinfo,
arena: settings.modules.arena_mode.enabled && room.arena && settings.modules.arena_mode.mode
}; };
}; };
...@@ -37,6 +42,7 @@ ...@@ -37,6 +42,7 @@
}); });
return server.on('connection', function(connection) { return server.on('connection', function(connection) {
var room; var room;
connection.filter = url.parse(connection.upgradeReq.url, true).query.filter || 'waiting';
return connection.send(JSON.stringify({ return connection.send(JSON.stringify({
event: 'init', event: 'init',
data: (function() { data: (function() {
...@@ -44,7 +50,7 @@ ...@@ -44,7 +50,7 @@
results = []; results = [];
for (i = 0, len = ROOM_all.length; i < len; i++) { for (i = 0, len = ROOM_all.length; i < len; i++) {
room = ROOM_all[i]; room = ROOM_all[i];
if (room && room.established && !room["private"] && !room.started) { if (room && room.established && !room["private"] && (room.started === (connection.filter === 'started'))) {
results.push(room_data(room)); results.push(room_data(room));
} }
} }
...@@ -55,18 +61,27 @@ ...@@ -55,18 +61,27 @@
}; };
create = function(room) { create = function(room) {
return broadcast('create', room_data(room)); return broadcast('create', room_data(room), 'waiting');
}; };
update = function(room) { update = function(room) {
return broadcast('update', room_data(room)); return broadcast('update', room_data(room), 'waiting');
}; };
_delete = function(room_id) { start = function(room) {
return broadcast('delete', room_id); broadcast('delete', room_data(room), 'waiting');
return broadcast('create', room_data(room), 'started');
};
_delete = function(room) {
if (room.started) {
return broadcast('delete', room.name, 'started');
} else {
return broadcast('delete', room.name, 'waiting');
}
}; };
broadcast = function(event, data) { broadcast = function(event, data, filter) {
var connection, i, len, message, ref, results; var connection, i, len, message, ref, results;
if (!server) { if (!server) {
return; return;
...@@ -79,9 +94,11 @@ ...@@ -79,9 +94,11 @@
results = []; results = [];
for (i = 0, len = ref.length; i < len; i++) { for (i = 0, len = ref.length; i < len; i++) {
connection = ref[i]; connection = ref[i];
try { if (connection.filter === filter) {
results.push(connection.send(message)); try {
} catch (error) {} results.push(connection.send(message));
} catch (error) {}
}
} }
return results; return results;
}; };
...@@ -90,6 +107,7 @@ ...@@ -90,6 +107,7 @@
init: init, init: init,
create: create, create: create,
update: update, update: update,
start: start,
"delete": _delete "delete": _delete
}; };
......
...@@ -503,7 +503,7 @@ class Room ...@@ -503,7 +503,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 and settings.modules.http.websocket_roomlist roomlist.delete this if !@private and @established and settings.modules.http.websocket_roomlist
return return
get_playing_player: -> get_playing_player: ->
...@@ -1352,7 +1352,7 @@ ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)-> ...@@ -1352,7 +1352,7 @@ ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)->
unless room.started #first start unless room.started #first start
room.started = true room.started = true
room.start_time = moment().format() room.start_time = moment().format()
roomlist.delete room.name if settings.modules.http.websocket_roomlist and not room.private roomlist.start room if settings.modules.http.websocket_roomlist and not room.private
#room.duels = [] #room.duels = []
room.dueling_players = [] room.dueling_players = []
for player in room.players when player.pos != 7 for player in room.players when player.pos != 7
...@@ -1797,4 +1797,4 @@ if settings.modules.http ...@@ -1797,4 +1797,4 @@ if settings.modules.http
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)
roomlist.init https_server, ROOM_all roomlist.init https_server, ROOM_all
https_server.listen settings.modules.http.ssl.port https_server.listen settings.modules.http.ssl.port
\ No newline at end of file
...@@ -676,8 +676,8 @@ ...@@ -676,8 +676,8 @@
if (index !== -1) { if (index !== -1) {
ROOM_all[index] = null; ROOM_all[index] = null;
} }
if (!this["private"] && !this.started && this.established && settings.modules.http.websocket_roomlist) { if (!this["private"] && this.established && settings.modules.http.websocket_roomlist) {
roomlist["delete"](this.name); roomlist["delete"](this);
} }
}; };
...@@ -1654,7 +1654,7 @@ ...@@ -1654,7 +1654,7 @@
room.started = true; room.started = true;
room.start_time = moment().format(); room.start_time = moment().format();
if (settings.modules.http.websocket_roomlist && !room["private"]) { if (settings.modules.http.websocket_roomlist && !room["private"]) {
roomlist["delete"](room.name); roomlist.start(room);
} }
room.dueling_players = []; room.dueling_players = [];
ref = room.players; ref = room.players;
......
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