fieldrules

package
v0.0.0-...-95891f2 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: AGPL-3.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
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.

View Source
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

View Source
var MigrationsFS embed.FS

MigrationsFS contains the embedded SQL migrations.

Functions

func ChooseWords

func ChooseWords(words []LocalWord, places []string, eraID int, director bool) map[string]string

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.

func Rename

func Rename(words map[string]string, text string) (string, bool)

Rename returns the local name for text when one applies. A plural made by adding "s" ("Marks") finds the singular word too, since prices are often written that way; the local word is used as the owner wrote it.

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 NewHandler

func NewHandler(svc Service) *Handler

NewHandler creates the handler.

func (*Handler) DeleteRule

func (h *Handler) DeleteRule(c *echo.Context) error

DeleteRule removes a rule. Fields that followed it keep any value typed by hand and otherwise show nothing.

func (*Handler) DeleteWord

func (h *Handler) DeleteWord(c *echo.Context) error

DeleteWord removes a local word.

func (*Handler) ListRules

func (h *Handler) ListRules(c *echo.Context) error

ListRules returns every rule.

func (*Handler) ListWords

func (h *Handler) ListWords(c *echo.Context) error

ListWords returns every local word.

func (*Handler) SaveRule

func (h *Handler) SaveRule(c *echo.Context) error

SaveRule creates (POST) or replaces (PUT) a rule.

func (*Handler) SaveWord

func (h *Handler) SaveWord(c *echo.Context) error

SaveWord creates (POST) or replaces (PUT) 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

type MoonNow struct {
	Day   int
	Words []string
}

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

func CleanRule(in RuleInput) (Rule, error)

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

func (r *Rule) Gives(value any) string

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.

func (*Rule) Needs

func (r *Rule) Needs() (calendar, trackers bool)

Needs says which facts a rule reads besides fields, so a page asks only for what its rules use.

func (*Rule) Result

func (r *Rule) Result(data map[string]any, sit Situation) string

Result is what the rule gives for a page: the first row that matches what the rule follows and whose conditions hold, then each adjustment whose tracker test holds. Pure, like Gives.

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

type Viewer struct {
	UserID string
	Role   int
}

Viewer is who is asking. Role is the campaign visibility role, so a granted co-Director counts as the Director.

func ViewerOf

func ViewerOf(cc *campaigns.CampaignContext, userID string) Viewer

ViewerOf is who is asking, by visibility role so a granted co-Director counts as the Director.

func (Viewer) IsDM

func (v Viewer) IsDM() bool

IsDM is true for the Director and a granted co-Director: the only people who change rules and words or see a word kept DM only.

type WordInput

type WordInput struct {
	ID      int
	Word    string
	Local   string
	PlaceID string
	EraID   int
	DMOnly  bool
}

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.

Jump to

Keyboard shortcuts

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