-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck_config.go
More file actions
147 lines (133 loc) · 4.53 KB
/
deck_config.go
File metadata and controls
147 lines (133 loc) · 4.53 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package anki
import (
"iter"
"time"
"github.com/lftk/anki/pb"
"google.golang.org/protobuf/proto"
)
// DeckConfig represents a deck configuration in Anki.
type DeckConfig struct {
ID int64
Name string
Modified time.Time
USN int
Config *pb.DeckConfig
}
// DefaultDeckConfig returns the default deck configuration.
func DefaultDeckConfig() *pb.DeckConfig {
return &pb.DeckConfig{
LearnSteps: nil,
RelearnSteps: nil,
NewPerDay: 20,
ReviewsPerDay: 200,
NewPerDayMinimum: 0,
InitialEase: 2.5,
EasyMultiplier: 1.3,
HardMultiplier: 1.2,
LapseMultiplier: 0.0,
IntervalMultiplier: 1.0,
MaximumReviewInterval: 36500,
MinimumLapseInterval: 1,
GraduatingIntervalGood: 1,
GraduatingIntervalEasy: 4,
NewCardInsertOrder: pb.DeckConfig_NEW_CARD_INSERT_ORDER_DUE,
NewCardGatherPriority: pb.DeckConfig_NEW_CARD_GATHER_PRIORITY_DECK,
NewCardSortOrder: pb.DeckConfig_NEW_CARD_SORT_ORDER_TEMPLATE,
ReviewOrder: pb.DeckConfig_REVIEW_CARD_ORDER_DAY,
NewMix: pb.DeckConfig_REVIEW_MIX_MIX_WITH_REVIEWS,
InterdayLearningMix: pb.DeckConfig_REVIEW_MIX_MIX_WITH_REVIEWS,
LeechAction: pb.DeckConfig_LEECH_ACTION_TAG_ONLY,
LeechThreshold: 8,
DisableAutoplay: false,
CapAnswerTimeToSecs: 60,
ShowTimer: false,
StopTimerOnAnswer: false,
SecondsToShowQuestion: 0.0,
SecondsToShowAnswer: 0.0,
QuestionAction: pb.DeckConfig_QUESTION_ACTION_SHOW_ANSWER,
AnswerAction: pb.DeckConfig_ANSWER_ACTION_BURY_CARD,
WaitForAudio: true,
SkipQuestionWhenReplayingAnswer: false,
BuryNew: false,
BuryReviews: false,
BuryInterdayLearning: false,
FsrsParams_4: nil,
FsrsParams_5: nil,
FsrsParams_6: nil,
DesiredRetention: 0.9,
Other: nil,
HistoricalRetention: 0.9,
ParamSearch: "",
IgnoreRevlogsBeforeDate: "",
EasyDaysPercentages: nil,
}
}
// AddDeckConfig adds a new deck configuration to the collection.
func (c *Collection) AddDeckConfig(config *DeckConfig) error {
return addDeckConfig(c.db, config)
}
// addDeckConfig is a helper function to add a deck configuration to the database.
func addDeckConfig(e sqlExecer, config *DeckConfig) error {
id := config.ID
if id == 0 {
id = time.Now().UnixMilli()
}
if config.Config == nil {
config.Config = DefaultDeckConfig()
}
inner, err := proto.Marshal(config.Config)
if err != nil {
return err
}
args := []any{
id,
config.Name,
config.USN,
timeUnix(config.Modified),
inner,
}
id, err = sqlInsert(e, addDeckConfigQuery, args...)
if err == nil {
config.ID = id
}
return err
}
// GetDeckConfig gets a deck configuration by its ID.
func (c *Collection) GetDeckConfig(id int64) (*DeckConfig, error) {
return sqlGet(c.db, scanDeckConfig, getDeckConfigQuery+" WHERE id = ?", id)
}
// DeleteDeckConfig deletes a deck configuration by its ID.
func (c *Collection) DeleteDeckConfig(id int64) error {
return sqlExecute(c.db, deleteDeckConfigQuery, id)
}
// ListDeckConfigsOptions specifies options for listing deck configurations.
type ListDeckConfigsOptions struct{}
// ListDeckConfigs lists all deck configurations.
func (c *Collection) ListDeckConfigs(*ListDeckConfigsOptions) iter.Seq2[*DeckConfig, error] {
return sqlSelectSeq(c.db, scanDeckConfig, getDeckConfigQuery)
}
// scanDeckConfig scans a deck configuration from a database row.
func scanDeckConfig(_ sqlQueryer, row sqlRow) (*DeckConfig, error) {
var c DeckConfig
var mod int64
var config []byte
if err := row.Scan(&c.ID, &c.Name, &mod, &c.USN, &config); err != nil {
return nil, err
}
c.Modified = time.Unix(mod, 0)
c.Config = new(pb.DeckConfig)
if err := proto.Unmarshal(config, c.Config); err != nil {
return nil, err
}
return &c, nil
}
// addDefaultDeckConfig adds the default deck configuration to the database.
func addDefaultDeckConfig(e sqlExecer) error {
return addDeckConfig(e, &DeckConfig{
ID: 1,
Name: "Default",
Modified: timeZero(),
USN: 0,
Config: DefaultDeckConfig(),
})
}