summaryrefslogtreecommitdiff
path: root/golang
diff options
context:
space:
mode:
authorJulien Dessaux2024-05-30 08:05:03 +0200
committerJulien Dessaux2024-05-30 08:05:03 +0200
commit4069b524b2c607dcf8fc1e378ae86077f8a89234 (patch)
tree3e42383cafb4a7f24252d4bc55b2481c3f9b014d /golang
parent[golang] added ships first api calls (diff)
downloadspacetraders-4069b524b2c607dcf8fc1e378ae86077f8a89234.tar.gz
spacetraders-4069b524b2c607dcf8fc1e378ae86077f8a89234.tar.bz2
spacetraders-4069b524b2c607dcf8fc1e378ae86077f8a89234.zip
[golang] simplified the api design some more
Diffstat (limited to 'golang')
-rw-r--r--golang/cmd/spacetraders/main.go4
-rw-r--r--golang/pkg/api/agents.go25
-rw-r--r--golang/pkg/api/api.go21
-rw-r--r--golang/pkg/api/register.go27
-rw-r--r--golang/pkg/api/ships.go46
-rw-r--r--golang/pkg/model/agent.go10
-rw-r--r--golang/pkg/model/register.go9
7 files changed, 56 insertions, 86 deletions
diff --git a/golang/cmd/spacetraders/main.go b/golang/cmd/spacetraders/main.go
index 42d43a6..75a9125 100644
--- a/golang/cmd/spacetraders/main.go
+++ b/golang/cmd/spacetraders/main.go
@@ -84,9 +84,5 @@ func run(
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
}
diff --git a/golang/pkg/api/agents.go b/golang/pkg/api/agents.go
index db5b7db..a2d5b6a 100644
--- a/golang/pkg/api/agents.go
+++ b/golang/pkg/api/agents.go
@@ -1,32 +1,17 @@
package api
import (
- "encoding/json"
- "fmt"
"net/url"
-)
-type AgentMessage struct {
- AccountID string `json:"accountId"`
- Credits int `json:"credits"`
- Headquarters string `json:"headquarters"`
- ShipCount int `json:"shipCount"`
- StartingFaction string `json:"startingFaction"`
- Symbol string `json:"symbol"`
-}
+ "git.adyxax.org/adyxax/spacetraders/v2/pkg/model"
+)
-func (c *Client) MyAgent() (*AgentMessage, error) {
+func (c *Client) MyAgent() (*model.Agent, error) {
uriRef := url.URL{Path: "my/agent"}
- msg, err := c.Send("GET", &uriRef, nil)
+ var response model.Agent
+ err := c.Send("GET", &uriRef, nil, &response)
if err != nil {
return nil, err
}
- if msg.Error != nil {
- return nil, msg.Error
- }
- var response AgentMessage
- if err := json.Unmarshal(msg.Data, &response); err != nil {
- return nil, fmt.Errorf("failed to unmarshal agent data: %w", err)
- }
return &response, nil
}
diff --git a/golang/pkg/api/api.go b/golang/pkg/api/api.go
index 7aee2fa..472d9e5 100644
--- a/golang/pkg/api/api.go
+++ b/golang/pkg/api/api.go
@@ -43,7 +43,7 @@ type Response struct {
Err error
}
-func (c *Client) Send(method string, uriRef *url.URL, payload any) (*APIMessage, error) {
+func (c *Client) Send(method string, uriRef *url.URL, payload any, response any) error {
responseChannel := make(chan *Response)
c.requestsChannel <- &Request{
method: method,
@@ -53,7 +53,24 @@ func (c *Client) Send(method string, uriRef *url.URL, payload any) (*APIMessage,
uri: c.baseURI.ResolveReference(uriRef),
}
res := <-responseChannel
- return res.Message, res.Err
+ if res.Err != nil {
+ return res.Err
+ }
+ err := res.Message.Error
+ if err != nil {
+ switch err.Code {
+ case 4214:
+ e := decodeShipInTransitError(err.Data)
+ time.Sleep(e.SecondsToArrival.Duration() * time.Second)
+ return c.Send(method, uriRef, payload, response)
+ default:
+ return err
+ }
+ }
+ if err := json.Unmarshal(res.Message.Data, &response); err != nil {
+ return fmt.Errorf("failed to unmarshal message: %w", err)
+ }
+ return nil
}
func queueProcessor(client *Client) {
diff --git a/golang/pkg/api/register.go b/golang/pkg/api/register.go
index 4f45cd1..d6c185c 100644
--- a/golang/pkg/api/register.go
+++ b/golang/pkg/api/register.go
@@ -1,38 +1,25 @@
package api
import (
- "encoding/json"
- "fmt"
"net/url"
-)
-type RegisterMessage struct {
- //agent
- //contract
- //faction
- //ship
- Token string `json:"token"`
-}
+ "git.adyxax.org/adyxax/spacetraders/v2/pkg/model"
+)
-func (c *Client) Register(faction, symbol string) (*RegisterMessage, error) {
+func (c *Client) Register(faction, symbol string) (*model.Register, error) {
type RegisterRequest struct {
Faction string `json:"faction"`
Symbol string `json:"symbol"`
}
uriRef := url.URL{Path: "register"}
- msg, err := c.Send("POST", &uriRef, RegisterRequest{
+ payload := RegisterRequest{
Faction: faction,
Symbol: symbol,
- })
+ }
+ var response model.Register
+ err := c.Send("POST", &uriRef, payload, &response)
if err != nil {
return nil, err
}
- if msg.Error != nil {
- return nil, msg.Error
- }
- var response RegisterMessage
- if err := json.Unmarshal(msg.Data, &response); err != nil {
- return nil, fmt.Errorf("failed to unmarshal register data: %w", err)
- }
return &response, nil
}
diff --git a/golang/pkg/api/ships.go b/golang/pkg/api/ships.go
index afcf530..dc6a27c 100644
--- a/golang/pkg/api/ships.go
+++ b/golang/pkg/api/ships.go
@@ -1,11 +1,9 @@
package api
import (
- "encoding/json"
"fmt"
"net/url"
"path"
- "time"
"git.adyxax.org/adyxax/spacetraders/v2/pkg/model"
)
@@ -18,41 +16,22 @@ func (c *Client) Dock(s *model.Ship) error {
Nav model.Nav `json:"nav"`
}
uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "dock")}
- msg, err := c.Send("POST", &uriRef, nil)
+ 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)
}
- 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)
+ var response []model.Ship
+ err := c.Send("GET", &uriRef, nil, &response)
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
}
@@ -64,24 +43,11 @@ func (c *Client) Orbit(s *model.Ship) error {
Nav model.Nav `json:"nav"`
}
uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "orbit")}
- msg, err := c.Send("POST", &uriRef, nil)
+ 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)
}
- 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
}
diff --git a/golang/pkg/model/agent.go b/golang/pkg/model/agent.go
new file mode 100644
index 0000000..1c2b80b
--- /dev/null
+++ b/golang/pkg/model/agent.go
@@ -0,0 +1,10 @@
+package model
+
+type Agent struct {
+ AccountID string `json:"accountId"`
+ Credits int `json:"credits"`
+ Headquarters string `json:"headquarters"`
+ ShipCount int `json:"shipCount"`
+ StartingFaction string `json:"startingFaction"`
+ Symbol string `json:"symbol"`
+}
diff --git a/golang/pkg/model/register.go b/golang/pkg/model/register.go
new file mode 100644
index 0000000..cf48b8b
--- /dev/null
+++ b/golang/pkg/model/register.go
@@ -0,0 +1,9 @@
+package model
+
+type Register struct {
+ Agent Agent `json:"agent"`
+ //contract
+ //faction
+ Ship Ship `json:"ship"`
+ Token string `json:"token"`
+}