1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package main
- import (
- "fmt"
- "time"
- )
- type Profile struct {
- node *Node // Node pointer for Update
- Name string
- Date string // for now it's a string (TODO date.go)
- Location Location // System and Plant
- Playtime float64 // Play time in seconds
- // TODO travel.go
- Ships []*Ship
- }
- // String representation of Playtime
- func (p *Profile) PlayTime() string {
- t := time.Duration(p.Playtime) * time.Second
- return t.String()
- }
- func (p *Profile) Parse(node *Node) error {
- if !node.HasTag("root") {
- return fmt.Errorf("non-root node given, expected root node")
- }
- p.node = node
- for _, kid := range node.Children {
- switch kid.Key() {
- case "pilot":
- p.Name = kid.Value()
- case "date":
- p.Date = kid.Value()
- case "system":
- if p.Location.System == "" {
- p.Location.Parse(kid)
- }
- case "planet":
- if p.Location.Planet == "" {
- p.Location.Parse(kid)
- }
- case "playtime":
- p.Playtime = kid.ValueFloat64()
- case "ship":
- s := &Ship{}
- err := s.Parse(kid)
- if err != nil {
- return err
- }
- p.Ships = append(p.Ships, s)
- }
- }
- return nil
- }
- func (p *Profile) Update() error {
- if p.node == nil {
- return fmt.Errorf("no node, profile")
- }
- err := p.Location.Update()
- if err != nil {
- return err
- }
- for _, s := range p.Ships {
- err := s.Update()
- if err != nil {
- return err
- }
- }
- for _, kid := range p.node.Children {
- switch kid.Key() {
- case "pilot":
- kid.SetValue(p.Name)
- case "date":
- kid.SetValue(p.Date)
- case "playtime":
- kid.SetValue(p.Playtime)
- }
- }
- return nil
- }
- func (p *Profile) Type() string {
- return "Profile"
- }
|