[golang] added ships first api calls
This commit is contained in:
parent
0d00bf9fd2
commit
860984057d
9 changed files with 164 additions and 0 deletions
|
@ -82,5 +82,11 @@ func run(
|
||||||
// ----- Update agent ------------------------------------------------------
|
// ----- Update agent ------------------------------------------------------
|
||||||
agent, err := client.MyAgent()
|
agent, err := client.MyAgent()
|
||||||
slog.Info("agent", "agent", agent, "err", err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
87
golang/pkg/api/ships.go
Normal file
87
golang/pkg/api/ships.go
Normal file
|
@ -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
|
||||||
|
}
|
7
golang/pkg/model/cargo.go
Normal file
7
golang/pkg/model/cargo.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package model
|
||||||
|
|
||||||
|
type Cargo struct {
|
||||||
|
Capacity int `json:"capacity"`
|
||||||
|
Inventory []Inventory `json:"inventory"`
|
||||||
|
Units int `json:"units"`
|
||||||
|
}
|
7
golang/pkg/model/cooldown.go
Normal file
7
golang/pkg/model/cooldown.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package model
|
||||||
|
|
||||||
|
type Cooldown struct {
|
||||||
|
//ShipSymbol int `json:"shipSymbol"`
|
||||||
|
RemainingSeconds int `json:"remainingSeconds"`
|
||||||
|
//TotalSeconds int `json:"totalSeconds"`
|
||||||
|
}
|
7
golang/pkg/model/fuel.go
Normal file
7
golang/pkg/model/fuel.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package model
|
||||||
|
|
||||||
|
type Fuel struct {
|
||||||
|
Capacity int `json:"capacity"`
|
||||||
|
//Consummed
|
||||||
|
Current int `json:"current"`
|
||||||
|
}
|
8
golang/pkg/model/inventory.go
Normal file
8
golang/pkg/model/inventory.go
Normal file
|
@ -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"`
|
||||||
|
}
|
9
golang/pkg/model/nav.go
Normal file
9
golang/pkg/model/nav.go
Normal file
|
@ -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"`
|
||||||
|
}
|
17
golang/pkg/model/route.go
Normal file
17
golang/pkg/model/route.go
Normal file
|
@ -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"`
|
||||||
|
}
|
16
golang/pkg/model/ship.go
Normal file
16
golang/pkg/model/ship.go
Normal file
|
@ -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"`
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue