134 lines
2.4 KiB
Go
134 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type (
|
|
db struct {
|
|
Knowledge knowledge
|
|
Users map[string]user
|
|
Cadence []duration
|
|
}
|
|
knowledge struct {
|
|
Questions map[string]Question
|
|
Answers map[string]Answer
|
|
}
|
|
user struct {
|
|
History map[string][]History
|
|
}
|
|
duration time.Duration
|
|
)
|
|
|
|
func (db db) HistoryOf(user string) map[string][]History {
|
|
return db.Users[user].History
|
|
}
|
|
|
|
func (db db) Next(user, q string) time.Time {
|
|
history := db.Users[user].History[q]
|
|
progress := 0
|
|
for i := range history {
|
|
if history[i].Pass {
|
|
progress += 1
|
|
} else {
|
|
progress -= 1
|
|
}
|
|
}
|
|
if progress < 0 {
|
|
progress = 0
|
|
} else if progress > len(db.Cadence) {
|
|
return time.Now().Add(time.Hour * 24 * 365 * 10)
|
|
}
|
|
return db.lastTS(user, q).Add(time.Duration(db.Cadence[progress]))
|
|
}
|
|
|
|
func (db db) Question(q string) Question {
|
|
return db.Knowledge.Questions[q]
|
|
}
|
|
|
|
func (db db) LastAnswer(user, q string) Answer {
|
|
for _, v := range db.Knowledge.Answers {
|
|
if v.Q == q && v.Author == user {
|
|
return v
|
|
}
|
|
}
|
|
return Answer{}
|
|
}
|
|
|
|
func (db db) Answer(a string) Answer {
|
|
return db.Knowledge.Answers[a]
|
|
}
|
|
|
|
func (db db) PushAnswer(user, q, a string, pass bool) error {
|
|
uuid := uuid.New().String()
|
|
db.Knowledge.Answers[uuid] = Answer{
|
|
Q: q,
|
|
A: a,
|
|
TS: time.Now().UnixNano(),
|
|
Author: user,
|
|
}
|
|
db.Users[user].History[q] = append(db.Users[user].History[q], History{
|
|
A: uuid,
|
|
Pass: pass,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (db db) lastTS(user, q string) time.Time {
|
|
max := int64(0)
|
|
for _, v := range db.Users[user].History[q] {
|
|
if v.TS > max {
|
|
max = v.TS
|
|
}
|
|
}
|
|
return time.Unix(0, max)
|
|
}
|
|
|
|
func (d duration) MarshalYAML() (interface{}, error) {
|
|
log.Println("marshalling duration", time.Duration(d))
|
|
return time.Duration(d).String(), nil
|
|
}
|
|
|
|
func (d *duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
var s string
|
|
if err := unmarshal(&s); err != nil {
|
|
return err
|
|
}
|
|
count := ""
|
|
var ttl time.Duration
|
|
for i := range s {
|
|
if s[i] < '0' || s[i] > '9' {
|
|
n, err := strconv.Atoi(count)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
count += s[i : i+1]
|
|
switch s[i] {
|
|
case 'w':
|
|
count = fmt.Sprintf("%dh", n*24*7)
|
|
case 'd':
|
|
count = fmt.Sprintf("%dh", n*24)
|
|
}
|
|
d, err := time.ParseDuration(count)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ttl += d
|
|
count = ""
|
|
} else {
|
|
count += s[i : i+1]
|
|
}
|
|
}
|
|
if count != "" {
|
|
return errors.New(count)
|
|
}
|
|
*d = duration(ttl)
|
|
return nil
|
|
}
|