[golang] refactored the api client to reduce the amount of db pointer shuffling around
This commit is contained in:
parent
312ef2eb57
commit
40c4a8df15
9 changed files with 58 additions and 57 deletions
|
@ -41,7 +41,7 @@ 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 {
|
||||
if err := a.client.Accept(contract); err != nil {
|
||||
return fmt.Errorf("failed to accept contract: %w", err)
|
||||
}
|
||||
//slog.Info("running contract", "contract", contract, "ship", ship.Symbol)
|
||||
|
@ -72,15 +72,15 @@ func (a *agent) runProcurement(contract *model.Contract, ship *model.Ship) error
|
|||
}
|
||||
}
|
||||
// deliver the goods
|
||||
if err := a.client.Navigate(ship, deliver.DestinationSymbol, a.db); err != nil {
|
||||
if err := a.client.Navigate(ship, deliver.DestinationSymbol); err != nil {
|
||||
return fmt.Errorf("failed to navigate to %s: %w", deliver.DestinationSymbol, err)
|
||||
}
|
||||
if err := a.client.Deliver(contract, ship, a.db); err != nil {
|
||||
if err := a.client.Deliver(contract, ship); err != nil {
|
||||
return fmt.Errorf("failed to deliver: %w", err)
|
||||
}
|
||||
deliver = contract.Terms.Deliver[0]
|
||||
if deliver.UnitsRequired == deliver.UnitsFulfilled {
|
||||
if err := a.client.Fulfill(contract, a.db); err != nil {
|
||||
if err := a.client.Fulfill(contract); err != nil {
|
||||
return fmt.Errorf("failed to fulfill: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -34,13 +34,13 @@ func (a *agent) buyTradeGood(ship *model.Ship, tradeGoodToBuy string) error {
|
|||
return &TradeGoodNotFoundError{}
|
||||
}
|
||||
// find the closest place to buy TODO
|
||||
waypoint, err := a.client.GetWaypoint(ship.Nav.WaypointSymbol, a.db)
|
||||
waypoint, err := a.client.GetWaypoint(ship.Nav.WaypointSymbol)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get nav waypoint %s: %w", ship.Nav.WaypointSymbol, err)
|
||||
}
|
||||
waypoints := make([]model.Waypoint, 0)
|
||||
for i := range markets {
|
||||
waypoint, err := a.client.GetWaypoint(markets[i].Symbol, a.db)
|
||||
waypoint, err := a.client.GetWaypoint(markets[i].Symbol)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get waypoint %s: %w", markets[i].Symbol, err)
|
||||
}
|
||||
|
@ -48,10 +48,10 @@ func (a *agent) buyTradeGood(ship *model.Ship, tradeGoodToBuy string) error {
|
|||
}
|
||||
sortByDistanceFrom(waypoint, waypoints)
|
||||
// Go there and refresh our market data
|
||||
if err := a.client.Navigate(ship, waypoints[0].Symbol, a.db); err != nil {
|
||||
if err := a.client.Navigate(ship, waypoints[0].Symbol); err != nil {
|
||||
return fmt.Errorf("failed to navigate to %s: %w", waypoints[0].Symbol, err)
|
||||
}
|
||||
market, err := a.client.GetMarket(waypoints[0].Symbol, a.db)
|
||||
market, err := a.client.GetMarket(waypoints[0].Symbol)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get market %s: %w", waypoints[0].Symbol, err)
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ func (a *agent) buyTradeGood(ship *model.Ship, tradeGoodToBuy string) error {
|
|||
if tradeGood.Type == "EXPORT" && tradeGood.Symbol == tradeGoodToBuy {
|
||||
for ship.Cargo.Units < ship.Cargo.Capacity {
|
||||
increment := min(ship.Cargo.Capacity-ship.Cargo.Units, tradeGood.TradeVolume)
|
||||
if err := a.client.Purchase(ship, tradeGoodToBuy, increment, a.db); err != nil {
|
||||
if err := a.client.Purchase(ship, tradeGoodToBuy, increment); err != nil {
|
||||
return fmt.Errorf("failed to purchase %d units of %s: %w", increment, tradeGoodToBuy, err)
|
||||
}
|
||||
}
|
||||
|
@ -98,13 +98,13 @@ func (a *agent) sellEverythingExcept(ship *model.Ship, keep string) error {
|
|||
return nil
|
||||
}
|
||||
// find the closest place to sell something TODO
|
||||
waypoint, err := a.client.GetWaypoint(ship.Nav.WaypointSymbol, a.db)
|
||||
waypoint, err := a.client.GetWaypoint(ship.Nav.WaypointSymbol)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get nav waypoint %s: %w", ship.Nav.WaypointSymbol, err)
|
||||
}
|
||||
waypoints := make([]model.Waypoint, 0)
|
||||
for i := range markets {
|
||||
waypoint, err := a.client.GetWaypoint(markets[i].Symbol, a.db)
|
||||
waypoint, err := a.client.GetWaypoint(markets[i].Symbol)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get waypoint %s: %w", markets[i].Symbol, err)
|
||||
}
|
||||
|
@ -112,10 +112,10 @@ func (a *agent) sellEverythingExcept(ship *model.Ship, keep string) error {
|
|||
}
|
||||
sortByDistanceFrom(waypoint, waypoints)
|
||||
// Go there and refresh our market data
|
||||
if err := a.client.Navigate(ship, waypoints[0].Symbol, a.db); err != nil {
|
||||
if err := a.client.Navigate(ship, waypoints[0].Symbol); err != nil {
|
||||
return fmt.Errorf("failed to navigate to %s: %w", waypoints[0].Symbol, err)
|
||||
}
|
||||
market, err := a.client.GetMarket(waypoints[0].Symbol, a.db)
|
||||
market, err := a.client.GetMarket(waypoints[0].Symbol)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get market %s: %w", waypoints[0].Symbol, err)
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ func (a *agent) sellEverythingExcept(ship *model.Ship, keep string) error {
|
|||
if tradeGood.Type == "IMPORT" && tradeGood.Symbol == cargoItem.Symbol {
|
||||
for units > 0 {
|
||||
increment := min(units, tradeGood.TradeVolume)
|
||||
if err := a.client.Sell(ship, cargoItem.Symbol, increment, a.db); err != nil {
|
||||
if err := a.client.Sell(ship, cargoItem.Symbol, increment); err != nil {
|
||||
return fmt.Errorf("failed to sell %d units of %s: %w", units, cargoItem.Symbol, err)
|
||||
}
|
||||
units = units - increment
|
||||
|
|
|
@ -25,7 +25,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)
|
||||
waypoints, err := a.client.ListWaypointsInSystem(systemSymbol)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list waypoints: %w", err)
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ func (a *agent) listMarketsInSystem(systemSymbol string) ([]model.Market, error)
|
|||
}
|
||||
var markets []model.Market
|
||||
for i := range waypoints {
|
||||
market, err := a.client.GetMarket(waypoints[i].Symbol, a.db)
|
||||
market, err := a.client.GetMarket(waypoints[i].Symbol)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get market %s: %w", waypoints[i].Symbol, err)
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ func (a *agent) listShipyardsInSystem(systemSymbol string) ([]model.Shipyard, er
|
|||
}
|
||||
var shipyards []model.Shipyard
|
||||
for i := range waypoints {
|
||||
shipyard, err := a.client.GetShipyard(waypoints[i].Symbol, a.db)
|
||||
shipyard, err := a.client.GetShipyard(waypoints[i].Symbol)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get shipyard %s: %w", waypoints[i].Symbol, err)
|
||||
}
|
||||
|
@ -104,7 +104,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 {
|
||||
if err := a.client.Navigate(ship, shipyards[0].Symbol); err != nil {
|
||||
return fmt.Errorf("failed to navigate to %s: %w", shipyards[0].Symbol, err)
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -23,29 +23,29 @@ func (a *agent) visitAllShipyards(ship *model.Ship) error {
|
|||
if len(shipyards) == 0 {
|
||||
return nil
|
||||
}
|
||||
waypoint, err := a.client.GetWaypoint(ship.Nav.WaypointSymbol, a.db)
|
||||
waypoint, err := a.client.GetWaypoint(ship.Nav.WaypointSymbol)
|
||||
if err != nil {
|
||||
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)
|
||||
waypoint, err := a.client.GetWaypoint(shipyards[i].Symbol)
|
||||
if err != nil {
|
||||
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 {
|
||||
if err := a.client.Navigate(ship, waypoints[0].Symbol); err != nil {
|
||||
return fmt.Errorf("failed to navigate to %s: %w", waypoints[0].Symbol, err)
|
||||
}
|
||||
if _, err := a.client.GetShipyard(waypoints[0].Symbol, a.db); err != nil {
|
||||
if _, err := a.client.GetShipyard(waypoints[0].Symbol); err != nil {
|
||||
return fmt.Errorf("failed to get shipyard %s: %w", waypoints[0].Symbol, err)
|
||||
}
|
||||
// If this waypoint is also a marketplace, get its data
|
||||
for _, trait := range waypoints[0].Traits {
|
||||
if trait.Symbol == "MARKETPLACE" {
|
||||
if _, err := a.client.GetMarket(waypoints[0].Symbol, a.db); err != nil {
|
||||
if _, err := a.client.GetMarket(waypoints[0].Symbol); err != nil {
|
||||
return fmt.Errorf("failed to get market %s: %w", waypoints[0].Symbol, err)
|
||||
}
|
||||
break
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue