Skip to content

Commit

Permalink
fix #46
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Jul 23, 2022
1 parent 3f7ce21 commit e43c4e4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
13 changes: 7 additions & 6 deletions client/multiplayer/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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]);
});
});
Expand Down
40 changes: 28 additions & 12 deletions rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e43c4e4

Please sign in to comment.