forked from etian6795/QuesGen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
200 lines (167 loc) · 6.18 KB
/
server.js
File metadata and controls
200 lines (167 loc) · 6.18 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
let getQuestion;
let checkAnswer;
(async () => {
const module = await import('./checker.mjs');
getQuestion = module.getQuestion;
})();
(async () => {
const module = await import('./checker.mjs');
checkAnswer = module.checkAnswer;
})();
let getNotesQuestion;
(async () => {
const module = await import('./checker.mjs');
getNotesQuestion = module.getNotesQuestion;
})();
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const path = require('path');
const fs = require('fs');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
let dataset = JSON.parse(fs.readFileSync('questions.json'));
app.use(express.static('public'));
const rooms = {};
io.on('connection', (socket) => {
console.log('User Connected');
socket.on('getQuestion', async (data) => {
let q = await getQuestion(data.topic);
socket.emit('returnQuestion', q);
});
socket.on('checkAnswer', async (data) => {
let res = await checkAnswer(data.question, data.answer);
io.in(data.room).emit('pulsate', {isCorrect: res, id: data.id});
socket.emit('returnAnswer', res);
if(res) {
io.in(data.room).emit('deselectPlayer', data.id);
rooms[data.room].questionsSet.add(data.question);
}
});
socket.on("isValidCode", (code) => {
socket.emit("validCodeVerdict", code in rooms, code);
});
socket.on('getNewRoomCode', () => {
roomCode = '';
while(roomCode.length == 0 || roomCode in rooms) {
roomCode = generateCode();
}
rooms[roomCode] = {
roomSize: 0,
playerOrder: [],
started: false,
topic: '',
turn: 0,
questionsSet: new Set()
};
socket.emit('returnRoomCode', roomCode);
});
socket.on('joinRoom', (data) => {
socket.join(data.room);
console.log(socket.id);
rooms[data.room].roomSize++;
rooms[data.room].playerOrder.push({name: data.name, id: socket.id, lives: 3});
console.log(rooms[data.room].playerOrder);
socket.broadcast.to(data.room).emit('addNewPlayer', {name: data.name, id: socket.id});
});
socket.on('getOthers', (room) => {
socket.emit('returnOthers', rooms[room].playerOrder);
});
socket.on('startGame', (data) => {
let room = rooms[data.room];
if(room.started) return;
room.started = true;
room.topic = data.topic;
if (room.topic === "pdf") {
dataset = {"pdf": data.dataset};
}
nextTurn(data.room, data.topic, room.playerOrder[room.turn].id)
});
socket.on('loseLife', (data) => {
io.in(data.room).emit('deselectPlayer', data.id);
io.in(data.room).emit('loseLife', {id: data.id, turn: rooms[data.room].turn});
rooms[data.room].playerOrder[rooms[data.room].turn].lives--;
});
socket.on('nextTurn', (data) => {
let room = rooms[data.room];
let nextturn = (room.turn+1)%room.playerOrder.length;
while(room.playerOrder[nextturn].lives == 0) {
nextturn = (nextturn+1)%room.playerOrder.length;
}
let cnt = 0;
for(let i = 0; i < room.roomSize; i++) {
if(room.playerOrder[i].lives>0) cnt++;
}
console.log(nextturn);
if(cnt==1) {
io.in(data.room).emit('gameOver', nextturn);
} else {
console.log(room.turn);
console.log(nextturn);
console.log(room.playerOrder);
room.turn = nextturn;
nextTurn(data.room, room.topic, room.playerOrder[nextturn].id);
}
});
socket.on('updateOthersText', (data) => {
socket.to(data.room).emit('updateActivePlayerText', {text: data.text, id: socket.id});
});
let pdfQuestions = [];
socket.on('loadFile', async (pagePromises, textContent) => {
console.log(textContent);
let questionsText = await getNotesQuestion(textContent);
console.log(questionsText);
pdfQuestions = parseQuestions(questionsText);
console.log(pdfQuestions);
socket.emit('loadFile', pdfQuestions);
});
});
function parseQuestions(questionsText) {
let splitQuestions = questionsText.split("\n");
for (let i = 0; i < splitQuestions.length; i++) {
splitQuestions[i] = {"question": splitQuestions[i].split(". ")[1]};
}
return splitQuestions;
}
function nextTurn(room, topic, id) {
let question = getRandomQuestion(topic, room);
io.in(room).emit('returnQuestion', question);
io.in(room).emit('setActivePlayer', id);
}
function getRandomQuestion(topic, room) {
if (topic === "pdf") {
console.log(dataset[topic][Math.floor(Math.random() * dataset[topic].length)]);
let q = dataset[topic][Math.floor(Math.random() * dataset[topic].length)].question;
while(rooms[room].questionsSet.has(q)) {
q = dataset[topic][Math.floor(Math.random() * dataset[topic].length)].question;
}
if(rooms[room].questionsSet.size > 10) {
rooms[room].questionsSet = new Set();
}
return q;
}
let q = dataset.topics[topic][Math.floor(Math.random() * dataset.topics[topic].length)].question;
while(rooms[room].questionsSet.has(q)) {
q = dataset.topics[topic][Math.floor(Math.random() * dataset.topics[topic].length)].question;
}
if(rooms[room].questionsSet.size > 10) {
rooms[room].questionsSet = new Set();
}
return q;
}
function generateCode() {
code = '';
for(let i = 0; i < 5; i++) {
code += String.fromCharCode(97+Math.floor(Math.random() * 26));
}
return code;
}
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'pages/index.html'));
});
app.get('/room', (req, res) => {
res.sendFile(path.join(__dirname, 'pages/room.html'));
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));