Documentation
¶
Overview ¶
Package homebrew keeps a campaign's Homebrew Workshop recipes: parts (the everyday widgets and the kinds other plugins register) wired together by When/Then rules, saved as one document per recipe.
This package owns the recipe format and the registries. It knows no kind of part itself: every kind, event and action is registered by the plugin that owns it, through adapters in internal/app, so no plugin imports another.
The browser HTTP surface for the Homebrew Workshop. Handlers bind, call the service and shape the response; the service owns every rule.
Route registration for the Homebrew Workshop.
Index ¶
- Constants
- Variables
- func RegisterRoutes(e *echo.Echo, h *Handler, campaignSvc campaigns.CampaignService, ...)
- type AI
- func (k *AI) Answer(ctx context.Context, campaignID string, a aiplug.Actor, _ aiplug.Record) (string, string, error)
- func (k *AI) Apply(ctx context.Context, campaignID string, a aiplug.Actor, r aiplug.Record) error
- func (*AI) Brief() string
- func (*AI) Doc() string
- func (k *AI) Export(ctx context.Context, campaignID string, a aiplug.Actor) (string, error)
- func (*AI) Label() string
- func (*AI) LookupInfo() aiplug.LookupInfo
- func (*AI) Name() string
- func (k *AI) Plan(ctx context.Context, campaignID string, a aiplug.Actor, r aiplug.Record) aiplug.Plan
- type Act
- type ActionDef
- type Catalog
- type CatalogEntry
- type CheckFunc
- type Do
- type Doc
- type Drawer
- type EventDef
- type Field
- type Fired
- type Handler
- func (h *Handler) Catalog(c *echo.Context) error
- func (h *Handler) Create(c *echo.Context) error
- func (h *Handler) Delete(c *echo.Context) error
- func (h *Handler) Dismiss(c *echo.Context) error
- func (h *Handler) Drawer(c *echo.Context) error
- func (h *Handler) Get(c *echo.Context) error
- func (h *Handler) List(c *echo.Context) error
- func (h *Handler) Page(c *echo.Context) error
- func (h *Handler) Placements(c *echo.Context) error
- func (h *Handler) Press(c *echo.Context) error
- func (h *Handler) RunHeld(c *echo.Context) error
- func (h *Handler) Update(c *echo.Context) error
- type Part
- type PartKind
- type PartSync
- type Placement
- type PlacementFinder
- type Pos
- type Press
- type Preview
- type Recipe
- type RecipeInput
- type RecipePart
- type RecipeSummary
- type Registry
- type Repository
- type Rule
- type Run
- type RunStep
- type RunStore
- type RunView
- type Service
- type Viewer
- type When
Constants ¶
const ( SeeParty = "party" // everyone in the campaign SeeName = "name" // players see that it exists, not its details SeeDirector = "director" // Directors only )
Who sees a part. Set once per part in the Workshop.
const ( TimingNow = "now" TimingEncounterEnd = "encounter_end" TimingHeld = "held" // waits in the Homebrew drawer for the Director )
When a rule's actions run.
const ( FieldWhole = "whole" // a whole number FieldText = "text" // one line of text )
Field types the inspector can draw.
const ( PreviewShape = "shape" // a part kind whose data is a trackers shape PreviewReach = "reach" // the part's number moves onto or past Arg PreviewAdd = "add" // add Arg (or 1) to the part's number PreviewSet = "set" // set the part's number to Arg )
Preview ops. An event can only reach; an action can only add or set.
const ( RunDone = "done" RunHeld = "held" // waits in the Homebrew drawer for the Director RunFailed = "failed" RunDismissed = "dismissed" RunStopped = "stopped" // rules kept setting each other off RunRunning = "running" // claimed by a Director pressing Run )
What happened to a run.
const ( MaxNameChars = 80 MaxTestChars = 200 MaxParts = 60 MaxRules = 60 MaxThen = 12 MaxRecipes = 200 MaxDataBytes = 16 << 10 MaxArgsBytes = 4 << 10 MaxBodyBytes = 512 << 10 MaxIDChars = 64 MaxCanvas = 20000 // how far from the origin a card may sit )
Limits. They keep one recipe a size the Workshop canvas can show and a request can carry.
const PluginSlug = "homebrew"
PluginSlug names this plugin in the registry and the health/migration bookkeeping.
Variables ¶
var MigrationsFS embed.FS
MigrationsFS contains the embedded SQL migration files.
var StaticAssetsFS embed.FS
StaticAssetsFS holds the Workshop widget and its styles, served at /static/plugins/homebrew/.
Functions ¶
func RegisterRoutes ¶
func RegisterRoutes(e *echo.Echo, h *Handler, campaignSvc campaigns.CampaignService, authSvc auth.AuthService)
RegisterRoutes mounts the routes under the campaign group. Everything here is the Director's, which the service checks (a role middleware cannot express the co-Director grant). CSRF is enforced by the global middleware.
Types ¶
type AI ¶
type AI struct {
// contains filtered or unexported fields
}
AI is the Workshop's part of the AI engine (aiplug, ADR-071): the `workshop` lookup, which shows the recipes, what a recipe can be built from and the Homebrew drawer, and the `workshop-recipe` block an AI asks to add, change or remove a recipe with. Both go through the Service, so they are the Director's only, as the Workshop is, and a request changes nothing until the owner approves it on the review page.
func (*AI) Answer ¶
func (k *AI) Answer(ctx context.Context, campaignID string, a aiplug.Actor, _ aiplug.Record) (string, string, error)
Answer reads the recipes, the catalog and the drawer as the Director.
func (*AI) LookupInfo ¶
func (*AI) LookupInfo() aiplug.LookupInfo
type Act ¶
type Act struct {
RecipeID string
// Part is the part the action is on; nil when it takes none.
Part *Part
Args json.RawMessage
// Scope is the page the event happened on; "" when it came from no page
// (a respite), which an owner may read as "everywhere it applies".
Scope string
// By is the Director whose change or press set the rule off.
By Viewer
// Note names the rule, for the line the owner writes in its own log.
Note string
}
Act is one action a rule is carrying out.
type ActionDef ¶
type ActionDef struct {
Name string // e.g. "step"
Label string
// PartKinds are the kinds of part it acts on; empty means it takes no part.
PartKinds []string
// ClientRun actions run in the Director's browser, through a handler a
// system package registered there, only when the Director presses Run.
// The server records and validates them but never runs them.
ClientRun bool
Fields []Field
// Preview lets the live preview act the action out; nil makes it a
// line saying what would happen.
Preview *Preview
Check CheckFunc
// Run carries the action out on the server and says in one line what
// changed. Without it, and always for a ClientRun action, a rule that
// uses the action waits in the Homebrew drawer for the Director.
Run func(ctx context.Context, campaignID string, a Act) (string, error)
// Waits marks an action that is the Director's call even though the
// server can run it: a rule that uses it is held in the drawer however
// it is timed, and Run does it when the Director presses Run. It is for
// anything bigger than a number moving, which runs at once.
Waits bool
}
ActionDef is something a rule can do.
type Catalog ¶
type Catalog struct {
Parts []CatalogEntry `json:"parts"`
Events []CatalogEntry `json:"events"`
Actions []CatalogEntry `json:"actions"`
}
Catalog is what the Workshop's palette and When/Then lists offer.
type CatalogEntry ¶
type CatalogEntry struct {
Name string `json:"name"`
Label string `json:"label"`
ForceSee string `json:"forceSee,omitempty"`
Widget string `json:"widget,omitempty"`
PartKinds []string `json:"partKinds,omitempty"`
ClientRun bool `json:"clientRun,omitempty"`
// Waits is an action's Waits, so the editor can say a rule that ends in it
// is held for the Director however it is timed.
Waits bool `json:"waits,omitempty"`
Fields []Field `json:"fields,omitempty"`
// PressArg is an event's PressArg, so the Workshop can name the drawer
// button a When stands for.
PressArg string `json:"pressArg,omitempty"`
// Preview is a part kind's PreviewShape, or an event's or action's op.
Preview any `json:"preview,omitempty"`
}
CatalogEntry is one registered name, as the Workshop shows it.
type CheckFunc ¶
type CheckFunc func(ctx context.Context, campaignID string, raw json.RawMessage) (json.RawMessage, error)
CheckFunc validates one owner's JSON and returns it normalised. Its error is shown to the Director, so it says what to fix in plain words.
type Do ¶
type Do struct {
Action string `json:"action"`
Part string `json:"part,omitempty"`
Args json.RawMessage `json:"args,omitempty"`
}
Do is one action of a rule, on one part of the same recipe.
type Doc ¶
type Doc struct {
Parts []Part `json:"parts"`
Rules []Rule `json:"rules"`
// Layout is where each card sits on the Workshop canvas, keyed by card:
// "p:<part key>", "r<rule>" for a rule's When, "r<rule>.<step>" for one
// of its Thens (both counted from 0). Only the Workshop reads it.
Layout map[string]Pos `json:"layout,omitempty"`
}
Doc is a recipe's parts and the rules wiring them.
type Drawer ¶
type Drawer struct {
Presses []Press `json:"presses"`
Waiting []RunView `json:"waiting"`
Recent []RunView `json:"recent"`
}
Drawer is the Homebrew drawer's rule sections.
type EventDef ¶
type EventDef struct {
Name string // e.g. "tracker.reached"
Label string
// PartKinds are the kinds of part that fire it; empty means it comes from
// outside any part (a moon changing, a respite).
PartKinds []string
Fields []Field // the args the Workshop asks for; nil when there are none
// Preview lets the live preview act the event out; nil makes it a
// button the Director presses to say it happened.
Preview *Preview
Check CheckFunc
// Press, when set, is the label of a drawer button the Director uses to
// say the event happened ("Log a respite"). Only an event from no part
// can have one.
Press string
// Pressed, when set, is the owner's own record of a press, run before
// any rule (Crafting gives every project a roll for a respite). An
// error stops the press before anything else happens.
Pressed func(ctx context.Context, campaignID string, by Viewer) error
// PressArg, when set, names one of the event's text Fields: each distinct
// value of it among the saved rules is a drawer button of its own,
// labelled with that value ("Dose"), so the Director names a button by
// writing a rule. A press fires the event with {"<PressArg>": value}.
// It needs a Match, or every button would start every rule; only an
// event from no part can have it, and not with Press or Pressed.
PressArg string
// Match, when set, says whether a firing meets a rule's stored args,
// given what happened (the payload the owner fired it with). Nil
// matches every firing.
Match func(args, payload json.RawMessage) (bool, error)
}
EventDef is something that can start a rule.
type Field ¶
type Field struct {
Name string `json:"name"` // the arg's JSON key
Label string `json:"label"` // what the inspector calls it
Type string `json:"type"` // FieldWhole or FieldText
// Optional fields start empty on a new When or Then, where a whole
// number otherwise starts at 1.
Optional bool `json:"optional,omitempty"`
}
Field is one arg of an event or action, as the Workshop's inspector asks for it. The owner's Check still has the last word on its value.
type Fired ¶
type Fired struct {
Event string
// RecipeID limits the firing to one recipe's rules, for an event a
// recipe's own part fires; "" reaches every recipe (a respite).
RecipeID string
// Part is the part key it came from; "" for an event from no part.
Part string
// Scope is the page it happened on; "" for none.
Scope string
// Payload is what happened, for the event's Match.
Payload json.RawMessage
// Once is for an event a player can set off as often as they like: a rule
// it would hold is not held again while the same rule already waits for
// the same scope and payload. The Director answers it once.
Once bool
}
Fired is an event an owner says happened.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler serves the Workshop routes.
func (*Handler) Page ¶
Page handles GET /campaigns/:id/homebrew: the Workshop itself. The catalog call is the service's Director check, so the page and its API keep everyone else out by the same rule.
func (*Handler) Placements ¶
Placements handles GET /campaigns/:id/homebrew/recipes/:rid/placements.
type Part ¶
type Part struct {
Key string `json:"key"`
Kind string `json:"kind"`
Label string `json:"label"`
See string `json:"see"`
Data json.RawMessage `json:"data,omitempty"`
}
Part is one widget or owned record in a recipe. Data belongs to the kind's owner, which validates it; this package only checks the envelope.
type PartKind ¶
type PartKind struct {
Name string // e.g. "tracker"; what a part's kind field holds
Label string // what the Workshop's palette calls it
// ForceSee, when set, is the only audience the kind allows (a creature
// recipe is always the Director's).
ForceSee string
// Preview, when PreviewShape, says data is a trackers shape, so the
// Workshop's live preview can show the part as a number it can change.
Preview string
// Widget, when set, names a widget the owner registered in the browser
// that draws and edits the part's data, so the Workshop shows the
// owner's own editor without knowing what it holds. See the
// plugin's .ai.md, "Owner-drawn parts".
Widget string
Check CheckFunc
// Sync, when set, keeps records the owner holds for this kind's parts
// in step with the recipes that hold them.
Sync *PartSync
}
PartKind is a kind of part, registered by the plugin that owns its data.
type PartSync ¶
type PartSync struct {
// Check says, without writing, whether Apply would refuse these parts.
// Its error is shown to the Director.
Check func(ctx context.Context, campaignID, recipeID string, parts []Part) error
// Apply makes the owner's records match. It runs after the recipe is
// stored.
Apply func(ctx context.Context, campaignID, recipeID string, parts []Part) error
}
PartSync keeps an owner's own records in step with a kind's parts: a tracker part is saved as a tracker kind. Each call gets every part of the kind in one recipe, none when the recipe is deleted, so a call is a whole picture and a repeat changes nothing.
type Placement ¶
Placement is one place a recipe shows up. Where is "category" for a category whose every page shows it, placed with the Customizer.
type PlacementFinder ¶
type PlacementFinder interface {
Placements(ctx context.Context, campaignID, recipeID string) ([]Placement, error)
}
PlacementFinder reads where page layouts place a recipe. It is implemented in internal/app over the entities plugin, which owns the layouts.
type Press ¶
type Press struct {
Event string `json:"event"`
Label string `json:"label"`
// Button is a named button's name, sent back with the press; Arg is the
// event's field that holds it. Both are empty for a registered press.
Button string `json:"button,omitempty"`
Arg string `json:"arg,omitempty"`
// Rules counts the saved rules it would start.
Rules int `json:"rules"`
}
Press is an event the Director sets off from the drawer.
type Preview ¶
type Preview struct {
Op string `json:"op"`
// Arg is the whole-number field holding the amount; empty means 1 for
// PreviewAdd. PreviewReach and PreviewSet need one.
Arg string `json:"arg,omitempty"`
// Every, for PreviewReach only, is an optional whole-number field that
// repeats the value: reach 3, again every 3, fires at 3, 6, 9 and on.
Every string `json:"every,omitempty"`
}
Preview says how the Workshop's live preview acts out an event or action on a part's number, so a recipe can be tried without the server and without the preview knowing any owner by name.
type Recipe ¶
type Recipe struct {
ID string `json:"id"`
CampaignID string `json:"-"`
Name string `json:"name"`
Doc Doc `json:"doc"`
Version int `json:"version"`
CreatedBy string `json:"-"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
Recipe is one saved recipe. Doc is the part the Workshop edits.
type RecipeInput ¶
type RecipeInput struct {
Name string `json:"name"`
Doc Doc `json:"doc"`
Version int `json:"version"`
}
RecipeInput is a create or update body. Version is required on update and a mismatch is a conflict.
type RecipePart ¶
RecipePart is one part of a saved recipe, with the recipe it belongs to.
type RecipeSummary ¶
type RecipeSummary struct {
ID string `json:"id"`
Name string `json:"name"`
Parts int `json:"parts"`
Rules int `json:"rules"`
// PartKinds are the kinds of part the recipe has, sorted, so a caller
// can tell what a recipe can put on a page without loading it.
PartKinds []string `json:"partKinds"`
Version int `json:"version"`
UpdatedAt time.Time `json:"updatedAt"`
}
RecipeSummary is a recipe in a list: enough to show and open it.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds what other plugins have plugged into the Workshop. It is filled once while the app starts and read for every request after.
func (*Registry) Catalog ¶
Catalog lists everything registered, sorted by name so the palette is stable between restarts.
func (*Registry) RegisterAction ¶
RegisterAction adds an action.
func (*Registry) RegisterEvent ¶
RegisterEvent adds an event.
func (*Registry) RegisterPart ¶
RegisterPart adds a kind of part. A second registration of a name is an error: two owners for one kind would make validation ambiguous.
type Repository ¶
type Repository interface {
List(ctx context.Context, campaignID string) ([]Recipe, error)
// ListContaining lists the recipes whose stored document holds text, so
// a caller looking for a rare setting skips decoding every recipe.
ListContaining(ctx context.Context, campaignID, text string) ([]Recipe, error)
Get(ctx context.Context, campaignID, id string) (Recipe, bool, error)
Count(ctx context.Context, campaignID string) (int, error)
Create(ctx context.Context, r Recipe) error
// Update writes r only if the stored version is still expected, and
// reports whether it did.
Update(ctx context.Context, r Recipe, expected int) (bool, error)
Delete(ctx context.Context, campaignID, id string) (bool, error)
}
Repository is the persistence boundary for recipes.
func NewRepository ¶
func NewRepository(db *sql.DB) Repository
NewRepository returns the MariaDB repository.
type Run ¶
type Run struct {
ID string
CampaignID string
RecipeID string
RecipeName string
What string
// Rule is the rule as it was when its event fired.
Rule Rule
Scope string
Payload json.RawMessage
Status string
Lines []string
CreatedBy string
CreatedAt time.Time
DoneBy string
DoneAt *time.Time
}
Run is one rule set off by one firing, with what it did.
type RunStep ¶
type RunStep struct {
Action string `json:"action"`
Label string `json:"label"`
Part string `json:"part,omitempty"`
Args json.RawMessage `json:"args,omitempty"`
Browser bool `json:"browser,omitempty"`
// ByHand is an action no one can run for the Director.
ByHand bool `json:"byHand,omitempty"`
}
RunStep is one action of a held run.
type RunStore ¶
type RunStore interface {
CreateRun(ctx context.Context, r Run) error
GetRun(ctx context.Context, campaignID, id string) (Run, bool, error)
// FinishRun writes a run's new status, lines and who finished it, only
// while its stored status is still from, and reports whether it did.
FinishRun(ctx context.Context, r Run, from string) (bool, error)
// ListRuns lists held runs, or finished ones, newest first.
ListRuns(ctx context.Context, campaignID string, held bool, limit int) ([]Run, error)
// PruneRuns deletes all but the newest keep finished runs.
PruneRuns(ctx context.Context, campaignID string, keep int) error
}
RunStore keeps runs.
func NewRunStore ¶
NewRunStore returns the MariaDB store of rule runs.
type RunView ¶
type RunView struct {
ID string `json:"id"`
Recipe string `json:"recipe"`
What string `json:"what"`
Status string `json:"status"`
Lines []string `json:"lines"`
At time.Time `json:"at"`
// Waits says why a held run is waiting.
Waits string `json:"waits,omitempty"`
// Steps are a held run's actions, so the drawer can say what Run will
// do and run the browser ones itself.
Steps []RunStep `json:"steps,omitempty"`
// RecipeID and Scope tell a held run's browser steps which recipe they
// belong to and the page the event happened on, so an owner's browser
// action can read its part and act on that page.
RecipeID string `json:"recipeId,omitempty"`
Scope string `json:"scope,omitempty"`
}
RunView is a run as the drawer shows it.
type Service ¶
type Service interface {
Catalog(v Viewer) (Catalog, error)
List(ctx context.Context, campaignID string, v Viewer) ([]RecipeSummary, error)
Get(ctx context.Context, campaignID, id string, v Viewer) (*Recipe, error)
Create(ctx context.Context, campaignID string, v Viewer, in RecipeInput) (*Recipe, error)
Update(ctx context.Context, campaignID, id string, v Viewer, in RecipeInput) (*Recipe, error)
Delete(ctx context.Context, campaignID, id string, v Viewer) error
// Check says, without writing, whether Create (id "") or Update (the
// recipe's id) would take in: the same checks, and every owner's word
// on its parts. The AI review shows its answer before the owner approves.
Check(ctx context.Context, campaignID, id string, v Viewer, in RecipeInput) error
// Placements lists where the Customizer has placed a recipe.
Placements(ctx context.Context, campaignID, id string, v Viewer) ([]Placement, error)
// PartsOfKind lists saved recipes' parts of the given kinds whose data
// holds dataHas (any data when ""), so an owner can find the parts that
// point at one of its records without reading every recipe.
PartsOfKind(ctx context.Context, campaignID string, v Viewer, dataHas string, kinds ...string) ([]RecipePart, error)
// owner makes for one change stop together after the same 50 runs.
ShareBudget(ctx context.Context) context.Context
// Fire starts the saved rules an event meets (run.go). Owners call it
// once they know the change was a Director's.
Fire(ctx context.Context, campaignID string, v Viewer, f Fired) error
// Drawer is what the Homebrew drawer shows about rules.
Drawer(ctx context.Context, campaignID string, v Viewer) (*Drawer, error)
// Press fires an event the drawer offers as a button; button is a named
// button's name, "" for a registered press.
Press(ctx context.Context, campaignID, event, button string, v Viewer) (*Drawer, error)
// RunHeld runs a waiting rule now; Dismiss sets it aside.
RunHeld(ctx context.Context, campaignID, id string, v Viewer) (*RunView, error)
Dismiss(ctx context.Context, campaignID, id string, v Viewer) (*RunView, error)
}
Service is the Workshop's recipe store. Every method is for Directors: players meet a recipe only through the widgets it places, which keep their own audience rules.
func NewService ¶
func NewService(repo Repository, reg *Registry, places PlacementFinder, runs RunStore) Service
NewService builds the service over a repository and the registry the app filled at start. places may be nil when nothing can place a recipe; a recipe is then on no page. runs may be nil, and rules then never run.