outfits.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import "fmt"
  3. type OutfitList struct {
  4. node *Node // Ref
  5. List []OutfitListItem
  6. }
  7. func (ol *OutfitList) Parse(node *Node) error {
  8. if node.Key() != "outfits" {
  9. return fmt.Errorf("not a outfit list")
  10. }
  11. ol.node = node
  12. ol.List = []OutfitListItem{}
  13. for _, kid := range node.Children {
  14. o := OutfitListItem{}
  15. o.Parse(kid)
  16. ol.AddOutfit(o)
  17. }
  18. return nil
  19. }
  20. func (ol *OutfitList) AddOutfit(outfit OutfitListItem) {
  21. ol.List = append(ol.List, outfit)
  22. }
  23. func (ol *OutfitList) Update() error {
  24. if ol.node == nil {
  25. return fmt.Errorf("invalid node")
  26. }
  27. ol.node.RmAllChildren()
  28. for _, outfit := range ol.List {
  29. outfit.Generate(ol.node)
  30. }
  31. return nil
  32. }
  33. type OutfitListItem struct {
  34. Name string
  35. Count int
  36. }
  37. func (o *OutfitListItem) Parse(node *Node) error {
  38. o.Name = node.Key()
  39. if CanInt(node.Value()) {
  40. o.Count = node.ValueInt()
  41. } else {
  42. o.Count = 1
  43. }
  44. return nil
  45. }
  46. func (o *OutfitListItem) Generate(node *Node) {
  47. n := node.NewChild()
  48. if o.Count > 1 {
  49. n.Set(o.Name, o.Count)
  50. } else {
  51. n.Line = n.Format(o.Name)
  52. }
  53. }