summaryrefslogtreecommitdiff
path: root/golang/pkg/api/ships.go
blob: dc6a27c305ad20843705506f03ac5e976a8ae47e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package api

import (
	"fmt"
	"net/url"
	"path"

	"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")}
	var response DockResponse
	err := c.Send("POST", &uriRef, nil, &response)
	if err != nil {
		return fmt.Errorf("failed to dock ship %s: %w", s.Symbol, err)
	}
	s.Nav = response.Nav
	return nil
}

func (c *Client) MyShips() ([]model.Ship, error) {
	uriRef := url.URL{Path: "my/ships"}
	var response []model.Ship
	err := c.Send("GET", &uriRef, nil, &response)
	if err != nil {
		return nil, fmt.Errorf("failed to get ships: %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")}
	var response OrbitResponse
	err := c.Send("POST", &uriRef, nil, &response)
	if err != nil {
		return fmt.Errorf("failed to orbit ship %s: %w", s.Symbol, err)
	}
	s.Nav = response.Nav
	return nil
}