Compare commits
4 Commits
623b53d542
...
master
Author | SHA1 | Date | |
---|---|---|---|
a18644d40e | |||
02b7708976 | |||
45ee87b6eb | |||
bcda8f8a4f |
@@ -1,4 +1,4 @@
|
|||||||
FROM node:12
|
FROM node:14
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
@@ -179,3 +179,21 @@ figure.participant figcaption {
|
|||||||
#participants {
|
#participants {
|
||||||
margin-top: 1em;
|
margin-top: 1em;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.score {
|
||||||
|
display: inline-block;
|
||||||
|
background: green;
|
||||||
|
border-radius: 100px;
|
||||||
|
color: white;
|
||||||
|
padding: 4px 9px;
|
||||||
|
vertical-align: top;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
.participant button {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.participant.buzzed button {
|
||||||
|
display: inline;
|
||||||
|
}
|
||||||
|
40
room.js
40
room.js
@@ -108,9 +108,11 @@ function reset(roomId) {
|
|||||||
room.canBuzz = true;
|
room.canBuzz = true;
|
||||||
|
|
||||||
room.participants.forEach(p => {
|
room.participants.forEach(p => {
|
||||||
p.ws.send(JSON.stringify({
|
if (p.ws) {
|
||||||
type: "reset"
|
p.ws.send(JSON.stringify({
|
||||||
}));
|
type: "reset"
|
||||||
|
}));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,4 +131,34 @@ function closeRoom(roomId) {
|
|||||||
delete rooms[roomId];
|
delete rooms[roomId];
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {getOrCreateRoom, addParticipant, addParticipantWS, addAudienceWS, buzz, removeParticipant, reset, closeRoom}
|
function updateScore(roomId, participantId, points) {
|
||||||
|
let room = getOrCreateRoom(roomId);
|
||||||
|
let participant = room.participants.find(p => p.participantId === participantId);
|
||||||
|
if (participant) {
|
||||||
|
if (!participant.score) participant.score = 0;
|
||||||
|
|
||||||
|
participant.score += points;
|
||||||
|
|
||||||
|
if (participant.ws) {
|
||||||
|
participant.ws.send(JSON.stringify({
|
||||||
|
type: "score",
|
||||||
|
score: participant.score,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
room.audience.forEach(ws => {
|
||||||
|
ws.send(JSON.stringify({
|
||||||
|
type: "participants",
|
||||||
|
participants: room.participants.map(p => { return {
|
||||||
|
participantId: p.participantId,
|
||||||
|
character: p.character,
|
||||||
|
participantName: p.participantName,
|
||||||
|
score: p.score,
|
||||||
|
active: p.active,
|
||||||
|
}})
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {getOrCreateRoom, addParticipant, addParticipantWS, addAudienceWS, buzz, removeParticipant, reset, closeRoom, updateScore}
|
||||||
|
@@ -72,6 +72,8 @@ wss.on('connection', (ws, req) => {
|
|||||||
message = JSON.parse(message);
|
message = JSON.parse(message);
|
||||||
if (message.type === "reset") {
|
if (message.type === "reset") {
|
||||||
rooms.reset(roomId);
|
rooms.reset(roomId);
|
||||||
|
} else if (message.type === "point") {
|
||||||
|
rooms.updateScore(roomId, message.participantId, message.points);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -51,6 +51,11 @@
|
|||||||
<figure class="participant ${!p.active ? 'hide': ''}" id="p-${p.participantId}">
|
<figure class="participant ${!p.active ? 'hide': ''}" id="p-${p.participantId}">
|
||||||
<img src="${p.character}" />
|
<img src="${p.character}" />
|
||||||
<figcaption>${p.participantName}</figcaption>
|
<figcaption>${p.participantName}</figcaption>
|
||||||
|
<div>
|
||||||
|
<button onclick="updateScore('${p.participantId}', -1)">-1</button>
|
||||||
|
<div class="score">${p.score || 0}</div>
|
||||||
|
<button onclick="updateScore('${p.participantId}', 1)">+1</button>
|
||||||
|
</div>
|
||||||
</figure>
|
</figure>
|
||||||
`;
|
`;
|
||||||
});
|
});
|
||||||
@@ -60,7 +65,15 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
function beep() {
|
function beep() {
|
||||||
window.navigator.vibrate(500);
|
if (window.navigator.vibrate) window.navigator.vibrate(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateScore(participantId, points) {
|
||||||
|
socket.send(JSON.stringify({
|
||||||
|
type: "point",
|
||||||
|
participantId,
|
||||||
|
points,
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
document.getElementById('resetBuzzers').addEventListener('click', function() {
|
document.getElementById('resetBuzzers').addEventListener('click', function() {
|
||||||
|
@@ -29,8 +29,7 @@
|
|||||||
<script>
|
<script>
|
||||||
document.querySelector('form').addEventListener('submit', function(event) {
|
document.querySelector('form').addEventListener('submit', function(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
let roomId = document.getElementById('roomId').value;
|
window.location = '//' + window.location.host + '/' + document.getElementById('roomId').value;
|
||||||
window.location = `//${window.location.host}/${roomId}`;
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
@@ -14,7 +14,7 @@
|
|||||||
</header>
|
</header>
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
<h2>{{name}} <img src="{{character}}"/></h2>
|
<h2>{{name}} <img src="{{character}}"/> <div class="score">0</div></h2>
|
||||||
|
|
||||||
<button id="buzzer">
|
<button id="buzzer">
|
||||||
BUZZ
|
BUZZ
|
||||||
@@ -26,8 +26,8 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
let proto = location.protocol == 'http:' ? 'ws' : 'wss'
|
var proto = location.protocol == 'http:' ? 'ws' : 'wss'
|
||||||
let socket = new WebSocket(`${proto}://${window.location.hostname}:${window.location.port}/{{room}}`);
|
var socket = new WebSocket(proto + '://' + window.location.hostname + ':' + window.location.port + '/{{room}}');
|
||||||
|
|
||||||
socket.onopen = function(e) {
|
socket.onopen = function(e) {
|
||||||
socket.send(JSON.stringify({
|
socket.send(JSON.stringify({
|
||||||
@@ -40,13 +40,15 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
socket.onmessage = function(event) {
|
socket.onmessage = function(event) {
|
||||||
let msg = JSON.parse(event.data);
|
var msg = JSON.parse(event.data);
|
||||||
if (msg.type === "buzz") {
|
if (msg.type === "buzz") {
|
||||||
showMessage(msg.msg);
|
showMessage(msg.msg);
|
||||||
} else if (msg.type === "reset") {
|
} else if (msg.type === "reset") {
|
||||||
reset();
|
reset();
|
||||||
} else if (msg.type === "close") {
|
} else if (msg.type === "close") {
|
||||||
roomClosed();
|
roomClosed();
|
||||||
|
} else if (msg.type === "score") {
|
||||||
|
document.querySelector('.score').innerText = msg.score;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -67,9 +69,9 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
function showMessage(msg) {
|
function showMessage(msg) {
|
||||||
window.navigator.vibrate(500);
|
if (window.navigator.vibrate) window.navigator.vibrate(500);
|
||||||
document.getElementById('buzzer').disabled = true;
|
document.getElementById('buzzer').disabled = true;
|
||||||
let mb = document.getElementById('messageBox');
|
var mb = document.getElementById('messageBox');
|
||||||
mb.innerHTML = msg;
|
mb.innerHTML = msg;
|
||||||
mb.classList.remove('hide');
|
mb.classList.remove('hide');
|
||||||
}
|
}
|
||||||
@@ -77,19 +79,19 @@
|
|||||||
function reset() {
|
function reset() {
|
||||||
document.getElementById('buzzer').disabled = false;
|
document.getElementById('buzzer').disabled = false;
|
||||||
document.getElementById('buzzer').classList.remove('active');
|
document.getElementById('buzzer').classList.remove('active');
|
||||||
let mb = document.getElementById('messageBox');
|
var mb = document.getElementById('messageBox');
|
||||||
mb.classList.add('hide');
|
mb.classList.add('hide');
|
||||||
}
|
}
|
||||||
|
|
||||||
function roomClosed() {
|
function roomClosed() {
|
||||||
document.getElementById('buzzer').disabled = true;
|
document.getElementById('buzzer').disabled = true;
|
||||||
let mb = document.getElementById('messageBox');
|
var mb = document.getElementById('messageBox');
|
||||||
mb.innerHTML = "Room closed";
|
mb.innerHTML = "Room closed";
|
||||||
mb.classList.remove('hide');
|
mb.classList.remove('hide');
|
||||||
}
|
}
|
||||||
|
|
||||||
function beep() {
|
function beep() {
|
||||||
window.navigator.vibrate(500);
|
if (window.navigator.vibrate) window.navigator.vibrate(500);
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
Reference in New Issue
Block a user