Commit eb0a701c authored by mercury233's avatar mercury233

观战列表

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