-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestionPool.js
More file actions
183 lines (159 loc) · 4.41 KB
/
QuestionPool.js
File metadata and controls
183 lines (159 loc) · 4.41 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
/**
* This object manages the questions.
* It provides features such as revealing, starting and ending questions.
*/
const MAX_SELECTED_QUESTIONS = 2;
class QuestionPool {
constructor(dataManager) {
this.dataManager = dataManager;
this.unselectedQuestions = Array.from({ length: dataManager.getQuestionCount() }, (_,i) => i);
this.selectedQuestions = [];
this.pastQuestions = [];
}
/**
* Returns the questions which are revealed or done.
*/
getVisibleQuestions() {
return this.pastQuestions.concat(this.selectedQuestions);
}
/**
* Reveals the specified question.
* If it is already revealed, then it starts the question.
*
* @param {number} questionIndex - the index of the selected question
*/
selectQuestion(questionIndex) {
if (!this.dataManager.getActiveQuestion()) {
const unselectedQuestionIndex = this.unselectedQuestions.indexOf(questionIndex);
if (unselectedQuestionIndex != -1) {
if (this.selectedQuestions.length < MAX_SELECTED_QUESTIONS) {
this.selectedQuestions.push(...this.unselectedQuestions.splice(unselectedQuestionIndex, 1));
this.fireSelectionChanged({ selectedQuestions: [questionIndex], unselectedQuestions: [] });
}
} else if (this.selectedQuestions.includes(questionIndex)) {
this.fireQuestionStarted(questionIndex);
}
}
}
/**
* Stops the active question.
*/
stopQuestion() {
const activeQuestionIndex = this.dataManager.getActiveQuestionIndex();
if (activeQuestionIndex != -1) {
this.pastQuestions.push(...this.selectedQuestions.splice(this.selectedQuestions.indexOf(activeQuestionIndex), 1));
const unselectedQuestions = this.selectedQuestions;
this.unselectedQuestions.push(...unselectedQuestions);
this.selectedQuestions = [];
this.fireSelectionChanged({ selectedQuestions: [], unselectedQuestions });
this.fireQuestionDone();
this.fireQuestionEnded();
}
}
/**
* Cancels the active question.
*/
cancelQuestion() {
if (this.dataManager.getActiveQuestion()) {
this.fireQuestionCanceled();
this.fireQuestionEnded();
}
}
/**
* Hides the question which was last revealed.
*/
cancelLastRevealedCard() {
if (this.selectedQuestions.length) {
const unselectedQuestion = this.selectedQuestions.pop();
this.unselectedQuestions.push(unselectedQuestion);
this.fireSelectionChanged({ selectedQuestions: [], unselectedQuestions: [unselectedQuestion] });
}
}
/**
* Returns true if all questions have been answered, false otherwise.
*/
allQuestionsAnswered() {
return !this.selectedQuestions.length && !this.unselectedQuestions.length;
}
/**
* Returns true if no questions are revealed or done.
*/
canDiscard() {
return !this.selectedQuestions.length && !this.pastQuestions.length;
}
/**
* Sets a listener to the selection changed event.
*
* @param {Function} callback - the listener
*/
onSelectionChanged(callback) {
this.onSelectionChangedHandler = callback;
}
/**
* Sets a listener to the question started event.
*
* @param {Function} callback - the listener
*/
onQuestionStarted(callback) {
this.onQuestionStartedHandler = callback;
}
/**
* Sets a listener to the question canceled event.
*
* @param {Function} callback - the listener
*/
onQuestionCanceled(callback) {
this.onQuestionCanceledHandler = callback;
}
/**
* Sets a listener to the question done event.
*
* @param {Function} callback - the listener
*/
onQuestionDone(callback) {
this.onQuestionDoneHandler = callback;
}
/**
* Sets a listener to the question ended event.
*
* @param {Function} callback - the listener
*/
onQuestionEnded(callback) {
this.onQuestionEndedHandler = callback;
}
/**
* Fires the selection changed event.
*
* @param {Object} questions - an object which contains the newly selected and unselected questions
*/
fireSelectionChanged(questions) {
this.onSelectionChangedHandler(questions);
}
/**
* Fires the question started event.
*
* @param {Object} startedQuestion - the started question
*/
fireQuestionStarted(startedQuestion) {
this.onQuestionStartedHandler(startedQuestion);
}
/**
* Fires the question canceled event.
*/
fireQuestionCanceled() {
this.onQuestionCanceledHandler();
}
/**
* Fires the question done event.
*/
fireQuestionDone() {
this.onQuestionDoneHandler();
}
/**
* Fires the question ended event.
*/
fireQuestionEnded() {
this.onQuestionEndedHandler();
}
}
module.exports = QuestionPool;