1
0
Fork 0

[golang] finished implementing procument contracts

This commit is contained in:
Julien Dessaux 2025-02-21 00:58:45 +01:00
parent 40c4a8df15
commit e887213aff
Signed by: adyxax
GPG key ID: F92E51B86E07177E
15 changed files with 252 additions and 56 deletions

View file

@ -30,6 +30,9 @@ func (c *Client) Accept(contract *model.Contract) error {
}
func (c *Client) Deliver(contract *model.Contract, ship *model.Ship) error {
if err := c.Dock(ship); err != nil {
return fmt.Errorf("failed to dock: %w", err)
}
deliver := contract.Terms.Deliver[0]
var units int
for _, cargoItem := range ship.Cargo.Inventory {

View file

@ -9,7 +9,7 @@ import (
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
)
func (c *Client) dock(s *model.Ship) error {
func (c *Client) Dock(s *model.Ship) error {
if s.Nav.Status == "DOCKED" {
return nil
}
@ -41,8 +41,6 @@ func (c *Client) Navigate(s *model.Ship, waypointSymbol string) error {
if err := c.orbit(s); err != nil {
return fmt.Errorf("failed to orbit: %w", err)
}
// TODO shortest path
// TODO go refuel if necessary
uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "navigate")}
type navigateRequest struct {
WaypointSymbol string `json:"waypointSymbol"`
@ -96,7 +94,7 @@ func (c *Client) orbit(s *model.Ship) error {
}
func (c *Client) Purchase(s *model.Ship, cargoItem string, units int) error {
if err := c.dock(s); err != nil {
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")}
@ -123,11 +121,11 @@ func (c *Client) Purchase(s *model.Ship, cargoItem string, units int) error {
return nil
}
func (c *Client) refuel(s *model.Ship, db *database.DB) error {
func (c *Client) Refuel(s *model.Ship) error {
if s.Fuel.Current == s.Fuel.Capacity {
return nil
}
if err := c.dock(s); err != nil {
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, "refuel")}
@ -151,7 +149,7 @@ func (c *Client) refuel(s *model.Ship, db *database.DB) error {
}
func (c *Client) Sell(s *model.Ship, cargoItem string, units int) error {
if err := c.dock(s); err != nil {
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")}

View file

@ -8,10 +8,12 @@ import (
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
)
func (c *Client) GetMarket(waypointSymbol string) (*model.Market, error) {
if market, err := c.db.LoadMarket(waypointSymbol); err == nil && market != nil {
// TODO check last updated time
return market, nil
func (c *Client) GetMarket(waypointSymbol string, ships []model.Ship) (*model.Market, error) {
if !isThereAShipDockerAtWaypoint(waypointSymbol, ships) {
if market, err := c.db.LoadMarket(waypointSymbol); err == nil && market != nil {
// TODO check last updated time
return market, nil
}
}
systemSymbol := WaypointSymbolToSystemSymbol(waypointSymbol)
uriRef := url.URL{Path: path.Join("systems", systemSymbol, "waypoints", waypointSymbol, "market")}
@ -25,11 +27,12 @@ func (c *Client) GetMarket(waypointSymbol string) (*model.Market, error) {
return &market, nil
}
func (c *Client) GetShipyard(waypointSymbol string) (*model.Shipyard, error) {
if shipyard, err := c.db.LoadShipyard(waypointSymbol); err == nil && shipyard != nil &&
(shipyard.Ships != nil) { // TODO || !IsThereAShipAtWaypoint(waypoint)) {
// TODO check last updated time
return shipyard, nil
func (c *Client) GetShipyard(waypointSymbol string, ships []model.Ship) (*model.Shipyard, error) {
if !isThereAShipDockerAtWaypoint(waypointSymbol, ships) {
if shipyard, err := c.db.LoadShipyard(waypointSymbol); err == nil && shipyard != nil {
// TODO check last updated time
return shipyard, nil
}
}
systemSymbol := WaypointSymbolToSystemSymbol(waypointSymbol)
uriRef := url.URL{Path: path.Join("systems", systemSymbol, "waypoints", waypointSymbol, "shipyard")}
@ -76,7 +79,7 @@ func (c *Client) GetWaypoint(waypointSymbol string) (*model.Waypoint, error) {
}
func (c *Client) ListWaypointsInSystem(systemSymbol string) ([]model.Waypoint, error) {
if waypoints, err := c.db.LoadWaypointsInSystem(systemSymbol); err == nil && waypoints != nil {
if waypoints, err := c.db.LoadWaypointsInSystem(systemSymbol); err == nil && len(waypoints) > 0 {
// TODO check last updated time
return waypoints, nil
}

View file

@ -1,6 +1,19 @@
package api
import "strings"
import (
"strings"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
)
func isThereAShipDockerAtWaypoint(symbol string, ships []model.Ship) bool {
for _, ship := range ships {
if ship.Nav.WaypointSymbol == symbol && ship.Nav.Status == "DOCKED" {
return true
}
}
return false
}
func WaypointSymbolToSystemSymbol(symbol string) string {
return strings.Join(strings.Split(symbol, "-")[:2], "-")