summaryrefslogtreecommitdiff
path: root/golang/pkg/api/ships.go
diff options
context:
space:
mode:
authorJulien Dessaux2025-02-19 00:19:58 +0100
committerJulien Dessaux2025-02-19 00:19:58 +0100
commit312ef2eb5792cd145d6829e2abc017dd304c5819 (patch)
treecf782f8356bea73eacb5495631794cf5584e00cb /golang/pkg/api/ships.go
parent[golang] update error handling and bootstrap trade procurement (diff)
downloadspacetraders-312ef2eb5792cd145d6829e2abc017dd304c5819.tar.gz
spacetraders-312ef2eb5792cd145d6829e2abc017dd304c5819.tar.bz2
spacetraders-312ef2eb5792cd145d6829e2abc017dd304c5819.zip
[golang] implement trading and contracting
Diffstat (limited to 'golang/pkg/api/ships.go')
-rw-r--r--golang/pkg/api/ships.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/golang/pkg/api/ships.go b/golang/pkg/api/ships.go
index 67a1ebd..21676d1 100644
--- a/golang/pkg/api/ships.go
+++ b/golang/pkg/api/ships.go
@@ -68,6 +68,18 @@ func (c *Client) Navigate(s *model.Ship, waypointSymbol string, db *database.DB)
return nil
}
+func (c *Client) NegotiateContract(s *model.Ship) (*model.Contract, error) {
+ uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "negotiate", "contract")}
+ type negotiateResponse struct {
+ Contract *model.Contract `json:"contract"`
+ }
+ var response negotiateResponse
+ if err := c.Send("POST", &uriRef, nil, &response); err != nil {
+ return nil, fmt.Errorf("failed API request: %w", err)
+ }
+ return response.Contract, nil
+}
+
func (c *Client) orbit(s *model.Ship) error {
if s.Nav.Status == "IN_ORBIT" {
return nil
@@ -84,6 +96,34 @@ func (c *Client) orbit(s *model.Ship) error {
return nil
}
+func (c *Client) Purchase(s *model.Ship, cargoItem string, units int, db *database.DB) error {
+ if err := c.dock(s); err != nil {
+ return fmt.Errorf("failed to dock: %w", err)
+ }
+ uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "purchase")}
+ type purchaseRequest struct {
+ Symbol string `json:"symbol"`
+ Units int `json:"units"`
+ }
+ type purchaseResponse struct {
+ Agent *model.Agent `json:"agent"`
+ Cargo *model.Cargo `json:"cargo"`
+ Transaction *model.Transaction `json:"transaction"`
+ }
+ var response purchaseResponse
+ if err := c.Send("POST", &uriRef, purchaseRequest{cargoItem, units}, &response); err != nil {
+ return fmt.Errorf("failed API request: %w", err)
+ }
+ if err := db.SaveAgent(response.Agent); err != nil {
+ return fmt.Errorf("failed to save agent: %w", err)
+ }
+ s.Cargo = response.Cargo
+ if err := db.AppendTransaction(response.Transaction); err != nil {
+ return fmt.Errorf("failed to append transaction: %w", err)
+ }
+ return nil
+}
+
func (c *Client) refuel(s *model.Ship, db *database.DB) error {
if s.Fuel.Current == s.Fuel.Capacity {
return nil
@@ -110,3 +150,31 @@ func (c *Client) refuel(s *model.Ship, db *database.DB) error {
}
return nil
}
+
+func (c *Client) Sell(s *model.Ship, cargoItem string, units int, db *database.DB) error {
+ if err := c.dock(s); err != nil {
+ return fmt.Errorf("failed to dock: %w", err)
+ }
+ uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "sell")}
+ type sellRequest struct {
+ Symbol string `json:"symbol"`
+ Units int `json:"units"`
+ }
+ type sellResponse struct {
+ Agent *model.Agent `json:"agent"`
+ Cargo *model.Cargo `json:"cargo"`
+ Transaction *model.Transaction `json:"transaction"`
+ }
+ var response sellResponse
+ if err := c.Send("POST", &uriRef, sellRequest{cargoItem, units}, &response); err != nil {
+ return fmt.Errorf("failed API request: %w", err)
+ }
+ if err := db.SaveAgent(response.Agent); err != nil {
+ return fmt.Errorf("failed to save agent: %w", err)
+ }
+ s.Cargo = response.Cargo
+ if err := db.AppendTransaction(response.Transaction); err != nil {
+ return fmt.Errorf("failed to append transaction: %w", err)
+ }
+ return nil
+}