summaryrefslogtreecommitdiff
path: root/golang/pkg/api/ships.go
blob: c3b707491a9ff8e4121a6e6bc62d1dcd41a6564e (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package api

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

	"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
)

func (c *Client) dock(s *model.Ship) error {
	if s.Nav.Status == "DOCKED" {
		return nil
	}
	uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "dock")}
	type dockResponse struct {
		Nav *model.Nav `json:"nav"`
	}
	var response dockResponse
	if err := c.Send("POST", &uriRef, nil, &response); err != nil {
		return fmt.Errorf("failed API request: %w", err)
	}
	s.Nav = response.Nav
	return nil
}

func (c *Client) MyShips() ([]model.Ship, error) {
	uriRef := url.URL{Path: "my/ships"}
	var ships []model.Ship
	if err := c.Send("GET", &uriRef, nil, &ships); err != nil {
		return nil, fmt.Errorf("failed API request: %w", err)
	}
	return ships, nil
}

func (c *Client) Navigate(s *model.Ship, waypointSymbol string) error {
	if s.Nav.WaypointSymbol == waypointSymbol {
		return nil
	}
	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"`
	}
	type navigateResponse struct {
		//Events        []model.Event        `json:"events"`
		Fuel *model.Fuel `json:"fuel"`
		Nav  *model.Nav  `json:"nav"`
	}
	var response navigateResponse
	if err := c.Send("POST", &uriRef, navigateRequest{waypointSymbol}, &response); err != nil {
		return fmt.Errorf("failed API request: %w", err)
	}
	s.Fuel = response.Fuel
	s.Nav = response.Nav
	select {
	case <-c.ctx.Done():
		return fmt.Errorf("failed: context cancelled")
	case <-time.After(s.Nav.Route.Arrival.Sub(time.Now())):
	}
	s.Nav.Status = "IN_ORBIT"
	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
	}
	uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "orbit")}
	type orbitResponse struct {
		Nav *model.Nav `json:"nav"`
	}
	var response orbitResponse
	if err := c.Send("POST", &uriRef, nil, &response); err != nil {
		return fmt.Errorf("failed API request: %w", err)
	}
	s.Nav = response.Nav
	return nil
}

func (c *Client) Purchase(s *model.Ship, cargoItem string, units int) 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 := c.db.SaveAgent(response.Agent); err != nil {
		return fmt.Errorf("failed to save agent: %w", err)
	}
	s.Cargo = response.Cargo
	if err := c.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
	}
	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")}
	type refuelResponse struct {
		Agent       *model.Agent       `json:"agent"`
		Fuel        *model.Fuel        `json:"fuel"`
		Transaction *model.Transaction `json:"transaction"`
	}
	var response refuelResponse
	if err := c.Send("POST", &uriRef, nil, &response); err != nil {
		return fmt.Errorf("failed API request: %w", err)
	}
	if err := c.db.SaveAgent(response.Agent); err != nil {
		return fmt.Errorf("failed to save agent: %w", err)
	}
	s.Fuel = response.Fuel
	if err := c.db.AppendTransaction(response.Transaction); err != nil {
		return fmt.Errorf("failed to append transaction: %w", err)
	}
	return nil
}

func (c *Client) Sell(s *model.Ship, cargoItem string, units int) 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 := c.db.SaveAgent(response.Agent); err != nil {
		return fmt.Errorf("failed to save agent: %w", err)
	}
	s.Cargo = response.Cargo
	if err := c.db.AppendTransaction(response.Transaction); err != nil {
		return fmt.Errorf("failed to append transaction: %w", err)
	}
	return nil
}