|
| 1 | +package component |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "regexp" |
| 7 | +) |
| 8 | + |
| 9 | +type Difficulty struct { |
| 10 | + parent *Subcategory |
| 11 | + ID string `json:"id"` |
| 12 | + Descr string `json:"description"` |
| 13 | + Hash string `json:"hash"` |
| 14 | + items []*Item |
| 15 | + checklist *Checklist |
| 16 | +} |
| 17 | + |
| 18 | +func (d *Difficulty) Resource() Resource { |
| 19 | + return Resource{ |
| 20 | + Slug: d.parent.Resource().Slug + "_" + d.ID, |
| 21 | + Content: []map[string]string{ |
| 22 | + map[string]string{"description": d.Descr}, |
| 23 | + }, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func (d *Difficulty) HasChildren() bool { |
| 28 | + return len(d.items) != 0 |
| 29 | +} |
| 30 | + |
| 31 | +func (d *Difficulty) Tree(html bool) interface{} { |
| 32 | + var items = make([]Item, len(d.items)) |
| 33 | + for i, v := range d.items { |
| 34 | + items[i] = *v |
| 35 | + if html { |
| 36 | + items[i].Body = items[i].htmlBody |
| 37 | + } |
| 38 | + } |
| 39 | + return map[string]interface{}{ |
| 40 | + "id": d.ID, |
| 41 | + "description": d.Descr, |
| 42 | + "items": items, |
| 43 | + "checks": d.checklist.Checks, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (d *Difficulty) SHA() string { |
| 48 | + return d.Hash |
| 49 | +} |
| 50 | + |
| 51 | +func (d *Difficulty) SetParent(s *Subcategory) { |
| 52 | + d.parent = s |
| 53 | +} |
| 54 | + |
| 55 | +func (d *Difficulty) MarshalJSON() ([]byte, error) { |
| 56 | + var m = map[string]interface{}{ |
| 57 | + "description": d.Descr, |
| 58 | + "items": d.ItemNames(), |
| 59 | + "checks": d.checklist.Checks, |
| 60 | + } |
| 61 | + if d.Hash != "" { |
| 62 | + m["hash"] = d.Hash |
| 63 | + } |
| 64 | + return json.Marshal(m) |
| 65 | +} |
| 66 | + |
| 67 | +func (d *Difficulty) Items() []Item { |
| 68 | + var dst = make([]Item, len(d.items)) |
| 69 | + for i, v := range d.items { |
| 70 | + dst[i] = *v |
| 71 | + } |
| 72 | + return dst |
| 73 | +} |
| 74 | + |
| 75 | +func (d *Difficulty) ItemNames() []string { |
| 76 | + var r = make([]string, 0, len(d.items)) |
| 77 | + for i := range d.items { |
| 78 | + r = append(r, d.items[i].ID) |
| 79 | + } |
| 80 | + return r |
| 81 | +} |
| 82 | + |
| 83 | +func (d *Difficulty) Checks() *Checklist { |
| 84 | + var dst []Check |
| 85 | + if d.checklist == nil { |
| 86 | + d.SetChecks(new(Checklist)) |
| 87 | + } |
| 88 | + dst = make([]Check, len(d.checklist.Checks)) |
| 89 | + for i, v := range d.checklist.Checks { |
| 90 | + dst[i] = v |
| 91 | + } |
| 92 | + return &Checklist{ |
| 93 | + parent: d, |
| 94 | + Hash: d.checklist.Hash, |
| 95 | + Checks: dst, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func (d *Difficulty) AddChecks(c ...Check) { |
| 100 | + if d.checklist == nil { |
| 101 | + d.SetChecks(new(Checklist)) |
| 102 | + } |
| 103 | + d.checklist.Add(c...) |
| 104 | +} |
| 105 | + |
| 106 | +func (d *Difficulty) SetChecks(c *Checklist) { |
| 107 | + d.checklist = c |
| 108 | + c.parent = d |
| 109 | +} |
| 110 | + |
| 111 | +func (d *Difficulty) AddItem(items ...*Item) error { |
| 112 | + for _, v := range items { |
| 113 | + if d.Item(v.ID) != nil { |
| 114 | + return fmt.Errorf("item %s exists in %s/%s", v.ID, d.parent.ID, d.ID) |
| 115 | + } |
| 116 | + v.parent = d |
| 117 | + } |
| 118 | + d.items = append(d.items, items...) |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +func (d *Difficulty) Item(id string) *Item { |
| 123 | + for _, v := range d.items { |
| 124 | + if v.ID == id { |
| 125 | + return v |
| 126 | + } |
| 127 | + } |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +func (d *Difficulty) basePath() string { |
| 132 | + return fmt.Sprintf("%s/%s", d.parent.basePath(), d.ID) |
| 133 | +} |
| 134 | + |
| 135 | +func (d *Difficulty) Path() string { |
| 136 | + return fmt.Sprintf("%s/.metadata%s", d.basePath(), fileExt) |
| 137 | +} |
| 138 | + |
| 139 | +var diffPath = regexp.MustCompile("contents(?:_[a-z]{2})?/[^/]+/[^/]+/([^/]+)/.metadata.md") |
| 140 | + |
| 141 | +func (d *Difficulty) SetPath(filepath string) error { |
| 142 | + p := diffPath.FindStringSubmatch(filepath) |
| 143 | + if len(p) == 0 { |
| 144 | + return ErrContent |
| 145 | + } |
| 146 | + d.ID = p[1] |
| 147 | + return nil |
| 148 | +} |
| 149 | + |
| 150 | +func (d *Difficulty) order() []string { return []string{"Description"} } |
| 151 | +func (d *Difficulty) pointers() args { return args{&d.Descr} } |
| 152 | +func (d *Difficulty) values() args { return args{d.Descr} } |
| 153 | + |
| 154 | +func (d *Difficulty) Contents() string { return getMeta(d) } |
| 155 | + |
| 156 | +func (d *Difficulty) SetContents(contents string) error { |
| 157 | + if err := checkMeta(contents, d); err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + if d.checklist == nil { |
| 161 | + d.checklist = new(Checklist) |
| 162 | + } |
| 163 | + return setMeta(contents, d) |
| 164 | +} |
0 commit comments