last answer returns ID and is actually the last by TS
parent
990733200f
commit
1e9daf2398
18
main.go
18
main.go
|
|
@ -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,14 +76,22 @@ 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":
|
||||||
|
case "n":
|
||||||
|
if err := db.PushAnswer(user, q, response, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
if err := db.PushAnswer(user, q, response, true); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
if b, _ := yaml.Marshal(db); len(b) > 0 {
|
if b, _ := yaml.Marshal(db); len(b) > 0 {
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
13
yamldb.go
13
yamldb.go
|
|
@ -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 {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue