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 @@