Compare commits

..

No commits in common. "c0761933a993a8af1df5fe12e52f9c525c111f47" and "d87be0bfcb5d9d15d38096e6fa992c3c66db29cb" have entirely different histories.

14 changed files with 113 additions and 48619 deletions

View File

@ -9,7 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- We are now using djb2xor instead of sha256 to hash puzzle answers - We are now using djb2xor instead of sha256 to hash puzzle answers
- Lots of work on the built-in theme - Lots of work on the built-in theme
- [moth.mjs](theme/moth.mjs) is now the standard MOTH library for ECMAScript - [moth.mjs](theme/moth.mjs) is now the standard MOTH library for ECMAScript
- Devel mode no longer accepts an empty team ID
## [v4.4.9] - 2022-05-12 ## [v4.4.9] - 2022-05-12
### Changed ### Changed

View File

@ -128,7 +128,7 @@ func TestHttpd(t *testing.T) {
if r := hs.TestRequest("/answer", map[string]string{"cat": "pategory", "points": "1", "answer": "answer123"}); r.Result().StatusCode != 200 { if r := hs.TestRequest("/answer", map[string]string{"cat": "pategory", "points": "1", "answer": "answer123"}); r.Result().StatusCode != 200 {
t.Error(r.Result()) t.Error(r.Result())
} else if r.Body.String() != `{"status":"fail","data":{"short":"not accepted","description":"points already awarded to this team in this category"}}` { } else if r.Body.String() != `{"status":"fail","data":{"short":"not accepted","description":"error awarding points: points already awarded to this team in this category"}}` {
t.Error("Unexpected body", r.Body.String()) t.Error("Unexpected body", r.Body.String())
} }
} }

View File

@ -156,7 +156,7 @@ func (mh *MothRequestHandler) CheckAnswer(cat string, points int, answer string)
return fmt.Errorf("invalid team ID") return fmt.Errorf("invalid team ID")
} }
if err := mh.State.AwardPoints(mh.teamID, cat, points); err != nil { if err := mh.State.AwardPoints(mh.teamID, cat, points); err != nil {
return err return fmt.Errorf("error awarding points: %s", err)
} }
return nil return nil

View File

@ -522,9 +522,6 @@ func (ds *DevelState) TeamName(teamID string) (string, error) {
if name, err := ds.StateProvider.TeamName(teamID); err == nil { if name, err := ds.StateProvider.TeamName(teamID); err == nil {
return name, nil return name, nil
} }
if teamID == "" {
return "", fmt.Errorf("Empty team ID")
}
return fmt.Sprintf("«devel:%s»", teamID), nil return fmt.Sprintf("«devel:%s»", teamID), nil
} }

View File

@ -75,9 +75,6 @@ input {
.category h2 { .category h2 {
margin: 0 0.2em; margin: 0 0.2em;
} }
.category .solved {
text-decoration: line-through;
}
nav ul, .category ul { nav ul, .category ul {
margin: 0; margin: 0;
padding: 0.2em 1em; padding: 0.2em 1em;

View File

@ -34,30 +34,10 @@ function WhenDOMLoaded(cb) {
} }
} }
/**
* Interprets a String as a Boolean.
*
* Values like "no" or "disabled" to mean false here.
*
* @param {String} s
* @returns {Boolean}
*/
function Truthy(s) {
switch (s.toLowerCase()) {
case "disabled":
case "no":
case "off":
case "false":
return false
}
return true
}
export { export {
Millisecond, Millisecond,
Second, Second,
Minute, Minute,
Toast, Toast,
WhenDOMLoaded, WhenDOMLoaded,
Truthy,
} }

View File

@ -1,4 +0,0 @@
{
"TrackSolved": true,
"__sentry__": "this is here so you don't have to remember to take the comma off the last item"
}

View File

@ -22,19 +22,9 @@
</form> </form>
<div class="puzzles"></div> <div class="puzzles"></div>
</main>
<div class="notification" data-track-solved="no">
<p>
Solved puzzle tracking: <b>disabled</b>.
</p>
<p>
Your team's Incident Coordinator can help coordinate team activity.
</p>
</div>
<div class="toasts"></div> <div class="toasts"></div>
</main>
<nav> <nav>
<ul> <ul>
<li><a href="scoreboard.html" target="_blank">Scoreboard</a></li> <li><a href="scoreboard.html" target="_blank">Scoreboard</a></li>

View File

@ -6,14 +6,11 @@ import * as common from "./common.mjs"
class App { class App {
constructor(basePath=".") { constructor(basePath=".") {
this.configURL = new URL("config.json", location)
this.config = {}
this.server = new moth.Server(basePath) this.server = new moth.Server(basePath)
let uuid = Math.floor(Math.random() * 1000000).toString(16) let uuid = Math.floor(Math.random() * 1000000).toString(16)
this.fakeRegistration = { this.fakeRegistration = {
TeamID: uuid, TeamId: uuid,
TeamName: `Team ${uuid}`, TeamName: `Team ${uuid}`,
} }
@ -24,11 +21,8 @@ class App {
e.addEventListener("click", () => this.Logout()) e.addEventListener("click", () => this.Logout())
} }
setInterval(() => this.UpdateState(), common.Minute/3) setInterval(() => this.Update(), common.Minute/3)
setInterval(() => this.UpdateConfig(), common.Minute* 5) this.Update()
this.UpdateConfig()
.finally(() => this.UpdateState())
} }
handleLoginSubmit(event) { handleLoginSubmit(event) {
@ -39,14 +33,14 @@ class App {
/** /**
* Attempt to log in to the server. * Attempt to log in to the server.
* *
* @param {String} teamID * @param {String} teamId
* @param {String} teamName * @param {String} teamName
*/ */
async Login(teamID, teamName) { async Login(teamId, teamName) {
try { try {
await this.server.Login(teamID, teamName) await this.server.Login(teamId, teamName)
common.Toast(`Logged in (team id = ${teamID})`) common.Toast(`Logged in (team id = ${teamId})`)
this.UpdateState() this.Update()
} }
catch (error) { catch (error) {
common.Toast(error) common.Toast(error)
@ -60,24 +54,13 @@ class App {
try { try {
this.server.Reset() this.server.Reset()
common.Toast("Logged out") common.Toast("Logged out")
this.UpdateState() this.Update()
} }
catch (error) { catch (error) {
common.Toast(error) common.Toast(error)
} }
} }
/**
* Update app configuration.
*
* Configuration can be updated less frequently than state, to reduce server
* load, since configuration should (hopefully) change less frequently.
*/
async UpdateConfig() {
let resp = await fetch(this.configURL)
this.config = await resp.json()
}
/** /**
* Update the entire page. * Update the entire page.
* *
@ -85,18 +68,12 @@ class App {
* what's returned. If we're in development mode and not logged in, auto * what's returned. If we're in development mode and not logged in, auto
* login too. * login too.
*/ */
async UpdateState() { async Update() {
this.state = await this.server.GetState() this.state = await this.server.GetState()
for (let e of document.querySelectorAll(".messages")) { for (let e of document.querySelectorAll(".messages")) {
e.innerHTML = this.state.Messages e.innerHTML = this.state.Messages
} }
// Update elements with data-track-solved
for (let e of document.querySelectorAll("[data-track-solved]")) {
// Only display if data-track-solved is the same as config.trackSolved
e.classList.toggle("hidden", common.Truthy(e.dataset.trackSolved) != this.config.TrackSolved)
}
for (let e of document.querySelectorAll(".login")) { for (let e of document.querySelectorAll(".login")) {
this.renderLogin(e, !this.server.LoggedIn()) this.renderLogin(e, !this.server.LoggedIn())
} }
@ -107,7 +84,7 @@ class App {
if (this.state.DevelopmentMode() && !this.server.LoggedIn()) { if (this.state.DevelopmentMode() && !this.server.LoggedIn()) {
common.Toast("Automatically logging in to devel server") common.Toast("Automatically logging in to devel server")
console.info("Logging in with generated Team ID and Team Name", this.fakeRegistration) console.info("Logging in with generated Team ID and Team Name", this.fakeRegistration)
return this.Login(this.fakeRegistration.TeamID, this.fakeRegistration.TeamName) return this.Login(this.fakeRegistration.TeamId, this.fakeRegistration.TeamName)
} }
} }
@ -156,13 +133,9 @@ class App {
a.textContent = puzzle.Points a.textContent = puzzle.Points
a.href = url a.href = url
a.target = "_blank" a.target = "_blank"
if (this.config.TrackSolved) {
a.classList.toggle("solved", this.state.IsSolved(puzzle))
}
} }
if (!this.state.ContainsUnsolved(cat)) { if (!this.state.HasUnsolved(cat)) {
l.appendChild(document.createElement("li")).textContent = "✿" l.appendChild(document.createElement("li")).textContent = "✿"
} }

View File

@ -130,7 +130,7 @@ class Puzzle {
/** Point value of this puzzle */ /** Point value of this puzzle */
this.Points = Number(points) this.Points = Number(points)
/** Error returned trying to retrieve this puzzle */ /** Error returned trying to fetch this puzzle */
this.Error = { this.Error = {
/** Status code provided by server */ /** Status code provided by server */
Status: 0, Status: 0,
@ -265,7 +265,7 @@ class State {
/** Log of points awarded /** Log of points awarded
* @type {Award[]} * @type {Award[]}
*/ */
this.PointsLog = obj.PointsLog.map(entry => new Award(entry[0], entry[1], entry[2], entry[3])) this.PointsLog = obj.PointsLog.map((t,i,c,p) => new Award(t,i,c,p))
} }
/** /**
@ -283,7 +283,7 @@ class State {
} }
/** /**
* Check whether a category contains unsolved puzzles. * Check whether a category has unsolved puzzles.
* *
* The server adds a puzzle with 0 points in every "solved" category, * The server adds a puzzle with 0 points in every "solved" category,
* so this just checks whether there is a 0-point puzzle in the category's point list. * so this just checks whether there is a 0-point puzzle in the category's point list.
@ -291,7 +291,7 @@ class State {
* @param {String} category * @param {String} category
* @returns {Boolean} * @returns {Boolean}
*/ */
ContainsUnsolved(category) { HasUnsolved(category) {
return !this.PointsByCategory[category].includes(0) return !this.PointsByCategory[category].includes(0)
} }
@ -330,26 +330,6 @@ class State {
} }
return ret return ret
} }
/**
* Has this puzzle been solved by this team?
*
* @param {Puzzle} puzzle
* @param {String} teamID Team to check, default the logged-in team
* @returns {Boolean}
*/
IsSolved(puzzle, teamID="self") {
for (let award of this.PointsLog) {
if (
(award.Category == puzzle.Category)
&& (award.Points == puzzle.Points)
&& (award.TeamID == teamID)
) {
return true
}
}
return false
}
} }
/** /**
@ -367,8 +347,8 @@ class Server {
throw("Must provide baseURL") throw("Must provide baseURL")
} }
this.baseUrl = new URL(baseUrl, location) this.baseUrl = new URL(baseUrl, location)
this.teamIDKey = this.baseUrl.toString() + " teamID" this.teamIdKey = this.baseUrl.toString() + " teamID"
this.TeamID = localStorage[this.teamIDKey] this.TeamId = localStorage[this.teamIdKey]
} }
/** /**
@ -377,25 +357,31 @@ class Server {
* If anything other than a 2xx code is returned, * If anything other than a 2xx code is returned,
* this function throws an error. * this function throws an error.
* *
* This always sends teamID. * This always sends teamId.
* If args is set, POST will be used instead of GET * If args is set, POST will be used instead of GET
* *
* @param {String} path Path to API endpoint * @param {String} path Path to API endpoint
* @param {Object.<String,String>} args Key/Values to send in POST data * @param {Object.<String,String>} args Key/Values to send in POST data
* @returns {Promise.<Response>} Response * @returns {Promise.<Response>} Response
*/ */
fetch(path, args={}) { fetch(path, args) {
let body = new URLSearchParams(args) let url = new URL(path, this.baseUrl)
if (this.TeamID && !body.has("id")) { if (this.TeamId & (!(args && args.id))) {
body.set("id", this.TeamID) url.searchParams.set("id", this.TeamId)
} }
let url = new URL(path, this.baseUrl) if (args) {
let formData = new FormData()
for (let k in args) {
formData.set(k, args[k])
}
return fetch(url, { return fetch(url, {
method: "POST", method: "POST",
body, body: formData,
}) })
} }
return fetch(url)
}
/** /**
* Send a request to a JSend API endpoint. * Send a request to a JSend API endpoint.
@ -404,7 +390,7 @@ class Server {
* @param {Object.<String,String>} args Key/Values to send in POST * @param {Object.<String,String>} args Key/Values to send in POST
* @returns {Promise.<Object>} JSend Data * @returns {Promise.<Object>} JSend Data
*/ */
async call(path, args={}) { async call(path, args) {
let resp = await this.fetch(path, args) let resp = await this.fetch(path, args)
let obj = await resp.json() let obj = await resp.json()
switch (obj.status) { switch (obj.status) {
@ -422,9 +408,6 @@ class Server {
/** /**
* Make a new URL for the given resource. * Make a new URL for the given resource.
* *
* The returned URL instance will be absolute, and immune to changes to the
* page that would affect relative URLs.
*
* @returns {URL} * @returns {URL}
*/ */
URL(url) { URL(url) {
@ -437,7 +420,7 @@ class Server {
* @returns {Boolean} * @returns {Boolean}
*/ */
LoggedIn() { LoggedIn() {
return this.TeamID ? true : false return this.TeamId ? true : false
} }
/** /**
@ -446,8 +429,8 @@ class Server {
* This is equivalent to logging out. * This is equivalent to logging out.
*/ */
Reset() { Reset() {
localStorage.removeItem(this.teamIDKey) localStorage.removeItem(this.teamIdKey)
this.TeamID = null this.TeamId = null
} }
/** /**
@ -467,15 +450,15 @@ class Server {
* This calls the server's registration endpoint; if the call succeds, or * This calls the server's registration endpoint; if the call succeds, or
* fails with "team already exists", the login is returned as successful. * fails with "team already exists", the login is returned as successful.
* *
* @param {String} teamID * @param {String} teamId
* @param {String} teamName * @param {String} teamName
* @returns {Promise.<String>} Success message from server * @returns {Promise.<String>} Success message from server
*/ */
async Login(teamID, teamName) { async Login(teamId, teamName) {
let data = await this.call("/register", {id: teamID, name: teamName}) let data = await this.call("/register", {id: teamId, name: teamName})
this.TeamID = teamID this.TeamId = teamId
this.TeamName = teamName this.TeamName = teamName
localStorage[this.teamIDKey] = teamID localStorage[this.teamIdKey] = teamId
return data.description || data.short return data.description || data.short
} }

View File

@ -26,11 +26,11 @@ async function formSubmitHandler(event) {
console.info(`Proposed answer: ${proposed}`) console.info(`Proposed answer: ${proposed}`)
try { try {
message = await window.app.puzzle.SubmitAnswer(proposed) message = await window.app.puzzle.SubmitAnswer(proposed)
common.Toast(message)
} }
catch (err) { catch (err) {
common.Toast(err) common.Toast(err)
} }
common.Toast(message)
console.groupEnd("Submit answer") console.groupEnd("Submit answer")
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,35 +1,22 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html>
<head> <head>
<title>KSA Report</title> <title>KSA Report</title>
<meta name="viewport" content="width=device-width"> <script src="ksa.mjs" type="module"></script>
<meta charset="utf-8">
<script src="ksa.mjs" type="module" async></script>
<script src="../background.mjs" type="module" async></script>
<link rel="stylesheet" href="../basic.css"> <link rel="stylesheet" href="../basic.css">
</head> </head>
<body> <body>
<h1>KSA Report</h1> <h1>KSA Report</h1>
<main>
<p> <p>
This report shows all KSAs covered by this server so far. This report shows all KSAs covered by this server so far.
This is not a report on your progress, but rather This is not a report on your progress, but rather
what you would have covered if you had worked every exercise available. what you would have covered if you had worked every exercise available.
</p> </p>
<div class="notification">
<p class="doing"></p>
<progress class="doing"></progress>
</div>
<h2>All KSAs across all content</h2>
<ul class="allKSAs"></ul>
<h2>All KSAs by Category</h2>
<div class="KSAsByCategory"> <div class="KSAsByCategory">
<h2>All KSAs by Category</h2>
</div> </div>
<h2>KSAs by Puzzle</h2>
<table class="puzzles"> <table class="puzzles">
<thead> <thead>
<tr> <tr>
@ -50,6 +37,5 @@
</template> </template>
</tbody> </tbody>
</table> </table>
</main>
</body> </body>
</html> </html>

View File

@ -1,140 +1,73 @@
import * as moth from "../moth.mjs" import * as moth from "../moth.mjs"
import * as common from "../common.mjs"
const server = new moth.Server("../") function doing(what) {
/**
* Update "doing" indicators
*
* @param {String | null} what Text to display, or null to not update text
* @param {Number | null} finished Percentage complete to display, or null to not update progress
*/
function doing(what, finished = null) {
for (let e of document.querySelectorAll(".doing")) { for (let e of document.querySelectorAll(".doing")) {
e.classList.remove("hidden")
if (what) { if (what) {
e.textContent = what e.style.display = "inherit"
}
if (finished) {
e.value = finished
} else { } else {
e.removeAttribute("value") e.style.display = "none"
} }
for (let p of e.querySelectorAll("p")) {
p.textContent = what
} }
} }
function done() {
for (let e of document.querySelectorAll(".doing")) {
e.classList.add("hidden")
}
}
async function GetNice() {
let NiceElementsByIdentifier = {}
let resp = await fetch("NICEFramework2017.json")
let obj = await resp.json()
for (let e of obj.elements) {
NiceElementsByIdentifier[e.element_identifier] = e
}
return NiceElementsByIdentifier
}
/**
* Fetch a puzzle, and fill its KSAs and rows.
*
* This is done once per puzzle, in an asynchronous function, allowing the
* application to perform multiple blocking operations simultaneously.
*/
async function FetchAndFill(puzzle, KSAs, rows) {
try {
await puzzle.Populate()
}
catch (error) {
// Keep on going with whatever Populate was able to fill
}
for (let KSA of (puzzle.KSAs || [])) {
KSAs.add(KSA)
}
for (let row of rows) {
row.querySelector(".category").textContent = puzzle.Category
row.querySelector(".points").textContent = puzzle.Points
row.querySelector(".ksas").textContent = (puzzle.KSAs || []).join(" ")
row.querySelector(".error").textContent = puzzle.Error.Body
}
} }
async function init() { async function init() {
doing("Fetching NICE framework data") let server = new moth.Server("../")
let nicePromise = GetNice()
doing("Retrieving server state") doing("Retrieving server state")
let state = await server.GetState() let state = await server.GetState()
doing("Retrieving all puzzles") doing("Retrieving all puzzles")
let KSAsByCategory = {}
let puzzlerowTemplate = document.querySelector("template#puzzlerow") let puzzlerowTemplate = document.querySelector("template#puzzlerow")
let puzzles = state.Puzzles() let puzzles = state.Puzzles()
let promises = []
for (let category of state.Categories()) {
KSAsByCategory[category] = new Set()
}
let pending = puzzles.length
for (let puzzle of puzzles) { for (let puzzle of puzzles) {
// Make space in the table, so everything fills in sorted order await puzzle.Populate().catch(x => {})
let rows = [] }
doing("Filling tables")
let KSAsByCategory = {}
for (let puzzle of puzzles) {
let KSAs = KSAsByCategory[puzzle.Category]
if (!KSAs) {
KSAs = new Set()
KSAsByCategory[puzzle.Category] = KSAs
}
for (let KSA of (puzzle.KSAs || [])) {
KSAs.add(KSA)
}
for (let tbody of document.querySelectorAll("tbody")) { for (let tbody of document.querySelectorAll("tbody")) {
let row = puzzlerowTemplate.content.cloneNode(true).firstElementChild let row = puzzlerowTemplate.content.cloneNode(true)
row.querySelector(".category").textContent = puzzle.Category
row.querySelector(".points").textContent = puzzle.Points
row.querySelector(".ksas").textContent = (puzzle.KSAs || []).join(" ")
row.querySelector(".error").textContent = puzzle.Error.Body
tbody.appendChild(row) tbody.appendChild(row)
rows.push(row)
}
// Queue up a fetch, and update progress bar
let promise = FetchAndFill(puzzle, KSAsByCategory[puzzle.Category], rows)
promises.push(promise)
promise.then(() => doing(null, 1 - (--pending / puzzles.length)))
if (promises.length > 50) {
// Chrome runs out of resources if you queue up too many of these at once
await Promise.all(promises)
promises = []
} }
} }
await Promise.all(promises)
doing("Retrieving NICE identifiers")
let NiceElementsByIdentifier = await nicePromise
doing("Filling KSAs By Category") doing("Filling KSAs By Category")
let allKSAs = new Set()
for (let div of document.querySelectorAll(".KSAsByCategory")) { for (let div of document.querySelectorAll(".KSAsByCategory")) {
for (let category of state.Categories()) { for (let category of state.Categories()) {
doing(`Filling KSAs for category: ${category}`)
let KSAs = [...KSAsByCategory[category]] let KSAs = [...KSAsByCategory[category]]
KSAs.sort() KSAs.sort()
div.appendChild(document.createElement("h3")).textContent = category div.appendChild(document.createElement("h3")).textContent = category
let ul = div.appendChild(document.createElement("ul")) let ul = div.appendChild(document.createElement("ul"))
for (let k of KSAs) { for (let k of KSAs) {
let ksa = k.split(/\s+/)[0] ul.appendChild(document.createElement("li")).textContent = k
let ne = NiceElementsByIdentifier[ksa] || { text: "???" }
let text = `${ksa}: ${ne.text}`
ul.appendChild(document.createElement("li")).textContent = text
allKSAs.add(text)
} }
} }
} }
doing("Filling KSAs") doing()
for (let e of document.querySelectorAll(".allKSAs")) {
let KSAs = [...allKSAs]
KSAs.sort()
for (let text of KSAs) {
e.appendChild(document.createElement("li")).textContent = text
}
} }
done() if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init)
} else {
init()
} }
common.WhenDOMLoaded(init)