diff --git a/backend/Client.js b/backend/Client.js index 078ee70..8d9e885 100644 --- a/backend/Client.js +++ b/backend/Client.js @@ -82,7 +82,7 @@ class Client { handleRegistration(clientType) { const validTypes = ['tv', 'host', 'player', 'debug'] - if (!validTypes.includes(clientType)) return + if (!validTypes.includes(clientType)) throw new Error(`Invalid client type: ${clientType}`) this.type = clientType logger.info('Client', `'${this.ip}' registered as`, this.type) diff --git a/backend/Music.js b/backend/Music.js index d1761ec..71cf362 100644 --- a/backend/Music.js +++ b/backend/Music.js @@ -183,6 +183,7 @@ class Music { static metadata = null static listeners = new Set() static #platform = null + static playingSince = null static init(platform) { this.#platform = platform @@ -214,6 +215,9 @@ class Music { if (!allowed.includes(status)) return this.status = status + if (status === 'playing') { + this.playingSince = new Date() + } logger.debug('Music', `status changed to`, status) } diff --git a/backend/Queue.js b/backend/Queue.js index 54810f2..4de27c0 100644 --- a/backend/Queue.js +++ b/backend/Queue.js @@ -1,17 +1,21 @@ import logger from '../logger.js' +import Music from './Music.js' + +function getTime() { + if (!Music.playingSince) return null + return (new Date() - Music.playingSince) / 1000 +} class Queue { static array = [] static active = null static add(client) { - if (this.isClientInQueue(client)) { - // if (Music.status == 'playing') this.active++ - return - } + if (this.isClientInQueue(client)) return if (this.isEmpty) this.active = 0 - this.array.push(client) + + this.array.push({ client, time: getTime() }) logger.debug('Queue', `client added to queue`, client.ip) } @@ -21,7 +25,7 @@ class Queue { } static isClientInQueue(client) { - return this.array.some(c => c.ip === client.ip) + return this.array.some(({ client: c }) => c.ip === client.ip) } static clear() { @@ -36,20 +40,21 @@ class Queue { } static get rendered() { - return this.array.map(client => client.nick) + return this.array.map(({ client, time }) => [client.nick, time]) } static get activeClient() { - return this.array[this.active] + return this.array[this.active]?.client } static debug() { return { - queue: this.array.map((client, index) => ({ + queue: this.array.map(({ client, time }, index) => ({ position: index, ip: client.ip, nick: client.nick || null, isActive: index === this.active, + time, })), active: this.active, length: this.array.length, diff --git a/package.json b/package.json index 58ad620..ff9792d 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "main": "server.js", "scripts": { "start": "node .", - "dev": "nodemon ." + "dev": "NODE_ENV=development nodemon ." }, "repository": { "type": "git", diff --git a/pages/debug.html b/pages/debug.html index 821d943..666e321 100644 --- a/pages/debug.html +++ b/pages/debug.html @@ -192,11 +192,12 @@ IP Nick Status + Time - No players in queue + No players in queue @@ -274,7 +275,7 @@ const queueBody = document.getElementById('queue-body') if (data.queue.queue.length === 0) { - queueBody.innerHTML = 'No players in queue' + queueBody.innerHTML = 'No players in queue' } else { queueBody.innerHTML = data.queue.queue.map(item => ` @@ -282,6 +283,7 @@ ${item.ip} ${item.nick || '--'} ${item.isActive ? 'ACTIVE' : ''} + ${item.time !== null ? item.time.toFixed(4) + 's' : '--'} `).join('') } diff --git a/pages/tv.html b/pages/tv.html index b09249d..1a2f43f 100644 --- a/pages/tv.html +++ b/pages/tv.html @@ -1,6 +1,5 @@ - @@ -62,9 +61,6 @@ transform: translate(-50%, -50%); z-index: -1; pointer-events: none; - /* display: flex; - align-items: center; - justify-content: center; */ font-size: 500px; --opacity: 0.25; text-shadow: 0 0 20px rgba(0, 0, 0, var(--opacity)); @@ -78,10 +74,8 @@ counter-reset: poz; } - #queue>.nick { + #queue > .nick { display: flex; - /* justify-content: space-between; - align-items: center; */ padding: 0.8rem 1.5rem; margin: 0.5rem 0; background-color: rgba(255, 255, 255, 0.15); @@ -89,7 +83,7 @@ color: #fff; } - #queue>.nick::before { + #queue > .nick::before { counter-increment: poz; content: '#' counter(poz) ' '; font-weight: bold; @@ -98,7 +92,16 @@ width: 1.5em; } - #queue>img { + #queue > .nick::after { + content: attr(data-time); + margin-left: auto; + font-weight: bold; + color: #eee; + min-width: 4em; + text-align: right; + } + + #queue > img { height: 10vh; } @@ -172,24 +175,24 @@ /* Bronze */ } - #scoreboard .item>.rank { + #scoreboard .item > .rank { font-weight: bold; margin-right: 1rem; min-width: 2.5rem; } - #scoreboard .item>.nick { + #scoreboard .item > .nick { flex: 1; text-align: left; } - #scoreboard .item>.score { + #scoreboard .item > .score { font-weight: bold; min-width: 3rem; text-align: right; } - + @@ -263,9 +266,21 @@ document.body.style.setProperty('--bgcolor', patternColors[status].bg) } + function queueTimeFormat(seconds) { + if (seconds === null) return '' + if (seconds > 15) return `...` + // two significant digits in seconds with decimals + return seconds.toFixed(2) + 's' + } + function setQueue(queue, active = 0) { nodeQueue.innerHTML = queue - .map((nick, i) => `
${nick}
`) + .map( + ([nick, time], i) => + `
${nick}
` + ) .join('') } @@ -287,13 +302,13 @@ ${player.score} ` }) - .join('') - } - + .join('') + } + - - - \ No newline at end of file +