gameserver

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package gameserver hosts authoritative game simulations and relays their command stream to connected clients over a transport. The simulation logic is the same engine the browser runs as wasm; this package only owns the authority loop (one fixed 40 Hz tick) and the fan-out of orders, command frames, snapshots and hashes. The transport is abstracted behind Conn so the orchestration is testable with an in-memory loopback and the real websocket adapter is a thin layer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DemoSpawnFunc

func DemoSpawnFunc() sim.SpawnFunc

DemoSpawnFunc returns a spawn provider that needs no game assets: it resolves the single synthetic unit type "scout" used by the standalone browser demo. `kbot host` falls back to this when no --root is given, so the multiplayer path can be exercised end-to-end without a TA install.

func FBISpawnFunc

func FBISpawnFunc(root string) sim.SpawnFunc

FBISpawnFunc returns a sim.SpawnFunc backed by the flattened game-asset tree at root (units/*.fbi, weapons/*.tdf). It is the asset bridge the native host uses to turn Spawn orders into real TA units.

Types

type CobSource

type CobSource func(name string) ([]byte, bool)

CobSource returns a unit type's raw COB bytecode, with ok=false for a script-less type. A match uses it to drive the authority's units through the same COB animation + scripted weapon-aim/death threads the browser clients run, so the authoritative simulation stays bit-identical to every client's prediction during combat — without it, the script-less server diverges from the COB-running clients the moment a fight starts.

func FBICobSource

func FBICobSource(root string) CobSource

FBICobSource returns a CobSource backed by the flattened asset tree's scripts/*.cob files, so the native host's authority runs each unit's COB in lockstep with the browser clients (which fetch the identical bytes).

type Conn

type Conn interface {
	Send(wire.ServerMsg) error
	Recv() (wire.ClientMsg, error)
	Close() error
}

Conn is one client's bidirectional message channel.

type Match

type Match struct {
	// contains filtered or unexported fields
}

Match is one authoritative game running its own tick loop.

func NewMatch

func NewMatch(id string, seed uint32, inputDelay uint64, spawn sim.SpawnFunc, cob CobSource) *Match

NewMatch creates a match with a fresh world and the given spawn provider. The cob source (optional) backs each spawned unit with its COB script, run by a per-match script runtime so the authority animates and fights identically to the clients; pass nil for a script-less authority.

func (*Match) AddConn

func (m *Match) AddConn(conn Conn)

AddConn registers a connection with the match and starts its read loop. The caller (the websocket handler) provides an already-handshaken Conn.

func (*Match) Run

func (m *Match) Run()

Run drives the authority loop until Stop. It must run on its own goroutine; it is the sole owner of the session, so no locking is needed.

func (*Match) ServeWS

func (m *Match) ServeWS(w http.ResponseWriter, r *http.Request)

ServeWS upgrades an HTTP request to a websocket and hands the resulting connection to the match. It blocks until the request context is closed so the underlying transport stays open for the duration of the read loop.

func (*Match) SetInfo

func (m *Match) SetInfo(name, kind string)

SetInfo records descriptive metadata for listings. It must be called before Run starts, while the match is still single-threaded.

func (*Match) Stop

func (m *Match) Stop()

Stop ends the match loop. It is safe to call more than once: the server's reaper and an explicit Server.Stop can both target the same match.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server hosts one or more matches and routes websocket upgrades to them. It owns the lifecycle of each match's authority goroutine.

func NewServer

func NewServer(spawn sim.SpawnFunc, cob CobSource, seed uint32, inputDelay uint64) *Server

NewServer creates an empty server that builds matches with the given spawn provider, COB source, seed and input delay. The cob source (optional, may be nil) backs each match's units with their scripts so the authority stays bit-identical to the COB-running clients. It starts a background reaper that retires matches left empty past the idle grace.

func (*Server) Match

func (s *Server) Match(id string, seed uint32, inputDelay uint64) *Match

Match returns the match with the given id, creating and starting it on first use so a client can connect to a fresh game by naming it.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP upgrades websocket connections and binds them to the match named by the "match" query parameter (defaulting to "default"). A connection that creates a new match may name and classify it via the "name" and "kind" parameters, which feed the session listing.

func (*Server) Sessions

func (s *Server) Sessions() []SessionInfo

Sessions returns a snapshot of every active match for discovery/listing, oldest first.

func (*Server) Stop

func (s *Server) Stop()

Stop ends every running match and halts the reaper.

type SessionInfo

type SessionInfo struct {
	ID        string    `json:"id"`
	Name      string    `json:"name"`
	Kind      string    `json:"kind"`
	Players   int       `json:"players"`
	Units     int       `json:"units"`
	CreatedAt time.Time `json:"createdAt"`
}

SessionInfo is a thread-safe snapshot of a match's descriptive state, used by the host's session-listing endpoint.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL