summaryrefslogtreecommitdiff
path: root/golang/pkg/api
diff options
context:
space:
mode:
authorJulien Dessaux2025-02-18 00:05:48 +0100
committerJulien Dessaux2025-02-18 00:05:48 +0100
commit3656b87b86c76580fe947f02ac920343b49041a1 (patch)
treefaecd73a77f6596b35b5c3d25178aaaf4a4ca30c /golang/pkg/api
parent[golang] implement shipyards visits (diff)
downloadspacetraders-3656b87b86c76580fe947f02ac920343b49041a1.tar.gz
spacetraders-3656b87b86c76580fe947f02ac920343b49041a1.tar.bz2
spacetraders-3656b87b86c76580fe947f02ac920343b49041a1.zip
[golang] implement sending the starting probe to a shipyard that sells other probes
Diffstat (limited to 'golang/pkg/api')
-rw-r--r--golang/pkg/api/ships.go17
-rw-r--r--golang/pkg/api/systems.go36
2 files changed, 28 insertions, 25 deletions
diff --git a/golang/pkg/api/ships.go b/golang/pkg/api/ships.go
index 16d4e10..83b4e7f 100644
--- a/golang/pkg/api/ships.go
+++ b/golang/pkg/api/ships.go
@@ -35,12 +35,15 @@ 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
+func (c *Client) Navigate(s *model.Ship, waypointSymbol string, db *database.DB) error {
+ if s.Nav.WaypointSymbol == waypointSymbol {
+ return nil
+ }
if err := c.orbit(s); err != nil {
- return fmt.Errorf("failed to navigate ship %s to %s: %w", s.Symbol, w.Symbol, err)
+ return fmt.Errorf("failed to navigate ship %s to %s: %w", s.Symbol, waypointSymbol, err)
}
+ // TODO shortest path
+ // TODO go refuel if necessary
uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "navigate")}
type navigateRequest struct {
WaypointSymbol string `json:"waypointSymbol"`
@@ -51,14 +54,14 @@ func (c *Client) Navigate(s *model.Ship, w *model.Waypoint, db *database.DB) err
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)
+ 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)
}
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)
+ return fmt.Errorf("failed to navigate ship %s to %s: ctx cancelled", s.Symbol, waypointSymbol)
case <-time.After(s.Nav.Route.Arrival.Sub(time.Now())):
}
s.Nav.Status = "IN_ORBIT"
diff --git a/golang/pkg/api/systems.go b/golang/pkg/api/systems.go
index 05e43e3..5f5def6 100644
--- a/golang/pkg/api/systems.go
+++ b/golang/pkg/api/systems.go
@@ -24,24 +24,6 @@ func (c *Client) GetSystem(symbol string, db *database.DB) (*model.System, error
return &system, nil
}
-func (c *Client) ListWaypointsInSystem(system *model.System, db *database.DB) ([]model.Waypoint, error) {
- 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 {
- return nil, fmt.Errorf("failed to list waypoints in system %s: %w", system.Symbol, err)
- }
- for _, waypoint := range waypoints {
- if err := db.SaveWaypoint(&waypoint); err != nil {
- return nil, fmt.Errorf("failed to list waypoints in system %s: %w", system.Symbol, err)
- }
- }
- 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)) {
@@ -75,3 +57,21 @@ func (c *Client) GetWaypoint(symbol string, db *database.DB) (*model.Waypoint, e
}
return &waypoint, nil
}
+
+func (c *Client) ListWaypointsInSystem(systemSymbol string, db *database.DB) ([]model.Waypoint, error) {
+ if waypoints, err := db.LoadWaypointsInSystem(systemSymbol); err == nil && waypoints != nil {
+ // TODO check last updated time
+ return waypoints, nil
+ }
+ 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)
+ }
+ 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 waypoints, nil
+}