2020-04-09 12:04:20 +00:00
|
|
|
const chars = require('./characters.json');
|
2020-04-09 10:42:19 +00:00
|
|
|
const rooms = {};
|
|
|
|
|
2020-04-09 12:16:21 +00:00
|
|
|
function shuffle(a) {
|
2020-04-10 08:22:50 +00:00
|
|
|
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;
|
2020-04-09 12:16:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 10:53:21 +00:00
|
|
|
function getOrCreateRoom(roomId) {
|
2020-04-09 10:42:19 +00:00
|
|
|
let room = rooms[roomId];
|
2020-11-27 18:29:18 +00:00
|
|
|
|
2020-04-09 10:42:19 +00:00
|
|
|
if (!room) {
|
|
|
|
room = {
|
|
|
|
roomId: roomId,
|
2020-04-09 12:04:20 +00:00
|
|
|
participants: [],
|
2020-04-09 15:38:44 +00:00
|
|
|
audience: [],
|
2020-04-11 12:12:23 +00:00
|
|
|
characters: shuffle(chars),
|
|
|
|
canBuzz: true
|
2020-04-09 10:42:19 +00:00
|
|
|
};
|
|
|
|
rooms[roomId] = room;
|
|
|
|
}
|
2020-11-27 18:29:18 +00:00
|
|
|
|
2020-04-09 10:42:19 +00:00
|
|
|
return room;
|
|
|
|
}
|
|
|
|
|
2020-04-09 10:53:21 +00:00
|
|
|
function addParticipant(roomId, participantId, participantName) {
|
|
|
|
let room = getOrCreateRoom(roomId);
|
2020-11-27 18:29:18 +00:00
|
|
|
|
2020-04-09 10:53:21 +00:00
|
|
|
room.participants.push({
|
2020-11-27 18:29:18 +00:00
|
|
|
participantId,
|
2020-04-09 10:53:21 +00:00
|
|
|
participantName,
|
2020-04-13 11:44:31 +00:00
|
|
|
character: room.characters[room.participants.length],
|
|
|
|
active: true
|
2020-04-09 10:53:21 +00:00
|
|
|
});
|
2020-11-27 18:29:18 +00:00
|
|
|
|
2020-04-09 10:53:21 +00:00
|
|
|
rooms[roomId] = room;
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:11:41 +00:00
|
|
|
function removeParticipant(roomId, participantId) {
|
|
|
|
let room = getOrCreateRoom(roomId);
|
2020-11-27 18:29:18 +00:00
|
|
|
let participant = room.participants.find(p => p.participantId === participantId);
|
|
|
|
if (participant) {
|
|
|
|
participant.active = false;
|
|
|
|
room.audience.forEach(ws => {
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
type: "participants",
|
|
|
|
participants: room.participants
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
2020-04-10 08:11:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 15:14:45 +00:00
|
|
|
function addParticipantWS(roomId, participantId, ws) {
|
|
|
|
let room = getOrCreateRoom(roomId);
|
2020-11-27 18:29:18 +00:00
|
|
|
let participant = room.participants.find(p => p.participantId === participantId);
|
|
|
|
if (participant) {
|
|
|
|
participant.ws = ws;
|
|
|
|
room.audience.forEach(ws => {
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
type: "participants",
|
|
|
|
participants: room.participants
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
2020-04-09 15:14:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 15:38:44 +00:00
|
|
|
function addAudienceWS(roomId, ws) {
|
|
|
|
let room = getOrCreateRoom(roomId);
|
|
|
|
room.audience.push(ws);
|
2020-04-13 11:44:31 +00:00
|
|
|
ws.send(JSON.stringify({
|
|
|
|
type: "participants",
|
|
|
|
participants: room.participants
|
|
|
|
}));
|
2020-04-09 15:38:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 15:26:47 +00:00
|
|
|
function buzz(roomId, participant) {
|
|
|
|
let room = getOrCreateRoom(roomId);
|
2020-04-11 12:12:23 +00:00
|
|
|
if (room.canBuzz) {
|
|
|
|
room.canBuzz = false;
|
2020-11-27 18:29:18 +00:00
|
|
|
|
2020-04-11 12:12:23 +00:00
|
|
|
participant = room.participants.find(p => p.participantId === participant.participantId);
|
|
|
|
|
2020-11-27 18:29:18 +00:00
|
|
|
if (participant) {
|
|
|
|
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({
|
2020-04-11 12:12:23 +00:00
|
|
|
type: "buzz",
|
2020-11-27 18:29:18 +00:00
|
|
|
participant: participant
|
2020-04-11 12:12:23 +00:00
|
|
|
}));
|
2020-11-27 18:29:18 +00:00
|
|
|
});
|
|
|
|
}
|
2020-04-11 12:12:23 +00:00
|
|
|
}
|
2020-04-09 15:26:47 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 18:29:18 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2020-11-27 19:13:27 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
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}
|