[golang] update error handling and bootstrap trade procurement
This commit is contained in:
parent
fd06283b8d
commit
a692a38d28
10 changed files with 66 additions and 50 deletions
|
@ -34,7 +34,7 @@ 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 nil, fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
return contracts, nil
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ func (c *Client) dock(s *model.Ship) error {
|
|||
}
|
||||
var response dockResponse
|
||||
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
|
||||
return fmt.Errorf("failed to dock ship %s: %w", s.Symbol, err)
|
||||
return fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
s.Nav = response.Nav
|
||||
return nil
|
||||
|
@ -30,7 +30,7 @@ func (c *Client) MyShips() ([]model.Ship, error) {
|
|||
uriRef := url.URL{Path: "my/ships"}
|
||||
var ships []model.Ship
|
||||
if err := c.Send("GET", &uriRef, nil, &ships); err != nil {
|
||||
return nil, fmt.Errorf("failed to get ships: %w", err)
|
||||
return nil, fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
return ships, nil
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ func (c *Client) Navigate(s *model.Ship, waypointSymbol string, db *database.DB)
|
|||
return nil
|
||||
}
|
||||
if err := c.orbit(s); err != nil {
|
||||
return fmt.Errorf("failed to navigate ship %s to %s: %w", s.Symbol, waypointSymbol, err)
|
||||
return fmt.Errorf("failed to orbit: %w", err)
|
||||
}
|
||||
// TODO shortest path
|
||||
// TODO go refuel if necessary
|
||||
|
@ -55,13 +55,13 @@ func (c *Client) Navigate(s *model.Ship, waypointSymbol string, db *database.DB)
|
|||
}
|
||||
var response navigateResponse
|
||||
if err := c.Send("POST", &uriRef, navigateRequest{waypointSymbol}, &response); err != nil {
|
||||
return fmt.Errorf("failed to navigate ship %s to %s: %w", s.Symbol, waypointSymbol, err)
|
||||
return fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
s.Fuel = response.Fuel
|
||||
s.Nav = response.Nav
|
||||
select {
|
||||
case <-c.ctx.Done():
|
||||
return fmt.Errorf("failed to navigate ship %s to %s: ctx cancelled", s.Symbol, waypointSymbol)
|
||||
return fmt.Errorf("failed: context cancelled")
|
||||
case <-time.After(s.Nav.Route.Arrival.Sub(time.Now())):
|
||||
}
|
||||
s.Nav.Status = "IN_ORBIT"
|
||||
|
@ -78,7 +78,7 @@ func (c *Client) orbit(s *model.Ship) error {
|
|||
}
|
||||
var response orbitResponse
|
||||
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
|
||||
return fmt.Errorf("failed to orbit ship %s: %w", s.Symbol, err)
|
||||
return fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
s.Nav = response.Nav
|
||||
return nil
|
||||
|
@ -89,7 +89,7 @@ func (c *Client) refuel(s *model.Ship, db *database.DB) error {
|
|||
return nil
|
||||
}
|
||||
if err := c.dock(s); err != nil {
|
||||
return fmt.Errorf("failed to refuel ship %s: %w", s.Symbol, err)
|
||||
return fmt.Errorf("failed to dock: %w", err)
|
||||
}
|
||||
uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "refuel")}
|
||||
type refuelResponse struct {
|
||||
|
@ -99,14 +99,14 @@ func (c *Client) refuel(s *model.Ship, db *database.DB) error {
|
|||
}
|
||||
var response refuelResponse
|
||||
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
|
||||
return fmt.Errorf("failed to refuel ship %s: %w", s.Symbol, err)
|
||||
return fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
if err := db.SaveAgent(response.Agent); err != nil {
|
||||
return fmt.Errorf("failed to refuel ship %s: %w", s.Symbol, err)
|
||||
return fmt.Errorf("failed to save agent: %w", 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)
|
||||
return fmt.Errorf("failed to append transaction: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -16,10 +16,10 @@ func (c *Client) GetSystem(symbol string, db *database.DB) (*model.System, error
|
|||
uriRef := url.URL{Path: path.Join("systems", symbol)}
|
||||
var system model.System
|
||||
if err := c.Send("GET", &uriRef, nil, &system); err != nil {
|
||||
return nil, fmt.Errorf("failed to get system %s: %w", symbol, err)
|
||||
return nil, fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
if err := db.SaveSystem(&system); err != nil {
|
||||
return nil, fmt.Errorf("failed to get system %s: %w", symbol, err)
|
||||
return nil, fmt.Errorf("failed to save system %s: %w", system.Symbol, err)
|
||||
}
|
||||
return &system, nil
|
||||
}
|
||||
|
@ -33,10 +33,10 @@ func (c *Client) GetShipyard(waypoint *model.Waypoint, db *database.DB) (*model.
|
|||
uriRef := url.URL{Path: path.Join("systems", waypoint.SystemSymbol, "waypoints", waypoint.Symbol, "shipyard")}
|
||||
var shipyard model.Shipyard
|
||||
if err := c.Send("GET", &uriRef, nil, &shipyard); err != nil {
|
||||
return nil, fmt.Errorf("failed to get shipyard at %s: %w", waypoint.Symbol, err)
|
||||
return nil, fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
if err := db.SaveShipyard(&shipyard); err != nil {
|
||||
return nil, fmt.Errorf("failed to get shipyard at %s: %w", waypoint.Symbol, err)
|
||||
return nil, fmt.Errorf("failed to save shipyard %s: %w", shipyard.Symbol, err)
|
||||
}
|
||||
return &shipyard, nil
|
||||
}
|
||||
|
@ -50,10 +50,10 @@ func (c *Client) GetWaypoint(symbol string, db *database.DB) (*model.Waypoint, e
|
|||
uriRef := url.URL{Path: path.Join("systems", systemSymbol, "waypoints", symbol)}
|
||||
var waypoint model.Waypoint
|
||||
if err := c.Send("GET", &uriRef, nil, &waypoint); err != nil {
|
||||
return nil, fmt.Errorf("failed to get waypoint %s: %w", symbol, err)
|
||||
return nil, fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
if err := db.SaveWaypoint(&waypoint); err != nil {
|
||||
return nil, fmt.Errorf("failed to get waypoint %s: %w", symbol, err)
|
||||
return nil, fmt.Errorf("failed to save waypoint %s: %w", waypoint.Symbol, err)
|
||||
}
|
||||
return &waypoint, nil
|
||||
}
|
||||
|
@ -66,11 +66,11 @@ func (c *Client) ListWaypointsInSystem(systemSymbol string, db *database.DB) ([]
|
|||
uriRef := url.URL{Path: path.Join("systems", systemSymbol, "waypoints")}
|
||||
var waypoints []model.Waypoint
|
||||
if err := c.Send("GET", &uriRef, nil, &waypoints); err != nil {
|
||||
return nil, fmt.Errorf("failed to list waypoints in system %s: %w", systemSymbol, err)
|
||||
return nil, fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
for _, waypoint := range waypoints {
|
||||
if err := db.SaveWaypoint(&waypoint); err != nil {
|
||||
return nil, fmt.Errorf("failed to list waypoints in system %s: %w", systemSymbol, err)
|
||||
return nil, fmt.Errorf("failed to save waypoint %s: %w", waypoint.Symbol, err)
|
||||
}
|
||||
}
|
||||
return waypoints, nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue