[golang] implement shipyards visits
This commit is contained in:
parent
3cae67aea4
commit
bd2fb50c81
15 changed files with 299 additions and 45 deletions
|
@ -72,14 +72,14 @@ func (c *Client) Send(method string, uriRef *url.URL, payload any, response any)
|
|||
return res.Err
|
||||
}
|
||||
if err := res.Message.Error; err != nil {
|
||||
return err
|
||||
}
|
||||
err := res.Message.Error
|
||||
if err != nil {
|
||||
switch err.Code {
|
||||
case 4214:
|
||||
e := decodeShipInTransitError(err.Data)
|
||||
time.Sleep(e.SecondsToArrival.Duration() * time.Second)
|
||||
select {
|
||||
case <-c.ctx.Done():
|
||||
return fmt.Errorf("failed to send: ctx cancelled")
|
||||
case <-time.After(e.SecondsToArrival.Duration() * time.Second):
|
||||
}
|
||||
return c.Send(method, uriRef, payload, response)
|
||||
default:
|
||||
return err
|
||||
|
@ -193,7 +193,11 @@ func (c *Client) sendOne(method string, uri *url.URL, payload any) (*APIMessage,
|
|||
switch resp.StatusCode {
|
||||
case 429:
|
||||
e := decodeRateLimitError(msg.Error.Data)
|
||||
time.Sleep(e.RetryAfter.Duration() * time.Second)
|
||||
select {
|
||||
case <-c.ctx.Done():
|
||||
return nil, fmt.Errorf("failed to sendOne: ctx cancelled")
|
||||
case <-time.After(e.RetryAfter.Duration() * time.Second):
|
||||
}
|
||||
return c.sendOne(method, uri, payload)
|
||||
}
|
||||
return &msg, nil
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/database"
|
||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
|
||||
|
@ -34,6 +35,36 @@ func (c *Client) MyShips() ([]model.Ship, error) {
|
|||
return ships, nil
|
||||
}
|
||||
|
||||
func (c *Client) Navigate(s *model.Ship, w *model.Waypoint, db *database.DB) error {
|
||||
// TODO shortest path
|
||||
// TODO go refuel if necessary
|
||||
if err := c.orbit(s); err != nil {
|
||||
return fmt.Errorf("failed to navigate ship %s to %s: %w", s.Symbol, w.Symbol, err)
|
||||
}
|
||||
uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "navigate")}
|
||||
type navigateRequest struct {
|
||||
WaypointSymbol string `json:"waypointSymbol"`
|
||||
}
|
||||
type navigateResponse struct {
|
||||
//Events []model.Event `json:"events"`
|
||||
Fuel *model.Fuel `json:"fuel"`
|
||||
Nav *model.Nav `json:"nav"`
|
||||
}
|
||||
var response navigateResponse
|
||||
if err := c.Send("POST", &uriRef, navigateRequest{w.Symbol}, &response); err != nil {
|
||||
return fmt.Errorf("failed to navigate ship %s to %s: %w", s.Symbol, w.Symbol, 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, w.Symbol)
|
||||
case <-time.After(s.Nav.Route.Arrival.Sub(time.Now())):
|
||||
}
|
||||
s.Nav.Status = "IN_ORBIT"
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) orbit(s *model.Ship) error {
|
||||
if s.Nav.Status == "IN_ORBIT" {
|
||||
return nil
|
||||
|
@ -50,7 +81,7 @@ func (c *Client) orbit(s *model.Ship) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Refuel(s *model.Ship, db *database.DB) error {
|
||||
func (c *Client) refuel(s *model.Ship, db *database.DB) error {
|
||||
if s.Fuel.Current == s.Fuel.Capacity {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -25,9 +25,10 @@ func (c *Client) GetSystem(symbol string, db *database.DB) (*model.System, error
|
|||
}
|
||||
|
||||
func (c *Client) ListWaypointsInSystem(system *model.System, db *database.DB) ([]model.Waypoint, error) {
|
||||
// TODO database caching
|
||||
// TODO pagination
|
||||
// TODO check updated
|
||||
if waypoints, err := db.LoadWaypointsInSystem(system); err == nil && waypoints != nil {
|
||||
// TODO check last updated time
|
||||
return waypoints, nil
|
||||
}
|
||||
uriRef := url.URL{Path: path.Join("systems", system.Symbol, "waypoints")}
|
||||
var waypoints []model.Waypoint
|
||||
if err := c.Send("GET", &uriRef, nil, &waypoints); err != nil {
|
||||
|
@ -41,9 +42,26 @@ func (c *Client) ListWaypointsInSystem(system *model.System, db *database.DB) ([
|
|||
return waypoints, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetShipyard(waypoint *model.Waypoint, db *database.DB) (*model.Shipyard, error) {
|
||||
if shipyard, err := db.LoadShipyard(waypoint.Symbol); err == nil && shipyard != nil &&
|
||||
(shipyard.Ships != nil) { // TODO || !IsThereAShipAtWaypoint(waypoint)) {
|
||||
// TODO check last updated time
|
||||
return shipyard, nil
|
||||
}
|
||||
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)
|
||||
}
|
||||
if err := db.SaveShipyard(&shipyard); err != nil {
|
||||
return nil, fmt.Errorf("failed to get shipyard at %s: %w", waypoint.Symbol, err)
|
||||
}
|
||||
return &shipyard, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetWaypoint(symbol string, db *database.DB) (*model.Waypoint, error) {
|
||||
// TODO check updated
|
||||
if waypoint, err := db.LoadWaypoint(symbol); err == nil && waypoint != nil {
|
||||
// TODO check last updated time
|
||||
return waypoint, nil
|
||||
}
|
||||
systemSymbol := WaypointSymbolToSystemSymbol(symbol)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue