moth/theme/scoreboard.mjs

183 lines
5.6 KiB
JavaScript
Raw Permalink 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
if (!config.Scoreboard) {
console.warn("config.json has empty Scoreboard section")
}
2023-11-16 23:44:32 -07:00
let ScoreboardConfig = config.Scoreboard ?? {}
let state = await server.GetState()
2023-11-16 23:44:32 -07:00
// Show URL of server
2023-09-15 16:09:08 -06:00
for (let e of document.querySelectorAll(".location")) {
e.textContent = common.BaseURL
e.classList.toggle("hidden", !(ScoreboardConfig.DisplayServerURLWhenEnabled && state.Enabled))
2023-09-15 16:09:08 -06:00
}
2023-11-16 23:44:32 -07:00
// Rotate views
for (let e of document.querySelectorAll(".rotate")) {
e.appendChild(e.firstChild)
}
// Render rankings
for (let e of document.querySelectorAll(".rankings")) {
if (e.classList.contains("classic")) {
classicRankings(e, state, ScoreboardConfig)
} else if (e.classList.contains("category")) {
categoryRankings(e, state, ScoreboardConfig)
}
}
}
async function classicRankings(rankingsElement, state, ScoreboardConfig) {
let ReplayHistory = ScoreboardConfig.ReplayHistory ?? false
let ReplayDurationMS = ScoreboardConfig.ReplayDurationMS ?? 300
let ReplayFPS = ScoreboardConfig.ReplayFPS ?? 24
2023-09-15 15:17:07 -06:00
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
// ReplayDurationMS.
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) {
2023-11-16 23:44:32 -07:00
let teamName = state.TeamNames[teamID] ?? "rodney"
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
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
2023-11-16 20:07:49 -07:00
let categoryNumber = [...scores.Categories].indexOf(category)
2023-09-15 15:17:07 -06:00
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
}
for (let e of document.querySelectorAll(".no-scores")) {
e.innerHTML = ScoreboardConfig.NoScoresHtml
e.classList.toggle("hidden", frame > 0)
}
2020-02-29 17:12:35 -07:00
}
2023-11-16 23:44:32 -07:00
/**
*
* @param {*} rankingsElement
* @param {moth.State} state
* @param {*} ScoreboardConfig
*/
async function categoryRankings(rankingsElement, state, ScoreboardConfig) {
while (rankingsElement.firstChild) rankingsElement.firstChild.remove()
let scores = state.CurrentScores()
for (let category of scores.Categories) {
let categoryBox = rankingsElement.appendChild(document.createElement("div"))
categoryBox.classList.add("category")
categoryBox.appendChild(document.createElement("h2")).textContent = category
let categoryScores = []
for (let teamID in state.TeamNames) {
categoryScores.push({
teamName: state.TeamNames[teamID],
score: scores.GetPoints(category, teamID),
})
}
categoryScores.sort((a, b) => b.score - a.score)
let table = categoryBox.appendChild(document.createElement("table"))
let rows = 0
for (let categoryScore of categoryScores) {
let row = table.appendChild(document.createElement("tr"))
row.appendChild(document.createElement("td")).textContent = categoryScore.teamName
let td = row.appendChild(document.createElement("td"))
td.textContent = categoryScore.score
td.classList.add("number")
rows += 1
if (rows == 5) {
break
}
}
}
}
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)