1
0
Fork 0

[golang] implement sending the starting probe to a shipyard that sells other probes

This commit is contained in:
Julien Dessaux 2025-02-18 00:05:48 +01:00
parent bd2fb50c81
commit 3656b87b86
Signed by: adyxax
GPG key ID: F92E51B86E07177E
6 changed files with 98 additions and 61 deletions

View file

@ -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 {
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, waypointSymbol, err)
}
// 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"`
@ -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"

View file

@ -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
}