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,