summaryrefslogtreecommitdiff
path: root/golang/pkg/api/contracts.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/contracts.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/contracts.go')
-rw-r--r--golang/pkg/api/contracts.go53
1 files changed, 51 insertions, 2 deletions
diff --git a/golang/pkg/api/contracts.go b/golang/pkg/api/contracts.go
index 573d938..6a9a28a 100644
--- a/golang/pkg/api/contracts.go
+++ b/golang/pkg/api/contracts.go
@@ -20,16 +20,65 @@ func (c *Client) Accept(contract *model.Contract, db *database.DB) error {
}
var response acceptResponse
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
- return fmt.Errorf("failed to accept contract %s: %w", contract.Id, err)
+ return fmt.Errorf("failed API request: %w", err)
}
if err := db.SaveAgent(response.Agent); err != nil {
- return fmt.Errorf("failed to accept contract %s: %w", contract.Id, err)
+ return fmt.Errorf("failed to save agent: %w", err)
}
contract.Accepted = response.Contract.Accepted
contract.Terms = response.Contract.Terms
return nil
}
+func (c *Client) Deliver(contract *model.Contract, ship *model.Ship, db *database.DB) error {
+ deliver := contract.Terms.Deliver[0]
+ var units int
+ for _, cargoItem := range ship.Cargo.Inventory {
+ if cargoItem.Symbol == deliver.TradeSymbol {
+ units = min(deliver.UnitsRequired-deliver.UnitsFulfilled, cargoItem.Units)
+ break
+ }
+ }
+ uriRef := url.URL{Path: path.Join("my/contracts", contract.Id, "deliver")}
+ type deliverRequest struct {
+ ShipSymbol string `json:"shipSymbol"`
+ TradeSymbol string `json:"tradeSymbol"`
+ Units int `json:"units"`
+ }
+ type deliverResponse struct {
+ Cargo *model.Cargo `json:"cargo"`
+ Contract *model.Contract `json:"contract"`
+ }
+ var response deliverResponse
+ if err := c.Send("POST", &uriRef, deliverRequest{ship.Symbol, deliver.TradeSymbol, units}, &response); err != nil {
+ return fmt.Errorf("failed API request: %w", err)
+ }
+ ship.Cargo = response.Cargo
+ contract.Terms = response.Contract.Terms
+ return nil
+}
+
+func (c *Client) Fulfill(contract *model.Contract, db *database.DB) error {
+ if contract.Fulfilled {
+ return nil
+ }
+ uriRef := url.URL{Path: path.Join("my/contracts", contract.Id, "fulfill")}
+ type fulfillResponse struct {
+ Agent *model.Agent `json:"agent"`
+ Contract *model.Contract `json:"contract"`
+ }
+ var response fulfillResponse
+ if err := c.Send("POST", &uriRef, nil, &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)
+ }
+ contract.Fulfilled = response.Contract.Fulfilled
+ contract.Terms = response.Contract.Terms
+ return nil
+}
+
func (c *Client) MyContracts() ([]model.Contract, error) {
uriRef := url.URL{Path: "my/contracts"}
var contracts []model.Contract