1
0
Fork 0

[golang] implement shipyards visits

This commit is contained in:
Julien Dessaux 2025-02-17 00:40:43 +01:00
parent 3cae67aea4
commit bd2fb50c81
Signed by: adyxax
GPG key ID: F92E51B86E07177E
15 changed files with 299 additions and 45 deletions

View file

@ -6,6 +6,7 @@ import (
"log/slog"
"os"
"os/signal"
"syscall"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/agent"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/api"
@ -23,7 +24,7 @@ func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, opts))
slog.SetDefault(logger)
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
db, err := database.NewDB(

View file

@ -11,7 +11,7 @@ func (a *agent) autoContracting(ship *model.Ship) {
defer a.wg.Done()
contracts, err := a.client.MyContracts()
if err != nil {
a.sendShipError(fmt.Errorf("failed to get my contracts: %w", err), ship)
a.channel <- fmt.Errorf("failed to get my contracts with ship %s: %w", ship.Symbol, err)
return
}
for _, contract := range contracts {
@ -21,12 +21,12 @@ func (a *agent) autoContracting(ship *model.Ship) {
now := time.Now()
if now.Before(contract.Terms.Deadline) {
if err := a.runContract(&contract, ship); err != nil {
a.sendShipError(fmt.Errorf("failed to run contracts: %w", err), ship)
a.channel <- fmt.Errorf("failed to run contracts with ship %s: %w", ship.Symbol, err)
return
}
}
}
a.sendShipError(fmt.Errorf("failed to run contracts: negotiating new contracts is not implemented yet"), ship)
a.channel <- fmt.Errorf("failed to run contracts: negotiating new contracts is not implemented yet")
// TODO
//for {
// negotiate

View file

@ -1,15 +0,0 @@
package agent
import "git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
type shipError struct {
err error
ship *model.Ship
}
func (a *agent) sendShipError(err error, ship *model.Ship) {
a.channel <- shipError{
err: err,
ship: ship,
}
}

View file

@ -11,7 +11,7 @@ import (
)
type agent struct {
channel chan shipError
channel chan error
client *api.Client
db *database.DB
getenv func(string) string
@ -32,7 +32,7 @@ func Run(
getenv func(string) string,
) error {
agent := agent{
channel: make(chan shipError),
channel: make(chan error),
client: client,
db: db,
getenv: getenv,
@ -57,12 +57,11 @@ func Run(
state++
case visit_all_shipyards:
if err := agent.visitAllShipyards(&agent.ships[1]); err != nil {
agent.sendShipError(fmt.Errorf("agent runner returned an error on state %d: %w", state, err), &agent.ships[1])
agent.channel <- fmt.Errorf("agent runner returned an error on state %d: %w", state, err)
}
state++
return
default:
agent.sendShipError(fmt.Errorf("agent runner reach an unknown state: %d", state), nil)
agent.channel <- fmt.Errorf("agent runner reach an unknown state: %d", state)
return
}
}
@ -71,8 +70,8 @@ func Run(
errWg.Add(1)
go func() {
defer errWg.Done()
for shipErr := range agent.channel {
slog.Error("ship error", "err", shipErr.err, "ship", shipErr.ship.Symbol)
for err := range agent.channel {
slog.Error("error", "err", err)
}
}()
agent.wg.Wait()

67
golang/pkg/agent/utils.go Normal file
View file

@ -0,0 +1,67 @@
package agent
import (
"cmp"
"fmt"
"slices"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
)
type Point interface {
GetX() int
GetY() int
}
func distance2(a Point, b Point) int {
x2 := a.GetX() - b.GetX()
y2 := a.GetY() - b.GetY()
return x2*x2 + y2*y2
}
func (a *agent) isThereAShipAtWaypoint(waypoint *model.Waypoint) bool {
for _, ship := range a.ships {
if ship.Nav.WaypointSymbol == waypoint.Symbol {
return true
}
}
return false
}
func (a *agent) listWaypointsInSystemWithTrait(system *model.System, trait string) ([]model.Waypoint, error) {
waypoints, err := a.client.ListWaypointsInSystem(system, a.db)
if err != nil {
return nil, fmt.Errorf("failed to list waypoints with trait: %w", err)
}
waypoints = slices.DeleteFunc(waypoints, func(waypoint model.Waypoint) bool {
for _, t := range waypoint.Traits {
if t.Symbol == trait {
return false
}
}
return true
})
return waypoints, nil
}
func (a *agent) listShipyardsInSystem(system *model.System) ([]model.Shipyard, error) {
waypoints, err := a.listWaypointsInSystemWithTrait(system, "SHIPYARD")
if err != nil {
return nil, fmt.Errorf("failed to list shipyards in system: %w", err)
}
var shipyards []model.Shipyard
for i := range waypoints {
shipyard, err := a.client.GetShipyard(&waypoints[i], a.db)
if err != nil {
return nil, fmt.Errorf("failed to list shipyards in system: %w", err)
}
shipyards = append(shipyards, *shipyard)
}
return shipyards, nil
}
func sortByDistanceFrom[P Point](origin P, destinations []P) {
slices.SortFunc(destinations, func(a, b P) int {
return cmp.Compare(distance2(origin, a), distance2(origin, b))
})
}

View file

@ -2,7 +2,7 @@ package agent
import (
"fmt"
"log/slog"
"slices"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
)
@ -12,13 +12,44 @@ func (a *agent) visitAllShipyards(ship *model.Ship) error {
if err != nil {
return fmt.Errorf("failed to visit all shipyards: %w", err)
}
waypoints, err := a.client.ListWaypointsInSystem(system, a.db)
shipyards, err := a.listShipyardsInSystem(system)
if err != nil {
return fmt.Errorf("failed to visit all shipyards: %w", err)
}
//slog.Info("get system", "system", system.Waypoints, "err", err)
//waypoint, err := a.client.GetWaypoint("X1-RR14-J88", a.db)
slog.Info("get waypoint", "waypoint", waypoints[0])
return fmt.Errorf("failed to visit all shipyards: not implemented yet")
shipyards = slices.DeleteFunc(shipyards, func(shipyard model.Shipyard) bool {
// filter out shipyards for which we already have ships prices
if shipyard.Ships != nil {
return true
}
// filter out shipyards for which a ship is either present or inbound
waypoint, err := a.client.GetWaypoint(shipyard.Symbol, a.db)
if err != nil {
panic(fmt.Errorf("failed to visit all shipyards: %w", err))
}
return a.isThereAShipAtWaypoint(waypoint)
})
if len(shipyards) == 0 {
return nil
}
waypoint, err := a.client.GetWaypoint(ship.Nav.WaypointSymbol, a.db)
if err != nil {
return fmt.Errorf("failed to visit all shipyards: %w", err)
}
waypoints := make([]model.Waypoint, 0)
for i := range shipyards {
waypoint, err := a.client.GetWaypoint(shipyards[i].Symbol, a.db)
if err != nil {
return fmt.Errorf("failed to visit all shipyards: %w", err)
}
waypoints = append(waypoints, *waypoint)
}
sortByDistanceFrom(*waypoint, waypoints)
if err := a.client.Navigate(ship, &waypoints[0], a.db); err != nil {
return fmt.Errorf("failed to visit all shipyards: %w", err)
}
if _, err := a.client.GetShipyard(&waypoints[0], a.db); err != nil {
return fmt.Errorf("failed to visit all shipyards: %w", err)
}
// TODO get market data
return a.visitAllShipyards(ship)
}

View file

@ -72,14 +72,14 @@ func (c *Client) Send(method string, uriRef *url.URL, payload any, response any)
return res.Err
}
if err := res.Message.Error; err != nil {
return err
}
err := res.Message.Error
if err != nil {
switch err.Code {
case 4214:
e := decodeShipInTransitError(err.Data)
time.Sleep(e.SecondsToArrival.Duration() * time.Second)
select {
case <-c.ctx.Done():
return fmt.Errorf("failed to send: ctx cancelled")
case <-time.After(e.SecondsToArrival.Duration() * time.Second):
}
return c.Send(method, uriRef, payload, response)
default:
return err
@ -193,7 +193,11 @@ func (c *Client) sendOne(method string, uri *url.URL, payload any) (*APIMessage,
switch resp.StatusCode {
case 429:
e := decodeRateLimitError(msg.Error.Data)
time.Sleep(e.RetryAfter.Duration() * time.Second)
select {
case <-c.ctx.Done():
return nil, fmt.Errorf("failed to sendOne: ctx cancelled")
case <-time.After(e.RetryAfter.Duration() * time.Second):
}
return c.sendOne(method, uri, payload)
}
return &msg, nil

View file

@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"path"
"time"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/database"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
@ -34,6 +35,36 @@ func (c *Client) MyShips() ([]model.Ship, error) {
return ships, nil
}
func (c *Client) Navigate(s *model.Ship, w *model.Waypoint, db *database.DB) error {
// TODO shortest path
// TODO go refuel if necessary
if err := c.orbit(s); err != nil {
return fmt.Errorf("failed to navigate ship %s to %s: %w", s.Symbol, w.Symbol, err)
}
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{w.Symbol}, &response); err != nil {
return fmt.Errorf("failed to navigate ship %s to %s: %w", s.Symbol, w.Symbol, err)
}
s.Fuel = response.Fuel
s.Nav = response.Nav
select {
case <-c.ctx.Done():
return fmt.Errorf("failed to navigate ship %s to %s: ctx cancelled", s.Symbol, w.Symbol)
case <-time.After(s.Nav.Route.Arrival.Sub(time.Now())):
}
s.Nav.Status = "IN_ORBIT"
return nil
}
func (c *Client) orbit(s *model.Ship) error {
if s.Nav.Status == "IN_ORBIT" {
return nil
@ -50,7 +81,7 @@ func (c *Client) orbit(s *model.Ship) error {
return nil
}
func (c *Client) Refuel(s *model.Ship, db *database.DB) error {
func (c *Client) refuel(s *model.Ship, db *database.DB) error {
if s.Fuel.Current == s.Fuel.Capacity {
return nil
}

View file

@ -25,9 +25,10 @@ func (c *Client) GetSystem(symbol string, db *database.DB) (*model.System, error
}
func (c *Client) ListWaypointsInSystem(system *model.System, db *database.DB) ([]model.Waypoint, error) {
// TODO database caching
// TODO pagination
// TODO check updated
if waypoints, err := db.LoadWaypointsInSystem(system); err == nil && waypoints != nil {
// TODO check last updated time
return waypoints, nil
}
uriRef := url.URL{Path: path.Join("systems", system.Symbol, "waypoints")}
var waypoints []model.Waypoint
if err := c.Send("GET", &uriRef, nil, &waypoints); err != nil {
@ -41,9 +42,26 @@ func (c *Client) ListWaypointsInSystem(system *model.System, db *database.DB) ([
return waypoints, nil
}
func (c *Client) GetShipyard(waypoint *model.Waypoint, db *database.DB) (*model.Shipyard, error) {
if shipyard, err := db.LoadShipyard(waypoint.Symbol); err == nil && shipyard != nil &&
(shipyard.Ships != nil) { // TODO || !IsThereAShipAtWaypoint(waypoint)) {
// TODO check last updated time
return shipyard, nil
}
uriRef := url.URL{Path: path.Join("systems", waypoint.SystemSymbol, "waypoints", waypoint.Symbol, "shipyard")}
var shipyard model.Shipyard
if err := c.Send("GET", &uriRef, nil, &shipyard); err != nil {
return nil, fmt.Errorf("failed to get shipyard at %s: %w", waypoint.Symbol, err)
}
if err := db.SaveShipyard(&shipyard); err != nil {
return nil, fmt.Errorf("failed to get shipyard at %s: %w", waypoint.Symbol, err)
}
return &shipyard, nil
}
func (c *Client) GetWaypoint(symbol string, db *database.DB) (*model.Waypoint, error) {
// TODO check updated
if waypoint, err := db.LoadWaypoint(symbol); err == nil && waypoint != nil {
// TODO check last updated time
return waypoint, nil
}
systemSymbol := WaypointSymbolToSystemSymbol(symbol)

View file

@ -0,0 +1,42 @@
package database
import (
"database/sql"
"encoding/json"
"fmt"
"time"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
)
func (db *DB) LoadShipyard(symbol string) (*model.Shipyard, error) {
var buf []byte
if err := db.QueryRow(`SELECT data FROM shipyards WHERE data->>'symbol' = ?;`, symbol).Scan(&buf); err != nil {
return nil, fmt.Errorf("failed to query shipyard: %w", err)
}
var shipyard model.Shipyard
if err := json.Unmarshal(buf, &shipyard); err != nil {
return nil, fmt.Errorf("failed to unmarshal shipyard: %w", err)
}
return &shipyard, nil
}
func (db *DB) SaveShipyard(shipyard *model.Shipyard) error {
data, err := json.Marshal(shipyard)
if err != nil {
return fmt.Errorf("failed to marshal shipyard: %w", err)
}
_, err = db.Exec(
`INSERT INTO shipyards(data, updated)
VALUES (json(:data), :updated)
ON CONFLICT DO UPDATE SET data = :data, updated = :updated
WHERE data->>'symbol' = :symbol;`,
sql.Named("data", data),
sql.Named("symbol", shipyard.Symbol),
sql.Named("updated", time.Now()),
)
if err != nil {
return fmt.Errorf("failed to append shipyard: %w", err)
}
return nil
}

View file

@ -10,7 +10,12 @@ CREATE TABLE markets (
);
CREATE INDEX markets_systemSymbol on markets (systemSymbol);
CREATE UNIQUE INDEX markets_data_symbol on markets(json_extract(data, '$.symbol'));
CREATE TABLE shipyards (
id INTEGER PRIMARY KEY,
data JSON NOT NULL,
updated DATE DEFAULT NULL
);
CREATE UNIQUE INDEX shipyards_data_symbol on shipyards (json_extract(data, '$.symbol'));
CREATE TABLE systems (
id INTEGER PRIMARY KEY,
data JSON NOT NULL

View file

@ -45,6 +45,30 @@ func (db *DB) LoadWaypoint(symbol string) (*model.Waypoint, error) {
return &waypoint, nil
}
func (db *DB) LoadWaypointsInSystem(system *model.System) ([]model.Waypoint, error) {
rows, err := db.Query(`SELECT data FROM waypoints WHERE data->>'systemSymbol' = ?;`, system.Symbol)
if err != nil {
return nil, fmt.Errorf("failed to query waypoints: %w", err)
}
defer rows.Close()
waypoints := make([]model.Waypoint, 0)
for rows.Next() {
var buf []byte
if err := rows.Scan(&buf); err != nil {
return nil, fmt.Errorf("failed to load waypoint from row: %w", err)
}
var waypoint model.Waypoint
if err := json.Unmarshal(buf, &waypoint); err != nil {
return nil, fmt.Errorf("failed to unmarshal waypoint: %w", err)
}
waypoints = append(waypoints, waypoint)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed to load waypoints from rows: %w", err)
}
return waypoints, nil
}
func (db *DB) SaveWaypoint(waypoint *model.Waypoint) error {
data, err := json.Marshal(waypoint)
if err != nil {

View file

@ -5,3 +5,7 @@ type Common struct {
//Name string `json:"name"`
Symbol string `json:"symbol"`
}
type CommonType struct {
Type string `json:"type"`
}

View file

@ -0,0 +1,35 @@
package model
import "time"
type Shipyard struct {
ModificationFee int `json:"modificationFee"`
Symbol string `json:"symbol"`
ShipTypes []CommonType `json:"shipTypes"`
Transactions []ShipyardTransaction `json:"transactions"`
Ships []ShipyardShip `json:"ships"`
}
type ShipyardShip struct {
Activity string `json:"activity"`
// crew
//Description string `json:"description"`
// engine
// frame
// modules
// mounts
//Name string `json:"name"`
PurchasePrice int `json:"purchasePrice"`
// reactor
Supply string `json:"supply"`
Type string `json:"type"`
}
type ShipyardTransaction struct {
AgentSymbol string `json:"agentSymbol"`
Price int `json:"price"`
ShipSymbol string `json:"shipSymbol"`
ShipType string `json:"shipType"`
Timestamp time.Time `json:"timestamp"`
WaypointSymbol string `json:"waypointSymbol"`
}

View file

@ -14,3 +14,11 @@ type Waypoint struct {
X int `json:"x"`
Y int `json:"y"`
}
func (w Waypoint) GetX() int {
return w.X
}
func (w Waypoint) GetY() int {
return w.Y
}