[golang] implement trading and contracting
This commit is contained in:
parent
a692a38d28
commit
312ef2eb57
12 changed files with 422 additions and 39 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue