-
Notifications
You must be signed in to change notification settings - Fork 76
/
roomlist.coffee
59 lines (48 loc) · 1.84 KB
/
roomlist.coffee
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
WebSocketServer = require('ws').Server
url = require('url')
settings = global.settings
server = null
room_data = (room)->
id: room.name,
title: room.title || room.name,
user: {username: room.username}
users: ({username: client.name, position: client.pos} for client in room.players),
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 = (http_server, ROOM_all)->
server = new WebSocketServer
server: http_server
server.on 'connection', (connection, upgradeReq) ->
connection.filter = url.parse(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 (connection.filter == 'started' or !room.private) and ((room.duel_stage != 0) == (connection.filter == 'started'))
clients.add connection
connection.on('close', () -> clients.delete connection if clients.has connection)
create = (room)->
broadcast('create', room_data(room), 'waiting') if !room.private
update = (room)->
broadcast('update', room_data(room), 'waiting') if !room.private
start = (room)->
broadcast('delete', room.name, 'waiting') if !room.private
broadcast('create', room_data(room), 'started')
_delete = (room)->
if(room.duel_stage != 0)
broadcast('delete', room.name, 'started')
else
broadcast('delete', room.name, 'waiting') if !room.private
broadcast = (event, data, filter)->
return if !server
message = JSON.stringify
event: event
data: data
for connection in Array.from(clients.values()) when connection.filter == filter
try
connection.send message
module.exports =
init: init
create: create
update: update
start: start
delete: _delete