Skip to content
Snippets Groups Projects
Commit 62e035b9 authored by ea654's avatar ea654
Browse files

Replace server.js with code to deal with api

parent ffe407d9
Branches
No related tags found
No related merge requests found
...@@ -15,73 +15,129 @@ app.use(express.static('public')); ...@@ -15,73 +15,129 @@ app.use(express.static('public'));
io.on('connection', (socket) => { io.on('connection', (socket) => {
console.log('A user connected:', socket.id); console.log('A user connected:', socket.id);
socket.on('createRoom', () => { // Create a room
socket.on('createRoom', (playerName) => {
const roomCode = Math.random().toString(36).substring(2, 7).toUpperCase(); const roomCode = Math.random().toString(36).substring(2, 7).toUpperCase();
rooms[roomCode] = { players: [], questions: [] }; rooms[roomCode] = {
players: {},
settings: null,
questions: []
};
rooms[roomCode].players[socket.id] = { name: playerName, score: 0 };
socket.join(roomCode); socket.join(roomCode);
socket.emit('roomCreated', roomCode); socket.emit('roomCreated', roomCode);
console.log(`Room created with code ${roomCode}`); console.log(`Room created with code ${roomCode} by ${playerName}`);
}); });
socket.on('joinRoom', (roomCode) => { // Join a room
if (rooms[roomCode]) { socket.on('joinRoom', ({ roomCode, playerName, playerId }) => {
const room = rooms[roomCode];
if (room) {
let player = Object.values(room.players).find(p => p.id === playerId);
if (player) {
player.socketId = socket.id;
console.log(`${playerName} rejoined room ${roomCode}`);
} else {
room.players[socket.id] = { id: playerId, name: playerName, score: 0 };
console.log(`${playerName} joined room ${roomCode}`);
}
socket.join(roomCode); socket.join(roomCode);
socket.emit('roomJoined'); socket.emit('roomJoined');
console.log(`User ${socket.id} joined room ${roomCode}`);
} else { } else {
socket.emit('error', 'Room not found'); socket.emit('error', 'Room not found');
} }
}); });
socket.on('startGame', ({ roomCode, category, difficulty }) => {
fetchQuestions(category, difficulty).then((questions) => {
socket.on('gameSettings', ({ roomCode, category, difficulty, numQuestions }) => {
if (rooms[roomCode]) {
rooms[roomCode].settings = { category, difficulty, numQuestions };
fetchQuestions(category, difficulty, numQuestions).then((questions) => {
if (!questions || questions.length === 0) { if (!questions || questions.length === 0) {
socket.emit('error', 'Failed to load questions'); io.to(roomCode).emit('error', 'Failed to load questions');
return; return;
} }
rooms[roomCode].questions = questions; rooms[roomCode].questions = questions;
io.in(roomCode).emit('newQuestion', questions[0]); io.to(roomCode).emit('startGameWithSettings', { category, difficulty, numQuestions });
sendNextQuestion(roomCode);
}); });
}
}); });
socket.on('submitAnswer', (data) => { socket.on('submitAnswer', ({ roomCode, answer }) => {
const room = rooms[data.roomCode]; const room = rooms[roomCode];
if (!room || !room.questions || room.questions.length === 0) { if (!room || !room.questions || room.questions.length === 0) {
socket.emit('error', 'No questions available'); socket.emit('error', 'No questions available');
return; return;
} }
const question = room.questions[0]; const currentQuestion = room.questions[0];
const correct = question.correctAnswer === data.answer; const correct = currentQuestion.correctAnswer === answer;
socket.emit('answerResult', { correct, correctAnswer: question.correctAnswer }); if (correct) {
room.players[socket.id].score += 1;
}
socket.emit('answerResult', { correct, correctAnswer: currentQuestion.correctAnswer });
}); });
socket.on('nextQuestion', (roomCode) => { socket.on('nextQuestion', (roomCode) => {
const room = rooms[roomCode]; sendNextQuestion(roomCode);
});
function sendNextQuestion(roomCode) {
const room = rooms[roomCode];
if (room && room.questions.length > 0) { if (room && room.questions.length > 0) {
room.questions.shift(); room.questions.shift();
if (room.questions.length > 0) { if (room.questions.length > 0) {
io.in(roomCode).emit('newQuestion', room.questions[0]); io.to(roomCode).emit('newQuestion', room.questions[0]);
} else { } else {
io.in(roomCode).emit('gameOver'); determineWinner(roomCode);
} }
} else {
io.in(roomCode).emit('gameOver');
} }
}
function determineWinner(roomCode) {
const room = rooms[roomCode];
if (!room) return;
const players = Object.entries(room.players);
const winner = players.reduce((topPlayer, [id, player]) => {
return player.score > topPlayer.score ? player : topPlayer;
}, { name: 'No one', score: 0 });
io.to(roomCode).emit('gameOver', {
winner: { name: winner.name, score: winner.score },
scores: Object.values(room.players).map(player => ({
name: player.name,
score: player.score
}))
}); });
delete rooms[roomCode];
}
socket.on('disconnect', () => { socket.on('disconnect', () => {
console.log('User disconnected:', socket.id); console.log('User disconnected:', socket.id);
for (const roomCode of Object.keys(rooms)) {
if (rooms[roomCode].players[socket.id]) {
delete rooms[roomCode].players[socket.id];
console.log(`User ${socket.id} removed from room ${roomCode}`);
}
}
}); });
}); });
function fetchQuestions(category, difficulty) { function fetchQuestions(category, difficulty, numQuestions) {
return axios.get(`https://opentdb.com/api.php`, { return axios.get(`https://opentdb.com/api.php`, {
params: { params: {
amount: 5, amount: numQuestions,
category, category,
difficulty, difficulty,
type: 'multiple', type: 'multiple',
...@@ -89,10 +145,7 @@ function fetchQuestions(category, difficulty) { ...@@ -89,10 +145,7 @@ function fetchQuestions(category, difficulty) {
}) })
.then((response) => { .then((response) => {
return response.data.results.map((q) => { return response.data.results.map((q) => {
let options = [q.correct_answer]; let options = [q.correct_answer, ...q.incorrect_answers];
for (let incorrect of q.incorrect_answers) {
options.push(incorrect);
}
options = shuffleArray(options); options = shuffleArray(options);
return { return {
question: q.question, question: q.question,
...@@ -116,3 +169,4 @@ function shuffleArray(array) { ...@@ -116,3 +169,4 @@ function shuffleArray(array) {
} }
server.listen(PORT, () => console.log(`Server is running on http://localhost:${PORT}`)); server.listen(PORT, () => console.log(`Server is running on http://localhost:${PORT}`));
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment