Better handling of room creation and management

This commit is contained in:
Marcus Noble 2020-11-27 18:29:18 +00:00
parent 5716a9338a
commit aad99975c6
11 changed files with 2898 additions and 91 deletions

View File

@ -16,4 +16,4 @@
"engines": { "engines": {
"node": "12.x" "node": "12.x"
} }
} }

1
public/build/bundle.css Normal file
View File

@ -0,0 +1 @@
main.svelte-1tky8bj{text-align:center;padding:1em;max-width:240px;margin:0 auto}h1.svelte-1tky8bj{color:#ff3e00;text-transform:uppercase;font-size:4em;font-weight:100}@media(min-width: 640px){main.svelte-1tky8bj{max-width:none}}main.svelte-1tky8bj{text-align:center;padding:1em;max-width:240px;margin:0 auto}h1.svelte-1tky8bj{color:#ff3e00;text-transform:uppercase;font-size:4em;font-weight:100}@media(min-width: 640px){main.svelte-1tky8bj{max-width:none}}main.svelte-1tky8bj{text-align:center;padding:1em;max-width:240px;margin:0 auto}h1.svelte-1tky8bj{color:#ff3e00;text-transform:uppercase;font-size:4em;font-weight:100}@media(min-width: 640px){main.svelte-1tky8bj{max-width:none}}main.svelte-1tky8bj{text-align:center;padding:1em;max-width:240px;margin:0 auto}h1.svelte-1tky8bj{color:#ff3e00;text-transform:uppercase;font-size:4em;font-weight:100}@media(min-width: 640px){main.svelte-1tky8bj{max-width:none}}

2726
public/build/bundle.js Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -24,6 +24,10 @@ form {
margin: 0 auto; margin: 0 auto;
} }
label {
font-weight: bold;
}
input[type=text] { input[type=text] {
text-transform: lowercase; text-transform: lowercase;
} }
@ -31,6 +35,7 @@ input[type=text] {
input { input {
border: 1px solid silver; border: 1px solid silver;
display: block; display: block;
text-align: center;
font-size: 16px; font-size: 16px;
margin-bottom: 10px; margin-bottom: 10px;
padding: 5px; padding: 5px;
@ -73,6 +78,11 @@ footer {
text-shadow: 2px 2px 2px black; text-shadow: 2px 2px 2px black;
} }
#buzzer.active {
background: greenyellow;
box-shadow: inset 4px 4px 4px rgba(0,0,0,.5);
}
#buzzer:active { #buzzer:active {
box-shadow: inset 4px 4px 4px rgba(0,0,0,.5); box-shadow: inset 4px 4px 4px rgba(0,0,0,.5);
} }
@ -112,7 +122,7 @@ figure.participant figcaption {
10%, 90% { 10%, 90% {
transform: rscale(1.3) otate(-10deg); transform: rscale(1.3) otate(-10deg);
} }
20%, 80% { 20%, 80% {
transform: rscale(1.3) otate(20deg); transform: rscale(1.3) otate(20deg);
} }
@ -148,4 +158,4 @@ figure.participant figcaption {
#messageBox.hide { #messageBox.hide {
display: none; display: none;
} }

103
room.js
View File

@ -11,7 +11,7 @@ function shuffle(a) {
function getOrCreateRoom(roomId) { function getOrCreateRoom(roomId) {
let room = rooms[roomId]; let room = rooms[roomId];
if (!room) { if (!room) {
room = { room = {
roomId: roomId, roomId: roomId,
@ -22,43 +22,49 @@ function getOrCreateRoom(roomId) {
}; };
rooms[roomId] = room; rooms[roomId] = room;
} }
return room; return room;
} }
function addParticipant(roomId, participantId, participantName) { function addParticipant(roomId, participantId, participantName) {
let room = getOrCreateRoom(roomId); let room = getOrCreateRoom(roomId);
room.participants.push({ room.participants.push({
participantId, participantId,
participantName, participantName,
character: room.characters[room.participants.length], character: room.characters[room.participants.length],
active: true active: true
}); });
rooms[roomId] = room; rooms[roomId] = room;
} }
function removeParticipant(roomId, participantId) { function removeParticipant(roomId, participantId) {
let room = getOrCreateRoom(roomId); let room = getOrCreateRoom(roomId);
room.participants.find(p => p.participantId === participantId).active = false; let participant = room.participants.find(p => p.participantId === participantId);
room.audience.forEach(ws => { if (participant) {
ws.send(JSON.stringify({ participant.active = false;
type: "participants", room.audience.forEach(ws => {
participants: room.participants ws.send(JSON.stringify({
})); type: "participants",
}); participants: room.participants
}));
});
}
} }
function addParticipantWS(roomId, participantId, ws) { function addParticipantWS(roomId, participantId, ws) {
let room = getOrCreateRoom(roomId); let room = getOrCreateRoom(roomId);
room.participants.find(p => p.participantId === participantId).ws = ws; let participant = room.participants.find(p => p.participantId === participantId);
room.audience.forEach(ws => { if (participant) {
ws.send(JSON.stringify({ participant.ws = ws;
type: "participants", room.audience.forEach(ws => {
participants: room.participants ws.send(JSON.stringify({
})); type: "participants",
}); participants: room.participants
}));
});
}
} }
function addAudienceWS(roomId, ws) { function addAudienceWS(roomId, ws) {
@ -74,26 +80,53 @@ function buzz(roomId, participant) {
let room = getOrCreateRoom(roomId); let room = getOrCreateRoom(roomId);
if (room.canBuzz) { if (room.canBuzz) {
room.canBuzz = false; room.canBuzz = false;
setTimeout(() => room.canBuzz = true, 5000);
participant = room.participants.find(p => p.participantId === participant.participantId); participant = room.participants.find(p => p.participantId === participant.participantId);
room.participants.forEach(p => { if (participant) {
if (p.ws && p.participantId !== participant.participantId) { room.participants.forEach(p => {
p.ws.send(JSON.stringify({ if (p.ws && p.participantId !== participant.participantId) {
p.ws.send(JSON.stringify({
type: "buzz",
participant: participant.participantName,
msg: `<img src="${participant.character}"><div>${participant.participantName} buzzed!</div>`
}));
}
});
room.audience.forEach(ws => {
ws.send(JSON.stringify({
type: "buzz", type: "buzz",
participant: participant.participantName, participant: participant
msg: `<img src="${participant.character}"><div>${participant.participantName} buzzed!</div>`
})); }));
} });
}); }
room.audience.forEach(ws => {
ws.send(JSON.stringify({
type: "buzz",
participant: participant
}));
});
} }
} }
module.exports = {getOrCreateRoom, addParticipant, addParticipantWS, addAudienceWS, buzz, removeParticipant} function reset(roomId) {
let room = getOrCreateRoom(roomId);
room.canBuzz = true;
room.participants.forEach(p => {
p.ws.send(JSON.stringify({
type: "reset"
}));
});
}
function closeRoom(roomId) {
let room = getOrCreateRoom(roomId);
room.canBuzz = false;
room.participants.forEach(p => {
if (p.ws) {
p.ws.send(JSON.stringify({
type: "close"
}));
}
});
delete rooms[roomId];
}
module.exports = {getOrCreateRoom, addParticipant, addParticipantWS, addAudienceWS, buzz, removeParticipant, reset, closeRoom}

View File

@ -6,6 +6,7 @@ const WebSocket = require('ws');
const http = require('http'); const http = require('http');
const randomWords = require('random-words'); const randomWords = require('random-words');
const rooms = require('./room'); const rooms = require('./room');
const room = require("./room");
const app = express(); const app = express();
const server = http.createServer(app); const server = http.createServer(app);
@ -25,32 +26,33 @@ app.get("/", (request, response) => {
}); });
}); });
app.get("/:roomId/join", (request, response) => { app.get("/:roomId", (request, response) => {
let room = rooms.getOrCreateRoom(request.params.roomId.toLowerCase()); let room = rooms.getOrCreateRoom(request.params.roomId.trim().toLowerCase());
let participant = room.participants.find(p => p.participantId === request.fingerprint.hash); let participant = room.participants.find(p => p.participantId === request.fingerprint.hash);
if (participant) { if (room.audience.length === 0) {
response.render('audience', {
layout: false,
room: request.params.roomId.trim().toLowerCase(),
participants: room.participants,
});
} else if (participant) {
response.render('room', { response.render('room', {
layout: false, layout: false,
room: request.params.roomId.toLowerCase(), room: request.params.roomId.trim().toLowerCase(),
name: participant.participantName, name: participant.participantName,
participantName: participant.participantName, participantName: participant.participantName,
participantId: participant.participantId, participantId: participant.participantId,
character: participant.character, character: participant.character,
}); });
} else { } else {
response.render('join', {layout: false, room: request.params.roomId.toLowerCase()}); response.render('join', {layout: false, room: request.params.roomId.trim().toLowerCase()});
} }
}); });
app.get("/:roomId/audience", (request, response) => { app.post("/:roomId", (request, response) => {
let room = rooms.getOrCreateRoom(request.params.roomId.toLowerCase());
response.render('audience', {layout: false, room: request.params.roomId.toLowerCase(), participants: room.participants });
});
app.post("/:roomId/join", (request, response) => {
rooms.addParticipant(request.params.roomId.toLowerCase(), request.fingerprint.hash, request.body.name); rooms.addParticipant(request.params.roomId.toLowerCase(), request.fingerprint.hash, request.body.name);
response.redirect(`/${request.params.roomId.toLowerCase()}/join`); response.redirect(`/${request.params.roomId.toLowerCase()}`);
}); });
server.listen(process.env.PORT, () => { server.listen(process.env.PORT, () => {
@ -65,6 +67,17 @@ wss.on('connection', (ws, req) => {
if (roomId.includes("/audience")) { if (roomId.includes("/audience")) {
roomId = roomId.replace("/audience", ""); roomId = roomId.replace("/audience", "");
rooms.addAudienceWS(roomId, ws); rooms.addAudienceWS(roomId, ws);
ws.on('message', (message) => {
message = JSON.parse(message);
if (message.type === "reset") {
rooms.reset(roomId);
}
});
ws.on('close', () => {
room.closeRoom(roomId);
});
} else { } else {
let participant; let participant;
ws.on('message', (message) => { ws.on('message', (message) => {

View File

@ -14,22 +14,32 @@
</header> </header>
<main> <main>
<p>
Share this link for players to join:<br>
<a id="playerRoom" href=""></a>
</p>
<button id="resetBuzzers">Reset</button>
<h2>Participants</h2> <h2>Participants</h2>
<div id="participants"></div> <div id="participants"></div>
</main> </main>
<script> <script>
let socket = new WebSocket(`wss://${window.location.hostname}/{{room}}/audience`); document.getElementById("playerRoom").href = window.location;
document.getElementById("playerRoom").innerText = window.location;
let proto = location.protocol == 'http:' ? 'ws' : 'wss'
let socket = new WebSocket(`${proto}://${window.location.hostname}:${window.location.port}/{{room}}/audience`);
let buzzed;
socket.onmessage = function(event) { socket.onmessage = function(event) {
let msg = JSON.parse(event.data); let msg = JSON.parse(event.data);
if (msg.type === "buzz") { if (msg.type === "buzz") {
beep(); beep();
let buzzed = document.getElementById(`p-${msg.participant.participantId}`); buzzed = document.getElementById(`p-${msg.participant.participantId}`);
buzzed.classList.add('buzzed'); buzzed.classList.add('buzzed');
setTimeout(() => buzzed.classList.remove('buzzed'), 5000)
} else if (msg.type === "participants") { } else if (msg.type === "participants") {
let participantContainer = document.getElementById('participants'); let participantContainer = document.getElementById('participants');
let contents = ''; let contents = '';
@ -44,10 +54,17 @@
participantContainer.innerHTML = contents; participantContainer.innerHTML = contents;
} }
}; };
function beep() { function beep() {
window.navigator.vibrate(500); window.navigator.vibrate(500);
} }
document.getElementById('resetBuzzers').addEventListener('click', function() {
socket.send(JSON.stringify({
type: "reset"
}));
buzzed.classList.remove('buzzed');
})
</script> </script>
</body> </body>
</html> </html>

View File

@ -14,12 +14,8 @@
</header> </header>
<main> <main>
<h2>Oh hi!</h2> <h2>Create or join a room</h2>
<p>
Create or join a room
</p>
<form> <form>
<label> <label>
Room ID Room ID
@ -28,13 +24,12 @@
<button type="submit" id="submit-name">Submit</button> <button type="submit" id="submit-name">Submit</button>
</form> </form>
</main> </main>
<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; let roomId = document.getElementById('roomId').value;
window.location = `//${window.location.host}/${roomId}/join`; window.location = `//${window.location.host}/${roomId}`;
}); });
</script> </script>
</body> </body>

View File

@ -10,13 +10,11 @@
</head> </head>
<body> <body>
<header> <header>
<h1>Join {{room}}</h1> <h1>Join '{{room}}'</h1>
</header> </header>
<main> <main>
<h2>Oh hi,</h2> <h2>Please tell me your (team) name:</h2>
<p>Tell me your (team) name:</p>
<form method="POST"> <form method="POST">
<label> <label>
@ -25,10 +23,6 @@
</label> </label>
<button type="submit" id="submit-name">Submit</button> <button type="submit" id="submit-name">Submit</button>
</form> </form>
<p>
<a href="/{{room}}/audience">Join the audience</a>
</p>
</main> </main>
</body> </body>
</html> </html>

View File

@ -14,19 +14,20 @@
</header> </header>
<main> <main>
<h2>Oh hi, {{name}} <img src="{{character}}"/></h2> <h2>{{name}} <img src="{{character}}"/></h2>
<button id="buzzer"> <button id="buzzer">
BUZZ BUZZ
</button> </button>
</main> </main>
<div id="messageBox" class="hide"> <div id="messageBox" class="hide">
</div> </div>
<script> <script>
let socket = new WebSocket(`wss://${window.location.hostname}/{{room}}`); let proto = location.protocol == 'http:' ? 'ws' : 'wss'
let 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({
@ -42,35 +43,51 @@
let msg = JSON.parse(event.data); let msg = JSON.parse(event.data);
if (msg.type === "buzz") { if (msg.type === "buzz") {
showMessage(msg.msg); showMessage(msg.msg);
} else if (msg.type === "reset") {
reset();
} else if (msg.type === "close") {
roomClosed();
} }
}; };
document.getElementById('buzzer').addEventListener('touchstart', function() { document.getElementById('buzzer').addEventListener('touchstart', function() {
beep(); beep();
socket.send(JSON.stringify({ socket.send(JSON.stringify({
type: "buzz" type: "buzz"
})); }));
document.getElementById('buzzer').classList.add('active');
}); });
document.getElementById('buzzer').addEventListener('mousedown', function() { document.getElementById('buzzer').addEventListener('mousedown', function() {
beep(); beep();
socket.send(JSON.stringify({ socket.send(JSON.stringify({
type: "buzz" type: "buzz"
})); }));
document.getElementById('buzzer').classList.add('active');
}); });
function showMessage(msg) { function showMessage(msg) {
window.navigator.vibrate(500); window.navigator.vibrate(500);
document.getElementById('buzzer').disabled = true; document.getElementById('buzzer').disabled = true;
let mb = document.getElementById('messageBox'); let mb = document.getElementById('messageBox');
mb.innerHTML = msg; mb.innerHTML = msg;
mb.classList.remove('hide'); mb.classList.remove('hide');
setTimeout(() => {
document.getElementById('buzzer').disabled = false;
mb.classList.add('hide');
}, 5000)
} }
function reset() {
document.getElementById('buzzer').disabled = false;
document.getElementById('buzzer').classList.remove('active');
let mb = document.getElementById('messageBox');
mb.classList.add('hide');
}
function roomClosed() {
document.getElementById('buzzer').disabled = true;
let mb = document.getElementById('messageBox');
mb.innerHTML = "Room closed";
mb.classList.remove('hide');
}
function beep() { function beep() {
window.navigator.vibrate(500); window.navigator.vibrate(500);
} }