Compare commits
18 Commits
v0.0.0
...
d32d8059bb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d32d8059bb | ||
|
|
64e992c6b6 | ||
|
|
0fd0981a39 | ||
|
|
aacc5d2fc6 | ||
|
|
08abc8b48f | ||
|
|
c960de86f3 | ||
|
|
7a464c2f09 | ||
|
|
c76da12b1a | ||
|
|
ef25a77254 | ||
|
|
80d987b69e | ||
|
|
1816500617 | ||
|
|
fe6b87563f | ||
|
|
273b412e30 | ||
|
|
da6097c5e2 | ||
|
|
365a857e2a | ||
|
|
40d5740817 | ||
|
|
933f87d11d | ||
|
|
a8b5942833 |
145
http.go
Normal file
145
http.go
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
_ "embed"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Context struct {
|
||||||
|
DB DB
|
||||||
|
User IDU
|
||||||
|
}
|
||||||
|
|
||||||
|
func HTTP(port int, db DB) error {
|
||||||
|
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
|
||||||
|
defer can()
|
||||||
|
|
||||||
|
foo := func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path == "/" {
|
||||||
|
httpGUI(w, r)
|
||||||
|
} else if strings.HasPrefix(r.URL.Path, "/static/") {
|
||||||
|
httpStatic(w, r)
|
||||||
|
} else if strings.HasPrefix(r.URL.Path, "/api/questions") && strings.HasSuffix(r.URL.Path, "/answers") && r.Method == http.MethodPost {
|
||||||
|
httpPostQuestionAnswers(w, r)
|
||||||
|
} else {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foo = withAuth(foo)
|
||||||
|
foo = withDB(foo, db)
|
||||||
|
foo = withCtx(foo, ctx)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
http.ListenAndServe(fmt.Sprintf(":%d", port), http.HandlerFunc(foo))
|
||||||
|
}()
|
||||||
|
<-ctx.Done()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func extract(ctx context.Context) Context {
|
||||||
|
v := ctx.Value("__context")
|
||||||
|
v2, _ := v.(Context)
|
||||||
|
return v2
|
||||||
|
}
|
||||||
|
|
||||||
|
func inject(ctx context.Context, v Context) context.Context {
|
||||||
|
return context.WithValue(ctx, "__context", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func withCtx(foo http.HandlerFunc, ctx context.Context) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
r = r.WithContext(ctx)
|
||||||
|
foo(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func withDB(foo http.HandlerFunc, db DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
c := extract(r.Context())
|
||||||
|
c.DB = db
|
||||||
|
r = r.WithContext(inject(r.Context(), c))
|
||||||
|
foo(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func withAuth(foo http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
u, _, ok := r.BasicAuth()
|
||||||
|
if !ok || u == "" {
|
||||||
|
w.Header().Set("WWW-Authenticate", "Basic")
|
||||||
|
w.WriteHeader(401)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c := extract(r.Context())
|
||||||
|
c.User = IDU(u)
|
||||||
|
r = r.WithContext(inject(r.Context(), c))
|
||||||
|
foo(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:embed public/root.html
|
||||||
|
var httpGUIHTML string
|
||||||
|
|
||||||
|
func httpGUI(w http.ResponseWriter, r *http.Request) {
|
||||||
|
body := httpGUIHTML
|
||||||
|
if os.Getenv("DEBUG") != "" {
|
||||||
|
b, _ := os.ReadFile("public/root.html")
|
||||||
|
body = string(b)
|
||||||
|
}
|
||||||
|
ctx := extract(r.Context())
|
||||||
|
body = strings.ReplaceAll(body, "{{USER}}", string(ctx.User))
|
||||||
|
assignments, err := httpAssignments(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
assignmentsB, _ := json.Marshal(assignments)
|
||||||
|
body = strings.ReplaceAll(body, "{{ASSIGNMENTS_JSON}}", string(assignmentsB))
|
||||||
|
w.Write([]byte(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
func httpStatic(w http.ResponseWriter, r *http.Request) {
|
||||||
|
p := strings.TrimPrefix(r.URL.Path, "/static/")
|
||||||
|
p = strings.ReplaceAll(p, "/..", "/")
|
||||||
|
p = path.Clean(p)
|
||||||
|
http.ServeFile(w, r, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func httpAssignments(ctx context.Context) (interface{}, error) {
|
||||||
|
db := extract(ctx).DB
|
||||||
|
user := extract(ctx).User
|
||||||
|
todo := map[IDQ]Question{}
|
||||||
|
for q, _ := range db.HistoryOf(user) {
|
||||||
|
if time.Until(db.Next(user, q)) > 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
todo[q] = db.Question(q)
|
||||||
|
}
|
||||||
|
return todo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func httpPostQuestionAnswers(w http.ResponseWriter, r *http.Request) {
|
||||||
|
idq := IDQ(path.Base(strings.Split(r.URL.Path, "/answers")[0]))
|
||||||
|
var payload struct {
|
||||||
|
Answer string `json:"answer"`
|
||||||
|
Passed bool `json:"passed"`
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx := extract(r.Context())
|
||||||
|
if err := ctx.DB.PushAnswer(ctx.User, idq, Renderable(payload.Answer), payload.Passed); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
50
http_test.go
Normal file
50
http_test.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHTTPPostQuestionAnswers(t *testing.T) {
|
||||||
|
p := path.Join(t.TempDir(), "db.yaml")
|
||||||
|
os.WriteFile(p, []byte("{}"), os.ModePerm)
|
||||||
|
db, err := newYamlDB(p)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
r := httptest.NewRequest(
|
||||||
|
http.MethodPost,
|
||||||
|
"/api/questions/QID/answers",
|
||||||
|
strings.NewReader(`{"answer":"a", "passed": false}`),
|
||||||
|
)
|
||||||
|
r.SetBasicAuth("u", "")
|
||||||
|
|
||||||
|
withAuth(withDB(httpPostQuestionAnswers, db))(w, r)
|
||||||
|
|
||||||
|
if w.Code != http.StatusOK {
|
||||||
|
t.Error(w.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, got := db.LastAnswer("u", "QID")
|
||||||
|
if got == (Answer{}) {
|
||||||
|
t.Error("no answer pushed:", got)
|
||||||
|
}
|
||||||
|
if got.Q != "QID" {
|
||||||
|
t.Error(got.Q)
|
||||||
|
} else if got.A != "a" {
|
||||||
|
t.Error(got.A)
|
||||||
|
} else if time.Since(time.Unix(0, got.TS)) > time.Minute {
|
||||||
|
t.Error(got.TS)
|
||||||
|
} else if got.Author != "u" {
|
||||||
|
t.Error(got.Author)
|
||||||
|
} else if got.Pass != false {
|
||||||
|
t.Error(got.Pass)
|
||||||
|
}
|
||||||
|
}
|
||||||
126
main.go
126
main.go
@@ -1,11 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -59,115 +56,10 @@ func Main() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
user := IDU(os.Getenv("USER"))
|
if port, _ := strconv.Atoi(os.Getenv("PORT")); port > 0 {
|
||||||
failed, err := Review(db, user)
|
return HTTP(port, db)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
for len(failed) > 0 {
|
return Terminal(db)
|
||||||
for i := len(failed) - 1; i >= 0; i-- {
|
|
||||||
if _, passed, err := ReviewQ(db, user, failed[i]); err != nil {
|
|
||||||
return err
|
|
||||||
} else if passed == CMD_PASS {
|
|
||||||
return nil
|
|
||||||
} else if passed != CMD_FAIL {
|
|
||||||
failed = append(failed[:i], failed[i+1:]...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Review(db DB, user IDU) ([]IDQ, error) {
|
|
||||||
failed := []IDQ{}
|
|
||||||
|
|
||||||
for q, _ := range db.HistoryOf(user) {
|
|
||||||
if time.Until(db.Next(user, q)) > 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
response, passed, err := ReviewQ(db, user, q)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
switch passed {
|
|
||||||
case CMD_QUIT:
|
|
||||||
return nil, nil
|
|
||||||
case CMD_FAIL:
|
|
||||||
failed = append(failed, q)
|
|
||||||
if err := db.PushAnswer(user, q, Renderable(response), false); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
case CMD_PASS:
|
|
||||||
if err := db.PushAnswer(user, q, Renderable(response), true); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
return failed, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReviewQ(db DB, user IDU, q IDQ) (string, string, error) {
|
|
||||||
question := db.Question(q)
|
|
||||||
fmt.Printf("> Q: %s\n", question.Q.ToString(db.UserResolution(user)))
|
|
||||||
fmt.Printf("\n")
|
|
||||||
fmt.Printf("> %+v\n", question.Tags)
|
|
||||||
var response string
|
|
||||||
gotAClue := false
|
|
||||||
for i := range question.Clues {
|
|
||||||
if i == 0 {
|
|
||||||
fmt.Printf("> /clue for a clue\n")
|
|
||||||
}
|
|
||||||
response = readline()
|
|
||||||
if response != "/clue" {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
gotAClue = true
|
|
||||||
fmt.Printf("> %s", question.Clues[i].ToString(db.UserResolution(user)))
|
|
||||||
if i+1 < len(question.Clues) {
|
|
||||||
fmt.Printf(" | /clue for another clue")
|
|
||||||
}
|
|
||||||
fmt.Printf("\n")
|
|
||||||
}
|
|
||||||
if len(question.Clues) == 0 || response == "/clue" {
|
|
||||||
response = readline()
|
|
||||||
}
|
|
||||||
if !gotAClue && len(question.Solution) > 0 && question.Solution[0].ToString(1) == response {
|
|
||||||
return response, CMD_PASS, nil
|
|
||||||
}
|
|
||||||
if len(question.Solution) > 0 {
|
|
||||||
fmt.Printf("> Solution:")
|
|
||||||
for i := range question.Solution {
|
|
||||||
fmt.Printf("\n\t%s", question.Solution[i].ToString(db.UserResolution(user)))
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
if id, _ := db.LastAnswer(user, q); id == "" {
|
|
||||||
} else if lastAnswer := db.Answer(id); lastAnswer.A != "" {
|
|
||||||
fmt.Printf("> Last time, you responded:\n\t%s\n", lastAnswer.A)
|
|
||||||
}
|
|
||||||
fmt.Printf("> Did you pass this time? [%s%s%s%s]\n",
|
|
||||||
strings.ToUpper(CMD_PASS),
|
|
||||||
strings.ToLower(CMD_FAIL),
|
|
||||||
strings.ToLower(CMD_SKIP),
|
|
||||||
strings.ToLower(CMD_QUIT),
|
|
||||||
)
|
|
||||||
switch strings.ToLower(readline()) {
|
|
||||||
case CMD_SKIP:
|
|
||||||
return response, CMD_SKIP, nil
|
|
||||||
case CMD_FAIL:
|
|
||||||
return response, CMD_FAIL, nil
|
|
||||||
case CMD_QUIT:
|
|
||||||
return response, CMD_QUIT, nil
|
|
||||||
}
|
|
||||||
return response, CMD_PASS, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func readline() string {
|
|
||||||
fmt.Printf("\t")
|
|
||||||
reader := bufio.NewReader(os.Stdin)
|
|
||||||
text, _ := reader.ReadString('\n')
|
|
||||||
return strings.TrimSpace(text)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDB() (DB, error) {
|
func NewDB() (DB, error) {
|
||||||
@@ -182,13 +74,3 @@ func (q Question) Tagged(tag IDT) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (renderable Renderable) ToString(resolution int) string {
|
|
||||||
s := string(renderable)
|
|
||||||
if !strings.HasPrefix(s, "img:") {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
buff := bytes.NewBuffer(nil)
|
|
||||||
ViewAt(buff, s[4:], resolution)
|
|
||||||
return string(buff.Bytes())
|
|
||||||
}
|
|
||||||
|
|||||||
165
public/root.html
Normal file
165
public/root.html
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<header>
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css">
|
||||||
|
<script>
|
||||||
|
let session = Number(
|
||||||
|
Date.
|
||||||
|
now().
|
||||||
|
toString().
|
||||||
|
split("").
|
||||||
|
reverse().
|
||||||
|
join("")
|
||||||
|
).toString(36)
|
||||||
|
console.log("session", session)
|
||||||
|
</script>
|
||||||
|
</header>
|
||||||
|
<body>
|
||||||
|
<!--{{USER}}-->
|
||||||
|
<form id="flash" action="">
|
||||||
|
<h1 name="status"></h1>
|
||||||
|
<input type="text" name="idq" readonly=true value="" style="display: none;">
|
||||||
|
<div name="question"></div>
|
||||||
|
<div name="clues"></div>
|
||||||
|
<div name="solution"></div>
|
||||||
|
<input type="button" value="clue">
|
||||||
|
<input type="text" name="answer">
|
||||||
|
<input type="button" value="pass" onclick="passQuestion(this.parentElement)">
|
||||||
|
<input type="button" value="fail" onclick="failQuestion(this.parentElement)">
|
||||||
|
<input type="button" value="skip" onclick="skipQuestion(this.parentElement)">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
<footer>
|
||||||
|
<script>
|
||||||
|
let knowledgebase = {{ASSIGNMENTS_JSON}}
|
||||||
|
knowledgebase = Object.
|
||||||
|
keys(knowledgebase).
|
||||||
|
map((key) => [
|
||||||
|
[key, knowledgebase[key]]
|
||||||
|
])
|
||||||
|
function shuffle(array) {
|
||||||
|
let currentIndex = array.length, randomIndex;
|
||||||
|
while (currentIndex != 0) {
|
||||||
|
randomIndex = Math.floor(Math.random() * currentIndex);
|
||||||
|
currentIndex--;
|
||||||
|
[array[currentIndex], array[randomIndex]] = [
|
||||||
|
array[randomIndex], array[currentIndex]];
|
||||||
|
}
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
shuffle(knowledgebase)
|
||||||
|
|
||||||
|
console.log(0, knowledgebase)
|
||||||
|
|
||||||
|
function passQuestion(form) {
|
||||||
|
if (!form.children.answer.retake) {
|
||||||
|
pushAnswer(form.children.idq.value, form.children.answer.value, true)
|
||||||
|
}
|
||||||
|
nextQuestion(form)
|
||||||
|
}
|
||||||
|
|
||||||
|
function failQuestion(form) {
|
||||||
|
if (!form.children.answer.retake) {
|
||||||
|
pushAnswer(form.children.idq.value, form.children.answer.value, false)
|
||||||
|
}
|
||||||
|
queueRedoQuestion(form)
|
||||||
|
nextQuestion(form)
|
||||||
|
}
|
||||||
|
|
||||||
|
function skipQuestion(form) {
|
||||||
|
queueRedoQuestion(form)
|
||||||
|
nextQuestion(form)
|
||||||
|
}
|
||||||
|
|
||||||
|
function queueRedoQuestion(form) {
|
||||||
|
knowledgebase.push([[
|
||||||
|
form.children.idq.value,
|
||||||
|
{
|
||||||
|
Q: form.children.question.value,
|
||||||
|
Clues: form.children.clues.value,
|
||||||
|
Solution: form.children.solution.value,
|
||||||
|
Retake: true,
|
||||||
|
},
|
||||||
|
]])
|
||||||
|
}
|
||||||
|
|
||||||
|
function pushAnswer(idq, answer, passed) {
|
||||||
|
http("post", `/api/questions/${idq}/answers`, noopcallback,
|
||||||
|
JSON.stringify(
|
||||||
|
{
|
||||||
|
"answer": answer,
|
||||||
|
"passed": new Boolean(passed),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function nextQuestion(form) {
|
||||||
|
form.children.answer.value = ""
|
||||||
|
let todo = knowledgebase.shift()
|
||||||
|
if (!todo) {
|
||||||
|
todo = [0]
|
||||||
|
}
|
||||||
|
todo = todo[0]
|
||||||
|
if (! todo) {
|
||||||
|
todo = ["", {Q: "ALL DONE"}]
|
||||||
|
}
|
||||||
|
|
||||||
|
form.children.status.innerHTML = knowledgebase.length.toString()
|
||||||
|
|
||||||
|
form.children.idq.value = todo[0]
|
||||||
|
|
||||||
|
if (todo[1].Q.startsWith("img:")) {
|
||||||
|
form.children.question.innerHTML = `<img src="static/${todo[1].Q.slice(4, 1000)}"/>`
|
||||||
|
} else {
|
||||||
|
form.children.question.innerHTML = `<h3>${todo[1].Q}</h3>`
|
||||||
|
}
|
||||||
|
form.children.question.value = todo[1].Q
|
||||||
|
|
||||||
|
let clues = ""
|
||||||
|
for (var i in todo[1].Clues) {
|
||||||
|
clues += `<details><summary>clue #${i}</summary>`
|
||||||
|
if (todo[1].Clues[i].startsWith("img:")) {
|
||||||
|
clues += `<img src="static/${todo[1].Clues[i].slice(4, 1000)}"/>`
|
||||||
|
} else {
|
||||||
|
clues += todo[1].Clues[i]
|
||||||
|
}
|
||||||
|
clues += `</details>`
|
||||||
|
}
|
||||||
|
form.children.clues.innerHTML = clues
|
||||||
|
form.children.clues.value = todo[1].Clues
|
||||||
|
|
||||||
|
let solution = ""
|
||||||
|
for (var i in todo[1].Solution) {
|
||||||
|
solution += `<br>${todo[1].Solution[i]}`
|
||||||
|
}
|
||||||
|
if (solution) {
|
||||||
|
solution = `<details><summary>solution</summary>${solution}</details>`
|
||||||
|
}
|
||||||
|
form.children.solution.innerHTML = solution
|
||||||
|
form.children.solution.value = todo[1].Solution
|
||||||
|
|
||||||
|
form.children.answer.retake = todo[1].Retake
|
||||||
|
}
|
||||||
|
|
||||||
|
function noopcallback(responseBody, responseStatus) {
|
||||||
|
console.log(responseStatus, responseBody)
|
||||||
|
}
|
||||||
|
function http(method, remote, callback, body) {
|
||||||
|
var xmlhttp = new XMLHttpRequest();
|
||||||
|
xmlhttp.onreadystatechange = function() {
|
||||||
|
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
|
||||||
|
callback(xmlhttp.responseText, xmlhttp.status)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xmlhttp.open(method, remote, true);
|
||||||
|
if (typeof body == "undefined") {
|
||||||
|
body = null
|
||||||
|
}
|
||||||
|
xmlhttp.send(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
nextQuestion(document.getElementById("flash"))
|
||||||
|
</script>
|
||||||
|
</footer>
|
||||||
|
</html>
|
||||||
132
terminal.go
Normal file
132
terminal.go
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Terminal(db DB) error {
|
||||||
|
user := IDU(os.Getenv("USER"))
|
||||||
|
failed, err := Review(db, user)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for len(failed) > 0 {
|
||||||
|
for i := len(failed) - 1; i >= 0; i-- {
|
||||||
|
if _, passed, err := ReviewQ(db, user, failed[i]); err != nil {
|
||||||
|
return err
|
||||||
|
} else if passed == CMD_PASS {
|
||||||
|
return nil
|
||||||
|
} else if passed != CMD_FAIL {
|
||||||
|
failed = append(failed[:i], failed[i+1:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Review(db DB, user IDU) ([]IDQ, error) {
|
||||||
|
failed := []IDQ{}
|
||||||
|
|
||||||
|
for q, _ := range db.HistoryOf(user) {
|
||||||
|
if time.Until(db.Next(user, q)) > 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
response, passed, err := ReviewQ(db, user, q)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
switch passed {
|
||||||
|
case CMD_QUIT:
|
||||||
|
return nil, nil
|
||||||
|
case CMD_FAIL:
|
||||||
|
failed = append(failed, q)
|
||||||
|
if err := db.PushAnswer(user, q, Renderable(response), false); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
case CMD_PASS:
|
||||||
|
if err := db.PushAnswer(user, q, Renderable(response), true); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
return failed, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReviewQ(db DB, user IDU, q IDQ) (string, string, error) {
|
||||||
|
question := db.Question(q)
|
||||||
|
fmt.Printf("> Q: %s\n", question.Q.ToString(db.UserResolution(user)))
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("> %+v\n", question.Tags)
|
||||||
|
var response string
|
||||||
|
gotAClue := false
|
||||||
|
for i := range question.Clues {
|
||||||
|
if i == 0 {
|
||||||
|
fmt.Printf("> /clue for a clue\n")
|
||||||
|
}
|
||||||
|
response = readline()
|
||||||
|
if response != "/clue" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
gotAClue = true
|
||||||
|
fmt.Printf("> %s", question.Clues[i].ToString(db.UserResolution(user)))
|
||||||
|
if i+1 < len(question.Clues) {
|
||||||
|
fmt.Printf(" | /clue for another clue")
|
||||||
|
}
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
if len(question.Clues) == 0 || response == "/clue" {
|
||||||
|
response = readline()
|
||||||
|
}
|
||||||
|
if !gotAClue && len(question.Solution) > 0 && question.Solution[0].ToString(1) == response {
|
||||||
|
return response, CMD_PASS, nil
|
||||||
|
}
|
||||||
|
if len(question.Solution) > 0 {
|
||||||
|
fmt.Printf("> Solution:")
|
||||||
|
for i := range question.Solution {
|
||||||
|
fmt.Printf("\n\t%s", question.Solution[i].ToString(db.UserResolution(user)))
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
if id, _ := db.LastAnswer(user, q); id == "" {
|
||||||
|
} else if lastAnswer := db.Answer(id); lastAnswer.A != "" {
|
||||||
|
fmt.Printf("> Last time, you responded:\n\t%s\n", lastAnswer.A)
|
||||||
|
}
|
||||||
|
fmt.Printf("> Did you pass this time? [%s%s%s%s]\n",
|
||||||
|
strings.ToUpper(CMD_PASS),
|
||||||
|
strings.ToLower(CMD_FAIL),
|
||||||
|
strings.ToLower(CMD_SKIP),
|
||||||
|
strings.ToLower(CMD_QUIT),
|
||||||
|
)
|
||||||
|
switch strings.ToLower(readline()) {
|
||||||
|
case CMD_SKIP:
|
||||||
|
return response, CMD_SKIP, nil
|
||||||
|
case CMD_FAIL:
|
||||||
|
return response, CMD_FAIL, nil
|
||||||
|
case CMD_QUIT:
|
||||||
|
return response, CMD_QUIT, nil
|
||||||
|
}
|
||||||
|
return response, CMD_PASS, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func readline() string {
|
||||||
|
fmt.Printf("\t")
|
||||||
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
text, _ := reader.ReadString('\n')
|
||||||
|
return strings.TrimSpace(text)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (renderable Renderable) ToString(resolution int) string {
|
||||||
|
s := string(renderable)
|
||||||
|
if !strings.HasPrefix(s, "img:") {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
buff := bytes.NewBuffer(nil)
|
||||||
|
ViewAt(buff, s[4:], resolution)
|
||||||
|
return string(buff.Bytes())
|
||||||
|
}
|
||||||
47
testdata/sample.yaml
vendored
Normal file → Executable file
47
testdata/sample.yaml
vendored
Normal file → Executable file
@@ -1,7 +1,21 @@
|
|||||||
knowledge:
|
knowledge:
|
||||||
questions:
|
questions:
|
||||||
uuid1:
|
uuid10:
|
||||||
q: whats a fieldset
|
q: whats a fieldset
|
||||||
|
solution:
|
||||||
|
- solution A
|
||||||
|
- solution B
|
||||||
|
clues:
|
||||||
|
- clue1 of 2
|
||||||
|
- img:testdata/tofugu.d/sa-hiragana-0.png
|
||||||
|
tags:
|
||||||
|
- ops
|
||||||
|
- data-platform
|
||||||
|
uuid11:
|
||||||
|
q: whats a responseset
|
||||||
|
solution:
|
||||||
|
- solution A
|
||||||
|
- solution B
|
||||||
clues:
|
clues:
|
||||||
- clue1 of 2
|
- clue1 of 2
|
||||||
- clue2 of 2
|
- clue2 of 2
|
||||||
@@ -9,19 +23,34 @@ knowledge:
|
|||||||
- ops
|
- ops
|
||||||
- data-platform
|
- data-platform
|
||||||
answers:
|
answers:
|
||||||
uuid2:
|
4ad47d8f-2e9b-46fe-8a12-67db85efc6f8:
|
||||||
q: uuid1
|
q: uuid11
|
||||||
|
a: ""
|
||||||
|
ts: 1680896808930227000
|
||||||
|
author: breel
|
||||||
|
pass: false
|
||||||
|
36648846-4e14-4518-b434-5d4ff3b0127b:
|
||||||
|
q: uuid10
|
||||||
|
a: ""
|
||||||
|
ts: 1680896805896000000
|
||||||
|
author: breel
|
||||||
|
pass: false
|
||||||
|
uuid20:
|
||||||
|
q: uuid10
|
||||||
a: a schema
|
a: a schema
|
||||||
ts: 123
|
ts: 123
|
||||||
author: breel
|
author: breel
|
||||||
uuid0:
|
pass: false
|
||||||
q: uuid1
|
uuid21:
|
||||||
|
q: uuid10
|
||||||
a: not a schema
|
a: not a schema
|
||||||
ts: 122
|
ts: 122
|
||||||
author: breel
|
author: breel
|
||||||
|
pass: false
|
||||||
users:
|
users:
|
||||||
breel:
|
breel:
|
||||||
history:
|
tags:
|
||||||
uuid1:
|
assignments:
|
||||||
- {a: uuid0, pass: true}
|
- ops
|
||||||
- {a: uuid2, pass: true}
|
cadence: 5s
|
||||||
|
resolution: 1
|
||||||
|
|||||||
@@ -3,9 +3,7 @@ todo:
|
|||||||
- exemplary answers; declaring
|
- exemplary answers; declaring
|
||||||
- exemplary answers; sharing
|
- exemplary answers; sharing
|
||||||
- tags; declare mastery
|
- tags; declare mastery
|
||||||
- tags; assign to a user
|
|
||||||
- entrypoint; explicit answer all
|
- entrypoint; explicit answer all
|
||||||
- entrypoint; answer tag
|
|
||||||
- entrypoint; edit question
|
- entrypoint; edit question
|
||||||
- entrypoint; delete question
|
- entrypoint; delete question
|
||||||
- ascii images from url
|
- ascii images from url
|
||||||
@@ -31,3 +29,7 @@ done:
|
|||||||
ts: Fri Apr 7 10:46:14 MDT 2023
|
ts: Fri Apr 7 10:46:14 MDT 2023
|
||||||
- todo: entrypoint; create question
|
- todo: entrypoint; create question
|
||||||
ts: Fri Apr 7 10:46:14 MDT 2023
|
ts: Fri Apr 7 10:46:14 MDT 2023
|
||||||
|
- todo: tags; assign to a user
|
||||||
|
ts: Fri Apr 7 14:06:39 MDT 2023
|
||||||
|
- todo: entrypoint; answer tag
|
||||||
|
ts: Fri Apr 7 14:06:39 MDT 2023
|
||||||
|
|||||||
Reference in New Issue
Block a user