From b337088cb61e405c8b083be5da5edc5401476fd9 Mon Sep 17 00:00:00 2001 From: "Glitch (hello-express)" Date: Thu, 9 Apr 2020 10:53:21 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=8B=F0=9F=8F=A5=20Checkpoint=20./room.?= =?UTF-8?q?js:530995/285=20./server.js:530995/832?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- room.js | 15 +++++++++++---- server.js | 17 ++++++++++++++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/room.js b/room.js index 7a3d9c6..5d6a380 100644 --- a/room.js +++ b/room.js @@ -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; } - -export addParticipant(roomId, participantId, participantName) { +function addParticipant(roomId, participantId, participantName) { + let room = getOrCreateRoom(roomId); -} \ No newline at end of file + room.participants.push({ + participantId, + participantName, + }); + + rooms[roomId] = room; +} + diff --git a/server.js b/server.js index 90ee12c..697c666 100644 --- a/server.js +++ b/server.js @@ -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,