forked from mycard/srvpro
-
Notifications
You must be signed in to change notification settings - Fork 14
/
roomlist.js
130 lines (116 loc) · 3.28 KB
/
roomlist.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Generated by CoffeeScript 2.6.1
(function() {
var WebSocketServer, _delete, broadcast, clients, 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) {
var client;
return {
id: room.name,
title: room.title || room.name,
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.get_roomlist_hostinfo(), // Should be updated when MyCard client updates
arena: settings.modules.arena_mode.enabled && room.arena && settings.modules.arena_mode.mode
};
};
clients = new Set();
init = function(http_server, ROOM_all) {
server = new WebSocketServer({
server: http_server
});
return server.on('connection', function(connection, upgradeReq) {
var room;
connection.filter = url.parse(upgradeReq.url, true).query.filter || 'waiting';
connection.send(JSON.stringify({
event: 'init',
data: (function() {
var i, len, results;
results = [];
for (i = 0, len = ROOM_all.length; i < len; i++) {
room = ROOM_all[i];
if (room && room.established && (connection.filter === 'started' || !room.private) && ((room.duel_stage !== 0) === (connection.filter === 'started'))) {
results.push(room_data(room));
}
}
return results;
})()
}));
clients.add(connection);
return connection.on('close', function() {
if (clients.has(connection)) {
return clients.delete(connection);
}
});
});
};
create = function(room) {
if (!room.private) {
return broadcast('create', room_data(room), 'waiting');
}
};
update = function(room) {
if (!room.private) {
return broadcast('update', room_data(room), 'waiting');
}
};
start = function(room) {
if (!room.private) {
broadcast('delete', room.name, 'waiting');
}
return broadcast('create', room_data(room), 'started');
};
_delete = function(room) {
if (room.duel_stage !== 0) {
return broadcast('delete', room.name, 'started');
} else {
if (!room.private) {
return broadcast('delete', room.name, 'waiting');
}
}
};
broadcast = function(event, data, filter) {
var connection, i, len, message, ref, results;
if (!server) {
return;
}
message = JSON.stringify({
event: event,
data: data
});
ref = Array.from(clients.values());
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;
};
module.exports = {
init: init,
create: create,
update: update,
start: start,
delete: _delete
};
}).call(this);