Skip to content
Snippets Groups Projects
Commit cc03d87c authored by enr27's avatar enr27
Browse files

Replace server.js

parent db99345a
Branches
No related tags found
No related merge requests found
...@@ -69,18 +69,18 @@ socket.on('joinRoom', ({ roomCode, playerName, playerId }) => { ...@@ -69,18 +69,18 @@ socket.on('joinRoom', ({ roomCode, playerName, playerId }) => {
socket.on('submitAnswer', ({ roomCode, answer }) => { socket.on('submitAnswer', ({ roomCode, answer }) => {
const room = rooms[roomCode]; const room = rooms[roomCode];
if (!room || !room.questions || room.questions.length === 0) { if (!room || !room.currentQuestion) {
socket.emit('error', 'No questions available'); socket.emit('error', 'No questions available');
return; return;
} }
const currentQuestion = room.questions[0]; const correct = room.currentQuestion.correctAnswer === answer;
const correct = currentQuestion.correctAnswer === answer;
if (correct) { if (correct) {
room.players[socket.id].score += 1; room.players[socket.id].score += 1;
socket.emit('scoreUpdate', room.players[socket.id].score);
} }
socket.emit('answerResult', { correct, correctAnswer: currentQuestion.correctAnswer }); socket.emit('answerResult', { correct, correctAnswer: room.currentQuestion.correctAnswer });
}); });
socket.on('nextQuestion', (roomCode) => { socket.on('nextQuestion', (roomCode) => {
...@@ -99,16 +99,18 @@ socket.on('joinRoom', ({ roomCode, playerName, playerId }) => { ...@@ -99,16 +99,18 @@ socket.on('joinRoom', ({ roomCode, playerName, playerId }) => {
const room = rooms[roomCode]; const room = rooms[roomCode];
if (!room) return; if (!room) return;
if (room.questions.length > 0) {
const currentQuestion = room.questions.shift();
io.to(roomCode).emit('newQuestion', currentQuestion);
if (room.timer) { if (room.timer) {
clearInterval(room.timer); clearInterval(room.timer);
room.timer = null; room.timer = null;
} }
room.timeRemaining = 60; if (room.questions.length > 0) {
const currentQuestion = room.questions.shift();
room.currentQuestion = currentQuestion;
io.to(roomCode).emit('newQuestion', currentQuestion);
room.timeRemaining = 30;
room.timer = setInterval(() => { room.timer = setInterval(() => {
room.timeRemaining -= 1; room.timeRemaining -= 1;
io.to(roomCode).emit('timerUpdate', room.timeRemaining); io.to(roomCode).emit('timerUpdate', room.timeRemaining);
...@@ -116,7 +118,7 @@ socket.on('joinRoom', ({ roomCode, playerName, playerId }) => { ...@@ -116,7 +118,7 @@ socket.on('joinRoom', ({ roomCode, playerName, playerId }) => {
if (room.timeRemaining <= 0) { if (room.timeRemaining <= 0) {
clearInterval(room.timer); clearInterval(room.timer);
room.timer = null; room.timer = null;
sendNextQuestion(roomCode); io.to(roomCode).emit('timeUp', { correctAnswer: room.currentQuestion.correctAnswer });
} }
}, 1000); }, 1000);
} else { } else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment