diff options
author | Julien Dessaux | 2025-02-17 00:40:43 +0100 |
---|---|---|
committer | Julien Dessaux | 2025-02-17 00:40:43 +0100 |
commit | bd2fb50c819c2b180758308ef3751e7ea73e6ab7 (patch) | |
tree | 19632f7098691a1147357039a5efedf7c40a8d53 /golang/pkg/agent | |
parent | [golang] handle paginated requests (diff) | |
download | spacetraders-bd2fb50c819c2b180758308ef3751e7ea73e6ab7.tar.gz spacetraders-bd2fb50c819c2b180758308ef3751e7ea73e6ab7.tar.bz2 spacetraders-bd2fb50c819c2b180758308ef3751e7ea73e6ab7.zip |
[golang] implement shipyards visits
Diffstat (limited to 'golang/pkg/agent')
-rw-r--r-- | golang/pkg/agent/contracting.go | 6 | ||||
-rw-r--r-- | golang/pkg/agent/error.go | 15 | ||||
-rw-r--r-- | golang/pkg/agent/run.go | 13 | ||||
-rw-r--r-- | golang/pkg/agent/utils.go | 67 | ||||
-rw-r--r-- | golang/pkg/agent/visit.go | 45 |
5 files changed, 114 insertions, 32 deletions
diff --git a/golang/pkg/agent/contracting.go b/golang/pkg/agent/contracting.go index 0f07170..a3eb7aa 100644 --- a/golang/pkg/agent/contracting.go +++ b/golang/pkg/agent/contracting.go @@ -11,7 +11,7 @@ func (a *agent) autoContracting(ship *model.Ship) { defer a.wg.Done() contracts, err := a.client.MyContracts() if err != nil { - a.sendShipError(fmt.Errorf("failed to get my contracts: %w", err), ship) + a.channel <- fmt.Errorf("failed to get my contracts with ship %s: %w", ship.Symbol, err) return } for _, contract := range contracts { @@ -21,12 +21,12 @@ func (a *agent) autoContracting(ship *model.Ship) { now := time.Now() if now.Before(contract.Terms.Deadline) { if err := a.runContract(&contract, ship); err != nil { - a.sendShipError(fmt.Errorf("failed to run contracts: %w", err), ship) + a.channel <- fmt.Errorf("failed to run contracts with ship %s: %w", ship.Symbol, err) return } } } - a.sendShipError(fmt.Errorf("failed to run contracts: negotiating new contracts is not implemented yet"), ship) + a.channel <- fmt.Errorf("failed to run contracts: negotiating new contracts is not implemented yet") // TODO //for { // negotiate diff --git a/golang/pkg/agent/error.go b/golang/pkg/agent/error.go deleted file mode 100644 index 64e6b8d..0000000 --- a/golang/pkg/agent/error.go +++ /dev/null @@ -1,15 +0,0 @@ -package agent - -import "git.adyxax.org/adyxax/spacetraders/golang/pkg/model" - -type shipError struct { - err error - ship *model.Ship -} - -func (a *agent) sendShipError(err error, ship *model.Ship) { - a.channel <- shipError{ - err: err, - ship: ship, - } -} diff --git a/golang/pkg/agent/run.go b/golang/pkg/agent/run.go index 1d70be8..8767c50 100644 --- a/golang/pkg/agent/run.go +++ b/golang/pkg/agent/run.go @@ -11,7 +11,7 @@ import ( ) type agent struct { - channel chan shipError + channel chan error client *api.Client db *database.DB getenv func(string) string @@ -32,7 +32,7 @@ func Run( getenv func(string) string, ) error { agent := agent{ - channel: make(chan shipError), + channel: make(chan error), client: client, db: db, getenv: getenv, @@ -57,12 +57,11 @@ func Run( state++ case visit_all_shipyards: if err := agent.visitAllShipyards(&agent.ships[1]); err != nil { - agent.sendShipError(fmt.Errorf("agent runner returned an error on state %d: %w", state, err), &agent.ships[1]) + agent.channel <- fmt.Errorf("agent runner returned an error on state %d: %w", state, err) } state++ - return default: - agent.sendShipError(fmt.Errorf("agent runner reach an unknown state: %d", state), nil) + agent.channel <- fmt.Errorf("agent runner reach an unknown state: %d", state) return } } @@ -71,8 +70,8 @@ func Run( errWg.Add(1) go func() { defer errWg.Done() - for shipErr := range agent.channel { - slog.Error("ship error", "err", shipErr.err, "ship", shipErr.ship.Symbol) + for err := range agent.channel { + slog.Error("error", "err", err) } }() agent.wg.Wait() diff --git a/golang/pkg/agent/utils.go b/golang/pkg/agent/utils.go new file mode 100644 index 0000000..5689405 --- /dev/null +++ b/golang/pkg/agent/utils.go @@ -0,0 +1,67 @@ +package agent + +import ( + "cmp" + "fmt" + "slices" + + "git.adyxax.org/adyxax/spacetraders/golang/pkg/model" +) + +type Point interface { + GetX() int + GetY() int +} + +func distance2(a Point, b Point) int { + x2 := a.GetX() - b.GetX() + y2 := a.GetY() - b.GetY() + return x2*x2 + y2*y2 +} + +func (a *agent) isThereAShipAtWaypoint(waypoint *model.Waypoint) bool { + for _, ship := range a.ships { + if ship.Nav.WaypointSymbol == waypoint.Symbol { + return true + } + } + return false +} + +func (a *agent) listWaypointsInSystemWithTrait(system *model.System, trait string) ([]model.Waypoint, error) { + waypoints, err := a.client.ListWaypointsInSystem(system, a.db) + if err != nil { + return nil, fmt.Errorf("failed to list waypoints with trait: %w", err) + } + waypoints = slices.DeleteFunc(waypoints, func(waypoint model.Waypoint) bool { + for _, t := range waypoint.Traits { + if t.Symbol == trait { + return false + } + } + return true + }) + return waypoints, nil +} + +func (a *agent) listShipyardsInSystem(system *model.System) ([]model.Shipyard, error) { + waypoints, err := a.listWaypointsInSystemWithTrait(system, "SHIPYARD") + if err != nil { + return nil, fmt.Errorf("failed to list shipyards in system: %w", err) + } + var shipyards []model.Shipyard + for i := range waypoints { + shipyard, err := a.client.GetShipyard(&waypoints[i], a.db) + if err != nil { + return nil, fmt.Errorf("failed to list shipyards in system: %w", err) + } + shipyards = append(shipyards, *shipyard) + } + return shipyards, nil +} + +func sortByDistanceFrom[P Point](origin P, destinations []P) { + slices.SortFunc(destinations, func(a, b P) int { + return cmp.Compare(distance2(origin, a), distance2(origin, b)) + }) +} diff --git a/golang/pkg/agent/visit.go b/golang/pkg/agent/visit.go index 019a6e5..2401b8d 100644 --- a/golang/pkg/agent/visit.go +++ b/golang/pkg/agent/visit.go @@ -2,7 +2,7 @@ package agent import ( "fmt" - "log/slog" + "slices" "git.adyxax.org/adyxax/spacetraders/golang/pkg/model" ) @@ -12,13 +12,44 @@ func (a *agent) visitAllShipyards(ship *model.Ship) error { if err != nil { return fmt.Errorf("failed to visit all shipyards: %w", err) } - waypoints, err := a.client.ListWaypointsInSystem(system, a.db) + shipyards, err := a.listShipyardsInSystem(system) if err != nil { return fmt.Errorf("failed to visit all shipyards: %w", err) } - //slog.Info("get system", "system", system.Waypoints, "err", err) - //waypoint, err := a.client.GetWaypoint("X1-RR14-J88", a.db) - slog.Info("get waypoint", "waypoint", waypoints[0]) - - return fmt.Errorf("failed to visit all shipyards: not implemented yet") + shipyards = slices.DeleteFunc(shipyards, func(shipyard model.Shipyard) bool { + // filter out shipyards for which we already have ships prices + if shipyard.Ships != nil { + return true + } + // filter out shipyards for which a ship is either present or inbound + waypoint, err := a.client.GetWaypoint(shipyard.Symbol, a.db) + if err != nil { + panic(fmt.Errorf("failed to visit all shipyards: %w", err)) + } + return a.isThereAShipAtWaypoint(waypoint) + }) + if len(shipyards) == 0 { + return nil + } + waypoint, err := a.client.GetWaypoint(ship.Nav.WaypointSymbol, a.db) + if err != nil { + return fmt.Errorf("failed to visit all shipyards: %w", err) + } + waypoints := make([]model.Waypoint, 0) + for i := range shipyards { + waypoint, err := a.client.GetWaypoint(shipyards[i].Symbol, a.db) + if err != nil { + return fmt.Errorf("failed to visit all shipyards: %w", err) + } + waypoints = append(waypoints, *waypoint) + } + sortByDistanceFrom(*waypoint, waypoints) + if err := a.client.Navigate(ship, &waypoints[0], a.db); err != nil { + return fmt.Errorf("failed to visit all shipyards: %w", err) + } + if _, err := a.client.GetShipyard(&waypoints[0], a.db); err != nil { + return fmt.Errorf("failed to visit all shipyards: %w", err) + } + // TODO get market data + return a.visitAllShipyards(ship) } |