diff options
Diffstat (limited to 'golang/pkg/api')
-rw-r--r-- | golang/pkg/api/contracts.go | 17 | ||||
-rw-r--r-- | golang/pkg/api/ships.go | 11 |
2 files changed, 23 insertions, 5 deletions
diff --git a/golang/pkg/api/contracts.go b/golang/pkg/api/contracts.go new file mode 100644 index 0000000..f82ee6d --- /dev/null +++ b/golang/pkg/api/contracts.go @@ -0,0 +1,17 @@ +package api + +import ( + "fmt" + "net/url" + + "git.adyxax.org/adyxax/spacetraders/golang/pkg/model" +) + +func (c *Client) MyContracts() ([]model.Contract, error) { + uriRef := url.URL{Path: "my/contracts"} + var contracts []model.Contract + if err := c.Send("GET", &uriRef, nil, &contracts); err != nil { + return nil, fmt.Errorf("failed to get contracts: %w", err) + } + return contracts, nil +} diff --git a/golang/pkg/api/ships.go b/golang/pkg/api/ships.go index 0674b3d..485f437 100644 --- a/golang/pkg/api/ships.go +++ b/golang/pkg/api/ships.go @@ -5,12 +5,11 @@ import ( "net/url" "path" - "git.adyxax.org/adyxax/spacetraders/golang/pkg/agent" "git.adyxax.org/adyxax/spacetraders/golang/pkg/database" "git.adyxax.org/adyxax/spacetraders/golang/pkg/model" ) -func (c *Client) Dock(s *model.Ship) error { +func (c *Client) dock(s *model.Ship) error { if s.Nav.Status == "DOCKED" { return nil } @@ -35,7 +34,7 @@ func (c *Client) MyShips() ([]model.Ship, error) { return ships, nil } -func (c *Client) Orbit(s *model.Ship) error { +func (c *Client) orbit(s *model.Ship) error { if s.Nav.Status == "IN_ORBIT" { return nil } @@ -55,7 +54,7 @@ func (c *Client) Refuel(s *model.Ship, db *database.DB) error { if s.Fuel.Current == s.Fuel.Capacity { return nil } - if err := c.Dock(s); err != nil { + if err := c.dock(s); err != nil { return fmt.Errorf("failed to refuel ship %s: %w", s.Symbol, err) } uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "refuel")} @@ -68,7 +67,9 @@ func (c *Client) Refuel(s *model.Ship, db *database.DB) error { if err := c.Send("POST", &uriRef, nil, &response); err != nil { return fmt.Errorf("failed to refuel ship %s: %w", s.Symbol, err) } - agent.SetAgent(response.Agent) + if err := db.SaveAgent(response.Agent); err != nil { + return fmt.Errorf("failed to refuel ship %s: %w", s.Symbol, err) + } s.Fuel = response.Fuel if err := db.AppendTransaction(response.Transaction); err != nil { return fmt.Errorf("failed to refuel ship %s: %w", s.Symbol, err) |