moth/theme/scoreboard.mjs

120 lines
3.7 KiB
JavaScript
Raw Normal View History

2023-09-15 15:17:07 -06:00
import * as moth from "./moth.mjs"
import * as common from "./common.mjs"
2020-02-29 17:12:35 -07:00
2023-09-15 15:17:07 -06:00
const server = new moth.Server(".")
/** Don't let any team's score exceed this percentage width */
2023-09-28 12:42:25 -06:00
const MaxScoreWidth = 90
2023-09-15 15:17:07 -06:00
/**
* Returns a promise that resolves after timeout.
2023-09-28 12:59:51 -06:00
*
* This uses setTimeout instead of some other fancy thing like
* requestAnimationFrame, because who actually cares about scoreboard update
* framerate?
*
2023-09-15 15:17:07 -06:00
* @param {Number} timeout How long to sleep (milliseconds)
* @returns {Promise}
*/
function sleep(timeout) {
return new Promise(resolve => setTimeout(resolve, timeout));
}
2023-09-15 15:17:07 -06:00
/**
* Pull new points log, and update the scoreboard.
*
* The update is animated, because I think that looks cool.
*/
async function update() {
2023-09-28 12:42:25 -06:00
let config = {}
try {
config = await common.Config()
}
catch (err) {
console.warn("Parsing config.json:", err)
}
// Pull configuration settings
let ScoreboardConfig = config.Scoreboard ?? {}
let ReplayHistory = ScoreboardConfig.ReplayHistory ?? false
let ReplayDurationMS = ScoreboardConfig.ReplayDurationMS ?? 300
let ReplayFPS = ScoreboardConfig.ReplayFPS ?? 24
if (!config.Scoreboard) {
console.warn("config.json has empty Scoreboard section")
}
2023-09-15 16:09:08 -06:00
for (let e of document.querySelectorAll(".location")) {
e.textContent = common.BaseURL
2023-09-28 12:42:25 -06:00
e.classList.toggle("hidden", !ScoreboardConfig.DisplayServerURL)
2023-09-15 16:09:08 -06:00
}
2023-09-15 15:17:07 -06:00
let state = await server.GetState()
let rankingsElement = document.querySelector("#rankings")
let logSize = state.PointsLog.length
2023-09-15 15:17:07 -06:00
// Figure out the timing so that we can replay the scoreboard in about
2023-09-28 12:42:25 -06:00
// ReplayDurationMS, but no more than 24 frames per second.
2023-09-15 15:17:07 -06:00
let frameModulo = 1
let delay = 0
2023-09-28 12:42:25 -06:00
while (delay < (common.Second / ReplayFPS)) {
2023-09-15 15:17:07 -06:00
frameModulo += 1
2023-09-28 12:42:25 -06:00
delay = ReplayDurationMS / (logSize / frameModulo)
}
2023-09-15 15:17:07 -06:00
let frame = 0
2023-09-27 16:10:31 -06:00
for (let scores of state.ScoresHistory()) {
2023-09-15 15:17:07 -06:00
frame += 1
2023-09-28 12:42:25 -06:00
if (frame < state.PointsLog.length) { // Always render the last frame
if (!ReplayHistory || (frame % frameModulo)) { // Skip if we're not animating, or if we need to drop frames
continue
}
2023-09-15 15:17:07 -06:00
}
2023-09-15 15:17:07 -06:00
while (rankingsElement.firstChild) rankingsElement.firstChild.remove()
2023-09-15 15:17:07 -06:00
let sortedTeamIDs = [...scores.TeamIDs]
sortedTeamIDs.sort((a, b) => scores.CyFiScore(a) - scores.CyFiScore(b))
sortedTeamIDs.reverse()
2020-02-29 17:12:35 -07:00
2023-09-15 15:17:07 -06:00
let topScore = scores.CyFiScore(sortedTeamIDs[0])
for (let teamID of sortedTeamIDs) {
let teamName = state.TeamNames[teamID]
2020-02-29 17:12:35 -07:00
2023-09-15 15:17:07 -06:00
let row = rankingsElement.appendChild(document.createElement("div"))
2020-02-29 17:12:35 -07:00
2023-09-27 17:56:40 -06:00
let teamname = row.appendChild(document.createElement("span"))
teamname.textContent = teamName
teamname.classList.add("teamname")
2023-09-15 15:17:07 -06:00
let categoryNumber = 0
2023-09-27 17:56:40 -06:00
let teampoints = row.appendChild(document.createElement("span"))
teampoints.classList.add("teampoints")
2023-09-15 15:17:07 -06:00
for (let category of scores.Categories) {
let score = scores.CyFiCategoryScore(category, teamID)
if (!score) {
continue
}
// XXX: Figure out how to do this properly with flexbox
let block = row.appendChild(document.createElement("span"))
2023-09-15 15:17:07 -06:00
let points = scores.GetPoints(category, teamID)
let width = MaxScoreWidth * score / topScore
block.textContent = category
block.title = `${points} points`
block.style.width = `${width}%`
2023-09-27 17:56:40 -06:00
block.classList.add("category", `cat${categoryNumber}`)
2023-09-28 12:42:25 -06:00
block.classList.toggle("topscore", (score == 1) && ScoreboardConfig.ShowCategoryLeaders)
2023-09-27 17:56:40 -06:00
2023-09-15 15:17:07 -06:00
categoryNumber += 1
}
2020-02-29 17:12:35 -07:00
}
2023-09-15 15:17:07 -06:00
await sleep(delay)
2020-02-29 17:12:35 -07:00
}
}
2023-09-15 15:17:07 -06:00
function init() {
setInterval(update, common.Minute)
update()
2020-02-29 17:12:35 -07:00
}
2023-09-15 15:17:07 -06:00
common.WhenDOMLoaded(init)