2023-09-12 17:30:36 -06:00
|
|
|
/**
|
|
|
|
* Hash/digest functions
|
|
|
|
*/
|
|
|
|
class Hash {
|
|
|
|
/**
|
|
|
|
* Dan Bernstein hash
|
|
|
|
*
|
|
|
|
* Used until MOTH v3.5
|
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} buf Input
|
|
|
|
* @returns {number}
|
2023-09-12 17:30:36 -06:00
|
|
|
*/
|
|
|
|
static djb2(buf) {
|
|
|
|
let h = 5381
|
|
|
|
for (let c of (new TextEncoder()).encode(buf)) { // Encode as UTF-8 and read in each byte
|
|
|
|
// JavaScript converts everything to a signed 32-bit integer when you do bitwise operations.
|
|
|
|
// So we have to do "unsigned right shift" by zero to get it back to unsigned.
|
2023-09-12 19:30:53 -06:00
|
|
|
h = ((h * 33) + c) >>> 0
|
2023-09-12 17:30:36 -06:00
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-15 15:17:07 -06:00
|
|
|
* Dan Bernstein hash with xor
|
2023-09-12 17:30:36 -06:00
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} buf Input
|
|
|
|
* @returns {number}
|
2023-09-12 17:30:36 -06:00
|
|
|
*/
|
|
|
|
static djb2xor(buf) {
|
|
|
|
let h = 5381
|
|
|
|
for (let c of (new TextEncoder()).encode(buf)) {
|
2023-09-12 19:30:53 -06:00
|
|
|
h = ((h * 33) ^ c) >>> 0
|
2023-09-12 17:30:36 -06:00
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SHA 256
|
|
|
|
*
|
|
|
|
* Used until MOTH v4.5
|
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} buf Input
|
|
|
|
* @returns {Promise.<string>} hex-encoded digest
|
2023-09-12 17:30:36 -06:00
|
|
|
*/
|
2023-09-12 19:30:53 -06:00
|
|
|
static async sha256(buf) {
|
|
|
|
const msgUint8 = new TextEncoder().encode(buf)
|
|
|
|
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8)
|
|
|
|
const hashArray = Array.from(new Uint8Array(hashBuffer))
|
|
|
|
return this.hexlify(hashArray);
|
|
|
|
}
|
2023-09-12 17:30:36 -06:00
|
|
|
|
2023-09-15 15:17:07 -06:00
|
|
|
/**
|
|
|
|
* SHA 1, but only the first 4 hexits (2 octets).
|
|
|
|
*
|
|
|
|
* Git uses this technique with 7 hexits (default) as a "short identifier".
|
|
|
|
*
|
|
|
|
* @param {string} buf Input
|
|
|
|
*/
|
|
|
|
static async sha1_slice(buf, end=4) {
|
|
|
|
const msgUint8 = new TextEncoder().encode(buf)
|
|
|
|
const hashBuffer = await crypto.subtle.digest("SHA-1", msgUint8)
|
|
|
|
const hashArray = Array.from(new Uint8Array(hashBuffer))
|
|
|
|
const hexits = this.hexlify(hashArray)
|
|
|
|
return hexits.slice(0, end)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hex-encode a byte array
|
|
|
|
*
|
|
|
|
* @param {number[]} buf Byte array
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
static hexlify(buf) {
|
|
|
|
return buf.map(b => b.toString(16).padStart(2, "0")).join("")
|
|
|
|
}
|
2023-09-12 19:30:53 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply every hash to the input buffer.
|
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} buf Input
|
|
|
|
* @returns {Promise.<string[]>}
|
2023-09-12 19:30:53 -06:00
|
|
|
*/
|
|
|
|
static async All(buf) {
|
|
|
|
return [
|
|
|
|
String(this.djb2(buf)),
|
|
|
|
await this.sha256(buf),
|
2023-09-15 15:17:07 -06:00
|
|
|
await this.sha1_slice(buf),
|
2023-09-12 19:30:53 -06:00
|
|
|
]
|
|
|
|
}
|
2023-09-12 17:30:36 -06:00
|
|
|
}
|
|
|
|
|
2023-09-07 16:16:46 -06:00
|
|
|
/**
|
|
|
|
* A point award.
|
|
|
|
*/
|
|
|
|
class Award {
|
|
|
|
constructor(when, teamid, category, points) {
|
|
|
|
/** Unix epoch timestamp for this award
|
2023-09-14 19:08:44 -06:00
|
|
|
* @type {number}
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
|
|
|
this.When = when
|
|
|
|
/** Team ID this award belongs to
|
2023-09-14 19:08:44 -06:00
|
|
|
* @type {string}
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
|
|
|
this.TeamID = teamid
|
|
|
|
/** Puzzle category for this award
|
2023-09-14 19:08:44 -06:00
|
|
|
* @type {string}
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
|
|
|
this.Category = category
|
|
|
|
/** Points value of this award
|
2023-09-14 19:08:44 -06:00
|
|
|
* @type {number}
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
|
|
|
this.Points = points
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A puzzle.
|
|
|
|
*
|
|
|
|
* A new Puzzle only knows its category and point value.
|
2023-09-07 17:29:21 -06:00
|
|
|
* If you want to populate it with meta-information, you must call Populate().
|
|
|
|
*
|
|
|
|
* Parameters created by Populate are described in the server source code:
|
|
|
|
* {@link https://pkg.go.dev/github.com/dirtbags/moth/v4/pkg/transpile#Puzzle}
|
|
|
|
*
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
|
|
|
class Puzzle {
|
|
|
|
/**
|
|
|
|
* @param {Server} server
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} category
|
|
|
|
* @param {number} points
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
|
|
|
constructor (server, category, points) {
|
|
|
|
if (points < 1) {
|
|
|
|
throw(`Invalid points value: ${points}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Server where this puzzle lives
|
|
|
|
* @type {Server}
|
|
|
|
*/
|
|
|
|
this.server = server
|
2023-09-07 17:29:21 -06:00
|
|
|
|
|
|
|
/** Category this puzzle belongs to */
|
|
|
|
this.Category = String(category)
|
|
|
|
|
|
|
|
/** Point value of this puzzle */
|
|
|
|
this.Points = Number(points)
|
2023-09-07 16:16:46 -06:00
|
|
|
|
2023-09-14 17:42:02 -06:00
|
|
|
/** Error returned trying to retrieve this puzzle */
|
2023-09-07 17:29:21 -06:00
|
|
|
this.Error = {
|
|
|
|
/** Status code provided by server */
|
|
|
|
Status: 0,
|
|
|
|
/** Status text provided by server */
|
|
|
|
StatusText: "",
|
|
|
|
/** Full text of server error */
|
|
|
|
Body: "",
|
|
|
|
}
|
2023-09-07 16:16:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate this Puzzle object with meta-information from the server.
|
|
|
|
*/
|
|
|
|
async Populate() {
|
|
|
|
let resp = await this.Get("puzzle.json")
|
|
|
|
if (!resp.ok) {
|
|
|
|
let body = await resp.text()
|
|
|
|
this.Error = {
|
|
|
|
Status: resp.status,
|
|
|
|
StatusText: resp.statusText,
|
|
|
|
Body: body,
|
|
|
|
}
|
|
|
|
throw(this.Error)
|
|
|
|
}
|
|
|
|
let obj = await resp.json()
|
|
|
|
Object.assign(this, obj)
|
|
|
|
|
|
|
|
// Make sure lists are lists
|
|
|
|
this.AnswerHashes ||= []
|
|
|
|
this.Answers ||= []
|
|
|
|
this.Attachments ||= []
|
|
|
|
this.Authors ||= []
|
|
|
|
this.Debug.Errors ||= []
|
|
|
|
this.Debug.Hints ||= []
|
|
|
|
this.Debug.Log ||= []
|
|
|
|
this.KSAs ||= []
|
|
|
|
this.Scripts ||= []
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a resource associated with this puzzle.
|
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} filename Attachment/Script to retrieve
|
2023-09-07 16:32:06 -06:00
|
|
|
* @returns {Promise.<Response>}
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
|
|
|
Get(filename) {
|
|
|
|
return this.server.GetContent(this.Category, this.Points, filename)
|
|
|
|
}
|
2023-09-12 17:30:36 -06:00
|
|
|
|
2023-09-12 19:30:53 -06:00
|
|
|
/**
|
|
|
|
* Check if a string is possibly correct.
|
|
|
|
*
|
|
|
|
* The server sends a list of answer hashes with each puzzle: this method
|
|
|
|
* checks to see if any of those hashes match a hash of the string.
|
|
|
|
*
|
|
|
|
* The MOTH development team likes obscure hash functions with a lot of
|
|
|
|
* collisions, which means that a given input may match another possible
|
|
|
|
* string's hash. We do this so that if you run a brute force attack against
|
|
|
|
* the list of hashes, you have to write your own brute force program, and
|
|
|
|
* you still have to pick through a lot of potentially correct answers when
|
|
|
|
* it's done.
|
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} str User-submitted possible answer
|
|
|
|
* @returns {Promise.<boolean>}
|
2023-09-12 19:30:53 -06:00
|
|
|
*/
|
2023-09-12 17:30:36 -06:00
|
|
|
async IsPossiblyCorrect(str) {
|
2023-09-12 19:30:53 -06:00
|
|
|
let userAnswerHashes = await Hash.All(str)
|
2023-09-12 17:30:36 -06:00
|
|
|
|
|
|
|
for (let pah of this.AnswerHashes) {
|
|
|
|
for (let uah of userAnswerHashes) {
|
|
|
|
if (pah == uah) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2023-09-13 18:52:52 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Submit a proposed answer for points.
|
|
|
|
*
|
|
|
|
* The returned promise will fail if anything goes wrong, including the
|
|
|
|
* proposed answer being rejected.
|
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} proposed Answer to submit
|
|
|
|
* @returns {Promise.<string>} Success message
|
2023-09-13 18:52:52 -06:00
|
|
|
*/
|
|
|
|
SubmitAnswer(proposed) {
|
|
|
|
return this.server.SubmitAnswer(this.Category, this.Points, proposed)
|
|
|
|
}
|
2023-09-07 16:16:46 -06:00
|
|
|
}
|
|
|
|
|
2023-09-15 15:17:07 -06:00
|
|
|
/**
|
|
|
|
* A snapshot of scores.
|
|
|
|
*/
|
|
|
|
class Scores {
|
|
|
|
constructor() {
|
|
|
|
/**
|
|
|
|
* Timestamp of this score snapshot
|
|
|
|
* @type number
|
|
|
|
*/
|
|
|
|
this.Timestamp = 0
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All categories present in this snapshot.
|
|
|
|
*
|
|
|
|
* ECMAScript sets preserve order, so iterating over this will yield
|
|
|
|
* categories as they were added to the points log.
|
|
|
|
*
|
|
|
|
* @type {Set.<string>}
|
|
|
|
*/
|
|
|
|
this.Categories = new Set()
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All team IDs present in this snapshot
|
|
|
|
* @type {Set.<string>}
|
|
|
|
*/
|
|
|
|
this.TeamIDs = new Set()
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Highest score in each category
|
|
|
|
* @type {Object.<string,number>}
|
|
|
|
*/
|
|
|
|
this.MaxPoints = {}
|
|
|
|
|
|
|
|
this.categoryTeamPoints = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a sorted list of category names
|
|
|
|
*
|
|
|
|
* @returns {string[]}
|
|
|
|
*/
|
|
|
|
SortedCategories() {
|
|
|
|
let categories = [...this.Categories]
|
|
|
|
categories.sort((a,b) => a.localeCompare(b, "en", {sensitivity: "base"}))
|
|
|
|
return categories
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an award to a team's score.
|
|
|
|
*
|
|
|
|
* Updates this.Timestamp to the award's timestamp.
|
|
|
|
*
|
|
|
|
* @param {Award} award
|
|
|
|
*/
|
|
|
|
Add(award) {
|
|
|
|
this.Timestamp = award.Timestamp
|
|
|
|
this.Categories.add(award.Category)
|
|
|
|
this.TeamIDs.add(award.TeamID)
|
|
|
|
|
|
|
|
let teamPoints = (this.categoryTeamPoints[award.Category] ??= {})
|
|
|
|
let points = (teamPoints[award.TeamID] || 0) + award.Points
|
|
|
|
teamPoints[award.TeamID] = points
|
|
|
|
|
|
|
|
let max = this.MaxPoints[award.Category] || 0
|
|
|
|
this.MaxPoints[award.Category] = Math.max(max, points)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a team's score within a category.
|
|
|
|
*
|
|
|
|
* @param {string} category
|
|
|
|
* @param {string} teamID
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
GetPoints(category, teamID) {
|
|
|
|
let teamPoints = this.categoryTeamPoints[category] || {}
|
|
|
|
return teamPoints[teamID] || 0
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate a team's score in a category, using the Cyber Fire algorithm.
|
|
|
|
*
|
|
|
|
*@param {string} category
|
|
|
|
* @param {string} teamID
|
|
|
|
*/
|
|
|
|
CyFiCategoryScore(category, teamID) {
|
|
|
|
return this.GetPoints(category, teamID) / this.MaxPoints[category]
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate a team's overall score, using the Cyber Fire algorithm.
|
|
|
|
*
|
|
|
|
*@param {string} category
|
|
|
|
* @param {string} teamID
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
CyFiScore(teamID) {
|
|
|
|
let score = 0
|
|
|
|
for (let category of this.Categories) {
|
|
|
|
score += this.CyFiCategoryScore(category, teamID)
|
|
|
|
}
|
|
|
|
return score
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-07 16:16:46 -06:00
|
|
|
/**
|
|
|
|
* MOTH instance state.
|
|
|
|
*/
|
|
|
|
class State {
|
|
|
|
/**
|
|
|
|
* @param {Server} server Server where we got this
|
|
|
|
* @param {Object} obj Raw state data
|
|
|
|
*/
|
|
|
|
constructor(server, obj) {
|
2023-09-19 16:48:24 -06:00
|
|
|
for (let key of ["Config", "TeamNames", "PointsLog"]) {
|
2023-09-07 16:16:46 -06:00
|
|
|
if (!obj[key]) {
|
|
|
|
throw(`Missing state property: ${key}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.server = server
|
|
|
|
|
|
|
|
/** Configuration */
|
|
|
|
this.Config = {
|
2023-09-13 18:52:52 -06:00
|
|
|
/** Is the server in development mode?
|
2023-09-14 19:08:44 -06:00
|
|
|
* @type {boolean}
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
2023-09-13 18:52:52 -06:00
|
|
|
Devel: obj.Config.Devel,
|
2023-09-07 16:16:46 -06:00
|
|
|
}
|
2023-09-13 18:52:52 -06:00
|
|
|
|
2023-09-07 16:16:46 -06:00
|
|
|
/** Global messages, in HTML
|
2023-09-14 19:08:44 -06:00
|
|
|
* @type {string}
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
|
|
|
this.Messages = obj.Messages
|
2023-09-13 18:52:52 -06:00
|
|
|
|
2023-09-07 16:16:46 -06:00
|
|
|
/** Map from Team ID to Team Name
|
2023-09-14 19:08:44 -06:00
|
|
|
* @type {Object.<string,string>}
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
|
|
|
this.TeamNames = obj.TeamNames
|
2023-09-13 18:52:52 -06:00
|
|
|
|
2023-09-07 16:16:46 -06:00
|
|
|
/** Map from category name to puzzle point values
|
2023-09-14 19:08:44 -06:00
|
|
|
* @type {Object.<string,number>}
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
|
|
|
this.PointsByCategory = obj.Puzzles
|
2023-09-13 18:52:52 -06:00
|
|
|
|
2023-09-07 16:16:46 -06:00
|
|
|
/** Log of points awarded
|
|
|
|
* @type {Award[]}
|
|
|
|
*/
|
2023-09-14 17:42:02 -06:00
|
|
|
this.PointsLog = obj.PointsLog.map(entry => new Award(entry[0], entry[1], entry[2], entry[3]))
|
2023-09-07 16:16:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a sorted list of open category names
|
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @returns {string[]} List of categories
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
|
|
|
Categories() {
|
|
|
|
let ret = []
|
|
|
|
for (let category in this.PointsByCategory) {
|
|
|
|
ret.push(category)
|
|
|
|
}
|
|
|
|
ret.sort()
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-14 17:42:02 -06:00
|
|
|
* Check whether a category contains unsolved puzzles.
|
2023-09-07 16:16:46 -06:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} category
|
|
|
|
* @returns {boolean}
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
2023-09-14 17:42:02 -06:00
|
|
|
ContainsUnsolved(category) {
|
2023-09-07 16:16:46 -06:00
|
|
|
return !this.PointsByCategory[category].includes(0)
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:52:52 -06:00
|
|
|
/**
|
|
|
|
* Is the server in development mode?
|
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @returns {boolean}
|
2023-09-13 18:52:52 -06:00
|
|
|
*/
|
|
|
|
DevelopmentMode() {
|
|
|
|
return this.Config && this.Config.Devel
|
|
|
|
}
|
|
|
|
|
2023-09-07 16:16:46 -06:00
|
|
|
/**
|
|
|
|
* Return all open puzzles.
|
|
|
|
*
|
|
|
|
* The returned list will be sorted by (category, points).
|
|
|
|
* If not categories are given, all puzzles will be returned.
|
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} categories Limit results to these categories
|
2023-09-07 16:16:46 -06:00
|
|
|
* @returns {Puzzle[]}
|
|
|
|
*/
|
|
|
|
Puzzles(...categories) {
|
|
|
|
if (categories.length == 0) {
|
|
|
|
categories = this.Categories()
|
|
|
|
}
|
|
|
|
let ret = []
|
|
|
|
for (let category of categories) {
|
|
|
|
for (let points of this.PointsByCategory[category]) {
|
|
|
|
if (0 == points) {
|
|
|
|
// This means all potential puzzles in the category are open
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let p = new Puzzle(this.server, category, points)
|
|
|
|
ret.push(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
2023-09-14 17:42:02 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Has this puzzle been solved by this team?
|
|
|
|
*
|
|
|
|
* @param {Puzzle} puzzle
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} teamID Team to check, default the logged-in team
|
|
|
|
* @returns {boolean}
|
2023-09-14 17:42:02 -06:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
}
|
2023-09-14 19:08:44 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replay scores.
|
|
|
|
*
|
2023-09-15 15:17:07 -06:00
|
|
|
* MOTH has no notion of who is "winning", we consider this a user interface
|
|
|
|
* decision. There are lots of interesting options: see
|
|
|
|
* [scoring]{@link ../docs/scoring.md} for more.
|
|
|
|
*
|
|
|
|
* @yields {Scores} Snapshot at a point in time
|
2023-09-14 19:08:44 -06:00
|
|
|
*/
|
2023-09-27 16:10:31 -06:00
|
|
|
* ScoresHistory() {
|
2023-09-15 15:17:07 -06:00
|
|
|
let scores = new Scores()
|
2023-09-14 19:08:44 -06:00
|
|
|
for (let award of this.PointsLog) {
|
2023-09-15 15:17:07 -06:00
|
|
|
scores.Add(award)
|
|
|
|
yield scores
|
2023-09-14 19:08:44 -06:00
|
|
|
}
|
|
|
|
}
|
2023-09-15 15:17:07 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the current scores.
|
|
|
|
*
|
|
|
|
* @returns {Scores}
|
|
|
|
*/
|
2023-09-27 16:10:31 -06:00
|
|
|
CurrentScores() {
|
2023-09-15 15:17:07 -06:00
|
|
|
let scores
|
|
|
|
for (scores of this.ScoreHistory());
|
|
|
|
return scores
|
|
|
|
}
|
2023-09-07 16:16:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A MOTH Server interface.
|
|
|
|
*
|
|
|
|
* This uses localStorage to remember Team ID,
|
|
|
|
* and will send a Team ID with every request, if it can find one.
|
|
|
|
*/
|
2023-09-01 17:59:09 -06:00
|
|
|
class Server {
|
2023-09-13 18:52:52 -06:00
|
|
|
/**
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string | URL} baseUrl Base URL to server, for constructing API URLs
|
2023-09-13 18:52:52 -06:00
|
|
|
*/
|
2023-09-01 17:59:09 -06:00
|
|
|
constructor(baseUrl) {
|
2023-09-13 18:52:52 -06:00
|
|
|
if (!baseUrl) {
|
|
|
|
throw("Must provide baseURL")
|
|
|
|
}
|
2023-09-07 16:16:46 -06:00
|
|
|
this.baseUrl = new URL(baseUrl, location)
|
2023-09-14 17:42:02 -06:00
|
|
|
this.teamIDKey = this.baseUrl.toString() + " teamID"
|
|
|
|
this.TeamID = localStorage[this.teamIDKey]
|
2023-09-01 17:59:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch a MOTH resource.
|
|
|
|
*
|
2023-09-07 16:16:46 -06:00
|
|
|
* If anything other than a 2xx code is returned,
|
|
|
|
* this function throws an error.
|
|
|
|
*
|
2023-09-14 17:42:02 -06:00
|
|
|
* This always sends teamID.
|
2023-09-13 18:52:52 -06:00
|
|
|
* If args is set, POST will be used instead of GET
|
2023-09-01 17:59:09 -06:00
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} path Path to API endpoint
|
|
|
|
* @param {Object.<string,string>} args Key/Values to send in POST data
|
2023-09-07 16:32:06 -06:00
|
|
|
* @returns {Promise.<Response>} Response
|
2023-09-01 17:59:09 -06:00
|
|
|
*/
|
2023-09-14 17:42:02 -06:00
|
|
|
fetch(path, args={}) {
|
|
|
|
let body = new URLSearchParams(args)
|
|
|
|
if (this.TeamID && !body.has("id")) {
|
|
|
|
body.set("id", this.TeamID)
|
2023-09-01 17:59:09 -06:00
|
|
|
}
|
2023-09-13 18:52:52 -06:00
|
|
|
|
2023-09-14 17:42:02 -06:00
|
|
|
let url = new URL(path, this.baseUrl)
|
|
|
|
return fetch(url, {
|
|
|
|
method: "POST",
|
|
|
|
body,
|
2023-09-15 16:09:08 -06:00
|
|
|
cache: "no-cache",
|
2023-09-14 17:42:02 -06:00
|
|
|
})
|
2023-09-01 17:59:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a request to a JSend API endpoint.
|
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} path Path to API endpoint
|
|
|
|
* @param {Object.<string,string>} args Key/Values to send in POST
|
2023-09-07 16:32:06 -06:00
|
|
|
* @returns {Promise.<Object>} JSend Data
|
2023-09-01 17:59:09 -06:00
|
|
|
*/
|
2023-09-14 17:42:02 -06:00
|
|
|
async call(path, args={}) {
|
2023-09-01 17:59:09 -06:00
|
|
|
let resp = await this.fetch(path, args)
|
|
|
|
let obj = await resp.json()
|
|
|
|
switch (obj.status) {
|
|
|
|
case "success":
|
|
|
|
return obj.data
|
2023-09-13 18:52:52 -06:00
|
|
|
case "fail":
|
2023-09-01 17:59:09 -06:00
|
|
|
throw new Error(obj.data.description || obj.data.short || obj.data)
|
|
|
|
case "error":
|
|
|
|
throw new Error(obj.message)
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown JSend status: ${obj.status}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:52:52 -06:00
|
|
|
/**
|
|
|
|
* Make a new URL for the given resource.
|
2023-09-14 17:42:02 -06:00
|
|
|
*
|
|
|
|
* The returned URL instance will be absolute, and immune to changes to the
|
|
|
|
* page that would affect relative URLs.
|
|
|
|
*
|
2023-09-13 18:52:52 -06:00
|
|
|
* @returns {URL}
|
|
|
|
*/
|
|
|
|
URL(url) {
|
|
|
|
return new URL(url, this.baseUrl)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Are we logged in to the server?
|
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @returns {boolean}
|
2023-09-13 18:52:52 -06:00
|
|
|
*/
|
|
|
|
LoggedIn() {
|
2023-09-14 17:42:02 -06:00
|
|
|
return this.TeamID ? true : false
|
2023-09-13 18:52:52 -06:00
|
|
|
}
|
|
|
|
|
2023-09-07 16:16:46 -06:00
|
|
|
/**
|
|
|
|
* Forget about any previous Team ID.
|
|
|
|
*
|
|
|
|
* This is equivalent to logging out.
|
|
|
|
*/
|
|
|
|
Reset() {
|
2023-09-14 17:42:02 -06:00
|
|
|
localStorage.removeItem(this.teamIDKey)
|
|
|
|
this.TeamID = null
|
2023-09-07 16:16:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch current contest state.
|
|
|
|
*
|
2023-09-13 18:52:52 -06:00
|
|
|
* @returns {Promise.<State>}
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
|
|
|
async GetState() {
|
|
|
|
let resp = await this.fetch("/state")
|
|
|
|
let obj = await resp.json()
|
|
|
|
return new State(this, obj)
|
|
|
|
}
|
|
|
|
|
2023-09-01 17:59:09 -06:00
|
|
|
/**
|
2023-09-13 18:52:52 -06:00
|
|
|
* Log in to a team.
|
|
|
|
*
|
|
|
|
* This calls the server's registration endpoint; if the call succeds, or
|
|
|
|
* fails with "team already exists", the login is returned as successful.
|
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} teamID
|
|
|
|
* @param {string} teamName
|
|
|
|
* @returns {Promise.<string>} Success message from server
|
2023-09-01 17:59:09 -06:00
|
|
|
*/
|
2023-09-14 17:42:02 -06:00
|
|
|
async Login(teamID, teamName) {
|
|
|
|
let data = await this.call("/register", {id: teamID, name: teamName})
|
|
|
|
this.TeamID = teamID
|
2023-09-13 18:52:52 -06:00
|
|
|
this.TeamName = teamName
|
2023-09-14 17:42:02 -06:00
|
|
|
localStorage[this.teamIDKey] = teamID
|
2023-09-01 17:59:09 -06:00
|
|
|
return data.description || data.short
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-13 18:52:52 -06:00
|
|
|
* Submit a proposed answer for points.
|
2023-09-07 16:16:46 -06:00
|
|
|
*
|
|
|
|
* The returned promise will fail if anything goes wrong, including the
|
2023-09-13 18:52:52 -06:00
|
|
|
* proposed answer being rejected.
|
2023-09-07 16:16:46 -06:00
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} category Category of puzzle
|
|
|
|
* @param {number} points Point value of puzzle
|
|
|
|
* @param {string} proposed Answer to submit
|
|
|
|
* @returns {Promise.<string>} Success message
|
2023-09-07 16:16:46 -06:00
|
|
|
*/
|
2023-09-13 18:52:52 -06:00
|
|
|
async SubmitAnswer(category, points, proposed) {
|
|
|
|
let data = await this.call("/answer", {
|
|
|
|
cat: category,
|
|
|
|
points,
|
|
|
|
answer: proposed,
|
|
|
|
})
|
|
|
|
return data.description || data.short
|
2023-09-07 16:16:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch a file associated with a puzzle.
|
2023-09-01 17:59:09 -06:00
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} category Category of puzzle
|
|
|
|
* @param {number} points Point value of puzzle
|
|
|
|
* @param {string} filename
|
2023-09-07 16:32:06 -06:00
|
|
|
* @returns {Promise.<Response>}
|
2023-09-01 17:59:09 -06:00
|
|
|
*/
|
2023-09-07 16:16:46 -06:00
|
|
|
GetContent(category, points, filename) {
|
|
|
|
return this.fetch(`/content/${category}/${points}/${filename}`)
|
2023-09-01 17:59:09 -06:00
|
|
|
}
|
2023-09-08 18:05:51 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a Puzzle object.
|
|
|
|
*
|
|
|
|
* New Puzzle objects only know their category and point value.
|
|
|
|
* See docstrings on the Puzzle object for more information.
|
|
|
|
*
|
2023-09-14 19:08:44 -06:00
|
|
|
* @param {string} category
|
|
|
|
* @param {number} points
|
2023-09-08 18:05:51 -06:00
|
|
|
* @returns {Puzzle}
|
|
|
|
*/
|
|
|
|
GetPuzzle(category, points) {
|
|
|
|
return new Puzzle(this, category, points)
|
|
|
|
}
|
2023-09-01 17:59:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2023-09-12 17:30:36 -06:00
|
|
|
Hash,
|
|
|
|
Server,
|
2023-09-01 17:59:09 -06:00
|
|
|
}
|