🐋🏥 Checkpoint

./room.js:530995/285
./server.js:530995/832
This commit is contained in:
Glitch (hello-express) 2020-04-09 10:53:21 +00:00
parent 0f24a75183
commit b337088cb6
2 changed files with 27 additions and 5 deletions

11
room.js
View File

@ -1,7 +1,7 @@
const rooms = {};
export function getOrCreateRoom(roomId) {
function getOrCreateRoom(roomId) {
let room = rooms[roomId];
if (!room) {
@ -15,7 +15,14 @@ export function getOrCreateRoom(roomId) {
return room;
}
function addParticipant(roomId, participantId, participantName) {
let room = getOrCreateRoom(roomId);
export addParticipant(roomId, participantId, participantName) {
room.participants.push({
participantId,
participantName,
});
rooms[roomId] = room;
}

View File

@ -3,6 +3,8 @@ const exphbs = require('express-handlebars');
const bodyParser = require('body-parser')
const Fingerprint = require('express-fingerprint')
const rooms = require('./room');
const app = express();
app.use(Fingerprint({
@ -24,10 +26,23 @@ app.set('view engine', 'handlebars');
app.use(express.static("public"));
app.get("/:roomId/join", (request, response) => {
response.render('join', {layout: false, room: request.params.roomId});
let room = rooms.getOrCreateRoom(request.params.roomId);
let participant = room.participants.find(p => p.participantId === request.fingerprint.hash);
if (participant) {
response.render('room', {
layout: false,
room: request.params.roomId,
name: participant.participantName
});
} else {
response.render('join', {layout: false, room: request.params.roomId});
}
});
app.post("/:roomId/join", (request, response) => {
rooms.addParticipant(request.params.roomId, request.fingerprint.hash, request.body.name);
response.render('room', {
layout: false,
room: request.params.roomId,