homepage/content/blog/clrg/2022-10-09-CLRG-Scoring/scorecard.mjs

66 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-10-09 20:59:33 -06:00
import {awardPoints} from "./awardPoints.mjs"
function scorecardUpdate(scorecard) {
let scores = []
let points = []
2022-10-09 22:34:44 -06:00
let highestRank = []
2022-10-09 20:59:33 -06:00
let firstRow = scorecard.querySelector("tbody tr")
for (let input of firstRow.querySelectorAll("input")) {
scores.push(0)
points.push(0)
}
for (let row of scorecard.querySelectorAll("tbody tr")) {
let i = 0
for (let input of row.querySelectorAll("input")) {
let ranking = Number(input.value)
scores[i] += ranking
points[i] += awardPoints[ranking]
2022-10-09 22:34:44 -06:00
highestRank[i] = Math.min(highestRank[i] || 100, ranking)
2022-10-09 20:59:33 -06:00
i += 1
}
}
{
let i = 0
for (let out of scorecard.querySelectorAll("tfoot output[name='points']")) {
out.value = points[i]
i += 1
}
}
{
2022-10-09 22:34:44 -06:00
let rankOffset = 0
let overallRanking = []
2022-10-09 21:42:30 -06:00
let rankedPoints = [...points].sort((a, b) => b - a)
2022-10-09 22:34:44 -06:00
for (let i = 0; i < points.length; i++) {
overallRanking[i] = rankedPoints.indexOf(points[i]) + 1
if (overallRanking[i] == 1) {
rankOffset = highestRank[i]
}
}
2022-10-09 20:59:33 -06:00
let i = 0
for (let out of scorecard.querySelectorAll("tfoot output[name='ranking']")) {
2022-10-09 22:34:44 -06:00
out.value = rankedPoints.indexOf(points[i]) + rankOffset
2022-10-09 20:59:33 -06:00
i += 1
}
}
}
function init() {
for (let scorecard of document.querySelectorAll(".scorecard")) {
for (let input of scorecard.querySelectorAll("input")) {
input.addEventListener("input", () => scorecardUpdate(scorecard))
}
scorecardUpdate(scorecard)
}
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init)
} else {
init()
}