From 6c1a308a03a027cc1c180aa04ef95939b5770935 Mon Sep 17 00:00:00 2001 From: "Glitch (hello-express)" Date: Fri, 10 Apr 2020 08:11:41 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=B6=F0=9F=8E=A5=20Checkpoint=20./views?= =?UTF-8?q?/room.handlebars:530995/80=20./views/audience.handlebars:530995?= =?UTF-8?q?/371=20./server.js:530995/328=20./room.js:530995/345=20./public?= =?UTF-8?q?/style.css:530995/79?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/style.css | 1 + room.js | 8 +++++++- server.js | 6 +++++- views/audience.handlebars | 10 ++++++---- views/room.handlebars | 2 ++ 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/public/style.css b/public/style.css index 0bb0444..eda8662 100644 --- a/public/style.css +++ b/public/style.css @@ -59,6 +59,7 @@ footer { max-height: 500px; max-width: 500px; margin: 0 auto; + margin-left: -8px; border-radius: 500px; background: red; border: 4px solid black; diff --git a/room.js b/room.js index 88590f1..6c501af 100644 --- a/room.js +++ b/room.js @@ -37,6 +37,12 @@ function addParticipant(roomId, participantId, participantName) { rooms[roomId] = room; } +function removeParticipant(roomId, participantId) { + let room = getOrCreateRoom(roomId); + room.participants = room.participants.filter(p => p.participantId !== participantId); + room.audience.forEach(ws => ws.send(JSON.stringify({type: "new_participant"}))); +} + function addParticipantWS(roomId, participantId, ws) { let room = getOrCreateRoom(roomId); room.participants.find(p => p.participantId === participantId).ws = ws; @@ -69,4 +75,4 @@ function buzz(roomId, participant) { }); } -module.exports = {getOrCreateRoom, addParticipant, addParticipantWS, addAudienceWS, buzz} \ No newline at end of file +module.exports = {getOrCreateRoom, addParticipant, addParticipantWS, addAudienceWS, buzz, removeParticipant} \ No newline at end of file diff --git a/server.js b/server.js index fd964fa..5ee5b20 100644 --- a/server.js +++ b/server.js @@ -76,7 +76,6 @@ wss.on('connection', (ws, req) => { roomId = roomId.replace("/audience", ""); rooms.addAudienceWS(roomId, ws); - } else { ws.on('message', (message) => { message = JSON.parse(message); @@ -89,5 +88,10 @@ wss.on('connection', (ws, req) => { rooms.buzz(roomId, participant); } }); + + + ws.on('close', () => { + rooms.removeParticipant(roomId, participant.participantId); + }); } }); \ No newline at end of file diff --git a/views/audience.handlebars b/views/audience.handlebars index 9ad31bd..db16f1a 100644 --- a/views/audience.handlebars +++ b/views/audience.handlebars @@ -35,6 +35,7 @@ socket.onmessage = function(event) { let msg = JSON.parse(event.data); if (msg.type === "buzz") { + beep(); let buzzed = document.getElementById(`p-${msg.participant.participantId}`); buzzed.classList.add('buzzed'); setTimeout(() => buzzed.classList.remove('buzzed'), 5000) @@ -42,10 +43,11 @@ window.location = window.location; } }; - - socket.onerror = function(error) { - alert(`[error] ${error.message}`); - }; + + function beep() { + window.navigator.vibrate(500); + new Audio("https://cdn.glitch.com/81aebabf-079d-4504-b844-d90d643962c4%2FGame-show-buzzer-sound-effect.mp3?v=1586451448374").play(); + } diff --git a/views/room.handlebars b/views/room.handlebars index 1b6b224..f2c79c5 100644 --- a/views/room.handlebars +++ b/views/room.handlebars @@ -62,6 +62,7 @@ }); function showMessage(msg) { + window.navigator.vibrate(500); document.getElementById('buzzer').disabled = true; let mb = document.getElementById('messageBox'); mb.innerHTML = msg; @@ -73,6 +74,7 @@ } function beep() { + window.navigator.vibrate(500); new Audio("https://cdn.glitch.com/81aebabf-079d-4504-b844-d90d643962c4%2FGame-show-buzzer-sound-effect.mp3?v=1586451448374").play(); }