diff options
author | Julien Dessaux | 2025-02-14 00:14:15 +0100 |
---|---|---|
committer | Julien Dessaux | 2025-02-14 00:14:15 +0100 |
commit | d97985a694b218713ddf63ed684b6a509f931f3b (patch) | |
tree | 84609f4e242419bf89301e0ead4927f450d7bfe5 /golang/pkg/api | |
parent | [golang] Bootstrap contracting and refactor the agent code (diff) | |
download | spacetraders-d97985a694b218713ddf63ed684b6a509f931f3b.tar.gz spacetraders-d97985a694b218713ddf63ed684b6a509f931f3b.tar.bz2 spacetraders-d97985a694b218713ddf63ed684b6a509f931f3b.zip |
[golang] implement automation loop and add contract accepting
Diffstat (limited to 'golang/pkg/api')
-rw-r--r-- | golang/pkg/api/contracts.go | 23 | ||||
-rw-r--r-- | golang/pkg/api/ships.go | 12 |
2 files changed, 29 insertions, 6 deletions
diff --git a/golang/pkg/api/contracts.go b/golang/pkg/api/contracts.go index f82ee6d..9478d4f 100644 --- a/golang/pkg/api/contracts.go +++ b/golang/pkg/api/contracts.go @@ -3,10 +3,33 @@ package api import ( "fmt" "net/url" + "path" + "git.adyxax.org/adyxax/spacetraders/golang/pkg/database" "git.adyxax.org/adyxax/spacetraders/golang/pkg/model" ) +func (c *Client) Accept(contract *model.Contract, db *database.DB) error { + if contract.Accepted { + return nil + } + uriRef := url.URL{Path: path.Join("my/contracts", contract.Id, "accept")} + type acceptResponse struct { + Agent *model.Agent `json:"agent"` + Contract *model.Contract `json:"contract"` + } + 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) + } + if err := db.SaveAgent(response.Agent); err != nil { + return fmt.Errorf("failed to accept contract %s: %w", contract.Id, err) + } + contract.Accepted = response.Contract.Accepted + contract.Terms = response.Contract.Terms + return nil +} + func (c *Client) MyContracts() ([]model.Contract, error) { uriRef := url.URL{Path: "my/contracts"} var contracts []model.Contract diff --git a/golang/pkg/api/ships.go b/golang/pkg/api/ships.go index 485f437..93963c3 100644 --- a/golang/pkg/api/ships.go +++ b/golang/pkg/api/ships.go @@ -14,10 +14,10 @@ func (c *Client) dock(s *model.Ship) error { return nil } uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "dock")} - type DockResponse struct { + type dockResponse struct { Nav *model.Nav `json:"nav"` } - var response DockResponse + var response dockResponse if err := c.Send("POST", &uriRef, nil, &response); err != nil { return fmt.Errorf("failed to dock ship %s: %w", s.Symbol, err) } @@ -39,10 +39,10 @@ func (c *Client) orbit(s *model.Ship) error { return nil } uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "orbit")} - type OrbitResponse struct { + type orbitResponse struct { Nav *model.Nav `json:"nav"` } - var response OrbitResponse + var response orbitResponse if err := c.Send("POST", &uriRef, nil, &response); err != nil { return fmt.Errorf("failed to orbit ship %s: %w", s.Symbol, err) } @@ -58,12 +58,12 @@ func (c *Client) Refuel(s *model.Ship, db *database.DB) error { return fmt.Errorf("failed to refuel ship %s: %w", s.Symbol, err) } uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "refuel")} - type RefuelResponse struct { + type refuelResponse struct { Agent *model.Agent `json:"agent"` Fuel *model.Fuel `json:"fuel"` Transaction *model.Transaction `json:"transaction"` } - var response RefuelResponse + var response refuelResponse if err := c.Send("POST", &uriRef, nil, &response); err != nil { return fmt.Errorf("failed to refuel ship %s: %w", s.Symbol, err) } |