Documentation
¶
Overview ¶
Package fieldrules holds two homebrew tools a campaign builds for itself in the system book's Homebrew tools: worked-out rules (a field's value follows another field through a table, such as a guild rank from echelon) and local words (what a place or an era calls a coin, a rank or any other word).
The rules and words are the campaign's data, never a game system's, so they survive a package update and work under any system. Pages ask this plugin what a rule gives and which local words apply; deciding who sees a page is theirs. The service decides who may change a rule or a word and which words a viewer is shown; the handler adds no rule of its own.
Index ¶
- Constants
- Variables
- func ChooseWords(words []LocalWord, places []string, eraID int, director bool) map[string]string
- func RegisterRoutes(e *echo.Echo, h *Handler, campaignSvc campaigns.CampaignService, ...)
- func Rename(words map[string]string, text string) (string, bool)
- type Handler
- func (h *Handler) DeleteRule(c *echo.Context) error
- func (h *Handler) DeleteWord(c *echo.Context) error
- func (h *Handler) ListRules(c *echo.Context) error
- func (h *Handler) ListWords(c *echo.Context) error
- func (h *Handler) SaveRule(c *echo.Context) error
- func (h *Handler) SaveWord(c *echo.Context) error
- type LocalWord
- type MoonNow
- type Repository
- type Rule
- type RuleAdjust
- type RuleInput
- type RuleRow
- type Service
- type Situation
- type Viewer
- type WordInput
- type World
Constants ¶
const ( MaxRules = 100 MaxRuleRows = 60 MaxWords = 500 MaxNameLen = 100 MaxGivesLen = 100 MaxRuleAdjust = 10 )
Limits keep a campaign's tools small enough to evaluate on every page view.
const ( // FollowMoonPrefix starts "moon:<moon name>": the moon's phase on the // campaign calendar's current date. FollowMoonPrefix = "moon:" // FollowTrackerPrefix starts "tracker:<tracker name>": the value of a // tracker on the same page. FollowTrackerPrefix = "tracker:" // FollowTimeOfDay follows the calendar's time of day: dawn, day, dusk or // night. FollowTimeOfDay = "time of day" )
What a rule can follow besides a field on the same page. A field key never holds a colon or a space, so these never collide with one.
const PluginSlug = "fieldrules"
PluginSlug names this plugin in the migration bookkeeping and the health registry, so a failed migration here leaves only these tools out.
Variables ¶
var MigrationsFS embed.FS
MigrationsFS contains the embedded SQL migrations.
Functions ¶
func ChooseWords ¶
ChooseWords picks, for each word, the local name a page sees: the nearest place in places (the page itself first, then up its parents) that names it, else the era. Words kept DM only count only for the Director. The result is keyed by the lower-cased usual word.
func RegisterRoutes ¶
func RegisterRoutes(e *echo.Echo, h *Handler, campaignSvc campaigns.CampaignService, authSvc auth.AuthService)
RegisterRoutes mounts the API under /campaigns/:id/homebrew.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler serves the homebrew tools' JSON. It binds and calls the service; who may read or change what is the service's.
func (*Handler) DeleteRule ¶
DeleteRule removes a rule. Fields that followed it keep any value typed by hand and otherwise show nothing.
func (*Handler) DeleteWord ¶
DeleteWord removes a local word.
type LocalWord ¶
type LocalWord struct {
ID int `json:"id"`
Word string `json:"word"`
Local string `json:"local"`
PlaceID string `json:"placeId,omitempty"`
PlaceName string `json:"placeName,omitempty"`
EraID int `json:"eraId,omitempty"`
EraName string `json:"eraName,omitempty"`
DMOnly bool `json:"dmOnly"`
}
LocalWord is what one place or one era calls a word. Exactly one of PlaceID and EraID is set.
type MoonNow ¶
MoonNow is one moon on the current date. Day is the day of its cycle, the new moon's day being 1, or 0 for a moon set by hand; Words are the phase words it matches today ("Waning Crescent", "waning", "crescent").
type Repository ¶
type Repository interface {
ListRules(ctx context.Context, campaignID string) ([]Rule, error)
GetRule(ctx context.Context, campaignID string, id int) (*Rule, error)
CountRules(ctx context.Context, campaignID string) (int, error)
InsertRule(ctx context.Context, campaignID string, r Rule) (int, error)
UpdateRule(ctx context.Context, campaignID string, r Rule) (bool, error)
DeleteRule(ctx context.Context, campaignID string, id int) (bool, error)
ListWords(ctx context.Context, campaignID string) ([]LocalWord, error)
CountWords(ctx context.Context, campaignID string) (int, error)
InsertWord(ctx context.Context, campaignID string, w LocalWord) (int, error)
UpdateWord(ctx context.Context, campaignID string, w LocalWord) (bool, error)
DeleteWord(ctx context.Context, campaignID string, id int) (bool, error)
}
Repository owns the field_rules and local_words tables. It decides nothing about who may do what.
func NewRepository ¶
func NewRepository(db *sql.DB) Repository
NewRepository creates the repository.
type Rule ¶
type Rule struct {
ID int `json:"id"`
CampaignID string `json:"-"`
Name string `json:"name"`
Follows string `json:"follows"` // the followed field's key
Rows []RuleRow `json:"rows"`
// Adjust changes a number the rows gave, each step whose test holds in
// order: "1 lower while Hunger is 1 or more, never below 1".
Adjust []RuleAdjust `json:"adjust,omitempty"`
UpdatedAt time.Time `json:"updatedAt"`
}
Rule works out a field's value from another field: the first row that matches the followed field's value gives the result.
func CleanRule ¶
CleanRule checks a rule and returns it trimmed, the way it would be saved. It reads nothing, so a rule kept elsewhere (a Workshop recipe) is held to the same limits.
func (*Rule) Gives ¶
Gives is what a rule gives for the followed field's value: the first row that matches, or "" when none does or the value is blank. Pure, so the matching rules are pinned by table tests.
type RuleAdjust ¶
type RuleAdjust struct {
Tracker string `json:"tracker"`
Op string `json:"op"` // gte, gt, lte, lt or eq
Value float64 `json:"value"`
Add float64 `json:"add"`
Min *float64 `json:"min,omitempty"`
Max *float64 `json:"max,omitempty"`
}
RuleAdjust adds Add to a numeric result while a tracker on the same page passes its test (Op against Value), then keeps the result within Min and Max. A tracker that can't be read fails the test.
type RuleInput ¶
type RuleInput struct {
ID int
Name string
Follows string
Rows []RuleRow
Adjust []RuleAdjust
}
RuleInput creates a rule (ID 0) or replaces one.
type RuleRow ¶
type RuleRow struct {
Is string `json:"is,omitempty"`
From *float64 `json:"from,omitempty"`
To *float64 `json:"to,omitempty"`
EventTag string `json:"eventTag,omitempty"`
Time string `json:"time,omitempty"`
Gives string `json:"gives"`
}
RuleRow matches one value (Is) or a range of numbers (From and/or To, both inclusive; one left out is open-ended), and only while its conditions hold: an event on today's date (EventTag) and the time of day (Time). A row with conditions and no value matches whatever the rule follows, so a row like "on a Bloodnight, 1" put first overrides the rest.
For a moon, Is names a phase ("Waning Crescent", or a broad word such as "full" or "crescent") and a range counts days of the cycle, the new moon's day being day 1. A moon's range may wrap, from 23 to 1.
type Service ¶
type Service interface {
Rules(ctx context.Context, campaignID string, v Viewer) ([]Rule, error)
SaveRule(ctx context.Context, campaignID string, in RuleInput, v Viewer) (*Rule, error)
DeleteRule(ctx context.Context, campaignID string, id int, v Viewer) error
// RuleNamed finds a rule by name, case-insensitively; nil when none.
RuleNamed(ctx context.Context, campaignID, name string) (*Rule, error)
// RuleExists reports whether the campaign has that rule.
RuleExists(ctx context.Context, campaignID string, id int) bool
// CheckRule and CheckWord are SaveRule's and SaveWord's checks without
// the write, for a review screen.
CheckRule(in RuleInput) error
CheckWord(ctx context.Context, campaignID string, in WordInput, v Viewer) error
// Eras is every era on the campaign's calendar by id, hidden ones too.
Eras(ctx context.Context, campaignID string, v Viewer) map[int]string
Words(ctx context.Context, campaignID string, v Viewer) ([]LocalWord, error)
SaveWord(ctx context.Context, campaignID string, in WordInput, v Viewer) (*LocalWord, error)
DeleteWord(ctx context.Context, campaignID string, id int, v Viewer) error
// Evaluate gives, per rule id, what the rule gives on a page for this
// viewer: from the page's field values, and the calendar and the page's
// trackers as the viewer may see them, so a result never rests on a fact
// hidden from them. A rule that is gone or matches nothing gives "".
Evaluate(ctx context.Context, campaignID, entityID string, ruleIDs []int, data map[string]any, v Viewer) map[int]string
// WordsFor is the local words a viewer sees on one page, keyed by the
// lower-cased usual word (see Rename).
WordsFor(ctx context.Context, campaignID, entityID string, v Viewer) map[string]string
}
Service is the homebrew rules and local words. Only the Director changes them; pages read what a rule gives and which words apply.
func NewService ¶
func NewService(repo Repository, world World) Service
NewService creates the service.
type Situation ¶
type Situation struct {
// Moons, keyed by lower-cased moon name.
Moons map[string]MoonNow
// EventTags are the lower-cased names of today's events and their kinds.
EventTags map[string]bool
// DayPart is dawn, day, dusk or night; "" when the calendar can't say.
DayPart string
// Trackers are the page's trackers by lower-cased name.
Trackers map[string]float64
}
Situation is what a rule may read besides the page's fields. Nil maps are facts that could not be read, and a test on a missing fact does not hold.
type Viewer ¶
Viewer is who is asking. Role is the campaign visibility role, so a granted co-Director counts as the Director.
type WordInput ¶
WordInput creates a local word (ID 0) or replaces one. Exactly one of PlaceID and EraID is set.
type World ¶
type World interface {
// PlaceName is the name of a page of the campaign, and whether it exists.
PlaceName(ctx context.Context, campaignID, entityID string) (string, bool)
// EraNames lists the eras of the campaign's main calendar by id.
EraNames(ctx context.Context, campaignID string, v Viewer) map[int]string
// PlaceChain is the page followed by its parents, nearest first. It is
// not narrowed to the viewer: it only decides which words apply.
PlaceChain(ctx context.Context, campaignID, entityID string) []string
// CurrentEra is the era the main calendar's date is in for this viewer,
// or 0 (none, or one still hidden from them).
CurrentEra(ctx context.Context, campaignID string, v Viewer) int
// Today is the main calendar's current date as this viewer may see it:
// its moons, today's events and the time of day. Trackers is left nil.
// A calendar that can't be read leaves the maps nil.
Today(ctx context.Context, campaignID string, v Viewer) Situation
// PageTrackers is the trackers on one page this viewer may see, by
// lower-cased name; nil when they can't be read.
PageTrackers(ctx context.Context, campaignID, entityID string, v Viewer) map[string]float64
}
World answers what this plugin needs to know about pages and eras, which belong to other plugins. It is implemented in internal/app.