Documentation
¶
Overview ¶
Package trackers keeps a campaign's trackers and clocks: kinds its Directors define, placed on pages, each with a dated log whose sum is the tracker's value.
Nothing here knows any game system or campaign: a kind is data (a number, named steps or a clock, with bands), and the controls are always this plugin's own. Hidden trackers and their logs never reach players.
The browser HTTP surface for trackers. Handlers bind, call the service and shape the response; the service owns every rule, so a mis-wired route cannot widen access.
Hand-written SQL for tracker kinds, trackers and their log.
SECURITY: every statement filters on campaign_id, so an id from another campaign never reads or changes anything here.
Route registration for trackers.
Index ¶
- Constants
- Variables
- func CheckKind(in KindInput) error
- func RegisterRoutes(e *echo.Echo, h *Handler, campaignSvc campaigns.CampaignService, ...)
- type Band
- type Change
- type ChangeListener
- type ChangeObserver
- type EntityDirectory
- type Entry
- type EntryView
- type GameClock
- type GameDate
- type Handler
- func (h *Handler) CreateKind(c *echo.Context) error
- func (h *Handler) DeleteKind(c *echo.Context) error
- func (h *Handler) ListKinds(c *echo.Context) error
- func (h *Handler) Log(c *echo.Context) error
- func (h *Handler) Page(c *echo.Context) error
- func (h *Handler) Pages(c *echo.Context) error
- func (h *Handler) Patch(c *echo.Context) error
- func (h *Handler) Place(c *echo.Context) error
- func (h *Handler) Remove(c *echo.Context) error
- func (h *Handler) SetPlacementChecker(p campaigns.BlockPlacementChecker)
- func (h *Handler) Strike(c *echo.Context) error
- func (h *Handler) UpdateKind(c *echo.Context) error
- func (h *Handler) WorldClocksBlock(c *echo.Context) error
- type Kind
- type KindChoice
- type KindInput
- type LogInput
- type Member
- type MemberChoice
- type MemberDirectory
- type OwnedKind
- type PageAccess
- type PageRef
- type PageView
- type PlaceInput
- type Player
- type PlayerPick
- type PlayerView
- type Repository
- type Service
- type Shape
- type SharedTracker
- type Step
- type Tracker
- type TrackerPatch
- type TrackerView
- type TypeRef
- type Viewer
Constants ¶
const ( // ShapeCount is a number, optionally bounded, with bands that say what // each level means. ShapeCount = "count" // ShapeSteps is a list of named steps; the value is the index of the // current one. ShapeSteps = "steps" // ShapeClock is a countdown drawn as a circle of segments. ShapeClock = "clock" )
Shapes a kind can take.
const ( SeeDirectors = "directors" SeeEveryone = "everyone" // SeePlayers shows the tracker to the Directors and to the players named // in Tracker.Players, and to nobody else. SeePlayers = "players" )
Who sees a tracker. A kind's default and a newly placed tracker use only the first two; "players" is chosen per placed tracker, with its list.
const ( OpAdd = "add" OpSet = "set" )
Log operations. A step forward or back is an add of +1 or -1.
const ( SourceHand = "hand" SourceRule = "rule" )
Where a log line came from. Only "hand" is written today; the others are reserved for the rules, item use and AI Import that write the same lines.
const ( MaxNameChars = 80 MaxTextChars = 300 MaxNoteChars = 200 MaxSteps = 20 MinSteps = 2 MaxBands = 12 MinSegments = 2 MaxSegments = 24 MaxKindsPerCamp = 200 MaxTrackersPage = 40 MaxPlayersChosen = 200 MaxEntries = 10000 MaxAbsValue = 1000000 MaxIDChars = 64 LogShown = 100 MaxBodyBytes = 64 << 10 )
Limits. They bound what a Director can store and keep the block's layout sane; the widget enforces the same numbers as a courtesy.
const BlockType = "trackers"
BlockType is the page layout block that shows a page's trackers.
const MaxOwnerChars = 160
MaxOwnerChars bounds an owner key; the column holds this many.
const PluginSlug = "trackers"
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 block widget and its styles, served at /static/plugins/trackers/.
Functions ¶
func CheckKind ¶
CheckKind says whether a kind's settings would be accepted, without saving anything, so a review can refuse a bad one before commit.
func RegisterRoutes ¶
func RegisterRoutes(e *echo.Echo, h *Handler, campaignSvc campaigns.CampaignService, authSvc auth.AuthService)
RegisterRoutes mounts the routes under the campaign group. Reading a page's trackers follows the page: anyone who can view it may read, and the service hides what the viewer must not see. Every write, and everything about kinds, needs a Director, which the service checks (a role middleware cannot express the co-Director grant). CSRF is enforced by the global middleware.
Types ¶
type Change ¶
Change is a tracker moving from one value to another. Owner is "" for a hand-made kind; KindName lets a recipe part linked to that kind hear it.
type ChangeListener ¶
type ChangeListener interface {
TrackerChanged(ctx context.Context, campaignID string, v Viewer, c Change) error
}
ChangeListener hears that a tracker moved, after the change is saved, so a recipe that owns or links its kind can act on it (its rules run).
type ChangeObserver ¶
type ChangeObserver interface {
TrackersChanged(campaignID string)
}
ChangeObserver hears that something a tracker-reading rule may judge has changed on a campaign: a value, a placement, a kind's bounds or name. Unlike ChangeListener it is told for every kind and carries nothing but the campaign, so the observer must look again rather than trust the event. It is told after the change is saved and must not block.
type EntityDirectory ¶
type EntityDirectory interface {
// Access answers what the viewer may do with a page, using the page's own
// rules. A page that is missing or in another campaign has Exists false.
Access(ctx context.Context, campaignID, entityID string, v Viewer) (PageAccess, error)
// Types lists the campaign's page categories, for "on every page of".
Types(ctx context.Context, campaignID string) ([]TypeRef, error)
// Names gives the names of the campaign's pages among ids; a page that
// is missing or trashed is left out.
Names(ctx context.Context, campaignID string, ids []string) (map[string]string, error)
}
EntityDirectory is owned by this plugin and implemented in internal/app over the entities service, so trackers never imports another plugin.
type Entry ¶
type Entry struct {
ID int64
TrackerID string
Op string
Amount int
Note string
Source string
Game *GameDate
CreatedBy string
CreatedAt time.Time
StruckAt *time.Time
}
Entry is one log line.
type EntryView ¶
type EntryView struct {
ID int64 `json:"id"`
Op string `json:"op"`
Amount int `json:"amount"`
Note string `json:"note"`
Source string `json:"source"`
// When is the in-game date's label, or "" when there was no calendar.
When string `json:"when"`
// At is the real time, for ordering and a tooltip.
At time.Time `json:"at"`
Struck bool `json:"struck"`
// By names who wrote a player's line: the player's name to a Director,
// "You" to the player. Empty for a Director's own lines and rule lines.
By string `json:"by,omitempty"`
// After is the value right after this line, so the log reads as a
// running balance.
After int `json:"after"`
}
EntryView is a log line as the widget shows it.
type GameClock ¶
type GameClock interface {
Today(ctx context.Context, campaignID string, v Viewer) (*GameDate, error)
}
GameClock reads today's in-game date, implemented in internal/app over the calendar service. It returns nil, nil when the campaign has no default calendar the viewer may see: a log line is still written, just undated.
type GameDate ¶
type GameDate struct {
Year int `json:"year"`
Month int `json:"month"`
Day int `json:"day"`
Label string `json:"label"`
}
GameDate is an in-game date from the campaign's default calendar.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler serves the tracker routes.
func (*Handler) CreateKind ¶
CreateKind handles POST /campaigns/:id/tracker-kinds.
func (*Handler) DeleteKind ¶
DeleteKind handles DELETE /campaigns/:id/tracker-kinds/:kid.
func (*Handler) Page ¶
Page handles GET /campaigns/:id/trackers/page/:eid; ?owner= limits it to the kinds owned under that prefix.
func (*Handler) SetPlacementChecker ¶
func (h *Handler) SetPlacementChecker(p campaigns.BlockPlacementChecker)
SetPlacementChecker wires the check a guest-facing block asks for a page placement.
func (*Handler) UpdateKind ¶
UpdateKind handles PUT /campaigns/:id/tracker-kinds/:kid.
func (*Handler) WorldClocksBlock ¶
WorldClocksBlock draws the block. A guest of a public campaign gets it only where the owner placed it on the Guests home; players and guests get an empty answer when there is nothing to show, and the owner a one-line hint. GET /campaigns/:id/world-clocks
type Kind ¶
type Kind struct {
ID string `json:"id"`
CampaignID string `json:"-"`
Name string `json:"name"`
Shape string `json:"shape"`
// Min and Max bound a count (nil is unbounded on that side). A clock's
// Max is its number of segments. Steps ignores both.
Min *int `json:"min"`
Max *int `json:"max"`
Steps []Step `json:"steps"`
Bands []Band `json:"bands"`
// Visibility is the default for trackers placed from this kind.
Visibility string `json:"visibility"`
// EveryTypeID puts this kind on every page of one category, so a
// per-hero tracker needs no setting up per page. 0 is none.
EveryTypeID int `json:"everyTypeId"`
Version int `json:"version"`
// Owner names the record another plugin keeps this kind in step with
// (a Homebrew Workshop recipe's part); "" for a kind made by hand. Such
// a kind is changed only through its owner.
Owner string `json:"-"`
// Managed tells the Director the kind is kept by its owner.
Managed bool `json:"managed"`
// InUse counts the trackers placed from this kind; filled for the
// kinds list only.
InUse int `json:"inUse"`
}
Kind is a campaign's own tracker definition.
type KindChoice ¶
type KindChoice struct {
ID string `json:"id"`
Name string `json:"name"`
Shape string `json:"shape"`
Visibility string `json:"visibility"`
}
KindChoice is a kind a Director can place on a page.
type KindInput ¶
type KindInput struct {
Name string `json:"name"`
Shape string `json:"shape"`
Min *int `json:"min"`
Max *int `json:"max"`
Steps []Step `json:"steps"`
Bands []Band `json:"bands"`
Visibility string `json:"visibility"`
EveryTypeID int `json:"everyTypeId"`
Version int `json:"version"`
}
KindInput is a create or update body for a kind. Version is required on update and a mismatch is a conflict.
type LogInput ¶
type LogInput struct {
TrackerID string `json:"trackerId"`
KindID string `json:"kindId"`
// Owner is the prefix of the owned view the change came from, which
// lets a first change create the row of a kind owned under it.
Owner string `json:"owner"`
Op string `json:"op"`
Amount int `json:"amount"`
Note string `json:"note"`
}
LogInput is one change. TrackerID names a placed tracker; a tracker a kind puts on every page and that has no row yet is named by KindID instead.
type Member ¶
type Member struct {
UserID string
Name string
// Character is the name of the member's own character page, or "".
Character string
// Director is the owner or a co-Director: someone who already sees every
// tracker and so cannot be chosen as a player.
Director bool
// JoinedAt is when the member joined; a player grant made before it is
// from an earlier stay and no longer counts.
JoinedAt time.Time
}
Member is one person in the campaign.
type MemberChoice ¶
type MemberChoice struct {
UserID string `json:"userId"`
Name string `json:"name"`
Character string `json:"character,omitempty"`
}
MemberChoice is a campaign player a Director can choose for a tracker.
type MemberDirectory ¶
type MemberDirectory interface {
Members(ctx context.Context, campaignID string) ([]Member, error)
}
MemberDirectory lists the campaign's members, implemented in internal/app over the campaigns service. It names the players a Director can choose and the people who wrote log lines.
type OwnedKind ¶
OwnedKind is a kind another plugin keeps in step with a record of its own: a Homebrew Workshop recipe's tracker or clock part. Owner is unique in the campaign and starts with the prefix the caller syncs under.
type PageAccess ¶
type PageAccess struct {
Exists bool
CanView bool
// TypeID is the page's category, which decides the kinds a page gets
// without anyone placing them.
TypeID int
}
PageAccess is one viewer's standing on one page.
type PageView ¶
type PageView struct {
Trackers []TrackerView `json:"trackers"`
CanEdit bool `json:"canEdit"`
// Kinds is filled for Directors only, for "Add a tracker".
Kinds []KindChoice `json:"kinds,omitempty"`
// Members is the players a Director can choose for a tracker.
Members []MemberChoice `json:"members,omitempty"`
}
PageView is everything the Trackers block needs for one page.
type PlaceInput ¶
type PlaceInput struct {
KindID string `json:"kindId"`
Name string `json:"name"`
Visibility string `json:"visibility"`
}
PlaceInput puts a kind on a page.
type Player ¶
type Player struct {
UserID string
// CanChange lets the player log add and set lines. It never lets them
// strike a line, rename, hide, remove or reshape the tracker.
CanChange bool
// GrantedAt is when the Director chose the player. A grant older than the
// member's joined_at belongs to an earlier stay and counts for nothing.
GrantedAt time.Time
}
Player is one player chosen to see a tracker.
type PlayerPick ¶
PlayerPick is one chosen player in a patch.
type PlayerView ¶
type PlayerView struct {
UserID string `json:"userId"`
Name string `json:"name"`
CanChange bool `json:"canChange"`
}
PlayerView is a chosen player as a Director sees them.
type Repository ¶
type Repository interface {
ListKinds(ctx context.Context, campaignID string) ([]Kind, error)
GetKind(ctx context.Context, campaignID, id string) (Kind, bool, error)
CountKinds(ctx context.Context, campaignID string) (int, error)
// CreateKind returns errDuplicate when the name is taken.
CreateKind(ctx context.Context, k Kind) error
// UpdateKind writes k if its stored version is still expected; ok is
// false on a lost race. errDuplicate when the new name is taken.
UpdateKind(ctx context.Context, k Kind, expected int) (ok bool, err error)
// DeleteKind removes an unused kind; inUse is true (and nothing is
// removed) while a tracker still uses it.
DeleteKind(ctx context.Context, campaignID, id string) (inUse bool, err error)
ListTrackers(ctx context.Context, campaignID, entityID string) ([]Tracker, error)
// ListAllTrackers lists every placed tracker in the campaign, by page.
ListAllTrackers(ctx context.Context, campaignID string) ([]Tracker, error)
GetTracker(ctx context.Context, campaignID, id string) (Tracker, bool, error)
TrackerForKind(ctx context.Context, campaignID, entityID, kindID string) (Tracker, bool, error)
// CreateTracker returns errDuplicate when the page already has a
// tracker of that name.
CreateTracker(ctx context.Context, t Tracker) error
// UpdateTracker saves name and visibility; errDuplicate on a name clash.
UpdateTracker(ctx context.Context, t Tracker) error
DeleteTracker(ctx context.Context, campaignID, id string) error
// SetPlayers replaces the players chosen for a tracker; an empty list
// clears them. Every tracker read returns them in Tracker.Players.
SetPlayers(ctx context.Context, campaignID, trackerID string, players []Player) error
// ListEntries returns each tracker's log, oldest first.
ListEntries(ctx context.Context, campaignID string, trackerIDs []string) (map[string][]Entry, error)
CountEntries(ctx context.Context, campaignID, trackerID string) (int, error)
AddEntry(ctx context.Context, campaignID string, e Entry) error
GetEntry(ctx context.Context, campaignID string, id int64) (Entry, bool, error)
// StrikeEntry strikes a line, or restores it when struck is false.
StrikeEntry(ctx context.Context, campaignID string, id int64, struck bool, userID string) error
}
Repository is the persistence boundary for trackers.
func NewRepository ¶
func NewRepository(db *sql.DB) Repository
NewRepository creates a MariaDB-backed repository.
type Service ¶
type Service interface {
// Page is every tracker on a page as v may see it, with the kinds a
// Director can add.
Page(ctx context.Context, campaignID, entityID string, v Viewer) (*PageView, error)
// OwnedPage is the trackers on a page from the kinds owned under prefix
// (one recipe's), with a virtual tracker for each such kind not placed
// yet: the block that shows them is their placement. It offers no kinds
// to add.
OwnedPage(ctx context.Context, campaignID, entityID, prefix string, v Viewer) (*PageView, error)
Place(ctx context.Context, campaignID, entityID string, v Viewer, in PlaceInput) (*TrackerView, error)
Patch(ctx context.Context, campaignID, trackerID string, v Viewer, in TrackerPatch) (*TrackerView, error)
Remove(ctx context.Context, campaignID, trackerID string, v Viewer) error
// Log writes one change on a tracker of the page and returns the
// tracker as it now stands.
Log(ctx context.Context, campaignID, entityID string, v Viewer, in LogInput) (*TrackerView, error)
// Strike strikes a log line, or restores it when struck is false.
Strike(ctx context.Context, campaignID string, entryID int64, struck bool, v Viewer) (*TrackerView, error)
Kinds(ctx context.Context, campaignID string, v Viewer) ([]Kind, []TypeRef, error)
CreateKind(ctx context.Context, campaignID string, v Viewer, in KindInput) (*Kind, error)
UpdateKind(ctx context.Context, campaignID, kindID string, v Viewer, in KindInput) (*Kind, error)
DeleteKind(ctx context.Context, campaignID, kindID string, v Viewer) error
// CheckOwned says, without writing, whether SyncOwned would refuse want.
CheckOwned(ctx context.Context, campaignID, prefix string, want []OwnedKind) error
// SyncOwned makes the kinds owned under prefix match want. Its caller
// has already decided the change is the Director's to make.
SyncOwned(ctx context.Context, campaignID, prefix string, want []OwnedKind) error
// LogOwned writes one change, marked as a rule's, on the trackers of
// the kind owned as owner: on entityID's page, creating its row if
// needed, or with entityID "" on every page that already has one.
LogOwned(ctx context.Context, campaignID, entityID, owner string, v Viewer, op string, amount int, note string) ([]TrackerView, error)
// LogKind writes one change, marked as a rule's, on the trackers of the
// hand-made kind called name (a recipe part linked to a page tracker): on
// entityID's page when it has one or the kind goes on every page of its
// category, or with entityID "" on every page that already has one. A
// page without it is left alone, since placing a hand-made kind is the
// Director's choice.
LogKind(ctx context.Context, campaignID, entityID, name string, v Viewer, op string, amount int, note string) ([]TrackerView, error)
// KindNamed finds a kind by name, ignoring case, for a recipe part that
// links to it.
KindNamed(ctx context.Context, campaignID, name string) (Kind, bool, error)
// player may open, for the campaign home's clocks block. It reads as a
// player whoever v is: a Director's hidden trackers and hidden pages
// never appear, so the owner sees what the table sees.
Shared(ctx context.Context, campaignID string, v Viewer) ([]SharedTracker, error)
// Pages lists the pages that have trackers, by name, for the DM
// Screen's Homebrew drawer.
Pages(ctx context.Context, campaignID string, v Viewer) ([]PageRef, error)
}
Service owns the tracker rules. Only Directors define kinds, place, rename, hide, strike or remove trackers, and see hidden ones. A player sees the trackers marked for everyone and those that name them, on pages they can open, without the log's notes except their own; a player named with "may change it" may also log add and set lines.
func NewService ¶
func NewService(repo Repository, entities EntityDirectory, members MemberDirectory, clock GameClock) Service
NewService builds the service. clock may be nil when the calendar plugin is unavailable; log lines are then undated. members may be nil, in which case no player can be chosen and no log line is named.
type Shape ¶
type Shape struct {
Shape string `json:"shape"`
Min *int `json:"min,omitempty"`
Max *int `json:"max,omitempty"`
Steps []Step `json:"steps,omitempty"`
Bands []Band `json:"bands,omitempty"`
}
Shape is a kind's rules without a name, a home or a default audience: what a Homebrew Workshop recipe part carries for a tracker or a clock.
func CheckShape ¶
CheckShape validates a shape by the same rules as a saved kind and returns it normalised, so a recipe can never hold a tracker the block would refuse.
type SharedTracker ¶
type SharedTracker struct {
// Bounded is true when the value has a top to measure against; Min is
// the bottom and Max the top (a clock's segments, a steps' last index).
SharedTracker is a shown-to-everyone tracker reduced to what a small ring needs. It carries no log, notes or kind id.
type Tracker ¶
type Tracker struct {
ID string
CampaignID string
EntityID string
KindID string
Name string
Visibility string
SortOrder int
// Players is who a SeePlayers tracker is shown to. The repository fills
// it on every read, so sees() cannot be asked without it.
Players []Player
}
Tracker is a kind placed on a page.
type TrackerPatch ¶
type TrackerPatch struct {
Name *string `json:"name"`
Visibility *string `json:"visibility"`
Players *[]PlayerPick `json:"players"`
}
TrackerPatch renames a tracker or changes who sees it; nil leaves a field. Players is a pointer to a slice so an absent key keeps the chosen players, while an empty list chooses nobody; it is read only when the visibility is (or becomes) "players".
type TrackerView ¶
type TrackerView struct {
// ID is "" for a tracker that a kind puts on every page of a category
// and that nobody has changed yet; the first change creates it.
ID string `json:"id"`
KindID string `json:"kindId"`
Name string `json:"name"`
Shape string `json:"shape"`
Visibility string `json:"visibility"`
Value int `json:"value"`
Min *int `json:"min"`
Max *int `json:"max"`
Steps []Step `json:"steps"`
Bands []Band `json:"bands"`
// Now is what the current value means: the step's or band's text.
Now string `json:"now"`
Log []EntryView `json:"log"`
LogCount int `json:"logCount"`
// CanChange is true for a Director and for a chosen player who may
// change this tracker; the widget draws its change controls from it.
CanChange bool `json:"canChange"`
// Players is who a "players" tracker is shown to; Directors only.
Players []PlayerView `json:"players,omitempty"`
// AlsoSeenBy names the other chosen players, for a chosen player's own
// copy, so it can say who else sees it.
AlsoSeenBy []string `json:"alsoSeenBy,omitempty"`
}
TrackerView is one tracker as the viewer may see it, with its value worked out from the log.
type Viewer ¶
type Viewer struct {
UserID string
// MemberRole is the real membership role (0 guest .. 3 owner).
MemberRole int
// VisibilityRole decides what the viewer may see; a member the owner
// gave DM access counts as an owner there.
VisibilityRole int
// Director is the owner or a co-Director: the only people who define
// kinds, see hidden trackers and change any tracker.
Director bool
}
Viewer is who is asking, taken from the campaign context in the handler.