summaryrefslogtreecommitdiff
path: root/golang/pkg/api
diff options
context:
space:
mode:
authorJulien Dessaux2024-05-29 23:08:24 +0200
committerJulien Dessaux2024-05-29 23:08:24 +0200
commit860984057d496999949f7b5680997a25b6c660c3 (patch)
tree32653e3f8e89464167d2ae3918a59a40364e020d /golang/pkg/api
parent[golang] fixed golang api client design mistakes (diff)
downloadspacetraders-860984057d496999949f7b5680997a25b6c660c3.tar.gz
spacetraders-860984057d496999949f7b5680997a25b6c660c3.tar.bz2
spacetraders-860984057d496999949f7b5680997a25b6c660c3.zip
[golang] added ships first api calls
Diffstat (limited to 'golang/pkg/api')
-rw-r--r--golang/pkg/api/ships.go87
1 files changed, 87 insertions, 0 deletions
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
+}