-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoreManager.js
More file actions
318 lines (272 loc) · 7.63 KB
/
Copy pathScoreManager.js
File metadata and controls
318 lines (272 loc) · 7.63 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* This object manages the scoring of the teams of students.
*/
const QuestionUtils = require('./QuestionUtils');
const Impl = require('./impl');
class ScoreManager {
constructor(dataManager) {
this.dataManager = dataManager;
this.scores = {};
this.turn = 0;
this.date = Date.now();
this.outOfTime = false;
}
/**
* Adds a team.
*
* @param {string} team - the team
*/
addTeam(team) {
if (!this.scores[team]) {
this.scores[team] = {};
}
}
/**
* Returns the score of the specified team.
*
* @param {string} team - the team
*/
getTeam(team) {
return {
team,
score: Object.values(this.scores[team]).reduce((sum, {score}) => sum + score, 0)
}
}
/**
* Returns the team whose turn it is.
*/
getTurn() {
const teams = this.dataManager.getTeams();
return teams.length ? teams[this.turn % teams.length] : null;
}
/**
* Returns the active question and all its linked questions.
*/
getQuestions() {
const questions = [];
let currentQuestion = this.dataManager.getActiveQuestion();
while (currentQuestion && !questions.includes(currentQuestion)) {
questions.push(currentQuestion);
if (currentQuestion.linkedQuestion) {
currentQuestion = this.dataManager.getLinkedQuestion(currentQuestion.linkedQuestion._id);
}
}
return questions;
}
/**
* Returns the active question for the specified team.
* If the team has already answered the active question, then it returns its linked question if any.
* This process continues if there are multiple linked questions.
*
* @param {string} team - the team
*/
getActiveQuestion(team) {
const unansweredQuestions = this.getQuestions().filter(({_id}) => !this.scores[team][_id]);
if (unansweredQuestions.length) {
return unansweredQuestions[0];
}
}
/**
* Returns the active question for the specified team by a call to getActiveQuestion(team).
* After that, it filters out the answer fields and returns it.
*
* @param {string} team - the team
*/
getFilteredActiveQuestion(team) {
const teamActiveQuestion = this.getActiveQuestion(team);
if (teamActiveQuestion) {
return QuestionUtils.getActiveQuestion(teamActiveQuestion);
}
}
/**
* Returns the scores of all currently connected teams.
*/
getTeams() {
return this.dataManager.getTeams().map(team => this.getTeam(team));
}
/**
* Returns the team which has the highest score.
*/
getLeadingTeams() {
return Object.keys(this.scores).map(team => this.getTeam(team)).reduce((prev, current) => {
if (prev.score < current.score) {
prev = { teams: [current.team], score: current.score };
} else if (prev.score === current.score) {
prev.teams.push(current.team);
}
return prev;
}, { teams: [], score: 0 }).teams;
}
/**
* Checks the answer of the team to their active question and updates their score accordingly.
*
* @param {string} team - the team
* @param {Object} studentQuestion - the answer from the team
* @param {Object} originalQuestion - the original question
* @param {boolean} linked - true if the question is a linked question, false otherwise
*/
updateScore(team, studentQuestion, originalQuestion, linked) {
if (!this.outOfTime) {
const originalQuestionId = originalQuestion._id.toString();
const alreadyAnswered = this.scores[team][originalQuestionId];
if (!alreadyAnswered) {
const teams = this.dataManager.getTeams();
let score = QuestionUtils.correctQuestion(studentQuestion, originalQuestion) ? originalQuestion.points : 0;
const correct = score === originalQuestion.points;
if (!linked && team === this.getTurn()) {
const [answers, correctAnswers] = Object.values(this.scores).reduce((acc, score) => {
let [ answers, correctAnswers ] = acc;
if (score[originalQuestionId]) {
answers++;
if (score[originalQuestionId].correct) {
correctAnswers++;
}
}
return [ answers, correctAnswers ];
}, [0, 0]);
if (correct && correctAnswers === 0) {
score++;
}
if (correct && answers === teams.length - 1) {
score--;
}
}
this.scores[team][originalQuestionId] = {
theme: originalQuestion.theme,
score,
correct
};
this.fireScoreChange();
const feedback = QuestionUtils.getFeedback(studentQuestion, originalQuestion);
feedback.positive = !!correct;
this.fireFeedback(feedback, team);
if (originalQuestion.linkedQuestion) {
const linkedQuestion = this.dataManager.getLinkedQuestion(originalQuestion.linkedQuestion._id);
if (linkedQuestion) {
this.fireLinkedQuestionStarted(team, QuestionUtils.getActiveQuestion(linkedQuestion));
}
}
}
return alreadyAnswered;
}
}
/**
* Checks the answer of the team to the specified question.
*
* @param {string} team - the team
* @param {Object} question - the question that the team answered
*/
correct(team, question) {
const activeQuestion = this.dataManager.getActiveQuestion();
const teamActiveQuestion = this.getActiveQuestion(team);
if (activeQuestion && teamActiveQuestion) {
this.updateScore(team, question, teamActiveQuestion, activeQuestion !== teamActiveQuestion);
}
}
/**
* Returns the number of teams which asked the active questions.
*/
teamsAnswered() {
const activeQuestion = this.dataManager.getActiveQuestion();
if (activeQuestion) {
const activeQuestionId = activeQuestion._id.toString();
return Object.values(this.scores).reduce((acc, score) => {
return acc + (score[activeQuestionId] ? 1 : 0);
}, 0);
} else {
return 0;
}
}
/**
* Removes the points gained by the teams to the active question as a result of the question being canceled.
*/
cancelQuestion() {
this.getQuestions().forEach(question => {
const questionId = question._id.toString();
Object.values(this.scores).forEach(score => delete score[questionId]);
});
this.fireScoreChange();
}
/**
* Updates the team whose turn it is.
*/
updateTurn() {
this.turn++;
}
/**
* Sets the time as elapsed.
*/
timeOut() {
this.outOfTime = true;
}
/**
* Reset the time.
*/
endQuestion() {
this.outOfTime = false;
}
/**
* Saves the session.
*
* @param {string} _id - the id of the session
* @param {string} idGame - the id of the game
*/
saveSession(_id, idGame) {
Impl.saveSession({ _id, idGame, scores: this.scores, date: this.date });
}
/**
* Returns true if no teams answered any question, false otherwise.
*/
canDiscard() {
return Object.values(this.scores).every(score => !Object.keys(score).length);
}
/**
* Sets a listener to the score change event.
*
* @param {Function} callback - the listener
*/
onScoreChange(callback) {
this.onScoreChangeHandler = callback;
}
/**
* Sets a listener to the feedback event.
*
* @param {Function} callback - the listener
*/
onFeedback(callback) {
this.onFeedbackHandler = callback;
}
/**
* Sets a listener to the linked question started event.
*
* @param {Function} callback - the listener
*/
onLinkedQuestionStarted(callback) {
this.onLinkedQuestionStarted = callback;
}
/**
* Fires the score change event.
*/
fireScoreChange() {
this.onScoreChangeHandler();
}
/**
* Fires the feedback event.
*
* @param {Object} feedback - the feedback to respond to the team
* @param {string} team - the team
*/
fireFeedback(feedback, team) {
this.onFeedbackHandler(feedback, team);
}
/**
* Fires the linked question started event.
*
* @param {string} team - the team
* @param {Object} linkedQuestion - the linked question which has started
*/
fireLinkedQuestionStarted(team, linkedQuestion) {
this.onLinkedQuestionStarted(team, linkedQuestion);
}
}
module.exports = ScoreManager;