moth/docs/api.md

431 lines
11 KiB
Markdown
Raw Permalink Normal View History

2020-10-16 15:28:36 -06:00
Moth APIs
=======
2020-10-16 15:28:36 -06:00
This document covers the following interfaces:
2020-10-16 15:28:36 -06:00
* HTTP Endpoints: what the Moth client sends the Moth server
* Puzzle executable: how the transpiler communicates with executables that provide puzzles
* Category executable: how the transpiler communicates with executables that provide categories
* Provider executable: how Moth communicates with things that provide puzzles (like the transpiler)
The Puzzle, Category, and Provider executalbes are all very closely related, since each is a subset of the next.
----
Here's a bad diagram of how this all fits together. I don't know if this is going to help at all. Please submit a merge request with something better.
HTTP provider API mothball API
🡗 🡗 🡗
client - mothd - mothball provider - category1.mb
- custom provider
category API
🡗
- internal transpiler - category2/mkcategory
- category3/1/puzzle.md
- category3/2/mkpuzzle
🡔
puzzle API
# HTTP Endpoints
The Moth server accepts
standard HTTP `GET` and `POST`.
Parameters may be encoded with standard `GET` query parameters
(like `GET /endpoint?a=1&b=2`),
or with `POST` as `application/x-www-form-encoded` data.
2020-10-16 15:28:36 -06:00
## `/state`
2020-10-16 15:28:36 -06:00
Returns the current Moth event state as a JSON object.
2020-10-16 15:28:36 -06:00
### Parameters
* `id`: team ID (optional)
2020-10-16 15:28:36 -06:00
### Return
2020-10-15 10:23:39 -06:00
```js
{
"Config": {
2020-10-15 10:23:39 -06:00
"Devel": false // true means this is a development server
},
"TeamNames": {
2020-10-15 10:23:39 -06:00
"self": "Requesting team name", // Only if regestered team id is a provided
"0": "Team 1 Name",
2020-10-15 10:23:39 -06:00
"1": "Team 2 Name"
// ...
},
2020-10-15 10:23:39 -06:00
"PointsLog": [
[1602679698, "0", "category", 1] // epochTime, teamID, category, points
// ...
],
"Puzzles": {
2020-10-15 10:23:39 -06:00
"category": [1, 2, 3, 6] // list of unlocked puzzles for category
// ...
}
}
```
2020-10-16 15:28:36 -06:00
### Example HTTP transaction
2020-10-16 15:28:36 -06:00
#### Request
```
GET /state HTTP/1.0
```
2020-10-16 15:28:36 -06:00
#### Response
This response has been reflowed for readability:
an actual on-wire response would not have newlines or indentation.
```
HTTP/1.0 200 OK
Content-Type: application/json
{"Config":
{"Devel":false},
"TeamNames":{
"0":"Mike and Jack",
"12":"Team 2",
"4":"Team 8"
},
"PointsLog":[
[1602702696,"0","nocode",1],
[1602702705,"0","sequence",1],
[1602702787,"0","nocode",2],
[1602702831,"0","sequence",2],
[1602702839,"4","nocode",3],
[1602702896,"0","sequence",8],
[1602702900,"4","nocode",4],
[1602702913,"0","sequence",16]
],
"Puzzles":{
"indy":[12],
"nocode":[1,2,3,4,10],
"sequence":[1,2,8,16,19],
"steg":[1]
}
}
```
2020-10-16 15:28:36 -06:00
## `/register`
Registers a name to a team ID.
This is only required once per team,
but user interfaces may find it less confusing to users
to present a "login" page.
For this reason "this team is already registered"
does not return an error.
2020-10-16 15:28:36 -06:00
### Parameters
* `id`: team ID
* `name`: team name
2020-10-16 15:28:36 -06:00
### Return
An object inspired by [JSend](https://github.com/omniti-labs/jsend):
```json
{
"status": "success/fail/error",
"data": {
"short": "short description",
"description": "long description"
}
}
```
2020-10-16 15:28:36 -06:00
### Example HTTP transaction
2020-10-16 15:28:36 -06:00
#### Request
```
POST /register HTTP/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 26
id=b387ca98&name=dirtbags
```
2020-10-16 15:28:36 -06:00
#### Repsonse
```
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length=86
{"status":"success","data":{"short":"registered","description":"Team ID registered"}}
```
2020-10-16 15:28:36 -06:00
## `/answer`
Submits an answer for points.
If the answer is wrong, no points are awarded 😉
2020-10-16 15:28:36 -06:00
### Parameters
* `id`: team ID
* `category`: along with `points`, uniquely identifies a puzzle
* `points`: along with `category`, uniquely identifies a puzzle
2020-10-16 15:28:36 -06:00
### Return
An object inspired by [JSend](https://github.com/omniti-labs/jsend):
```json
{
"status": "success/fail/error",
"data": {
"short": "short description",
"description": "long description"
}
}
```
2020-10-16 15:28:36 -06:00
### Example HTTP transaction
2020-10-16 15:28:36 -06:00
#### Request
```
POST /answer HTTP/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 62
id=b387ca98&category=sequence&points=2&answer=achilles+turnip
```
2020-10-16 15:28:36 -06:00
#### Repsonse
```
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length=83
{"status":"fail","data":{"short":"not accepted","description":"Incorrect answer"}}
```
2020-10-16 15:28:36 -06:00
## `/content/{category}/{points}/puzzle.json`
2020-10-16 15:28:36 -06:00
Retrieves the JSON object describing a puzzle.
Parameters are all in the URL for this endpoint,
so `curl` and `wget` can be used.
2020-10-16 15:28:36 -06:00
### Parameters
* `{category}` (in URL): along with `{points}`, uniquely identifies a puzzle
* `{points}` (in URL): along with `{category}`, uniquely identifies a puzzle
* `{filename}` (in URL): filename to retrieve
2020-10-16 15:28:36 -06:00
### Return
2020-10-16 15:28:36 -06:00
JSON object describing a puzzle.
2020-10-16 15:39:20 -06:00
#### JSON Puzzle Object
2020-10-15 10:23:39 -06:00
```js
{
2020-10-15 10:23:39 -06:00
"Pre": { // Things which appear before the puzzle is solved
"Authors": ["Neale Pickett"], // List of puzzle authors, usually rendered as a footnote
"Attachments": ["tiger.jpg"], // List of files attached to the puzzle
"Scripts": [], // List of scripts which should be included in the HTML render of the puzzle
"Body": "<p>Can you find the hidden text?</p><p><img src=\"tiger.jpg\" alt=\"Grr\" /></p>\n", // HTML puzzle body
"AnswerPattern": "", // Regular expression to include in HTML input tag for validation
"AnswerHashes": [ // List of SHA265 hashes of correct answers, for client-side answer checking
"f91b1fe875cdf9e969e5bccd3e259adec5a987dcafcbc9ca8da62e341a7f29c6"
]
},
2020-10-15 10:23:39 -06:00
"Post": { // Things reveal after the puzzle is solved
"Objective": "Learn to examine images for hidden text", // Learning objective
"Success": { // Measures of learning success
"Acceptable": "Visually examine image to find hidden text",
"Mastery": "Visually examine image to find hidden text"
},
2020-10-15 10:23:39 -06:00
"KSAs": null // Knowledge, Skills, and Abilities covered by this puzzle
},
2020-10-15 10:23:39 -06:00
"Debug": { // Debugging output used in development: all fields are emptied when making mothballs
2020-10-16 15:28:36 -06:00
"Log": [ // Debug message log
"Input image size: 600x400",
"Applying gaussian blur",
"Text width 58, left offset 513",
"Complete in 0.028s"
],
2020-10-15 10:23:39 -06:00
"Errors": [], // Errors encountered generating this puzzzle
"Hints": [ // Hints for instructional assistants to provide to participants
"Zoom in to the image and examine all sections carefully"
],
2020-10-15 10:23:39 -06:00
"Summary": "text in image" // Summary of this puzzle, to help identify it in an overview of puzzles
},
2020-10-15 10:23:39 -06:00
"Answers": ["sandwich"] // List of answers: empty in production
}
```
2020-10-16 15:28:36 -06:00
### Example HTTP transaction
#### Request
```
GET /content/sequence/1/puzzle.json HTTP/1.0
```
#### Repsonse
```
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 397
{"Pre":{"Authors":["neale"],"Attachments":[],"Scripts":[],"Body":"\u003cp\u003e1 2 3 4 5 ⬜\u003c/p\u003e\n","AnswerPattern":"","AnswerHashes":["e7f6c011776e8db7cd330b54174fd76f7d0216b612387a5ffcfb81e6f0919683"]},"Post":{"Objective":"","Success":{"Acceptable":"","Mastery":""},"KSAs":null},"Debug":{"Log":[],"Errors":[],"Hints":[],"Summary":"Simple introduction to how this works"},"Answers":[]}
```
## `/content/{category}/{points}/{filename}`
Retrieves static content associated with a puzzle.
Parameters are all in the URL for this endpoint,
so `curl` and `wget` can be used.
### Parameters
* `{category}` (in URL): along with `{points}`, uniquely identifies a puzzle
* `{points}` (in URL): along with `{category}`, uniquely identifies a puzzle
* `{filename}` (in URL): filename to retrieve
### Return
Raw file octets,
with a (hopefully) suitable
`Content-type` HTTP header field.
### Example HTTP transaction
#### Request
```
GET /content/sequence/1/attachment.txt HTTP/1.0
```
#### Repsonse
```
HTTP/1.0 200 OK
Content-Type: text/plain
Content-Length: 98
This is an attachment file! This is just plain text for the example. Many attachments are JPEGs.
```
# Puzzle
A puzzle contains one question and one or more associated answers.
Puzzles are not aware of their point value: this is set by the category they are in.
Puzzle executables must be named `mkpuzzle`.
## `mkpuzzle puzzle`
2020-10-16 15:39:20 -06:00
puzzles/category3/1 $ ./mkpuzzle puzzle
{JSON PUZZLE OBJECT}
Also see [JSON Puzzle Object](#json-puzzle-object)
2020-10-16 15:28:36 -06:00
## `mkpuzzle file {filename}`
2020-10-16 15:39:20 -06:00
puzzles/category3/1 $ ./mkpuzzle file attachment.txt
This is an attachment file! It's just plain text for this example. Many attachments are JPEGs.
2020-10-16 15:28:36 -06:00
## `mkpuzzle answer {answer}`
2020-10-16 15:39:20 -06:00
puzzles/category3/1 $ ./mkpuzzle answer "cow goes moo"
{"Correct":false}
2020-10-16 15:28:36 -06:00
# Category
Categories are collections of puzzles.
Each puzzle has a unique point value, determined by the category.
Category executables must be called `mkcategory`.
## `mkcategory inventory`
2020-10-16 15:39:20 -06:00
puzzles/category2 $ ./mkcategory inventory
2020-10-27 09:50:06 -06:00
{"Puzzles": [1, 2, 3, 5, 10, 20, 30, 50, 100]}
2020-10-16 15:28:36 -06:00
## `mkcategory puzzle {points}`
2020-10-16 15:39:20 -06:00
puzzles/category2 $ ./mkcategory puzzle 1
{JSON PUZZLE OBJECT}
Also see [JSON Puzzle Object](#json-puzzle-object)
2020-10-16 15:28:36 -06:00
## `mkcategory file {points} {filename}`
2020-10-16 15:39:20 -06:00
puzzles/category2 $ ./mkcategory file 1 attachment.txt
This is an attachment file's contents!
2020-10-16 15:28:36 -06:00
## `mkcategory answer {points} {answer}`
2020-10-16 15:39:20 -06:00
puzzles/category2 $ ./mkcategory answer 1 "cow goes moo"
{"Correct":false}
2020-10-16 15:28:36 -06:00
# Provider API
This is how Claire gets her dynamic graders.
*Notice: this is not complete in the code base!*
I'm writing here how it *should* work.
If anybody wants this,
please let me know,
and I'll finish the code.
This could ostensibly be expanded to call HTTP servers,
with the four endpoints described here.
If somebody were to want such a thing.
2020-10-16 15:39:20 -06:00
## `provider inventory`
2020-10-16 15:28:36 -06:00
$ provider inventory
{
"category1": [1, 2, 3, 4, 5, 10, 20, 30],
"category2": [20, 40, 70, 150]
}
2020-10-16 15:39:20 -06:00
## `provider puzzle {category} {points}`
2020-10-16 15:28:36 -06:00
$ provider puzzle category1 20
{JSON PUZZLE OBJECT}
2020-10-16 15:39:20 -06:00
Also see [JSON Puzzle Object](#json-puzzle-object)
## `provider file {category} {points} {filename}`
2020-10-16 15:28:36 -06:00
$ provider file category1 20 attachment.txt
This is an attachment! Yay!
2020-10-16 15:39:20 -06:00
## `provider answer {category} {points} {answer}`
2020-10-16 15:28:36 -06:00
$ provider answer category1 20 "cow goes moo"
{"Correct":true}