-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.js
More file actions
199 lines (175 loc) · 4.08 KB
/
DataManager.js
File metadata and controls
199 lines (175 loc) · 4.08 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* This object manages the shared data.
* It is used as a single source of truth.
*/
const QuestionUtils = require('./QuestionUtils');
const MAX_TEAMS = 8;
class DataManager {
constructor(io, url, questions, linkedQuestions) {
this.io = io;
this.room = url;
this.questions = questions;
this.linkedQuestions = linkedQuestions;
this.activeQuestion = null;
this.teams = {};
this.admins = {};
}
/**
* Broadcasts the specified event to all connected users.
*
* @param {string} event - the event to broadcast
* @param {any} payload - the event payload
*/
broadcast(event, payload) {
this.io.to(this.room).emit(event, payload);
}
/**
* Emits the specified event to the specified team.
*
* @param {string} team - the team
* @param {string} event - the event
* @param {any} payload - the event payload
*/
emit(team, event, payload) {
const socketId = Object.entries(this.teams).find(([_, t]) => t === team)[0];
this.io.to(socketId).emit(event, payload);
}
/**
* Starts the specified question.
*
* @param {number} index - the index of the question
*/
startQuestion(index) {
this.activeQuestion = this.questions[index];
}
/**
* Ends the active question.
*/
endQuestion() {
this.activeQuestion = null;
}
/**
* Returns the active question.
*/
getActiveQuestion() {
return this.activeQuestion;
}
/**
* Returns the index of the active question.
*/
getActiveQuestionIndex() {
return this.questions.indexOf(this.activeQuestion);
}
/**
* Returns the specified linked question.
*
* @param {string} linkedQuestionId - the id of the linkedQuestion
*/
getLinkedQuestion(linkedQuestionId) {
return this.linkedQuestions.find(({_id}) => _id.equals(linkedQuestionId));
}
/**
* Returns the specified question.
*
* @param {number} index - the index of the question
*/
getQuestion(index) {
return this.questions[index];
}
/**
* Returns the next linked question of the specified question if any.
*
* @param {Object} question - the question
*/
getNextQuestion(question) {
if (question) {
const currentQuestion = this.questions.find(({_id}) => _id.equals(question._id));
if (currentQuestion && currentQuestion.linkedQuestion) {
return this.getLinkedQuestion(currentQuestion.linkedQuestion._id);
}
}
}
/**
* Returns the number of questions (excluding linked questions)
*/
getQuestionCount() {
return this.questions.length;
}
/**
* Returns all questions with only three fields:
*
* - id
* - theme
* - points
*/
getFilteredQuestions() {
return this.questions.map(question => QuestionUtils.getQuestion(question));
}
/**
* Returns the maximum number of points that a team can reach.
*/
getMaxPoints() {
return this.questions.concat(this.linkedQuestions).reduce((sum, question) => sum + question.points, 0) + Math.ceil(this.questions.length / 2);
}
/**
* Adds an administrator.
*
* @param {string} socketId - the id of the socket
*/
addAdmin(socketId) {
this.admins[socketId] = true;
}
/**
* Adds a team.
*
* @param {string} socketId - the id of the socket
* @param {string} team - the team
*/
addTeam(socketId, team) {
this.teams[socketId] = team;
}
/**
* Removes a team.
*
* @param {string} socketId - the id of the socket
*/
removeTeam(socketId) {
delete this.teams[socketId];
}
/**
* Removes an administrator.
*
* @param {string} socketId - the id of the socket
*/
removeAdmin(socketId) {
delete this.admins[socketId];
}
/**
* Returns all connected teams.
*/
getTeams() {
return Object.values(this.teams).sort();
}
/**
* Returns all disconnected teams.
*/
getAvailableTeams() {
const teams = this.getTeams();
return Array.from({ length: MAX_TEAMS }, (_,i) => i + 1).filter(team => !teams.includes(team));
}
/**
* Returns the id of the game.
*/
getRoom() {
return this.room;
}
/**
* Returns true if there is no active questions and no users connected.
*/
canDiscard() {
return this.activeQuestion === null
&& !Object.keys(this.teams).length
&& !Object.keys(this.admins).length;
}
}
module.exports = DataManager;