Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Adding lantence in ms #4

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,29 @@ const io = socketio(server);

const title = 'Buffer Buzzer'

let data = {
const initData = () => ({
users: new Set(),
buzzes: new Set(),
}
buzzes: new Map(),
firstResponse: null,
});

let data = initData();

const getBuzzerValues = (data) => Array.from(data.buzzes, ([ _key, values ]) => values);


const getData = () => ({
users: [...data.users],
buzzes: [...data.buzzes].map(b => {
const [ name, team ] = b.split('-')
return { name, team }
})
buzzes: [...getBuzzerValues(data)]
})

const computeLatence = (firstResponse) => new Date().getTime() - firstResponse

app.use(express.static('public'))
app.set('view engine', 'pug')

app.get('/', (req, res) => res.render('index', { title }))
app.get('/host', (req, res) => res.render('host', Object.assign({ title }, getData())))
app.get('/', (_req, res) => res.render('index', { title }))
app.get('/host', (_req, res) => res.render('host', Object.assign({ title }, getData())))

io.on('connection', (socket) => {
socket.on('join', (user) => {
Expand All @@ -35,16 +40,31 @@ io.on('connection', (socket) => {
})

socket.on('buzz', (user) => {
data.buzzes.add(`${user.name}-${user.team}`)
const latence = data.firstResponse
? computeLatence(data.firstResponse)
: null;

if(data.buzzes.size === 0) {
data.firstResponse = new Date().getTime()
}

if(!data.buzzes.get(user.name)){
data.buzzes.set(
user.name,
{ latence, user: { name: user.name, team: user.team } }
)
}

io.emit('buzzes', [...data.buzzes])
console.log(`${user.name} buzzed in!`)
console.log(`${latence} ${user.name} buzzed in! ${latence ? `(+ ${latence}ms)` : ''}`)
})

socket.on('clear', () => {
data.buzzes = new Set()
data = initData();
io.emit('buzzes', [...data.buzzes])
console.log(`Clear buzzes`)
})
})

server.listen(8090, () => console.log('Listening on 8090'))
const port = process.env.PORT || 8090;
server.listen(port, () => console.log(`Listening on ${port}`))
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "Dan Farrelly <[email protected]> (http://danfarrelly.nyc)",
"license": "MIT",
Expand Down
13 changes: 7 additions & 6 deletions public/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ socket.on('active', (numberActive) => {
active.innerText = `${numberActive} joined`
})

socket.on('buzzes', (buzzes) => {
socket.on('buzzes', (buzzesMap) => {
const buzzes = Array.from(buzzesMap, ([ _key, values ]) => values);
buzzList.innerHTML = buzzes
.map(buzz => {
const p = buzz.split('-')
return { name: p[0], team: p[1] }
})
.map(user => `<li>${user.name} on Team ${user.team}</li>`)
.map(
({
latence,
user: { name, team }
}) => `<li>${name} on Team ${team} ${latence ? `(+${latence}ms)` : ''}</li>`)
.join('')
})

Expand Down
Loading