transformations

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: 20 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 transformations keeps layers on a hero: a vampire's bite, rising as a revenant, a curse, a polymorph. A layer names the sheet fields it changes and leaves the hero's own fields alone, so taking it off puts the hero back exactly as they were.

This package stores and serves layers; it knows no game's rules. What a change means on a sheet (a damage weakness, a trait) is drawn by the game system's sheet, and ready-made layers come from the system's data. Only the Director puts a layer on or takes one off; anyone who can see the page sees the layers on it.

The browser HTTP surface for layers. 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 the hero_layers table.

SECURITY: every statement filters on campaign_id, so a layer id from another campaign never reads or deletes a row here.

Route registration for layers.

Index

Constants

View Source
const (
	// OpSet replaces the field's value.
	OpSet = "set"
	// OpAdd adds a number to a numeric field.
	OpAdd = "add"
	// OpAppend adds an entry to a field that holds a list.
	OpAppend = "append"
	// OpRemove hides the list entry with this name.
	OpRemove = "remove"
)

How one change treats the field it names. The sheet that draws the hero decides what a value means; these say only how it combines with the field underneath.

View Source
const (
	MaxBodyBytes     = 64 << 10
	MaxLayersPerPage = 20
	MaxChanges       = 40
	MaxValueBytes    = 4 << 10
	MaxNameChars     = 80
	MaxSourceChars   = 120
	MaxNoteChars     = 1000
	MaxLabelChars    = 120
	MaxFieldChars    = 64
	MaxIDChars       = 64
)

Limits keep a layer small: a transformation names a handful of fields, and every page view sends its layers.

View Source
const AddonSlug = "transformations"

AddonSlug is the campaign switch for transformations, one of the Characters app's add-ons.

View Source
const PluginSlug = "transformations"

PluginSlug names this plugin in the registry and the health/migration bookkeeping, so a failed migration leaves only transformations out.

Variables

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

Functions

func RegisterRoutes

func RegisterRoutes(e *echo.Echo, h *Handler, campaignSvc campaigns.CampaignService, authSvc auth.AuthService, addonSvc addons.AddonService)

RegisterRoutes mounts the routes under the campaign group. Every route needs a signed-in member and the Transformations add-on on; the service then decides who may read and refuses every write but the Director's, so the gate here is not the only one. CSRF is enforced by the global middleware.

Types

type AILayers

type AILayers struct {
	// contains filtered or unexported fields
}

AILayers is the transformations' part of the AI engine (aiplug, ADR-071): the `layer` block an AI can ask to put on or take off a hero, and the `layers` lookup that says what is changing a hero now. Both go through the Service, so the Director-only and page-edit rules are the same as on the sheet, and both answer nothing while the campaign has the add-on off.

func NewAILayers

func NewAILayers(svc Service, pages PageFinder, on func(ctx context.Context, campaignID string) bool) *AILayers

NewAILayers builds the AI part. on reports whether the campaign has the add-on switched on.

func (*AILayers) Answer

func (k *AILayers) Answer(ctx context.Context, campaignID string, a aiplug.Actor, r aiplug.Record) (string, string, error)

func (*AILayers) Apply

func (k *AILayers) Apply(ctx context.Context, campaignID string, a aiplug.Actor, r aiplug.Record) error

func (*AILayers) Brief

func (*AILayers) Brief() string

func (*AILayers) Doc

func (*AILayers) Doc() string

func (*AILayers) Export

Export is empty: layers are read a page at a time with `what: layers`.

func (*AILayers) Label

func (*AILayers) Label() string

func (*AILayers) LookupInfo

func (*AILayers) LookupInfo() aiplug.LookupInfo

func (*AILayers) Name

func (*AILayers) Name() string

func (*AILayers) Plan

func (k *AILayers) Plan(ctx context.Context, campaignID string, a aiplug.Actor, r aiplug.Record) aiplug.Plan

type AddInput

type AddInput struct {
	Name    string   `json:"name"`
	Source  string   `json:"source"`
	Note    string   `json:"note"`
	Changes []Change `json:"changes"`
}

AddInput is a Director putting a layer on. Choices a preset asks for are settled before this, so the changes are final.

func CheckLayer

func CheckLayer(in AddInput) (AddInput, error)

CheckLayer checks a layer exactly as Add does, without storing it, and returns it trimmed and re-encoded. A preset saved in the Homebrew Workshop is checked with it, so a saved preset can always be put on a hero.

type Change

type Change struct {
	// Field is the sheet field's key, as the game system names it.
	Field string `json:"field"`
	Op    string `json:"op"`
	// Value is a string, number or JSON object; OpAdd takes a number and
	// OpRemove a name.
	Value json.RawMessage `json:"value"`
	// Label is how the sheet names the change when the field alone says too
	// little, such as the trait an appended entry is.
	Label string `json:"label,omitempty"`
}

Change is one field a layer changes.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler serves the layer routes.

func NewHandler

func NewHandler(svc Service) *Handler

NewHandler builds the handler.

func (*Handler) Add

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

Add handles POST /campaigns/:id/pages/:eid/layers.

func (*Handler) List

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

List handles GET /campaigns/:id/pages/:eid/layers.

func (*Handler) Remove

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

Remove handles DELETE /campaigns/:id/layers/:lid.

type Layer

type Layer struct {
	ID       string `json:"id"`
	EntityID string `json:"entityId"`
	Name     string `json:"name"`
	// Source is the preset it came from, such as "drawsteel:revenant", so
	// the sheet can link the rule; empty for a layer made by hand.
	Source    string    `json:"source"`
	Note      string    `json:"note"`
	Changes   []Change  `json:"changes"`
	CreatedAt time.Time `json:"createdAt"`
}

Layer is one transformation on a hero, as stored and sent.

type Notifier

type Notifier interface {
	LayersChanged(campaignID, entityID string)
}

Notifier is told after a layer is put on or taken off a page, so the Foundry module can redraw that hero's effects. It is told only after the change is stored.

type Option

type Option func(*service)

Option configures the service.

func WithNotifier

func WithNotifier(n Notifier) Option

WithNotifier sets who is told when a page's layers change.

type PageAccess

type PageAccess struct {
	Exists  bool
	CanView bool
	CanEdit bool
}

PageAccess is one viewer's standing on one page.

type PageDirectory

type PageDirectory 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 comes back
	// with Exists false.
	Access(ctx context.Context, campaignID, entityID string, v Viewer) (PageAccess, error)
}

PageDirectory is owned by this plugin and implemented in internal/app over the entities service, so this plugin never imports another one.

type PageFinder

type PageFinder interface {
	// FindPage is the page's id and stored name, ok false when there is none.
	FindPage(ctx context.Context, campaignID, name string) (id, stored string, ok bool, err error)
}

PageFinder finds a page by the name an AI wrote. Implemented in internal/app over the entities service; the Service still decides whether the asker may see or edit it.

type PageLayers

type PageLayers struct {
	Layers  []Layer `json:"layers"`
	CanEdit bool    `json:"canEdit"`
}

PageLayers is what a page's sheet reads: its layers, oldest first, and whether this viewer may put more on or take them off.

type Repository

type Repository interface {
	// ListForPage returns a page's layers, oldest first.
	ListForPage(ctx context.Context, campaignID, entityID string) ([]Row, error)
	// Get returns one layer; found is false when there is none.
	Get(ctx context.Context, campaignID, id string) (r Row, found bool, err error)
	// Create inserts a layer unless the page already has max of them;
	// ok is false when it was full.
	Create(ctx context.Context, r Row, userID string, max int) (ok bool, err error)
	// Delete removes one layer; ok is false when there was none.
	Delete(ctx context.Context, campaignID, id string) (ok bool, err error)
}

Repository is the persistence boundary for layers.

func NewRepository

func NewRepository(db *sql.DB) Repository

NewRepository creates a MariaDB-backed repository.

type Row

type Row struct {
	ID         string
	CampaignID string
	EntityID   string
	Name       string
	Source     string
	Data       []byte
	CreatedAt  time.Time
}

Row is one stored layer.

type Service

type Service interface {
	// ListForPage returns a page's layers, oldest first.
	ListForPage(ctx context.Context, campaignID, entityID string, v Viewer) (*PageLayers, error)
	// Add puts a layer on a page.
	Add(ctx context.Context, campaignID, entityID string, v Viewer, in AddInput) (*Layer, error)
	// Remove takes a layer off, leaving the page as it was underneath.
	Remove(ctx context.Context, campaignID, id string, v Viewer) error
}

Service owns the layer rules. Reading a page's layers needs only the page; putting one on or taking one off is the Director's, on a page they may edit.

func NewService

func NewService(repo Repository, pages PageDirectory, opts ...Option) Service

NewService builds the service.

type Viewer

type Viewer struct {
	UserID string
	// VisibilityRole is the role used for what a viewer may see; a member the
	// owner gave DM access counts as an owner there.
	VisibilityRole int
}

Viewer is who is asking, taken from the campaign context in the handler.

func (Viewer) IsDM

func (v Viewer) IsDM() bool

IsDM is Owner visibility, which an owner and a member with DM access both have.

Jump to

Keyboard shortcuts

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