diff --git a/client/multiplayer/room.js b/client/multiplayer/room.js index 95b1e8de6..1a2d8486e 100644 --- a/client/multiplayer/room.js +++ b/client/multiplayer/room.js @@ -125,7 +125,7 @@ async function processSocketMessage(data) { processBuzz(data.userId, data.username); break; case 'give-answer': - processAnswer(data.userId, data.username, data.givenAnswer, data.score); + processAnswer(data.userId, data.username, data.givenAnswer, data.score, data.celerity); break; case 'pause': logEvent(data.username, `${paused ? 'un' : ''}paused the game`); @@ -194,8 +194,7 @@ function clearStats(userId) { function createPlayerAccordionItem(player) { - console.log(player); - let { userId, username, powers = 0, tens = 0, negs = 0, tuh = 0, points = 0 } = player; + let { userId, username, powers = 0, tens = 0, negs = 0, tuh = 0, points = 0, celerity = 0 } = player; let button = document.createElement('button'); button.className = 'accordion-button collapsed'; button.type = 'button'; @@ -270,7 +269,7 @@ function createPlayerAccordionItem(player) { accordionBody.innerHTML += ' pts, celerity: '; let celeritySpan = document.createElement('span'); - celeritySpan.innerHTML = 0; + celeritySpan.innerHTML = Math.round(1000 * celerity) / 1000; celeritySpan.id = 'celerity-' + userId; celeritySpan.classList.add('stats'); celeritySpan.classList.add('stats-' + userId); @@ -319,7 +318,7 @@ function pause() { } -function processAnswer(userId, username, givenAnswer, score) { +function processAnswer(userId, username, givenAnswer, score, celerity) { logEvent(username, `${score > 0 ? '' : 'in'}correctly answered with "${givenAnswer}" for ${score} points`); document.getElementById('next').disabled = false; @@ -359,6 +358,7 @@ function processAnswer(userId, username, givenAnswer, score) { } document.getElementById('points-' + userId).innerHTML = parseInt(document.getElementById('points-' + userId).innerHTML) + score; + document.getElementById('celerity-' + userId).innerHTML = Math.round(1000 * celerity) / 1000; document.getElementById('accordion-button-points-' + userId).innerHTML = parseInt(document.getElementById('accordion-button-points-' + userId).innerHTML) + score; sortPlayerAccordion(); @@ -536,7 +536,7 @@ document.getElementById('answer-form').addEventListener('submit', function (even document.getElementById('answer-input-group').classList.add('d-none'); let characterCount = document.getElementById('question').innerHTML.length; - let celerity = 1 - characterCount / document.getElementById('question').innerHTML.length; + let celerity = 1 - characterCount / question.question.length; socket.send(JSON.stringify({ type: 'give-answer', @@ -602,6 +602,7 @@ window.onload = () => { Object.keys(data.players).forEach(player => { if (data.players[player].userId === USER_ID) return; + data.players[player].celerity = data.players[player].celerity.correct.average; createPlayerAccordionItem(data.players[player]); }); }); diff --git a/rooms.js b/rooms.js index 2eb19fffd..a9cb88757 100644 --- a/rooms.js +++ b/rooms.js @@ -43,7 +43,8 @@ async function parseMessage(roomName, message) { return message; case 'give-answer': let score = quizbowl.scoreTossup(rooms[roomName].question.answer, message.givenAnswer, message.inPower, message.endOfQuestion); - updateScore(roomName, message.userId, score); + updateScore(roomName, message.userId, score, message.celerity); + message.celerity = rooms[roomName].players[message.userId].celerity.correct.average; message.score = score; return message; case 'set-name': @@ -88,7 +89,17 @@ function createPlayer(roomName, userId, username, overrideExistingPlayer = false zeroes: 0, negs: 0, points: 0, - tuh: 0 + tuh: 0, + celerity: { + all: { + total: 0, + average: 0 + }, + correct: { + total: 0, + average: 0 + } + } }; return true; @@ -153,15 +164,7 @@ function updateUsername(roomName, userId, username) { } -function updateScore(roomName, userId, score) { - if (score > 0) { - // increase TUH for every player by 1 - for (let player in rooms[roomName].players) { - rooms[roomName].players[player].tuh++; - } - rooms[roomName].isQuestionInProgress = false; - } - +function updateScore(roomName, userId, score, celerity) { if (score > 10) { rooms[roomName].players[userId].powers++; } else if (score === 10) { @@ -172,8 +175,21 @@ function updateScore(roomName, userId, score) { rooms[roomName].players[userId].negs++; } - rooms[roomName].players[userId].points += score; + if (score > 0) { + // increase TUH for every player by 1 + for (let player in rooms[roomName].players) { + rooms[roomName].players[player].tuh++; + } + rooms[roomName].isQuestionInProgress = false; + + let numCorrect = rooms[roomName].players[userId].powers + rooms[roomName].players[userId].tens; + rooms[roomName].players[userId].celerity.correct.total += celerity; + rooms[roomName].players[userId].celerity.correct.average = rooms[roomName].players[userId].celerity.correct.total / numCorrect; + } + rooms[roomName].players[userId].points += score; + rooms[roomName].players[userId].celerity.all.total += celerity; + rooms[roomName].players[userId].celerity.all.average = rooms[roomName].players[userId].celerity.all.total / rooms[roomName].players[userId].tuh; return score; } diff --git a/server.js b/server.js index 38a8ec29b..02229be0c 100644 --- a/server.js +++ b/server.js @@ -67,6 +67,7 @@ wss.on('connection', (ws) => { message = JSON.parse(message); message.userId = ws.userId; message = await rooms.parseMessage(ws.protocol, message); + console.log(message); if (message.type === 'join' || message.type === 'change-username') { ws.username = message.username;