summaryrefslogtreecommitdiff
path: root/golang
diff options
context:
space:
mode:
Diffstat (limited to 'golang')
-rw-r--r--golang/cmd/spacetraders/main.go6
-rw-r--r--golang/pkg/api/ships.go87
-rw-r--r--golang/pkg/model/cargo.go7
-rw-r--r--golang/pkg/model/cooldown.go7
-rw-r--r--golang/pkg/model/fuel.go7
-rw-r--r--golang/pkg/model/inventory.go8
-rw-r--r--golang/pkg/model/nav.go9
-rw-r--r--golang/pkg/model/route.go17
-rw-r--r--golang/pkg/model/ship.go16
9 files changed, 164 insertions, 0 deletions
diff --git a/golang/cmd/spacetraders/main.go b/golang/cmd/spacetraders/main.go
index 5a4dc4a..42d43a6 100644
--- a/golang/cmd/spacetraders/main.go
+++ b/golang/cmd/spacetraders/main.go
@@ -82,5 +82,11 @@ func run(
// ----- Update agent ------------------------------------------------------
agent, err := client.MyAgent()
slog.Info("agent", "agent", agent, "err", err)
+ // ----- Get ships ---------------------------------------------------------
+ ships, err := client.MyShips()
+ err = client.Dock(&ships[0])
+ slog.Info("dock", "ship", ships[0], "err", err)
+ err = client.Orbit(&ships[0])
+ slog.Info("orbit", "ship", ships[0], "err", err)
return nil
}
diff --git a/golang/pkg/api/ships.go b/golang/pkg/api/ships.go
new file mode 100644
index 0000000..afcf530
--- /dev/null
+++ b/golang/pkg/api/ships.go
@@ -0,0 +1,87 @@
+package api
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/url"
+ "path"
+ "time"
+
+ "git.adyxax.org/adyxax/spacetraders/v2/pkg/model"
+)
+
+func (c *Client) Dock(s *model.Ship) error {
+ if s.Nav.Status == "DOCKED" {
+ return nil
+ }
+ type DockResponse struct {
+ Nav model.Nav `json:"nav"`
+ }
+ uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "dock")}
+ msg, err := c.Send("POST", &uriRef, nil)
+ if err != nil {
+ return fmt.Errorf("failed to dock ship %s: %w", s.Symbol, err)
+ }
+ if msg.Error != nil {
+ switch msg.Error.Code {
+ case 4214:
+ e := decodeShipInTransitError(msg.Error.Data)
+ time.Sleep(e.SecondsToArrival.Duration() * time.Second)
+ return c.Dock(s)
+ default:
+ return msg.Error
+ }
+ }
+ var response DockResponse
+ if err := json.Unmarshal(msg.Data, &response); err != nil {
+ return fmt.Errorf("failed to unmarshal dock data: %w", err)
+ }
+ s.Nav = response.Nav
+ return nil
+}
+
+func (c *Client) MyShips() ([]model.Ship, error) {
+ uriRef := url.URL{Path: "my/ships"}
+ msg, err := c.Send("GET", &uriRef, nil)
+ if err != nil {
+ return nil, fmt.Errorf("failed to get ships: %w", err)
+ }
+ if msg.Error != nil {
+ return nil, fmt.Errorf("failed to get ships: %w", msg.Error)
+ }
+ var response []model.Ship
+ if err := json.Unmarshal(msg.Data, &response); err != nil {
+ return nil, fmt.Errorf("failed to unmarshal ships data: %w", err)
+ }
+ return response, nil
+}
+
+func (c *Client) Orbit(s *model.Ship) error {
+ if s.Nav.Status == "IN_ORBIT" {
+ return nil
+ }
+ type OrbitResponse struct {
+ Nav model.Nav `json:"nav"`
+ }
+ uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "orbit")}
+ msg, err := c.Send("POST", &uriRef, nil)
+ if err != nil {
+ return fmt.Errorf("failed to orbit ship %s: %w", s.Symbol, err)
+ }
+ if msg.Error != nil {
+ switch msg.Error.Code {
+ case 4214:
+ e := decodeShipInTransitError(msg.Error.Data)
+ time.Sleep(e.SecondsToArrival.Duration() * time.Second)
+ return c.Orbit(s)
+ default:
+ return msg.Error
+ }
+ }
+ var response OrbitResponse
+ if err := json.Unmarshal(msg.Data, &response); err != nil {
+ return fmt.Errorf("failed to unmarshal orbit data: %w", err)
+ }
+ s.Nav = response.Nav
+ return nil
+}
diff --git a/golang/pkg/model/cargo.go b/golang/pkg/model/cargo.go
new file mode 100644
index 0000000..bf58204
--- /dev/null
+++ b/golang/pkg/model/cargo.go
@@ -0,0 +1,7 @@
+package model
+
+type Cargo struct {
+ Capacity int `json:"capacity"`
+ Inventory []Inventory `json:"inventory"`
+ Units int `json:"units"`
+}
diff --git a/golang/pkg/model/cooldown.go b/golang/pkg/model/cooldown.go
new file mode 100644
index 0000000..e092b4e
--- /dev/null
+++ b/golang/pkg/model/cooldown.go
@@ -0,0 +1,7 @@
+package model
+
+type Cooldown struct {
+ //ShipSymbol int `json:"shipSymbol"`
+ RemainingSeconds int `json:"remainingSeconds"`
+ //TotalSeconds int `json:"totalSeconds"`
+}
diff --git a/golang/pkg/model/fuel.go b/golang/pkg/model/fuel.go
new file mode 100644
index 0000000..395fbdb
--- /dev/null
+++ b/golang/pkg/model/fuel.go
@@ -0,0 +1,7 @@
+package model
+
+type Fuel struct {
+ Capacity int `json:"capacity"`
+ //Consummed
+ Current int `json:"current"`
+}
diff --git a/golang/pkg/model/inventory.go b/golang/pkg/model/inventory.go
new file mode 100644
index 0000000..d685250
--- /dev/null
+++ b/golang/pkg/model/inventory.go
@@ -0,0 +1,8 @@
+package model
+
+type Inventory struct {
+ //Description string `json:"description"`
+ Units int `json:"units"`
+ //Name string `json:"name"`
+ Symbol string `json:"symbol"`
+}
diff --git a/golang/pkg/model/nav.go b/golang/pkg/model/nav.go
new file mode 100644
index 0000000..f9bb930
--- /dev/null
+++ b/golang/pkg/model/nav.go
@@ -0,0 +1,9 @@
+package model
+
+type Nav struct {
+ FlightMode string `json:"flightMode"`
+ Route Route `json:"route"`
+ Status string `json:"status"`
+ SystemSymbol string `json:"systemSymbol"`
+ WaypointSymbol string `json:"waypointSymbol"`
+}
diff --git a/golang/pkg/model/route.go b/golang/pkg/model/route.go
new file mode 100644
index 0000000..0533e61
--- /dev/null
+++ b/golang/pkg/model/route.go
@@ -0,0 +1,17 @@
+package model
+
+import "time"
+
+type Route struct {
+ Arrival time.Time `json:"arrival"`
+ DepartureTime time.Time `json:"departureTime"`
+ Destination RouteEndpoint `json:"destination"`
+}
+
+type RouteEndpoint struct {
+ Type string `json:"type"`
+ Symbol string `json:"symbol"`
+ SystemSymbol string `json:"systemSymbol"`
+ X int `json:"x"`
+ Y int `json:"y"`
+}
diff --git a/golang/pkg/model/ship.go b/golang/pkg/model/ship.go
new file mode 100644
index 0000000..549f09e
--- /dev/null
+++ b/golang/pkg/model/ship.go
@@ -0,0 +1,16 @@
+package model
+
+type Ship struct {
+ Cargo Cargo `json:"cargo"`
+ Cooldown Cooldown `json:"cooldown"`
+ //// crew
+ //// engine
+ //// frame
+ Fuel Fuel `json:"fuel"`
+ //// modules
+ //// mounts
+ Nav Nav `json:"nav"`
+ //// reactor
+ //registration: Registration;
+ Symbol string `json:"symbol"`
+}