[golang] implement shipyards visits
This commit is contained in:
parent
3cae67aea4
commit
bd2fb50c81
15 changed files with 299 additions and 45 deletions
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
|
67
golang/pkg/agent/utils.go
Normal file
67
golang/pkg/agent/utils.go
Normal file
|
@ -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))
|
||||
})
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue