-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstats.go
67 lines (58 loc) · 1.55 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package leaf
import (
"encoding/json"
"time"
)
// SRSAlgorithm calculates review intervals
type SRSAlgorithm interface {
json.Marshaler
json.Unmarshaler
// Advance advances supermemo state for a card.
Advance(rating float64) (interval float64)
// NextReviewAt returns next review timestamp for a card.
NextReviewAt() time.Time
// Less defines card order for the review.
Less(other SRSAlgorithm) bool
}
// Stats store SM2+ parameters for a Card.
type Stats struct {
SRSAlgorithm
}
// CardWithStats joins Stats to a Card
type CardWithStats struct {
Card
*Stats
}
// SRS defines supported spaced-repetiton algorithms.
type SRS string
const (
// SRSSupermemo2 represents Supermemo2 algorithm
SRSSupermemo2 SRS = "sm2"
// SRSSupermemo2Plus represents Supermemo2Plus algorithm
SRSSupermemo2Plus = "sm2+"
// SRSSupermemo2PlusCustom represents Supermemo2PlusCustom algorithm
SRSSupermemo2PlusCustom = "sm2+c"
// SRSEbisu represents Ebisu algorithm
SRSEbisu = "ebisu"
)
// NewStats returns a new Stats initialized with provided algorithm
// with default values. Supported values: sm2, sm2+, sm2+c. If smAlgo
// is missing or unknown will default to Supermemo2PlusCustom.
func NewStats(srs SRS) *Stats {
var sm SRSAlgorithm
switch srs {
case SRSSupermemo2:
sm = NewSupermemo2()
case SRSSupermemo2Plus:
sm = NewSupermemo2Plus()
case SRSEbisu:
sm = NewEbisu()
default:
sm = NewSupermemo2PlusCustom()
}
return &Stats{sm}
}
// IsReady signals whether card is read for review.
func (s Stats) IsReady() bool {
return s.NextReviewAt().Before(time.Now())
}