[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
|
@ -36,7 +36,7 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
apiClient := api.NewClient(ctx)
|
apiClient := api.NewClient(ctx, db)
|
||||||
defer apiClient.Close()
|
defer apiClient.Close()
|
||||||
if err := agent.Run(
|
if err := agent.Run(
|
||||||
apiClient,
|
apiClient,
|
||||||
|
|
|
@ -41,7 +41,7 @@ func (a *agent) autoContracting(ship *model.Ship) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *agent) runContract(contract *model.Contract, ship *model.Ship) error {
|
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)
|
return fmt.Errorf("failed to accept contract: %w", err)
|
||||||
}
|
}
|
||||||
//slog.Info("running contract", "contract", contract, "ship", ship.Symbol)
|
//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
|
// 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)
|
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)
|
return fmt.Errorf("failed to deliver: %w", err)
|
||||||
}
|
}
|
||||||
deliver = contract.Terms.Deliver[0]
|
deliver = contract.Terms.Deliver[0]
|
||||||
if deliver.UnitsRequired == deliver.UnitsFulfilled {
|
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 fmt.Errorf("failed to fulfill: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -34,13 +34,13 @@ func (a *agent) buyTradeGood(ship *model.Ship, tradeGoodToBuy string) error {
|
||||||
return &TradeGoodNotFoundError{}
|
return &TradeGoodNotFoundError{}
|
||||||
}
|
}
|
||||||
// find the closest place to buy TODO
|
// 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 {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get nav waypoint %s: %w", ship.Nav.WaypointSymbol, err)
|
return fmt.Errorf("failed to get nav waypoint %s: %w", ship.Nav.WaypointSymbol, err)
|
||||||
}
|
}
|
||||||
waypoints := make([]model.Waypoint, 0)
|
waypoints := make([]model.Waypoint, 0)
|
||||||
for i := range markets {
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get waypoint %s: %w", markets[i].Symbol, err)
|
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)
|
sortByDistanceFrom(waypoint, waypoints)
|
||||||
// Go there and refresh our market data
|
// 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)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get market %s: %w", waypoints[0].Symbol, err)
|
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 {
|
if tradeGood.Type == "EXPORT" && tradeGood.Symbol == tradeGoodToBuy {
|
||||||
for ship.Cargo.Units < ship.Cargo.Capacity {
|
for ship.Cargo.Units < ship.Cargo.Capacity {
|
||||||
increment := min(ship.Cargo.Capacity-ship.Cargo.Units, tradeGood.TradeVolume)
|
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)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
// find the closest place to sell something TODO
|
// 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 {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get nav waypoint %s: %w", ship.Nav.WaypointSymbol, err)
|
return fmt.Errorf("failed to get nav waypoint %s: %w", ship.Nav.WaypointSymbol, err)
|
||||||
}
|
}
|
||||||
waypoints := make([]model.Waypoint, 0)
|
waypoints := make([]model.Waypoint, 0)
|
||||||
for i := range markets {
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get waypoint %s: %w", markets[i].Symbol, err)
|
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)
|
sortByDistanceFrom(waypoint, waypoints)
|
||||||
// Go there and refresh our market data
|
// 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)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get market %s: %w", waypoints[0].Symbol, err)
|
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 {
|
if tradeGood.Type == "IMPORT" && tradeGood.Symbol == cargoItem.Symbol {
|
||||||
for units > 0 {
|
for units > 0 {
|
||||||
increment := min(units, tradeGood.TradeVolume)
|
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)
|
return fmt.Errorf("failed to sell %d units of %s: %w", units, cargoItem.Symbol, err)
|
||||||
}
|
}
|
||||||
units = units - increment
|
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) {
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to list waypoints: %w", err)
|
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
|
var markets []model.Market
|
||||||
for i := range waypoints {
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get market %s: %w", waypoints[i].Symbol, err)
|
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
|
var shipyards []model.Shipyard
|
||||||
for i := range waypoints {
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get shipyard %s: %w", waypoints[i].Symbol, err)
|
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)
|
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 fmt.Errorf("failed to navigate to %s: %w", shipyards[0].Symbol, err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -23,29 +23,29 @@ func (a *agent) visitAllShipyards(ship *model.Ship) error {
|
||||||
if len(shipyards) == 0 {
|
if len(shipyards) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
waypoint, err := a.client.GetWaypoint(ship.Nav.WaypointSymbol, a.db)
|
waypoint, err := a.client.GetWaypoint(ship.Nav.WaypointSymbol)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get nav waypoint %s: %w", ship.Nav.WaypointSymbol, err)
|
return fmt.Errorf("failed to get nav waypoint %s: %w", ship.Nav.WaypointSymbol, err)
|
||||||
}
|
}
|
||||||
waypoints := make([]model.Waypoint, 0)
|
waypoints := make([]model.Waypoint, 0)
|
||||||
for i := range shipyards {
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get waypoint %s: %w", shipyards[i].Symbol, err)
|
return fmt.Errorf("failed to get waypoint %s: %w", shipyards[i].Symbol, err)
|
||||||
}
|
}
|
||||||
waypoints = append(waypoints, *waypoint)
|
waypoints = append(waypoints, *waypoint)
|
||||||
}
|
}
|
||||||
sortByDistanceFrom(waypoint, waypoints)
|
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)
|
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)
|
return fmt.Errorf("failed to get shipyard %s: %w", waypoints[0].Symbol, err)
|
||||||
}
|
}
|
||||||
// If this waypoint is also a marketplace, get its data
|
// If this waypoint is also a marketplace, get its data
|
||||||
for _, trait := range waypoints[0].Traits {
|
for _, trait := range waypoints[0].Traits {
|
||||||
if trait.Symbol == "MARKETPLACE" {
|
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)
|
return fmt.Errorf("failed to get market %s: %w", waypoints[0].Symbol, err)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
|
|
@ -6,10 +6,13 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.adyxax.org/adyxax/spacetraders/golang/pkg/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
baseURI *url.URL
|
baseURI *url.URL
|
||||||
|
db *database.DB
|
||||||
requestsChannel chan *Request
|
requestsChannel chan *Request
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
headers *http.Header
|
headers *http.Header
|
||||||
|
@ -17,7 +20,7 @@ type Client struct {
|
||||||
pq *PriorityQueue
|
pq *PriorityQueue
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(ctx context.Context) *Client {
|
func NewClient(ctx context.Context, db *database.DB) *Client {
|
||||||
baseURI, err := url.Parse("https://api.spacetraders.io/v2/")
|
baseURI, err := url.Parse("https://api.spacetraders.io/v2/")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("baseURI failed to parse")
|
panic("baseURI failed to parse")
|
||||||
|
@ -26,6 +29,7 @@ func NewClient(ctx context.Context) *Client {
|
||||||
heap.Init(&pq)
|
heap.Init(&pq)
|
||||||
client := &Client{
|
client := &Client{
|
||||||
baseURI: baseURI,
|
baseURI: baseURI,
|
||||||
|
db: db,
|
||||||
requestsChannel: make(chan *Request),
|
requestsChannel: make(chan *Request),
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
headers: &http.Header{
|
headers: &http.Header{
|
||||||
|
|
|
@ -5,11 +5,10 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/database"
|
|
||||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
|
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Client) Accept(contract *model.Contract, db *database.DB) error {
|
func (c *Client) Accept(contract *model.Contract) error {
|
||||||
if contract.Accepted {
|
if contract.Accepted {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -22,7 +21,7 @@ func (c *Client) Accept(contract *model.Contract, db *database.DB) error {
|
||||||
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
|
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
|
||||||
return fmt.Errorf("failed API request: %w", err)
|
return fmt.Errorf("failed API request: %w", err)
|
||||||
}
|
}
|
||||||
if err := db.SaveAgent(response.Agent); err != nil {
|
if err := c.db.SaveAgent(response.Agent); err != nil {
|
||||||
return fmt.Errorf("failed to save agent: %w", err)
|
return fmt.Errorf("failed to save agent: %w", err)
|
||||||
}
|
}
|
||||||
contract.Accepted = response.Contract.Accepted
|
contract.Accepted = response.Contract.Accepted
|
||||||
|
@ -30,7 +29,7 @@ func (c *Client) Accept(contract *model.Contract, db *database.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Deliver(contract *model.Contract, ship *model.Ship, db *database.DB) error {
|
func (c *Client) Deliver(contract *model.Contract, ship *model.Ship) error {
|
||||||
deliver := contract.Terms.Deliver[0]
|
deliver := contract.Terms.Deliver[0]
|
||||||
var units int
|
var units int
|
||||||
for _, cargoItem := range ship.Cargo.Inventory {
|
for _, cargoItem := range ship.Cargo.Inventory {
|
||||||
|
@ -58,7 +57,7 @@ func (c *Client) Deliver(contract *model.Contract, ship *model.Ship, db *databas
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Fulfill(contract *model.Contract, db *database.DB) error {
|
func (c *Client) Fulfill(contract *model.Contract) error {
|
||||||
if contract.Fulfilled {
|
if contract.Fulfilled {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -71,7 +70,7 @@ func (c *Client) Fulfill(contract *model.Contract, db *database.DB) error {
|
||||||
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
|
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
|
||||||
return fmt.Errorf("failed API request: %w", err)
|
return fmt.Errorf("failed API request: %w", err)
|
||||||
}
|
}
|
||||||
if err := db.SaveAgent(response.Agent); err != nil {
|
if err := c.db.SaveAgent(response.Agent); err != nil {
|
||||||
return fmt.Errorf("failed to save agent: %w", err)
|
return fmt.Errorf("failed to save agent: %w", err)
|
||||||
}
|
}
|
||||||
contract.Fulfilled = response.Contract.Fulfilled
|
contract.Fulfilled = response.Contract.Fulfilled
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/database"
|
|
||||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
|
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -35,7 +34,7 @@ func (c *Client) MyShips() ([]model.Ship, error) {
|
||||||
return ships, nil
|
return ships, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Navigate(s *model.Ship, waypointSymbol string, db *database.DB) error {
|
func (c *Client) Navigate(s *model.Ship, waypointSymbol string) error {
|
||||||
if s.Nav.WaypointSymbol == waypointSymbol {
|
if s.Nav.WaypointSymbol == waypointSymbol {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -96,7 +95,7 @@ func (c *Client) orbit(s *model.Ship) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Purchase(s *model.Ship, cargoItem string, units int, db *database.DB) error {
|
func (c *Client) Purchase(s *model.Ship, cargoItem string, units int) error {
|
||||||
if err := c.dock(s); err != nil {
|
if err := c.dock(s); err != nil {
|
||||||
return fmt.Errorf("failed to dock: %w", err)
|
return fmt.Errorf("failed to dock: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -114,11 +113,11 @@ func (c *Client) Purchase(s *model.Ship, cargoItem string, units int, db *databa
|
||||||
if err := c.Send("POST", &uriRef, purchaseRequest{cargoItem, units}, &response); err != nil {
|
if err := c.Send("POST", &uriRef, purchaseRequest{cargoItem, units}, &response); err != nil {
|
||||||
return fmt.Errorf("failed API request: %w", err)
|
return fmt.Errorf("failed API request: %w", err)
|
||||||
}
|
}
|
||||||
if err := db.SaveAgent(response.Agent); err != nil {
|
if err := c.db.SaveAgent(response.Agent); err != nil {
|
||||||
return fmt.Errorf("failed to save agent: %w", err)
|
return fmt.Errorf("failed to save agent: %w", err)
|
||||||
}
|
}
|
||||||
s.Cargo = response.Cargo
|
s.Cargo = response.Cargo
|
||||||
if err := db.AppendTransaction(response.Transaction); err != nil {
|
if err := c.db.AppendTransaction(response.Transaction); err != nil {
|
||||||
return fmt.Errorf("failed to append transaction: %w", err)
|
return fmt.Errorf("failed to append transaction: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -141,17 +140,17 @@ func (c *Client) refuel(s *model.Ship, db *database.DB) error {
|
||||||
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
|
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
|
||||||
return fmt.Errorf("failed API request: %w", err)
|
return fmt.Errorf("failed API request: %w", err)
|
||||||
}
|
}
|
||||||
if err := db.SaveAgent(response.Agent); err != nil {
|
if err := c.db.SaveAgent(response.Agent); err != nil {
|
||||||
return fmt.Errorf("failed to save agent: %w", err)
|
return fmt.Errorf("failed to save agent: %w", err)
|
||||||
}
|
}
|
||||||
s.Fuel = response.Fuel
|
s.Fuel = response.Fuel
|
||||||
if err := db.AppendTransaction(response.Transaction); err != nil {
|
if err := c.db.AppendTransaction(response.Transaction); err != nil {
|
||||||
return fmt.Errorf("failed to append transaction: %w", err)
|
return fmt.Errorf("failed to append transaction: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Sell(s *model.Ship, cargoItem string, units int, db *database.DB) error {
|
func (c *Client) Sell(s *model.Ship, cargoItem string, units int) error {
|
||||||
if err := c.dock(s); err != nil {
|
if err := c.dock(s); err != nil {
|
||||||
return fmt.Errorf("failed to dock: %w", err)
|
return fmt.Errorf("failed to dock: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -169,11 +168,11 @@ func (c *Client) Sell(s *model.Ship, cargoItem string, units int, db *database.D
|
||||||
if err := c.Send("POST", &uriRef, sellRequest{cargoItem, units}, &response); err != nil {
|
if err := c.Send("POST", &uriRef, sellRequest{cargoItem, units}, &response); err != nil {
|
||||||
return fmt.Errorf("failed API request: %w", err)
|
return fmt.Errorf("failed API request: %w", err)
|
||||||
}
|
}
|
||||||
if err := db.SaveAgent(response.Agent); err != nil {
|
if err := c.db.SaveAgent(response.Agent); err != nil {
|
||||||
return fmt.Errorf("failed to save agent: %w", err)
|
return fmt.Errorf("failed to save agent: %w", err)
|
||||||
}
|
}
|
||||||
s.Cargo = response.Cargo
|
s.Cargo = response.Cargo
|
||||||
if err := db.AppendTransaction(response.Transaction); err != nil {
|
if err := c.db.AppendTransaction(response.Transaction); err != nil {
|
||||||
return fmt.Errorf("failed to append transaction: %w", err)
|
return fmt.Errorf("failed to append transaction: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -5,12 +5,11 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/database"
|
|
||||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
|
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Client) GetMarket(waypointSymbol string, db *database.DB) (*model.Market, error) {
|
func (c *Client) GetMarket(waypointSymbol string) (*model.Market, error) {
|
||||||
if market, err := db.LoadMarket(waypointSymbol); err == nil && market != nil {
|
if market, err := c.db.LoadMarket(waypointSymbol); err == nil && market != nil {
|
||||||
// TODO check last updated time
|
// TODO check last updated time
|
||||||
return market, nil
|
return market, nil
|
||||||
}
|
}
|
||||||
|
@ -20,14 +19,14 @@ func (c *Client) GetMarket(waypointSymbol string, db *database.DB) (*model.Marke
|
||||||
if err := c.Send("GET", &uriRef, nil, &market); err != nil {
|
if err := c.Send("GET", &uriRef, nil, &market); err != nil {
|
||||||
return nil, fmt.Errorf("failed API request: %w", err)
|
return nil, fmt.Errorf("failed API request: %w", err)
|
||||||
}
|
}
|
||||||
if err := db.SaveMarket(&market); err != nil {
|
if err := c.db.SaveMarket(&market); err != nil {
|
||||||
return nil, fmt.Errorf("failed to save market %s: %w", market.Symbol, err)
|
return nil, fmt.Errorf("failed to save market %s: %w", market.Symbol, err)
|
||||||
}
|
}
|
||||||
return &market, nil
|
return &market, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetShipyard(waypointSymbol string, db *database.DB) (*model.Shipyard, error) {
|
func (c *Client) GetShipyard(waypointSymbol string) (*model.Shipyard, error) {
|
||||||
if shipyard, err := db.LoadShipyard(waypointSymbol); err == nil && shipyard != nil &&
|
if shipyard, err := c.db.LoadShipyard(waypointSymbol); err == nil && shipyard != nil &&
|
||||||
(shipyard.Ships != nil) { // TODO || !IsThereAShipAtWaypoint(waypoint)) {
|
(shipyard.Ships != nil) { // TODO || !IsThereAShipAtWaypoint(waypoint)) {
|
||||||
// TODO check last updated time
|
// TODO check last updated time
|
||||||
return shipyard, nil
|
return shipyard, nil
|
||||||
|
@ -38,14 +37,14 @@ func (c *Client) GetShipyard(waypointSymbol string, db *database.DB) (*model.Shi
|
||||||
if err := c.Send("GET", &uriRef, nil, &shipyard); err != nil {
|
if err := c.Send("GET", &uriRef, nil, &shipyard); err != nil {
|
||||||
return nil, fmt.Errorf("failed API request: %w", err)
|
return nil, fmt.Errorf("failed API request: %w", err)
|
||||||
}
|
}
|
||||||
if err := db.SaveShipyard(&shipyard); err != nil {
|
if err := c.db.SaveShipyard(&shipyard); err != nil {
|
||||||
return nil, fmt.Errorf("failed to save shipyard %s: %w", shipyard.Symbol, err)
|
return nil, fmt.Errorf("failed to save shipyard %s: %w", shipyard.Symbol, err)
|
||||||
}
|
}
|
||||||
return &shipyard, nil
|
return &shipyard, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetSystem(systemSymbol string, db *database.DB) (*model.System, error) {
|
func (c *Client) GetSystem(systemSymbol string) (*model.System, error) {
|
||||||
if system, err := db.LoadSystem(systemSymbol); err == nil && system != nil {
|
if system, err := c.db.LoadSystem(systemSymbol); err == nil && system != nil {
|
||||||
return system, nil
|
return system, nil
|
||||||
}
|
}
|
||||||
uriRef := url.URL{Path: path.Join("systems", systemSymbol)}
|
uriRef := url.URL{Path: path.Join("systems", systemSymbol)}
|
||||||
|
@ -53,14 +52,14 @@ func (c *Client) GetSystem(systemSymbol string, db *database.DB) (*model.System,
|
||||||
if err := c.Send("GET", &uriRef, nil, &system); err != nil {
|
if err := c.Send("GET", &uriRef, nil, &system); err != nil {
|
||||||
return nil, fmt.Errorf("failed API request: %w", err)
|
return nil, fmt.Errorf("failed API request: %w", err)
|
||||||
}
|
}
|
||||||
if err := db.SaveSystem(&system); err != nil {
|
if err := c.db.SaveSystem(&system); err != nil {
|
||||||
return nil, fmt.Errorf("failed to save system %s: %w", system.Symbol, err)
|
return nil, fmt.Errorf("failed to save system %s: %w", system.Symbol, err)
|
||||||
}
|
}
|
||||||
return &system, nil
|
return &system, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetWaypoint(waypointSymbol string, db *database.DB) (*model.Waypoint, error) {
|
func (c *Client) GetWaypoint(waypointSymbol string) (*model.Waypoint, error) {
|
||||||
if waypoint, err := db.LoadWaypoint(waypointSymbol); err == nil && waypoint != nil {
|
if waypoint, err := c.db.LoadWaypoint(waypointSymbol); err == nil && waypoint != nil {
|
||||||
// TODO check last updated time
|
// TODO check last updated time
|
||||||
return waypoint, nil
|
return waypoint, nil
|
||||||
}
|
}
|
||||||
|
@ -70,14 +69,14 @@ func (c *Client) GetWaypoint(waypointSymbol string, db *database.DB) (*model.Way
|
||||||
if err := c.Send("GET", &uriRef, nil, &waypoint); err != nil {
|
if err := c.Send("GET", &uriRef, nil, &waypoint); err != nil {
|
||||||
return nil, fmt.Errorf("failed API request: %w", err)
|
return nil, fmt.Errorf("failed API request: %w", err)
|
||||||
}
|
}
|
||||||
if err := db.SaveWaypoint(&waypoint); err != nil {
|
if err := c.db.SaveWaypoint(&waypoint); err != nil {
|
||||||
return nil, fmt.Errorf("failed to save waypoint %s: %w", waypoint.Symbol, err)
|
return nil, fmt.Errorf("failed to save waypoint %s: %w", waypoint.Symbol, err)
|
||||||
}
|
}
|
||||||
return &waypoint, nil
|
return &waypoint, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) ListWaypointsInSystem(systemSymbol string, db *database.DB) ([]model.Waypoint, error) {
|
func (c *Client) ListWaypointsInSystem(systemSymbol string) ([]model.Waypoint, error) {
|
||||||
if waypoints, err := db.LoadWaypointsInSystem(systemSymbol); err == nil && waypoints != nil {
|
if waypoints, err := c.db.LoadWaypointsInSystem(systemSymbol); err == nil && waypoints != nil {
|
||||||
// TODO check last updated time
|
// TODO check last updated time
|
||||||
return waypoints, nil
|
return waypoints, nil
|
||||||
}
|
}
|
||||||
|
@ -87,7 +86,7 @@ func (c *Client) ListWaypointsInSystem(systemSymbol string, db *database.DB) ([]
|
||||||
return nil, fmt.Errorf("failed API request: %w", err)
|
return nil, fmt.Errorf("failed API request: %w", err)
|
||||||
}
|
}
|
||||||
for _, waypoint := range waypoints {
|
for _, waypoint := range waypoints {
|
||||||
if err := db.SaveWaypoint(&waypoint); err != nil {
|
if err := c.db.SaveWaypoint(&waypoint); err != nil {
|
||||||
return nil, fmt.Errorf("failed to save waypoint %s: %w", waypoint.Symbol, err)
|
return nil, fmt.Errorf("failed to save waypoint %s: %w", waypoint.Symbol, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue