add queue time
This commit is contained in:
1 parent
f112f84c2f
commit
6563c00dec
6 files changed
+60
-35
No files matched your search
+1
-1
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
+14
-9
@@ -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,
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"start": "node .",
|
||||
"dev": "nodemon ."
|
||||
"dev": "NODE_ENV=development nodemon ."
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
+4
-2
@@ -192,11 +192,12 @@
|
||||
<th>IP</th>
|
||||
<th>Nick</th>
|
||||
<th>Status</th>
|
||||
<th>Time</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="queue-body">
|
||||
<tr>
|
||||
<td colspan="4" class="empty">No players in queue</td>
|
||||
<td colspan="5" class="empty">No players in queue</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -274,7 +275,7 @@
|
||||
const queueBody = document.getElementById('queue-body')
|
||||
|
||||
if (data.queue.queue.length === 0) {
|
||||
queueBody.innerHTML = '<tr><td colspan="4" class="empty">No players in queue</td></tr>'
|
||||
queueBody.innerHTML = '<tr><td colspan="5" class="empty">No players in queue</td></tr>'
|
||||
} else {
|
||||
queueBody.innerHTML = data.queue.queue.map(item => `
|
||||
<tr>
|
||||
@@ -282,6 +283,7 @@
|
||||
<td>${item.ip}</td>
|
||||
<td>${item.nick || '--'}</td>
|
||||
<td>${item.isActive ? '<span class="status active">ACTIVE</span>' : ''}</td>
|
||||
<td>${item.time !== null ? item.time.toFixed(4) + 's' : '--'}</td>
|
||||
</tr>
|
||||
`).join('')
|
||||
}
|
||||
|
||||
+36
-22
@@ -1,6 +1,5 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
@@ -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;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
</head>
|
||||
|
||||
<body class="patterned">
|
||||
<!-- Status Indicator -->
|
||||
@@ -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) => `<div class="nick ${i == active ? 'active' : ''}">${nick}</div>`)
|
||||
.map(
|
||||
([nick, time], i) =>
|
||||
`<div class="nick ${i == active ? 'active' : ''}" data-time="${queueTimeFormat(
|
||||
time
|
||||
)}">${nick}</div>`
|
||||
)
|
||||
.join('')
|
||||
}
|
||||
|
||||
@@ -287,13 +302,13 @@
|
||||
<span class="score">${player.score}</span>
|
||||
</div>`
|
||||
})
|
||||
.join('')
|
||||
}
|
||||
</script>
|
||||
.join('')
|
||||
}
|
||||
</script>
|
||||
|
||||
<script>
|
||||
requestWakeLock()
|
||||
setStatus()
|
||||
<script>
|
||||
requestWakeLock()
|
||||
setStatus()
|
||||
|
||||
socket.on('update', ({ board, active, queue, status }) => {
|
||||
setQueue(queue, active)
|
||||
@@ -306,5 +321,4 @@
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</html>
|
||||
Reference in new issue
Block a user