1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
package api
import (
"fmt"
"net/url"
"path"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
)
func (c *Client) Accept(contract *model.Contract) 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 API request: %w", err)
}
if err := c.db.SaveAgent(response.Agent); err != nil {
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) 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) 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 := c.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
if err := c.Send("GET", &uriRef, nil, &contracts); err != nil {
return nil, fmt.Errorf("failed API request: %w", err)
}
return contracts, nil
}
|