Documentation
¶
Overview ¶
Package systemstate keeps per-page game-system state: small JSON documents a system package's widget reads and writes for one entity (for example a negotiation tracker on an NPC page). Chronicle stores and gates the documents; it never interprets them, so any system can use the table without a Chronicle release. A campaign-wide twin holds documents that belong to the whole campaign rather than one page.
Each document has two halves. The public half is visible to everyone who can view the page; the gm half is only ever returned to the campaign owner and members granted DM access.
The browser HTTP surface for system state. Handlers only bind, call the service and shape the response; the one decision they own is the audience split: the gm half leaves the server only for callers who may author DM-only content.
Hand-written SQL for entity_system_state.
SECURITY: every method takes campaignID and filters on it, so a lookup with an entity id from another campaign cannot match. MariaDB has no row-level security, so this scoping and the service's entity check are the guard.
Route registration for system state.
Index ¶
- Constants
- Variables
- func RegisterRoutes(e *echo.Echo, h *Handler, campaignSvc campaigns.CampaignService, ...)
- type AIPageState
- type EntityAccess
- type EntityLookup
- type Handler
- type ListItem
- type ListItemResponse
- type ListResponse
- type Repository
- type Service
- type State
- type StateResponse
- type SystemChecker
- type SystemResolver
- type UpdatePublisher
- type Write
Constants ¶
const MaxHalfBytes = 16 * 1024
MaxHalfBytes caps each JSON half. The state is a small tracker document, not a content store, and the cap keeps one page from growing an unbounded row.
const MaxListItems = 200
MaxListItems caps the campaign-wide listing. The service asks for one more row than this so it can tell "exactly the cap" from "more exist".
const PluginSlug = "systemstate"
PluginSlug names this plugin in the registry and the health/migration bookkeeping.
Variables ¶
var MigrationsFS embed.FS
MigrationsFS contains the embedded SQL migration files so the table is created from the compiled binary regardless of the working directory (same pattern as the other plugins).
Functions ¶
func RegisterRoutes ¶
func RegisterRoutes(e *echo.Echo, h *Handler, campaignSvc campaigns.CampaignService, authSvc auth.AuthService)
RegisterRoutes mounts the browser routes under the campaign group. Reads are open to every member who can view the page, or to every member for a campaign-wide document (the handler withholds the gm half unless the caller may author DM-only content); writes are DM-team only. CSRF is enforced by the global middleware.
Types ¶
type AIPageState ¶
type AIPageState struct {
// contains filtered or unexported fields
}
AIPageState is this plugin's part of the AI engine (aiplug): the `page-state` lookup, which reads one key of the game system's per-page state across the campaign, both halves as stored. Chronicle never interprets the documents, so the answer gives them as the system's widgets keep them. It is the Director team's: the `gm` half is theirs, and the `public` half a widget derives is only safe to show per page, which the campaign listing does not check, so anyone else is refused. It only reads; the widget on each page is what changes the state.
func NewAIPageState ¶
func NewAIPageState(svc Service, systems SystemResolver) *AIPageState
NewAIPageState builds the lookup over the service.
func (*AIPageState) Answer ¶
func (l *AIPageState) Answer(ctx context.Context, campaignID string, a aiplug.Actor, r aiplug.Record) (string, string, error)
Answer lists the pages carrying the key, with both halves.
func (*AIPageState) LookupInfo ¶
func (*AIPageState) LookupInfo() aiplug.LookupInfo
LookupInfo describes `what: page-state` and its tool.
type EntityAccess ¶
type EntityAccess interface {
ResolveViewableEntity(ctx context.Context, entityID string, role int, userID string) (campaignID string, canView bool, err error)
}
EntityAccess answers whether the viewer may see an entity and which campaign owns it, using the entities plugin's own visibility rules so a private page stays hidden here too. Implemented in app (T-B2: no entities import here).
type EntityLookup ¶
type EntityLookup interface {
// EntityCampaignID returns the entity's campaign, or a NotFound error.
EntityCampaignID(ctx context.Context, entityID string) (string, error)
}
EntityLookup resolves which campaign an entity belongs to. The service compares it with the campaign in the URL, so a caller can never reach another campaign's page by pairing its own campaign id with a foreign entity id. Implemented in app over the entities service (T-B2: no import here).
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler serves the system-state routes.
func NewHandler ¶
func NewHandler(svc Service, access EntityAccess) *Handler
NewHandler builds the handler.
func (*Handler) Get ¶
Get returns the state of one page for any member who can view the page.
GET /campaigns/:id/entities/:eid/system-state/:system/:key
func (*Handler) GetCampaign ¶
GetCampaign returns the campaign-wide document for the key to any member; the gm half only to the DM team, as on a page.
GET /campaigns/:id/campaign-system-state/:system/:key
func (*Handler) List ¶
List returns every page of the campaign carrying the key, both halves. The route is gated to the DM team and the handler re-derives that from the capability. Visibility needs no per-row check: the DM team reads with Owner-level visibility (VisibilityRole), which sees every live page, and the repository already scopes by campaign and drops trashed pages.
GET /campaigns/:id/system-state/:system/:key
type ListItem ¶
type ListItem struct {
EntityID string
EntityName string
GM json.RawMessage
Public json.RawMessage
UpdatedAt time.Time
}
ListItem is one page carrying a given key, as the campaign-wide listing returns it.
type ListItemResponse ¶
type ListItemResponse struct {
EntityID string `json:"entityId"`
EntityName string `json:"entityName"`
Public json.RawMessage `json:"public"`
GM json.RawMessage `json:"gm"`
UpdatedAt time.Time `json:"updatedAt"`
}
ListItemResponse is one page in the campaign-wide listing.
type ListResponse ¶
type ListResponse struct {
SystemID string `json:"systemId"`
Key string `json:"key"`
Items []ListItemResponse `json:"items"`
Truncated bool `json:"truncated"`
}
ListResponse is the wire shape of the campaign-wide listing.
type Repository ¶
type Repository interface {
// Get returns the stored row, or nil when there is none.
Get(ctx context.Context, campaignID, entityID, systemID, key string) (*State, error)
// Upsert writes the row in one statement. A nil half keeps the stored half
// (and is stored as {} when the row is new); a non-nil half replaces it.
Upsert(ctx context.Context, w Write) error
// ListByKey returns up to limit rows for the key in one campaign, ordered
// by entity name. Trashed pages are excluded.
ListByKey(ctx context.Context, campaignID, systemID, key string, limit int) ([]ListItem, error)
// ListByKeyForPage is ListByKey for the pages with one name only, so a
// page past the listing's limit can still be read by its name.
ListByKeyForPage(ctx context.Context, campaignID, systemID, key, name string, limit int) ([]ListItem, error)
// GetCampaign returns the campaign-wide row, or nil when there is none.
GetCampaign(ctx context.Context, campaignID, systemID, key string) (*State, error)
// UpsertCampaign writes the campaign-wide row with Upsert's partial
// rule. w.EntityID is ignored.
UpsertCampaign(ctx context.Context, w Write) error
}
Repository is the persistence boundary for system state.
func NewRepository ¶
func NewRepository(db *sql.DB) Repository
NewRepository creates a MariaDB-backed repository.
type Service ¶
type Service interface {
// Get returns both halves; a page with no row reads as two empty objects.
// Which halves a caller may see is decided by the caller.
Get(ctx context.Context, campaignID, entityID, systemID, key string) (State, error)
// Put stores the halves with the partial-update contract: a nil half
// keeps the stored half, a non-nil half replaces it. Returns the state
// as stored.
Put(ctx context.Context, campaignID, entityID, systemID, key string, gm, public *json.RawMessage, userID string) (State, error)
// ListByKey returns the live pages of the campaign that carry the key,
// by entity name, at most MaxListItems; truncated reports that more exist.
// Both halves are returned unfiltered; the caller decides the audience.
ListByKey(ctx context.Context, campaignID, systemID, key string) (items []ListItem, truncated bool, err error)
// ListByKeyForPage is ListByKey for the pages called name only, read
// directly rather than picked from the capped listing.
ListByKeyForPage(ctx context.Context, campaignID, systemID, key, name string) (items []ListItem, truncated bool, err error)
// GetCampaign returns the campaign-wide document for the key; a key with
// no row reads as two empty objects. The caller decides the audience.
GetCampaign(ctx context.Context, campaignID, systemID, key string) (State, error)
// PutCampaign stores the campaign-wide document with Put's partial-update
// contract and returns it as stored.
PutCampaign(ctx context.Context, campaignID, systemID, key string, gm, public *json.RawMessage, userID string) (State, error)
}
Service is the business-logic boundary for system state.
func NewService ¶
func NewService(repo Repository, entities EntityLookup, systems SystemChecker, publisher UpdatePublisher) Service
NewService builds the service. publisher may be nil (no live notification).
type State ¶
type State struct {
// GM is the DM-team half. The service returns it unfiltered; deciding who
// may see it is the caller's job (handlers and the sync adapter).
GM json.RawMessage
// Public is the half any viewer of the page may read.
Public json.RawMessage
// UpdatedAt is nil when nothing has been stored yet.
UpdatedAt *time.Time
}
State is one stored document. Both halves are always valid JSON objects: a missing row reads as two empty objects, so callers never branch on absence.
type StateResponse ¶
type StateResponse struct {
SystemID string `json:"systemId"`
Key string `json:"key"`
Public json.RawMessage `json:"public"`
// GM is omitted entirely (not null, not {}) for callers without DM
// access, so its absence reveals nothing about whether data exists.
GM *json.RawMessage `json:"gm,omitempty"`
IsGM bool `json:"isGm"`
UpdatedAt *time.Time `json:"updatedAt"`
}
StateResponse is the wire shape of GET and PUT.
func NewStateResponse ¶
func NewStateResponse(systemID, key string, st State, includeGM bool) StateResponse
NewStateResponse shapes a State for a caller. includeGM must come from the caller's verified capability, never from the request.
type SystemChecker ¶
type SystemChecker interface {
IsSystemEnabled(ctx context.Context, campaignID, systemID string) (bool, error)
}
SystemChecker reports whether a game system is enabled for a campaign, so a disabled system cannot accumulate state. Implemented in app over the systems registry.
type SystemResolver ¶
SystemResolver names the game system a campaign has on, "" for none. Implemented in app over the systems handler.
type UpdatePublisher ¶
type UpdatePublisher interface {
PublishSystemStateUpdated(campaignID, entityID, systemID, key string)
}
UpdatePublisher announces a successful write. The message carries ids only, never state content; it is delivered to the GM side. Implemented in app over the websocket bus.
type Write ¶
type Write struct {
CampaignID string
// EntityID is empty for a campaign-wide document.
EntityID string
SystemID string
Key string
GM []byte
Public []byte
// UpdatedBy is empty when there is no acting user.
UpdatedBy string
}
Write is one upsert. GM and Public are nil to keep the stored half.