1
0
Fork 0

[golang] implement systems and waypoints lookups

This commit is contained in:
Julien Dessaux 2025-02-15 01:22:36 +01:00
parent d97985a694
commit 66466e161f
Signed by: adyxax
GPG key ID: F92E51B86E07177E
10 changed files with 205 additions and 0 deletions

View file

@ -26,6 +26,7 @@ func (a *agent) autoContracting(ship *model.Ship) {
}
}
}
a.sendShipError(fmt.Errorf("failed to run contracts: negotiating new contracts is not implemented yet"), ship)
// TODO
//for {
// negotiate
@ -37,6 +38,7 @@ func (a *agent) runContract(contract *model.Contract, ship *model.Ship) error {
if err := a.client.Accept(contract, a.db); err != nil {
return fmt.Errorf("failed to run contract: %w", err)
}
//slog.Info("running contract", "contract", contract, "ship", ship.Symbol)
switch contract.Type {
// TODO
//case "PROCUREMENT":

View file

@ -23,6 +23,7 @@ type State int
const (
start_running_contracts_with_the_command_ship = iota
visit_all_shipyards
)
func Run(
@ -54,6 +55,11 @@ func Run(
agent.wg.Add(1)
go agent.autoContracting(&agent.ships[0])
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])
}
state++
return
default:
agent.sendShipError(fmt.Errorf("agent runner reach an unknown state: %d", state), nil)

24
golang/pkg/agent/visit.go Normal file
View file

@ -0,0 +1,24 @@
package agent
import (
"fmt"
"log/slog"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
)
func (a *agent) visitAllShipyards(ship *model.Ship) error {
system, err := a.client.GetSystem(ship.Nav.SystemSymbol, a.db)
if err != nil {
return fmt.Errorf("failed to visit all shipyards: %w", err)
}
waypoints, err := a.client.ListWaypointsInSystem(system, a.db)
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")
}

59
golang/pkg/api/systems.go Normal file
View file

@ -0,0 +1,59 @@
package api
import (
"fmt"
"net/url"
"path"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/database"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
)
func (c *Client) GetSystem(symbol string, db *database.DB) (*model.System, error) {
if system, err := db.LoadSystem(symbol); err == nil && system != nil {
return system, nil
}
uriRef := url.URL{Path: path.Join("systems", symbol)}
var system model.System
if err := c.Send("GET", &uriRef, nil, &system); err != nil {
return nil, fmt.Errorf("failed to get system %s: %w", symbol, err)
}
if err := db.SaveSystem(&system); err != nil {
return nil, fmt.Errorf("failed to get system %s: %w", symbol, err)
}
return &system, nil
}
func (c *Client) ListWaypointsInSystem(system *model.System, db *database.DB) ([]model.Waypoint, error) {
// TODO database caching
// TODO pagination
// TODO check updated
uriRef := url.URL{Path: path.Join("systems", system.Symbol, "waypoints")}
var waypoints []model.Waypoint
if err := c.Send("GET", &uriRef, nil, &waypoints); err != nil {
return nil, fmt.Errorf("failed to list waypoints in system %s: %w", system.Symbol, err)
}
for _, waypoint := range waypoints {
if err := db.SaveWaypoint(&waypoint); err != nil {
return nil, fmt.Errorf("failed to list waypoints in system %s: %w", system.Symbol, err)
}
}
return waypoints, 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 {
return waypoint, nil
}
systemSymbol := WaypointSymbolToSystemSymbol(symbol)
uriRef := url.URL{Path: path.Join("systems", systemSymbol, "waypoints", symbol)}
var waypoint model.Waypoint
if err := c.Send("GET", &uriRef, nil, &waypoint); err != nil {
return nil, fmt.Errorf("failed to get waypoint %s: %w", symbol, err)
}
if err := db.SaveWaypoint(&waypoint); err != nil {
return nil, fmt.Errorf("failed to get waypoint %s: %w", symbol, err)
}
return &waypoint, nil
}

7
golang/pkg/api/utils.go Normal file
View file

@ -0,0 +1,7 @@
package api
import "strings"
func WaypointSymbolToSystemSymbol(symbol string) string {
return strings.Join(strings.Split(symbol, "-")[:2], "-")
}

View file

@ -0,0 +1,57 @@
package database
import (
"encoding/json"
"fmt"
"time"
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
)
// ----- System -----------------------------------------------------------------
func (db *DB) LoadSystem(symbol string) (*model.System, error) {
var buf []byte
if err := db.QueryRow(`SELECT data FROM systems WHERE data->>'symbol' = ?;`, symbol).Scan(&buf); err != nil {
return nil, fmt.Errorf("failed to query system: %w", err)
}
var system model.System
if err := json.Unmarshal(buf, &system); err != nil {
return nil, fmt.Errorf("failed to unmarshal system: %w", err)
}
return &system, nil
}
func (db *DB) SaveSystem(system *model.System) error {
data, err := json.Marshal(system)
if err != nil {
return fmt.Errorf("failed to marshal system: %w", err)
}
if _, err := db.Exec(`INSERT INTO systems(data) VALUES (json(?));`, data); err != nil {
return fmt.Errorf("failed to append system: %w", err)
}
return nil
}
// ----- Waypoint ---------------------------------------------------------------
func (db *DB) LoadWaypoint(symbol string) (*model.Waypoint, error) {
var buf []byte
if err := db.QueryRow(`SELECT data FROM waypoints WHERE data->>'symbol' = ?;`, symbol).Scan(&buf); err != nil {
return nil, fmt.Errorf("failed to query waypoint: %w", err)
}
var waypoint model.Waypoint
if err := json.Unmarshal(buf, &waypoint); err != nil {
return nil, fmt.Errorf("failed to unmarshal waypoint: %w", err)
}
return &waypoint, nil
}
func (db *DB) SaveWaypoint(waypoint *model.Waypoint) error {
data, err := json.Marshal(waypoint)
if err != nil {
return fmt.Errorf("failed to marshal waypoint: %w", err)
}
if _, err := db.Exec(`INSERT INTO waypoints(data, updated) VALUES (json(?), ?);`, data, time.Now()); err != nil {
return fmt.Errorf("failed to append waypoint: %w", err)
}
return nil
}

View file

@ -0,0 +1,7 @@
package model
type Common struct {
//Description string `json:"description"`
//Name string `json:"name"`
Symbol string `json:"symbol"`
}

View file

@ -0,0 +1,16 @@
package model
type Faction struct {
Symbol string `json:"symbol"`
//"name": "string",
//"description": "string",
//"headquarters": "string",
//"traits": [
// {
// "symbol": "BUREAUCRATIC",
// "name": "string",
// "description": "string"
// }
//],
//"isRecruiting": true
}

View file

@ -0,0 +1,11 @@
package model
type System struct {
Factions []Faction `json:"factions"`
SectorSymbol string `json:"sectorSymbol"`
Symbol string `json:"symbol"`
Type string `json:"type"`
Waypoints []Waypoint `json:"waypoints"`
X int `json:"x"`
Y int `json:"y"`
}

View file

@ -0,0 +1,16 @@
package model
type Waypoint struct {
Chart *Chart `json:"chart"`
Factions []string `json:"factions"`
IsUnderConstruction bool `json:"isUnderConstruction"`
Modifiers []Common `json:"modifiers"`
//orbitals: Array<{symbol: string;}>;
//orbits: string;
Symbol string `json:"symbol"`
SystemSymbol string `json:"systemSymbol"`
Traits []Common `json:"traits"`
Type string `json:"type"`
X int `json:"x"`
Y int `json:"y"`
}