last answer returns ID and is actually the last by TS

master
bel 2023-04-04 19:41:31 -06:00
parent 990733200f
commit 1e9daf2398
4 changed files with 29 additions and 10 deletions

BIN
anki.d

Binary file not shown.

20
main.go
View File

@ -16,7 +16,7 @@ type (
HistoryOf(string) map[string][]History HistoryOf(string) map[string][]History
Next(string, string) time.Time Next(string, string) time.Time
Question(string) Question Question(string) Question
LastAnswer(string, string) Answer LastAnswer(string, string) (string, Answer)
Answer(string) Answer Answer(string) Answer
PushAnswer(string, string, string, bool) error PushAnswer(string, string, string, bool) error
} }
@ -76,13 +76,21 @@ func Main() error {
if len(question.Clues) == 0 || response == "/clue" { if len(question.Clues) == 0 || response == "/clue" {
response = readline() response = readline()
} }
if lastAnswer := db.Answer(db.LastAnswer(user, q).A); lastAnswer.A != "" { 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("> Last time, you responded:\n\t%s\n", lastAnswer.A)
} }
fmt.Printf("> Did you pass this time? [Yn] ") fmt.Printf("> Did you pass this time? [Yns]\n")
pass := readline() == "n" switch readline() {
if err := db.PushAnswer(user, q, response, pass); err != nil { case "s":
return err case "n":
if err := db.PushAnswer(user, q, response, false); err != nil {
return err
}
default:
if err := db.PushAnswer(user, q, response, true); err != nil {
return err
}
} }
fmt.Println() fmt.Println()
} }

View File

@ -14,10 +14,16 @@ knowledge:
a: a schema a: a schema
ts: 123 ts: 123
author: breel author: breel
uuid0:
q: uuid1
a: not a schema
ts: 122
author: breel
users: users:
breel: breel:
history: history:
uuid1: uuid1:
- {a: uuid0, pass: true}
- {a: uuid2, pass: true} - {a: uuid2, pass: true}
cadence: cadence:
- 1d - 1d

View File

@ -47,13 +47,18 @@ func (db yamlDB) Question(q string) Question {
return db.Knowledge.Questions[q] return db.Knowledge.Questions[q]
} }
func (db yamlDB) LastAnswer(user, q string) Answer { func (db yamlDB) LastAnswer(user, q string) (string, Answer) {
for _, v := range db.Knowledge.Answers { maxk := ""
var maxv Answer
for k, v := range db.Knowledge.Answers {
if v.Q == q && v.Author == user { if v.Q == q && v.Author == user {
return v if maxv.TS < v.TS {
maxk = k
maxv = v
}
} }
} }
return Answer{} return maxk, maxv
} }
func (db yamlDB) Answer(a string) Answer { func (db yamlDB) Answer(a string) Answer {