Documentation
¶
Index ¶
- Variables
- func NewSPAFileServer(root string) http.Handler
- func WriteJSON(w http.ResponseWriter, status int, v any)
- type AgentCache
- type AgentInfo
- type AgentSession
- func (s *AgentSession) Close()
- func (s *AgentSession) IsConnected() bool
- func (s *AgentSession) OnConnected(state *game.State)
- func (s *AgentSession) OnDisconnected(err error)
- func (s *AgentSession) OnMessage(resp protocol.Response)
- func (s *AgentSession) SendCommand(ctx context.Context, msg protocol.Message) error
- type BaseDetailResponse
- type BaseJSON
- type BrowserClient
- type BrowserHub
- type BrowserMessage
- type Cache
- func (c *Cache) GetCacheData(key string) json.RawMessage
- func (c *Cache) GetGalaxyMap() json.RawMessage
- func (c *Cache) GetRecipes() json.RawMessage
- func (c *Cache) GetShipClasses() json.RawMessage
- func (c *Cache) GetSystem(systemID string) (json.RawMessage, bool)
- func (c *Cache) SetGalaxyMap(data json.RawMessage)
- func (c *Cache) SetRecipes(data json.RawMessage)
- func (c *Cache) SetShipClasses(data json.RawMessage)
- func (c *Cache) SetSystem(systemID string, data json.RawMessage)
- type CacheEntry
- type CachePolicy
- type CatalogItemJSON
- type ConnectedSystemJSON
- type FacilityJSON
- type ObserverServer
- func (s *ObserverServer) AddAgent(ctx context.Context, username string) error
- func (s *ObserverServer) Close()
- func (s *ObserverServer) GetAgent(username string) *AgentSession
- func (s *ObserverServer) HandleAPIAgents(w http.ResponseWriter, r *http.Request)
- func (s *ObserverServer) HandleBrowserWS(w http.ResponseWriter, r *http.Request)
- func (s *ObserverServer) HandleGetBase(w http.ResponseWriter, r *http.Request)
- func (s *ObserverServer) HandleGetBaseByPOI(w http.ResponseWriter, r *http.Request)
- func (s *ObserverServer) HandleGetCatalogItems(w http.ResponseWriter, r *http.Request)
- func (s *ObserverServer) HandleGetPlayerSkills(w http.ResponseWriter, r *http.Request)
- func (s *ObserverServer) HandleGetSkill(w http.ResponseWriter, r *http.Request)
- func (s *ObserverServer) HandleGetSkills(w http.ResponseWriter, _ *http.Request)
- func (s *ObserverServer) HandleGetSystem(w http.ResponseWriter, r *http.Request)
- func (s *ObserverServer) HandleGetSystemPOIs(w http.ResponseWriter, r *http.Request)
- func (s *ObserverServer) HandleGetSystems(w http.ResponseWriter, _ *http.Request)
- func (s *ObserverServer) ListAgents() []AgentInfo
- func (s *ObserverServer) RegisterRoutes(mux *http.ServeMux)
- func (s *ObserverServer) RemoveAgent(username string) error
- type POIJSON
- type SPAFileServer
- type ServerMessage
- type SkillJSON
- type SystemDetailResponse
- type SystemJSON
Constants ¶
This section is empty.
Variables ¶
var CachePolicies = map[string]CachePolicy{
"get_system": {MaxAgeTicks: 30},
"get_ship": {MaxAgeTicks: 0},
"get_ships": {MaxAgeTicks: 200},
"get_base": {MaxAgeTicks: 60},
"get_poi": {MaxAgeTicks: 60},
"get_skills": {MaxAgeTicks: 100},
"get_cargo": {MaxAgeTicks: 0},
"get_recipes": {MaxAgeTicks: 1000},
"get_map": {MaxAgeTicks: 3600},
"view_storage": {MaxAgeTicks: 0},
"view_faction_storage": {MaxAgeTicks: 0},
}
CachePolicies maps command names to their caching policy. Commands not listed here are never cached.
var InvalidationMap = map[string][]string{
"switch_ship": {"get_ship", "get_cargo"},
"install_mod": {"get_ship"},
"install": {"get_ship"},
"uninstall_mod": {"get_ship"},
"uninstall": {"get_ship"},
"buy": {"get_ship", "get_cargo"},
"sell": {"get_ship", "get_cargo"},
"create_sell_order": {"get_ship", "get_cargo"},
"mine": {"get_ship", "get_cargo"},
"craft": {"get_ship", "get_cargo"},
"jettison": {"get_ship", "get_cargo"},
"loot_wreck": {"get_ship", "get_cargo"},
"salvage_wreck": {"get_ship", "get_cargo"},
"use_item": {"get_ship", "get_cargo"},
"refuel": {"get_ship"},
"repair": {"get_ship"},
"deposit_items": {"get_ship", "get_cargo", "view_storage"},
"withdraw_items": {"get_ship", "get_cargo", "view_storage"},
"faction_deposit_items": {"get_ship", "get_cargo", "view_faction_storage"},
"faction_withdraw_items": {"get_ship", "get_cargo", "view_faction_storage"},
"faction_deposit_credits": {"view_faction_storage"},
"faction_withdraw_credits": {"view_faction_storage"},
"jump": {"get_system", "get_poi", "get_base"},
"travel": {"get_poi"},
"dock": {"get_base"},
"undock": {"get_base"},
"buy_ship": {"get_ship", "get_cargo"},
"sell_ship": {"get_ship", "get_cargo"},
}
InvalidationMap maps mutation commands to the cache keys they invalidate.
Functions ¶
func NewSPAFileServer ¶
NewSPAFileServer creates a new SPA file server rooted at the given directory.
Types ¶
type AgentCache ¶
type AgentCache struct {
// contains filtered or unexported fields
}
AgentCache provides per-agent response caching keyed by command name.
func (*AgentCache) Get ¶
func (c *AgentCache) Get(command string, currentTick int64) json.RawMessage
Get returns cached data for the given command if the entry exists and is still fresh relative to currentTick. Returns nil on miss or stale entry.
func (*AgentCache) Invalidate ¶
func (c *AgentCache) Invalidate(commands ...string)
Invalidate removes the specified commands from the cache.
func (*AgentCache) Set ¶
func (c *AgentCache) Set(command string, data json.RawMessage, tick int64)
Set stores a response for the given command at the specified tick.
type AgentInfo ¶
type AgentInfo struct {
Username string `json:"username"`
Connected bool `json:"connected"`
System string `json:"system,omitempty"`
POI string `json:"poi,omitempty"`
Docked bool `json:"docked"`
}
AgentInfo is the summary returned in agent list responses.
type AgentSession ¶
type AgentSession struct {
// contains filtered or unexported fields
}
AgentSession wraps a game.Client and relays all messages to browser clients.
func NewAgentSession ¶
func NewAgentSession(username string, client *game.Client, server *ObserverServer, ctx context.Context, cancel context.CancelFunc, logger *log.Logger) *AgentSession
NewAgentSession creates a session that relays game messages to the browser hub.
func (*AgentSession) IsConnected ¶
func (s *AgentSession) IsConnected() bool
IsConnected returns the connection status.
func (*AgentSession) OnConnected ¶
func (s *AgentSession) OnConnected(state *game.State)
OnConnected implements game.MessageHandler.
func (*AgentSession) OnDisconnected ¶
func (s *AgentSession) OnDisconnected(err error)
OnDisconnected implements game.MessageHandler with enhanced error handling.
func (*AgentSession) OnMessage ¶
func (s *AgentSession) OnMessage(resp protocol.Response)
OnMessage implements game.MessageHandler.
func (*AgentSession) SendCommand ¶
SendCommand sends a command to the game server on behalf of a browser client. This is a fire-and-forget operation - responses are handled asynchronously via the message handler.
type BaseDetailResponse ¶
type BaseDetailResponse struct {
Base BaseJSON `json:"base"`
}
BaseDetailResponse is the response shape for GET /api/bases/{id}.
type BaseJSON ¶
type BaseJSON struct {
ID string `json:"id"`
POIID string `json:"poi_id"`
Name string `json:"name"`
Description string `json:"description"`
Empire string `json:"empire"`
DefenseLevel int `json:"defense_level"`
HasDrones bool `json:"has_drones"`
PublicAccess bool `json:"public_access"`
Services map[string]bool `json:"services"`
Facilities []FacilityJSON `json:"facilities"`
}
BaseJSON is the JSON representation of a base for API responses.
type BrowserClient ¶
type BrowserClient struct {
// contains filtered or unexported fields
}
BrowserClient represents a single browser WebSocket connection.
func NewBrowserClient ¶
func NewBrowserClient(conn *websocket.Conn, server *ObserverServer, logger *log.Logger) *BrowserClient
NewBrowserClient creates a browser client and starts its read/write loops.
func (*BrowserClient) Run ¶
func (bc *BrowserClient) Run(ctx context.Context)
Run starts the read and write loops for this browser client.
type BrowserHub ¶
type BrowserHub struct {
// contains filtered or unexported fields
}
BrowserHub manages all connected browser WebSocket clients.
func NewBrowserHub ¶
func NewBrowserHub(logger *log.Logger) *BrowserHub
NewBrowserHub creates a new hub for browser clients.
func (*BrowserHub) Broadcast ¶
func (h *BrowserHub) Broadcast(agentName string, data []byte)
Broadcast sends a message to all browser clients watching the given agent.
func (*BrowserHub) BroadcastAll ¶
func (h *BrowserHub) BroadcastAll(data []byte)
BroadcastAll sends a message to all connected browser clients regardless of subscription.
func (*BrowserHub) Register ¶
func (h *BrowserHub) Register(client *BrowserClient)
Register adds a browser client to the hub.
func (*BrowserHub) Unregister ¶
func (h *BrowserHub) Unregister(client *BrowserClient)
Unregister removes a browser client from the hub.
type BrowserMessage ¶
type BrowserMessage struct {
Type string `json:"type"`
Agent string `json:"agent,omitempty"`
Command string `json:"command,omitempty"`
Payload map[string]any `json:"payload,omitempty"`
Key string `json:"key,omitempty"`
}
BrowserMessage is a message sent from the browser to the server.
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache stores shared game data that doesn't change often.
func (*Cache) GetCacheData ¶
func (c *Cache) GetCacheData(key string) json.RawMessage
GetCacheData returns cached data by key name.
func (*Cache) GetGalaxyMap ¶
func (c *Cache) GetGalaxyMap() json.RawMessage
func (*Cache) GetRecipes ¶
func (c *Cache) GetRecipes() json.RawMessage
func (*Cache) GetShipClasses ¶
func (c *Cache) GetShipClasses() json.RawMessage
func (*Cache) SetGalaxyMap ¶
func (c *Cache) SetGalaxyMap(data json.RawMessage)
func (*Cache) SetRecipes ¶
func (c *Cache) SetRecipes(data json.RawMessage)
func (*Cache) SetShipClasses ¶
func (c *Cache) SetShipClasses(data json.RawMessage)
type CacheEntry ¶
type CacheEntry struct {
// contains filtered or unexported fields
}
CacheEntry holds a cached response and the tick at which it was stored.
type CachePolicy ¶
type CachePolicy struct {
MaxAgeTicks int64
}
CachePolicy defines how long a cached entry remains valid.
type CatalogItemJSON ¶
type CatalogItemJSON struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Category string `json:"category"`
Rarity string `json:"rarity"`
Size int `json:"size"`
BaseValue int `json:"base_value"`
Stackable bool `json:"stackable"`
Tradeable bool `json:"tradeable"`
}
CatalogItemJSON is the JSON representation of a catalog item for API responses.
type ConnectedSystemJSON ¶
type ConnectedSystemJSON struct {
ID string `json:"id"`
Name string `json:"name"`
Position game.Position `json:"position"`
Distance int `json:"distance"`
}
ConnectedSystemJSON is a minimal system reference for connection endpoints.
type FacilityJSON ¶
type FacilityJSON struct {
ID string `json:"id"`
Name string `json:"name"`
Category string `json:"category"`
Level int `json:"level"`
LastUpdated int64 `json:"last_updated"`
}
FacilityJSON is the JSON representation of a facility for API responses.
type ObserverServer ¶
type ObserverServer struct {
BrowserHub *BrowserHub
// contains filtered or unexported fields
}
ObserverServer manages agent connections and browser clients.
func NewObserverServer ¶
func NewObserverServer(creds credentials.Provider, kb knowledge.Base, gameURL string, logger *log.Logger) *ObserverServer
NewObserverServer creates a new observer server.
func (*ObserverServer) AddAgent ¶
func (s *ObserverServer) AddAgent(ctx context.Context, username string) error
AddAgent connects an agent to the game server and starts relaying messages. Enhanced with better error handling and connection recovery.
func (*ObserverServer) Close ¶
func (s *ObserverServer) Close()
Close shuts down all agent sessions.
func (*ObserverServer) GetAgent ¶
func (s *ObserverServer) GetAgent(username string) *AgentSession
GetAgent returns the session for the given agent username.
func (*ObserverServer) HandleAPIAgents ¶
func (s *ObserverServer) HandleAPIAgents(w http.ResponseWriter, r *http.Request)
HandleAPIAgents handles REST API requests for agent management with enhanced error handling.
func (*ObserverServer) HandleBrowserWS ¶
func (s *ObserverServer) HandleBrowserWS(w http.ResponseWriter, r *http.Request)
HandleBrowserWS upgrades an HTTP connection to a WebSocket for browser clients.
func (*ObserverServer) HandleGetBase ¶
func (s *ObserverServer) HandleGetBase(w http.ResponseWriter, r *http.Request)
HandleGetBase returns a single base with its facilities. GET /api/bases/{id}
func (*ObserverServer) HandleGetBaseByPOI ¶
func (s *ObserverServer) HandleGetBaseByPOI(w http.ResponseWriter, r *http.Request)
HandleGetBaseByPOI returns a base by its POI ID. GET /api/pois/{id}/base
func (*ObserverServer) HandleGetCatalogItems ¶
func (s *ObserverServer) HandleGetCatalogItems(w http.ResponseWriter, r *http.Request)
HandleGetCatalogItems returns all catalog items from the knowledge base. GET /api/catalog/items
func (*ObserverServer) HandleGetPlayerSkills ¶
func (s *ObserverServer) HandleGetPlayerSkills(w http.ResponseWriter, r *http.Request)
HandleGetPlayerSkills calculates XP progress for player's current skills. GET /api/player/skills?agent={username}
func (*ObserverServer) HandleGetSkill ¶
func (s *ObserverServer) HandleGetSkill(w http.ResponseWriter, r *http.Request)
HandleGetSkill returns a single skill by ID. GET /api/skills/{id}
func (*ObserverServer) HandleGetSkills ¶
func (s *ObserverServer) HandleGetSkills(w http.ResponseWriter, _ *http.Request)
HandleGetSkills returns all available skills. GET /api/skills
func (*ObserverServer) HandleGetSystem ¶
func (s *ObserverServer) HandleGetSystem(w http.ResponseWriter, r *http.Request)
HandleGetSystem returns a single system with its POIs and connected system positions.
func (*ObserverServer) HandleGetSystemPOIs ¶
func (s *ObserverServer) HandleGetSystemPOIs(w http.ResponseWriter, r *http.Request)
HandleGetSystemPOIs returns POIs for a system (lighter endpoint).
func (*ObserverServer) HandleGetSystems ¶
func (s *ObserverServer) HandleGetSystems(w http.ResponseWriter, _ *http.Request)
HandleGetSystems returns all known systems for the galaxy map.
func (*ObserverServer) ListAgents ¶
func (s *ObserverServer) ListAgents() []AgentInfo
ListAgents returns info about all connected agents.
func (*ObserverServer) RegisterRoutes ¶
func (s *ObserverServer) RegisterRoutes(mux *http.ServeMux)
RegisterRoutes registers all observer HTTP routes on the given mux.
func (*ObserverServer) RemoveAgent ¶
func (s *ObserverServer) RemoveAgent(username string) error
RemoveAgent disconnects an agent and removes it from the server.
type POIJSON ¶
type POIJSON struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Position game.Position `json:"position"`
Resources []game.POIResource `json:"resources"`
}
POIJSON is the JSON representation of a POI for API responses.
type SPAFileServer ¶
type SPAFileServer struct {
// contains filtered or unexported fields
}
SPAFileServer serves static files with SPA fallback: any request for a path that doesn't match an existing file returns index.html.
func (*SPAFileServer) ServeHTTP ¶
func (s *SPAFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
type ServerMessage ¶
type ServerMessage struct {
Type string `json:"type"`
Agent string `json:"agent,omitempty"`
Message json.RawMessage `json:"message,omitempty"`
Agents []AgentInfo `json:"agents,omitempty"`
Connected *bool `json:"connected,omitempty"`
Error string `json:"error,omitempty"`
Key string `json:"key,omitempty"`
}
ServerMessage is a message sent from the server to the browser.
type SkillJSON ¶
type SkillJSON struct {
ID string `json:"id"`
Name string `json:"name"`
Category string `json:"category"`
Description string `json:"description"`
MaxLevel int `json:"max_level"`
XPPerLevel []int `json:"xp_per_level"`
}
SkillJSON is the JSON representation of a skill for API responses.
type SystemDetailResponse ¶
type SystemDetailResponse struct {
System SystemJSON `json:"system"`
POIs []POIJSON `json:"pois"`
Connections []ConnectedSystemJSON `json:"connections"`
}
SystemDetailResponse is the response shape for GET /api/systems/{id}.
type SystemJSON ¶
type SystemJSON struct {
ID string `json:"id"`
Name string `json:"name"`
Position game.Position `json:"position"`
PoliceLevel int `json:"police_level"`
SecurityStatus string `json:"security_status"`
Empire string `json:"empire"`
IsStronghold bool `json:"is_stronghold"`
Connections []string `json:"connections"`
LastVisitedTick int64 `json:"last_visited_tick,omitempty"`
}
SystemJSON is the JSON representation of a system for API responses.