1
0
Fork 0

[golang] implement trading and contracting

This commit is contained in:
Julien Dessaux 2025-02-19 00:19:58 +01:00
parent a692a38d28
commit 312ef2eb57
Signed by: adyxax
GPG key ID: F92E51B86E07177E
12 changed files with 422 additions and 39 deletions

View file

@ -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

View file

@ -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
}

View file

@ -9,28 +9,31 @@ import (
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
)
func (c *Client) GetSystem(symbol string, db *database.DB) (*model.System, error) {
if system, err := db.LoadSystem(symbol); err == nil && system != nil {
return system, nil
func (c *Client) GetMarket(waypointSymbol string, db *database.DB) (*model.Market, error) {
if market, err := db.LoadMarket(waypointSymbol); err == nil && market != nil {
// TODO check last updated time
return market, nil
}
uriRef := url.URL{Path: path.Join("systems", symbol)}
var system model.System
if err := c.Send("GET", &uriRef, nil, &system); err != nil {
systemSymbol := WaypointSymbolToSystemSymbol(waypointSymbol)
uriRef := url.URL{Path: path.Join("systems", systemSymbol, "waypoints", waypointSymbol, "market")}
var market model.Market
if err := c.Send("GET", &uriRef, nil, &market); err != nil {
return nil, fmt.Errorf("failed API request: %w", err)
}
if err := db.SaveSystem(&system); err != nil {
return nil, fmt.Errorf("failed to save system %s: %w", system.Symbol, err)
if err := db.SaveMarket(&market); err != nil {
return nil, fmt.Errorf("failed to save market %s: %w", market.Symbol, err)
}
return &system, nil
return &market, nil
}
func (c *Client) GetShipyard(waypoint *model.Waypoint, db *database.DB) (*model.Shipyard, error) {
if shipyard, err := db.LoadShipyard(waypoint.Symbol); err == nil && shipyard != nil &&
func (c *Client) GetShipyard(waypointSymbol string, db *database.DB) (*model.Shipyard, error) {
if shipyard, err := db.LoadShipyard(waypointSymbol); err == nil && shipyard != nil &&
(shipyard.Ships != nil) { // TODO || !IsThereAShipAtWaypoint(waypoint)) {
// TODO check last updated time
return shipyard, nil
}
uriRef := url.URL{Path: path.Join("systems", waypoint.SystemSymbol, "waypoints", waypoint.Symbol, "shipyard")}
systemSymbol := WaypointSymbolToSystemSymbol(waypointSymbol)
uriRef := url.URL{Path: path.Join("systems", systemSymbol, "waypoints", waypointSymbol, "shipyard")}
var shipyard model.Shipyard
if err := c.Send("GET", &uriRef, nil, &shipyard); err != nil {
return nil, fmt.Errorf("failed API request: %w", err)
@ -41,13 +44,28 @@ func (c *Client) GetShipyard(waypoint *model.Waypoint, db *database.DB) (*model.
return &shipyard, nil
}
func (c *Client) GetWaypoint(symbol string, db *database.DB) (*model.Waypoint, error) {
if waypoint, err := db.LoadWaypoint(symbol); err == nil && waypoint != nil {
func (c *Client) GetSystem(systemSymbol string, db *database.DB) (*model.System, error) {
if system, err := db.LoadSystem(systemSymbol); err == nil && system != nil {
return system, nil
}
uriRef := url.URL{Path: path.Join("systems", systemSymbol)}
var system model.System
if err := c.Send("GET", &uriRef, nil, &system); err != nil {
return nil, fmt.Errorf("failed API request: %w", err)
}
if err := db.SaveSystem(&system); err != nil {
return nil, fmt.Errorf("failed to save system %s: %w", system.Symbol, err)
}
return &system, nil
}
func (c *Client) GetWaypoint(waypointSymbol string, db *database.DB) (*model.Waypoint, error) {
if waypoint, err := db.LoadWaypoint(waypointSymbol); err == nil && waypoint != nil {
// TODO check last updated time
return waypoint, nil
}
systemSymbol := WaypointSymbolToSystemSymbol(symbol)
uriRef := url.URL{Path: path.Join("systems", systemSymbol, "waypoints", symbol)}
systemSymbol := WaypointSymbolToSystemSymbol(waypointSymbol)
uriRef := url.URL{Path: path.Join("systems", systemSymbol, "waypoints", waypointSymbol)}
var waypoint model.Waypoint
if err := c.Send("GET", &uriRef, nil, &waypoint); err != nil {
return nil, fmt.Errorf("failed API request: %w", err)