[golang] update error handling and bootstrap trade procurement
This commit is contained in:
parent
fd06283b8d
commit
a692a38d28
10 changed files with 66 additions and 50 deletions
|
@ -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.channel <- fmt.Errorf("failed to run contracts with ship %s: %w", ship.Symbol, err)
|
||||
a.channel <- fmt.Errorf("failed to run contract %s with ship %s: %w", contract.Id, ship.Symbol, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
a.channel <- fmt.Errorf("failed to run contracts: negotiating new contracts is not implemented yet")
|
||||
a.channel <- fmt.Errorf("negotiating new contracts is not implemented yet")
|
||||
// TODO
|
||||
//for {
|
||||
// negotiate
|
||||
|
@ -36,14 +36,30 @@ func (a *agent) autoContracting(ship *model.Ship) {
|
|||
|
||||
func (a *agent) runContract(contract *model.Contract, ship *model.Ship) error {
|
||||
if err := a.client.Accept(contract, a.db); err != nil {
|
||||
return fmt.Errorf("failed to run contract: %w", err)
|
||||
return fmt.Errorf("failed to accept contract: %w", err)
|
||||
}
|
||||
//slog.Info("running contract", "contract", contract, "ship", ship.Symbol)
|
||||
switch contract.Type {
|
||||
// TODO
|
||||
//case "PROCUREMENT":
|
||||
case "PROCUREMENT":
|
||||
if err := a.runProcurement(contract, ship); err != nil {
|
||||
return fmt.Errorf("failed to run procurement: %w", err)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("failed to run contract: handling contracts of type %s is not implemented yet", contract.Type)
|
||||
return fmt.Errorf("handling contracts of type %s is not implemented yet", contract.Type)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *agent) runProcurement(contract *model.Contract, ship *model.Ship) error {
|
||||
deliveryCargo := contract.Terms.Deliver[0].TradeSymbol
|
||||
deliveryWaypoint, err := a.client.GetWaypoint(contract.Terms.Deliver[0].DestinationSymbol, a.db)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get delivery waypoint: %w", err)
|
||||
}
|
||||
for !contract.Fullfilled {
|
||||
_ = deliveryCargo
|
||||
_ = deliveryWaypoint
|
||||
return fmt.Errorf("not implemented")
|
||||
}
|
||||
return fmt.Errorf("not implemented")
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ func Run(
|
|||
}
|
||||
|
||||
if agent.ships, err = client.MyShips(); err != nil {
|
||||
return fmt.Errorf("failed to init the agent's ships: %w", err)
|
||||
return fmt.Errorf("failed to get my ships: %w", err)
|
||||
}
|
||||
var state State = start_running_contracts_with_the_command_ship
|
||||
agent.wg.Add(1)
|
||||
|
@ -58,18 +58,18 @@ func Run(
|
|||
state++
|
||||
case visit_all_shipyards_with_the_starting_probe:
|
||||
if err := agent.visitAllShipyards(&agent.ships[1]); err != nil {
|
||||
agent.channel <- fmt.Errorf("failed agent run: %w", err)
|
||||
agent.channel <- fmt.Errorf("failed to visit all shipyards with ship %s: %w", agent.ships[1].Symbol, err)
|
||||
return
|
||||
}
|
||||
state++
|
||||
case send_the_starting_probe_to_a_shipyard_that_sells_probes:
|
||||
if err := agent.sendShipToShipyardThatSells(&agent.ships[1], "SHIP_PROBE"); err != nil {
|
||||
agent.channel <- fmt.Errorf("failed agent run: %w", err)
|
||||
agent.channel <- fmt.Errorf("failed to send the starting probe to a shipyard that sells probes: %w", err)
|
||||
return
|
||||
}
|
||||
state++
|
||||
default:
|
||||
agent.channel <- fmt.Errorf("agent runner reach an unknown state: %d", state)
|
||||
agent.channel <- fmt.Errorf("state not implemented: %d", state)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ func Run(
|
|||
go func() {
|
||||
defer errWg.Done()
|
||||
for err := range agent.channel {
|
||||
slog.Error("error", "err", err)
|
||||
slog.Error("agent run error", "err", err)
|
||||
}
|
||||
}()
|
||||
agent.wg.Wait()
|
||||
|
|
|
@ -27,7 +27,7 @@ func (a *agent) isThereAShipAtWaypoint(waypointSymbol string) bool {
|
|||
func (a *agent) listWaypointsInSystemWithTrait(systemSymbol string, trait string) ([]model.Waypoint, error) {
|
||||
waypoints, err := a.client.ListWaypointsInSystem(systemSymbol, a.db)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list waypoints with trait: %w", err)
|
||||
return nil, fmt.Errorf("failed to list waypoints: %w", err)
|
||||
}
|
||||
waypoints = slices.DeleteFunc(waypoints, func(waypoint model.Waypoint) bool {
|
||||
for _, t := range waypoint.Traits {
|
||||
|
@ -43,13 +43,13 @@ func (a *agent) listWaypointsInSystemWithTrait(systemSymbol string, trait string
|
|||
func (a *agent) listShipyardsInSystem(systemSymbol string) ([]model.Shipyard, error) {
|
||||
waypoints, err := a.listWaypointsInSystemWithTrait(systemSymbol, "SHIPYARD")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list shipyards in system %s: %w", systemSymbol, err)
|
||||
return nil, fmt.Errorf("failed to list waypoints in system %s with trait SHIPYARD: %w", systemSymbol, 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 %s: %w", systemSymbol, err)
|
||||
return nil, fmt.Errorf("failed to get shipyard %s: %w", waypoints[i].Symbol, err)
|
||||
}
|
||||
shipyards = append(shipyards, *shipyard)
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ func (a *agent) listShipyardsInSystem(systemSymbol string) ([]model.Shipyard, er
|
|||
func (a *agent) sendShipToShipyardThatSells(ship *model.Ship, shipType string) error {
|
||||
shipyards, err := a.listShipyardsInSystem(ship.Nav.SystemSymbol)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send ship %s to a shipyard that sells %s: %w", ship.Symbol, shipType, err)
|
||||
return fmt.Errorf("failed to list shipyards in system %s: %w", ship.Nav.SystemSymbol, err)
|
||||
}
|
||||
// filter out the shipyards that do not sell our ship
|
||||
shipyards = slices.DeleteFunc(shipyards, func(shipyard model.Shipyard) bool {
|
||||
|
@ -89,7 +89,7 @@ func (a *agent) sendShipToShipyardThatSells(ship *model.Ship, shipType string) e
|
|||
return cmp.Compare(aPrice, bPrice)
|
||||
})
|
||||
if err := a.client.Navigate(ship, shipyards[0].Symbol, a.db); err != nil {
|
||||
return fmt.Errorf("failed to send ship %s to a shipyard that sells %s: %w", ship.Symbol, shipType, err)
|
||||
return fmt.Errorf("failed to navigate to %s: %w", shipyards[0].Symbol, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
func (a *agent) visitAllShipyards(ship *model.Ship) error {
|
||||
shipyards, err := a.listShipyardsInSystem(ship.Nav.SystemSymbol)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to visit all shipyards: %w", err)
|
||||
return fmt.Errorf("failed to list shipyards in system %s: %w", ship.Nav.SystemSymbol, err)
|
||||
}
|
||||
shipyards = slices.DeleteFunc(shipyards, func(shipyard model.Shipyard) bool {
|
||||
// filter out shipyards for which we already have ships prices
|
||||
|
@ -25,22 +25,22 @@ func (a *agent) visitAllShipyards(ship *model.Ship) error {
|
|||
}
|
||||
waypoint, err := a.client.GetWaypoint(ship.Nav.WaypointSymbol, a.db)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to visit all shipyards: %w", err)
|
||||
return fmt.Errorf("failed to get nav waypoint %s: %w", ship.Nav.WaypointSymbol, 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)
|
||||
return fmt.Errorf("failed to get waypoint %s: %w", shipyards[i].Symbol, err)
|
||||
}
|
||||
waypoints = append(waypoints, *waypoint)
|
||||
}
|
||||
sortByDistanceFrom(waypoint, waypoints)
|
||||
if err := a.client.Navigate(ship, waypoints[0].Symbol, a.db); err != nil {
|
||||
return fmt.Errorf("failed to visit all shipyards: %w", err)
|
||||
return fmt.Errorf("failed to navigate to %s: %w", waypoints[0].Symbol, err)
|
||||
}
|
||||
if _, err := a.client.GetShipyard(&waypoints[0], a.db); err != nil {
|
||||
return fmt.Errorf("failed to visit all shipyards: %w", err)
|
||||
return fmt.Errorf("failed to get shipyard %s: %w", waypoints[0].Symbol, err)
|
||||
}
|
||||
// TODO get market data
|
||||
return a.visitAllShipyards(ship)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue