-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguest.js
More file actions
99 lines (86 loc) · 2.27 KB
/
guest.js
File metadata and controls
99 lines (86 loc) · 2.27 KB
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
const winston = require('winston');
module.exports = class Guest {
constructor(socket, data, hostLocations) {
this.socket = socket;
this.connectionId = socket.id;
this.locationId = data.locationId;
this.id = data.id;
this.hostLocations = hostLocations;
this.host = null;
socket.on('disconnect', this.onDisconnectGuest.bind(this));
socket.on('pickHost', this.onPickHost.bind(this));
socket.on('addEntry', this.onAddEntry.bind(this));
socket.on('vote', this.onVote.bind(this));
socket.on('toHost', this.onToHost.bind(this));
this.pushAvailableHosts();
}
onDisconnectGuest() {
if (this.host) {
this.host.guests.delete(this);
}
}
onPickHost(data) {
const hosts = this.hostLocations.get(this.locationId) || null;
this.host = (hosts && hosts.find(h => h.id === data.hostId)) || null;
if (this.host) {
this.host.guests.add(this);
this.pushPlaylist();
}
}
onVote(data) {
if (!this.host) {
return winston.error('guest: onVote with no host set!');
}
const entry = this.host.playlist.getEntry(data.type, data.id);
if (!entry) {
return winston.error(`guest: upVote on nonexisting playlist entry ${data.type}, ${data.id}!`);
}
switch (data.dir) {
case 'up':
entry.voteUp(this); break;
case 'down':
entry.voteDown(this); break;
default:
return winston.error(`guest: onVote with invalid vote direction ${data.dir}!`);
}
this.host.pushPlaylistToAll();
}
onAddEntry(data) {
if(this.host) {
this.host.playlist.addEntry(data.type, data.id, data.name);
}
}
/**
* forward to host
*/
onToHost(data) {
winston.debug(`guest: tunnel to host`, data);
if (this.host) {
this.host.pushToHost({...data, guestId: this.id});
} else {
winston.error(`guest: no host set!`);
}
}
/**
* send up-to-date playlist to this guest.
*/
pushPlaylist() {
if (this.host) {
const playlist = this.host.playlist.serializeGuest(this);
this.socket.emit('setPlaylist', {playlist});
}
}
pushAvailableHosts() {
const hosts = (this.hostLocations.get(this.locationId) || []).map(h => ({name: h.name, id: h.id}));
this.socket.emit('availableHosts', {hosts});
}
/**
* forward to guest
*/
pushToGuest(data) {
this.socket.emit('toGuest', data);
}
pushDisconnectHost() {
this.socket.emit('disconnectHost');
}
};