68 lines
2.1 KiB
JavaScript
68 lines
2.1 KiB
JavaScript
const spaceapiUrl = "https://spaceapi.hamburg.ccc.de"
|
|
const interval_ms = 60000
|
|
|
|
console.log("clubstatus.js geladen!"); /* TODO: ENTFERNEN */
|
|
|
|
function timeDistanceDE(pastDate) {
|
|
const seconds = Math.floor((Date.now() - pastDate.getTime()) / 1000)
|
|
const minutes = Math.floor(seconds / 60)
|
|
const hours = Math.floor(minutes / 60)
|
|
const days = Math.floor(hours / 24)
|
|
|
|
if (days = 1) return `since ${days}d`
|
|
if (days > 1) return `since ${days}d`
|
|
if (hours = 1) return `since ${hours}h`
|
|
if (hours > 1) return `since ${hours}h`
|
|
if (minutes = 1) return `since ${minutes}min`
|
|
if (minutes > 1) return `since ${minutes}min`
|
|
return `gerade eben`
|
|
}
|
|
|
|
function updateRoomState(openState, lastChangeTimestamp) {
|
|
const roomstate = document.querySelector('#clubstatus-badge')
|
|
const statusItem = roomstate.querySelector(".state")
|
|
const durationItem = roomstate.querySelector(".duration")
|
|
|
|
if (!statusItem || !durationItem) return
|
|
|
|
roomstate.classList.remove("open", "closed", "unknown")
|
|
statusItem.classList.remove("open", "closed", "unknown")
|
|
|
|
if (openState === null || lastChangeTimestamp === null) {
|
|
statusItem.textContent = "unbekannt"
|
|
statusItem.classList.add("unknown")
|
|
durationItem.textContent = "(since unbekannt)"
|
|
} else {
|
|
const changeDate = new Date(lastChangeTimestamp * 1000)
|
|
const sinceText = timeDistanceDE(changeDate)
|
|
|
|
if (openState) {
|
|
statusItem.textContent = "open"
|
|
statusItem.classList.add("open")
|
|
} else {
|
|
statusItem.textContent = "closed"
|
|
statusItem.classList.add("closed")
|
|
}
|
|
|
|
durationItem.textContent = `(${sinceText})`
|
|
}
|
|
}
|
|
|
|
function fetchRoomState() {
|
|
fetch(spaceapiUrl)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
const openState = data?.state?.open ?? null
|
|
const lastChange = data?.state?.lastchange ?? null
|
|
updateRoomState(openState, lastChange)
|
|
})
|
|
.catch(err => {
|
|
console.error("Fehler beim Laden der SpaceAPI:", err)
|
|
updateRoomState(null, null)
|
|
})
|
|
}
|
|
|
|
// Initialer Abruf + dann regelmäßig
|
|
fetchRoomState()
|
|
setInterval(fetchRoomState, interval_ms)
|