-
Notifications
You must be signed in to change notification settings - Fork 6
/
cmp_difficulty.go
162 lines (140 loc) · 3.28 KB
/
cmp_difficulty.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package component
import (
"encoding/json"
"fmt"
"regexp"
)
type Difficulty struct {
parent *Subcategory
ID string `json:"id"`
Descr string `json:"description"`
Hash string `json:"hash"`
items []*Item
checklist *Checklist
}
func (d *Difficulty) Resource() Resource {
return Resource{
Slug: d.parent.Resource().Slug + "_" + d.ID,
Content: []map[string]string{
map[string]string{"description": d.Descr},
},
}
}
func (d *Difficulty) HasChildren() bool {
return len(d.items) != 0
}
func (d *Difficulty) Tree(html bool) interface{} {
var items = make([]Item, len(d.items))
for i, v := range d.items {
items[i] = *v
if html {
items[i].Body = items[i].htmlBody
}
}
return map[string]interface{}{
"id": d.ID,
"description": d.Descr,
"items": items,
"checks": d.checklist.Checks,
}
}
func (d *Difficulty) SHA() string {
return d.Hash
}
func (d *Difficulty) SetParent(s *Subcategory) {
d.parent = s
}
func (d *Difficulty) MarshalJSON() ([]byte, error) {
var m = map[string]interface{}{
"description": d.Descr,
"items": d.ItemNames(),
"checks": d.checklist.Checks,
}
if d.Hash != "" {
m["hash"] = d.Hash
}
return json.Marshal(m)
}
func (d *Difficulty) Items() []Item {
var dst = make([]Item, len(d.items))
for i, v := range d.items {
dst[i] = *v
}
return dst
}
func (d *Difficulty) ItemNames() []string {
var r = make([]string, 0, len(d.items))
for i := range d.items {
r = append(r, d.items[i].ID)
}
return r
}
func (d *Difficulty) Checks() *Checklist {
var dst []Check
if d.checklist == nil {
d.SetChecks(new(Checklist))
}
dst = make([]Check, len(d.checklist.Checks))
for i, v := range d.checklist.Checks {
dst[i] = v
}
return &Checklist{
parent: d,
Hash: d.checklist.Hash,
Checks: dst,
}
}
func (d *Difficulty) AddChecks(c ...Check) {
if d.checklist == nil {
d.SetChecks(new(Checklist))
}
d.checklist.Add(c...)
}
func (d *Difficulty) SetChecks(c *Checklist) {
d.checklist = c
c.parent = d
}
func (d *Difficulty) AddItem(items ...*Item) error {
for _, v := range items {
if d.Item(v.ID) != nil {
return fmt.Errorf("item %s exists in %s/%s", v.ID, d.parent.ID, d.ID)
}
v.parent = d
}
d.items = append(d.items, items...)
return nil
}
func (d *Difficulty) Item(id string) *Item {
for _, v := range d.items {
if v.ID == id {
return v
}
}
return nil
}
func (d *Difficulty) basePath() string {
return fmt.Sprintf("%s/%s", d.parent.basePath(), d.ID)
}
func (d *Difficulty) Path() string {
return fmt.Sprintf("%s/.metadata%s", d.basePath(), fileExt)
}
var diffPath = regexp.MustCompile("contents(?:_[a-z]{2})?/[^/]+/[^/]+/([^/]+)/.metadata.md")
func (d *Difficulty) SetPath(filepath string) error {
p := diffPath.FindStringSubmatch(filepath)
if len(p) == 0 {
return ErrContent
}
d.ID = p[1]
return nil
}
func (*Difficulty) order() []string { return []string{"Description"} }
func (*Difficulty) optionals() []string { return nil }
func (d *Difficulty) pointers() args { return args{&d.Descr} }
func (d *Difficulty) values() args { return args{d.Descr} }
func (d *Difficulty) Contents() string { return getMeta(d) }
func (d *Difficulty) SetContents(contents string) error {
if d.checklist == nil {
d.checklist = new(Checklist)
}
return setMeta(contents, d)
}