1
0
Fork 0

[golang] refactor ships api

This commit is contained in:
Julien Dessaux 2025-02-12 23:43:52 +01:00
parent d16dd4f38e
commit 23dd8f4a27
Signed by: adyxax
GPG key ID: F92E51B86E07177E
3 changed files with 40 additions and 32 deletions

View file

@ -32,14 +32,14 @@ func main() {
"./spacetraders.db?_txlock=immediate",
)
if err != nil {
fmt.Fprintf(os.Stderr, "DbInit error %+v\n", err)
fmt.Fprintf(os.Stderr, "failed to open database: %+v\n", err)
os.Exit(1)
}
client := api.NewClient(ctx)
defer client.Close()
apiClient := api.NewClient(ctx)
defer apiClient.Close()
if err := run(
client,
apiClient,
db,
); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
@ -51,11 +51,11 @@ func main() {
}
func run(
client *api.Client,
apiClient *api.Client,
db *database.DB,
) error {
// ----- Get token or register ---------------------------------------------
register, err := client.Register("COSMIC", "ADYXAX-GO")
register, err := apiClient.Register("COSMIC", "ADYXAX-GO")
if err != nil {
apiError := &api.APIError{}
if errors.As(err, &apiError) {
@ -65,8 +65,8 @@ func run(
if err != nil || token == "" {
return fmt.Errorf("failed to register and failed to get a token from the database: someone stole our agent's callsign: %w", err)
}
client.SetToken(token)
agent, err := client.MyAgent()
apiClient.SetToken(token)
agent, err := apiClient.MyAgent()
if err != nil {
return fmt.Errorf("failed to get agent: %w", err)
}
@ -83,18 +83,28 @@ func run(
if err := db.AddToken(register.Token); err != nil {
return fmt.Errorf("failed to save token: %w", err)
}
client.SetToken(register.Token)
apiClient.SetToken(register.Token)
} else {
// We successfully registered but have a tainted database
slog.Error("token", "token", register.Token)
return fmt.Errorf("TODO server reset not implemented yet")
}
}
// ----- Get ships ---------------------------------------------------------
ships, err := client.MyShips()
err = client.Dock(&ships[0])
slog.Info("dock", "ship", ships[0].Nav.Status, "err", err)
err = client.Orbit(&ships[0])
// ----- run agent ---------------------------------------------------------
ships, err := apiClient.MyShips()
if err != nil {
return err
}
slog.Info("start", "ship", ships[0].Nav.Status, "err", err)
err = apiClient.Orbit(&ships[0])
if err != nil {
return err
}
slog.Info("orbit", "ship", ships[0].Nav.Status, "err", err)
err = apiClient.Dock(&ships[0])
if err != nil {
return err
}
slog.Info("dock", "ship", ships[0].Nav.Status, "err", err)
return nil
}

View file

@ -5,6 +5,7 @@ import (
"net/url"
"path"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/agent"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
)
@ -12,13 +13,12 @@ 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")}
type DockResponse struct {
Nav *model.Nav `json:"nav"`
}
var response DockResponse
err := c.Send("POST", &uriRef, nil, &response)
if err != nil {
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
return fmt.Errorf("failed to dock ship %s: %w", s.Symbol, err)
}
s.Nav = response.Nav
@ -27,25 +27,23 @@ func (c *Client) Dock(s *model.Ship) error {
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 {
var ships []model.Ship
if err := c.Send("GET", &uriRef, nil, &ships); err != nil {
return nil, fmt.Errorf("failed to get ships: %w", err)
}
return response, nil
return ships, 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")}
type OrbitResponse struct {
Nav *model.Nav `json:"nav"`
}
var response OrbitResponse
err := c.Send("POST", &uriRef, nil, &response)
if err != nil {
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
return fmt.Errorf("failed to orbit ship %s: %w", s.Symbol, err)
}
s.Nav = response.Nav

View file

@ -1,15 +1,15 @@
package model
type Ship struct {
Cargo Cargo `json:"cargo"`
Cooldown Cooldown `json:"cooldown"`
Cargo *Cargo `json:"cargo"`
Cooldown *Cooldown `json:"cooldown"`
//// crew
//// engine
//// frame
Fuel Fuel `json:"fuel"`
Fuel *Fuel `json:"fuel"`
//// modules
//// mounts
Nav Nav `json:"nav"`
Nav *Nav `json:"nav"`
//// reactor
//registration: Registration;
Symbol string `json:"symbol"`