-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticipant.js
150 lines (125 loc) · 3.82 KB
/
participant.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
const Room = require("./room");
// Questionable global var
var ROOMS = {};
const getRoom = (roomId) => {
const room = ROOMS[roomId];
if (!room) throw new Error("Room not found!");
return room;
};
const generateId = (length) => {
var result = "";
var characters = "0123456789";
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
};
class Participant {
constructor(io, socket) {
this.socket = socket;
this.io = io;
this.displayName = null;
this.isSpectator = false;
this.id = this.socket.id;
this.room = null;
socket.on("create", this.onCreateRoom.bind(this));
socket.on("join", this.onJoinRoom.bind(this));
socket.on("disconnect", this.onDisconnect.bind(this));
socket.on("name", this.onChangeName.bind(this));
socket.on("profile", this.onProfileChange.bind(this));
socket.on("vote", this.onVote.bind(this));
socket.on("reveal", this.onReveal.bind(this));
socket.on("reset", this.onReset.bind(this));
socket.on("options", this.onChangeOptions.bind(this));
socket.on("leave", this.onDisconnect.bind(this));
}
onCreateRoom(options, callback) {
console.log(`${new Date().toISOString()} ${this.displayName || this.id} created room`);
if (this.room != null) {
this.onDisconnect();
}
this.room = new Room(generateId(4), options);
ROOMS[this.room.id] = this.room;
this.room.join(this);
this.room.notifyParticipants();
callback(this.room.id);
}
onJoinRoom(roomId, callback) {
console.log(`${new Date().toISOString()} ${this.displayName || this.id} is joining room`);
if (this.room != null) {
if (this.room.id === roomId) {
this.room.notifyParticipants();
return;
}
this.onDisconnect();
}
try {
this.room = getRoom(roomId);
this.room.join(this);
this.room.notifyParticipants();
console.log(`${new Date().toISOString()} ${this.displayName || this.id} joined room`);
} catch (error) {
callback(false);
console.error(error);
}
}
onDisconnect() {
console.log(`${new Date().toISOString()} ${this.displayName || this.id} left room`);
if (this.room) {
this.room.leave(this);
this.room.notifyParticipants();
if (this.room.participants.length === 0) {
ROOMS[this.room.id] = null;
}
this.room = null;
}
}
onChangeName(name) {
if (this.displayName === name) return;
this.displayName = name;
console.log(`${new Date().toISOString()} ${this.displayName || this.id} changed name to ${name}`);
if (this.room) {
this.room.notifyParticipants();
}
}
onProfileChange(profile) {
this.displayName = profile.displayName;
this.isSpectator = !!profile.isSpectator;
if (this.room) {
if (this.isSpectator) {
this.room.removeVoteForParticipant(this);
}
this.room.notifyParticipants();
}
}
onChangeOptions(options) {
console.log(`${new Date().toISOString()} ${this.displayName || this.id} changed options`);
if (this.room) {
this.room.changeOptions(options);
this.room.notifyParticipants();
}
}
onVote(vote) {
console.log(`${new Date().toISOString()} ${this.displayName || this.id} voted`);
if (this.room) {
this.room.vote(vote, this);
this.room.notifyParticipants();
}
}
onReveal() {
console.log(`${new Date().toISOString()} ${this.displayName || this.id} revealed`);
if (this.room) {
this.room.reveal();
this.room.notifyParticipants();
}
}
onReset() {
console.log(`${new Date().toISOString()} ${this.displayName || this.id} reset`);
if (this.room) {
this.room.reset();
this.room.notifyParticipants();
}
}
}
module.exports = Participant;