buzzers/room.js
Glitch (hello-express) 84d7efd9fe ❄️🌿 Checkpoint
./server.js:530995/89
./views/room.handlebars:530995/664
./public/style.css:530995/706
./room.js:530995/22
2020-04-09 16:49:47 +00:00

72 lines
1.8 KiB
JavaScript

const chars = require('./characters.json');
const rooms = {};
function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
function getOrCreateRoom(roomId) {
let room = rooms[roomId];
if (!room) {
room = {
roomId: roomId,
participants: [],
audience: [],
characters: shuffle(chars)
};
rooms[roomId] = room;
}
return room;
}
function addParticipant(roomId, participantId, participantName) {
let room = getOrCreateRoom(roomId);
room.participants.push({
participantId,
participantName,
character: room.characters[room.participants.length]
});
rooms[roomId] = room;
}
function addParticipantWS(roomId, participantId, ws) {
let room = getOrCreateRoom(roomId);
room.participants.find(p => p.participantId === participantId).ws = ws;
room.audience.forEach(ws => ws.send(JSON.stringify({type: "new_participant"})));
}
function addAudienceWS(roomId, ws) {
let room = getOrCreateRoom(roomId);
room.audience.push(ws);
}
function buzz(roomId, participant) {
let room = getOrCreateRoom(roomId);
participant = room.participants.find(p => p.participantId === participant.participantId);
room.participants.forEach(p => {
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",
participant: participant
}));
});
}
module.exports = {getOrCreateRoom, addParticipant, addParticipantWS, addAudienceWS, buzz}