profile.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. type Profile struct {
  7. node *Node // Node pointer for Update
  8. Name string
  9. Date string // for now it's a string (TODO date.go)
  10. Location Location // System and Plant
  11. Playtime float64 // Play time in seconds
  12. // TODO travel.go
  13. Ships []*Ship
  14. }
  15. // String representation of Playtime
  16. func (p *Profile) PlayTime() string {
  17. t := time.Duration(p.Playtime) * time.Second
  18. return t.String()
  19. }
  20. func (p *Profile) Parse(node *Node) error {
  21. if !node.HasTag("root") {
  22. return fmt.Errorf("non-root node given, expected root node")
  23. }
  24. p.node = node
  25. for _, kid := range node.Children {
  26. switch kid.Key() {
  27. case "pilot":
  28. p.Name = kid.Value()
  29. case "date":
  30. p.Date = kid.Value()
  31. case "system":
  32. if p.Location.System == "" {
  33. p.Location.Parse(kid)
  34. }
  35. case "planet":
  36. if p.Location.Planet == "" {
  37. p.Location.Parse(kid)
  38. }
  39. case "playtime":
  40. p.Playtime = kid.ValueFloat64()
  41. case "ship":
  42. s := &Ship{}
  43. err := s.Parse(kid)
  44. if err != nil {
  45. return err
  46. }
  47. p.Ships = append(p.Ships, s)
  48. }
  49. }
  50. return nil
  51. }
  52. func (p *Profile) Update() error {
  53. if p.node == nil {
  54. return fmt.Errorf("no node, profile")
  55. }
  56. err := p.Location.Update()
  57. if err != nil {
  58. return err
  59. }
  60. for _, s := range p.Ships {
  61. err := s.Update()
  62. if err != nil {
  63. return err
  64. }
  65. }
  66. for _, kid := range p.node.Children {
  67. switch kid.Key() {
  68. case "pilot":
  69. kid.SetValue(p.Name)
  70. case "date":
  71. kid.SetValue(p.Date)
  72. case "playtime":
  73. kid.SetValue(p.Playtime)
  74. }
  75. }
  76. return nil
  77. }
  78. func (p *Profile) Type() string {
  79. return "Profile"
  80. }