forked from ably-labs/realtime-quiz-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz-room-server.js
241 lines (216 loc) · 6.46 KB
/
quiz-room-server.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
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
const randomQuestions = require('./quiz-default-questions');
const { parentPort, workerData } = require('worker_threads');
const Ably = require('ably/promises');
const START_TIMER_SEC = 5;
const QUESTION_TIMER_SEC = 30;
const ABLY_API_KEY = process.env.ABLY_API_KEY;
const globalPlayersState = {};
const playerChannels = {};
let didQuizStart = false;
let totalPlayers = 0;
const quizRoomChName = `${workerData.hostRoomCode}:primary`;
const hostAdminChName = `${workerData.hostRoomCode}:host`;
let hostAdminCh;
const roomCode = workerData.hostRoomCode;
const hostClientId = workerData.hostClientId;
let quizRoomChannel;
let numPlayersAnswered = 0;
let customQuestions = [];
let skipTimer = false;
console.log('this is the worker thread');
console.log('room code is' + workerData.hostRoomCode);
let questions = [];
const realtime = Ably.Realtime({
key: ABLY_API_KEY,
echoMessages: false
});
realtime.connection.once('connected', () => {
hostAdminCh = realtime.channels.get(hostAdminChName);
quizRoomChannel = realtime.channels.get(quizRoomChName);
subscribeToHostEvents();
quizRoomChannel.presence.subscribe('enter', handleNewPlayerEntered);
quizRoomChannel.presence.subscribe('leave', handleExistingPlayerLeft);
quizRoomChannel.publish('thread-ready', { start: true });
});
function handleNewPlayerEntered(player) {
console.log(player.clientId + 'player entered quiz room');
const newPlayerId = player.clientId;
totalPlayers++;
parentPort.postMessage({
roomCode: roomCode,
totalPlayers: totalPlayers,
didQuizStart: didQuizStart
});
let newPlayerState = {
id: newPlayerId,
nickname: player.data.nickname,
avatarColor: player.data.avatarColor,
isHost: player.data.isHost,
score: 0
};
if (player.data.isHost) {
let quizType = player.data.quizType;
quizType === 'CustomQuiz'
? (questions = customQuestions)
: (questions = randomQuestions);
} else {
playerChannels[newPlayerId] = realtime.channels.get(
`${roomCode}:player-ch-${player.clientId}`
);
subscribeToPlayerChannel(playerChannels[newPlayerId], newPlayerId);
}
globalPlayersState[newPlayerId] = newPlayerState;
quizRoomChannel.publish('new-player', {
newPlayerState
});
}
function handleExistingPlayerLeft(player) {
console.log('leaving player', player.clientId);
const leavingPlayerId = player.clientId;
totalPlayers--;
parentPort.postMessage({
roomCode: roomCode,
totalPlayers: totalPlayers
});
delete globalPlayersState[leavingPlayerId];
if (leavingPlayerId === hostClientId) {
quizRoomChannel.publish('host-left', {
endQuiz: true
});
forceQuizEnd();
}
}
async function publishTimer(event, countDownSec) {
while (countDownSec > 0) {
quizRoomChannel.publish(event, {
countDownSec: countDownSec
});
await new Promise((resolve) => setTimeout(resolve, 1000));
countDownSec -= 1;
if (event === 'question-timer' && skipTimer) break;
}
}
function subscribeToHostEvents() {
hostAdminCh.subscribe('start-quiz', async () => {
didQuizStart = true;
parentPort.postMessage({
roomCode,
didQuizStart
});
await publishTimer('start-quiz-timer', START_TIMER_SEC);
publishQuestion(0, false);
});
hostAdminCh.subscribe('quiz-questions', (msg) => {
for (let i = 0; i < msg.data.questions.length; i++) {
let item = msg.data.questions[i];
let newQuestionObject = {
questionNumber: parseInt(item['question number']),
showImg: item['image link'].substr(0, 4) === 'http' ? true : false,
question: item.question,
choices: [
item['option 1'],
item['option 2'],
item['option 3'],
item['option 4']
],
correct: parseInt(item['correct answer option number']) - 1,
pic: item['image link']
};
customQuestions.push(newQuestionObject);
}
});
hostAdminCh.subscribe('next-question', (msg) => {
let prevQIndex = msg.data.prevQIndex;
let newQIndex = prevQIndex + 1;
let lastQIndex = questions.length - 1;
if (newQIndex < lastQIndex) {
publishQuestion(newQIndex, false);
} else if (newQIndex === lastQIndex) {
publishQuestion(newQIndex, true);
}
});
hostAdminCh.subscribe('end-quiz-now', () => {
forceQuizEnd();
});
}
function forceQuizEnd() {
quizRoomChannel.publish('quiz-ending', {
quizEnding: true
});
killWorkerThread();
}
async function publishQuestion(qIndex, isLast) {
numPlayersAnswered = 0;
await quizRoomChannel.publish('new-question', {
numAnswered: 0,
numPlaying: totalPlayers - 1,
questionNumber: qIndex + 1,
question: questions[qIndex].question,
choices: questions[qIndex].choices,
isLastQuestion: isLast,
showImg: questions[qIndex].showImg,
imgLink: questions[qIndex].pic
});
skipTimer = false;
await publishTimer('question-timer', QUESTION_TIMER_SEC);
await quizRoomChannel.publish('correct-answer', {
questionNumber: qIndex + 1,
correctAnswerIndex: questions[qIndex].correct
});
computeTopScorers();
if (isLast) {
killWorkerThread();
}
}
function computeTopScorers() {
let leaderboard = new Array();
for (let item in globalPlayersState) {
if (item != hostClientId) {
leaderboard.push({
nickname: globalPlayersState[item].nickname,
score: globalPlayersState[item].score
});
}
}
leaderboard.sort((a, b) => b.score - a.score);
quizRoomChannel.publish('full-leaderboard', {
leaderboard: leaderboard
});
}
function subscribeToPlayerChannel(playerChannel, playerId) {
playerChannel.subscribe('player-answer', (msg) => {
numPlayersAnswered++;
if (
questions[msg.data.questionIndex].correct === msg.data.playerAnswerIndex
) {
globalPlayersState[playerId].score += 5;
}
updateLiveStatsForHost(numPlayersAnswered, totalPlayers - 1);
});
updateLiveStatsForHost(numPlayersAnswered, totalPlayers - 1);
}
function updateLiveStatsForHost(numAnswered, numPlaying) {
quizRoomChannel.publish('live-stats-update', {
numAnswered: numAnswered,
numPlaying: numPlaying
});
if (numAnswered === numPlaying) {
skipTimer = true;
}
}
function killWorkerThread() {
console.log('killing thread');
for (const item in playerChannels) {
if (playerChannels[item]) {
playerChannels[item].detach();
}
}
hostAdminCh.detach();
quizRoomChannel.detach();
parentPort.postMessage({
killWorker: true,
roomCode: roomCode,
totalPlayers: totalPlayers
});
process.exit(0);
}