-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cliques.js
154 lines (131 loc) · 3.15 KB
/
Cliques.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const cliques = {}
const shortid = require('shortid')
const url = require('url')
const onecolor = require('onecolor')
function Clique(name) {
this.name = name
this.friends = []
this.destination = null
if(cliques[name])
throw new Error("Trying to create duplicate clique "+name)
cliques[name] = this
}
Clique.prototype.destroy = function() {
console.log(`Destroying clique ${this.name}`)
this.friends.forEach((c) => c.close())
delete cliques[this.name]
}
Clique.prototype.add = function(ws) {
const query = url.parse(ws.upgradeReq.url, true).query
if(!['name', 'latitude', 'longitude'].every((p) => p in query)) {
console.error(`Dropping invalid join from ${ws.name}`)
ws.close()
return
}
ws.id = shortid.generate()
ws.name = query.name
ws.position = {
coords: {
latitude: Number(query.latitude),
longitude: Number(query.longitude)
}
}
ws.hiding = false
ws.color = pickAColor()
console.log(`${ws.name} (${ws.id}) joined ${this.name}`)
this.friends.push(ws)
ws.on('message', (m) => this.onMessage(ws, m))
ws.on('close', (m) => { this.remove(ws) })
this.getFriendUpToSpeed(ws)
this.sendToOthers(ws, {
event: 'friend-joined',
name: ws.name,
id: ws.id,
position: ws.position,
hiding: ws.hiding,
color: ws.color
})
}
Clique.prototype.onMessage = function(sender, message) {
var payload = {}
try {
payload = JSON.parse(message)
payload.id = sender.id
payload.name = sender.name
switch (payload.event) {
case 'friend-state-updated':
sender.hiding = payload.hiding
break
case 'friend-updated':
sender.position = payload.position
break
case 'destination-set':
this.destination = payload.placeId
break
default:
break
}
this.broadcast(payload)
} catch (e) {
console.error(e)
sender.close()
}
}
Clique.prototype.remove = function(ws) {
console.log(`${ws.name} (${ws.id}) left ${this.name}`)
if(this.friends.indexOf(ws) == -1)
console.error("Trying to remove a non-member from clique "+this.name)
else
this.friends.splice(this.friends.indexOf(ws), 1)
if(!this.friends.length)
this.destroy()
else
this.broadcast({
event: 'friend-left',
id: ws.id,
name: ws.name
})
}
Clique.prototype.broadcast = function(message) {
this.friends.forEach((f) => {
this.sendTo(f, message)
})
}
Clique.prototype.sendToOthers = function(sender, message) {
this.friends.filter(f => f != sender)
.forEach(f => this.sendTo(f, message))
}
Clique.prototype.sendTo = function(recipient, message) {
var json = JSON.stringify(message)
try {
recipient.send(json)
} catch (e) {
console.error(`Failed to send message: ${e}`)
}
}
Clique.prototype.getFriendUpToSpeed = function(friend) {
this.sendTo(friend, {
event: 'friends-list-updated',
friends: this.friends.map(f => {
return {
id: f.id,
position: f.position,
name: f.name,
hiding: f.hiding,
color: f.color
}
})
})
this.sendTo(friend, {
event: 'destination-set',
placeId: this.destination
})
}
function pickAColor() {
return new onecolor.HSL(Math.random(), 1, 0.5).hex()
}
module.exports.get = function getClique(name) {
if(!(name in cliques))
cliques[name] = new Clique(name)
return cliques[name]
}