Documentation
¶
Overview ¶
Package rolls is the campaign's roll log: rolls made at Chronicle's Table tab, rolls other apps report (Foundry), and rolls other plugins make on the server (crafting). Chronicle decides its own rolls here with the system's secure random source, so a browser never picks a result, and every roll is sent live only to the people its mode lets see it.
Index ¶
- Constants
- Variables
- func CustomizeTabFactory(dice DiceSettingsReader) func(*campaigns.CampaignContext) campaigns.CustomizeTab
- func RegisterRoutes(e *echo.Echo, h *Handler, campaignSvc campaigns.CampaignService, ...)
- type Audience
- type CharacterSource
- type CritSource
- type DiceSettingsReader
- type Die
- type Handler
- type LogInput
- type Page
- type PowerRollLine
- type PowerRollSource
- type PowerStat
- type Publisher
- type ReportInput
- type Repository
- type Roll
- type Service
- type Settings
- type TableInput
- type UpdateSettingsInput
- type View
- type Viewer
Constants ¶
const ( CritSucc = "succ" CritFail = "fail" )
The crit a die carries, as the server judged it.
const ( // ModePublic is seen by everyone at the table. ModePublic = "public" // ModeGM is seen by the Directors and the roller. ModeGM = "gm" // ModeBlind is seen by the Directors; the roller learns only that it was made. ModeBlind = "blind" // ModeSelf is seen by the roller alone. ModeSelf = "self" )
Who may see a roll, the same four modes Foundry has.
const ( OriginChronicle = "chronicle" OriginFoundry = "foundry" )
Which app decided a roll's numbers.
const ( SourceTable = "table" SourceCrafting = "crafting" SourceShop = "shop" SourceFoundry = "foundry" )
What made a roll inside its app.
const ( LookQuick = "quick" LookRune = "rune" LookNone = "none" )
The table's dice looks. Each person picks their own; a new player starts on the table's.
const ( MaxDice = 100 MaxSides = 1000 MaxModifier = 9999 MaxTotal = 1000000 FormulaMax = 120 LabelMax = 160 NameMax = 100 OutcomeMax = 160 WhyMax = 300 FromMax = 160 OriginIDMax = 80 DefaultPage = 50 MaxPage = 200 )
Bounds on one roll, so the log stays small and a hostile client can at worst fill its own campaign's log with short rows.
const ( QualityAuto = "auto" QualityBasic = "basic" QualityHD = "hd" )
Quality is how much detail the 3D dice draw. Empty means Auto, which the browser measures per device.
const CharacterIDMax = 64
CharacterIDMax bounds a character id from a client; entity ids are UUIDs.
const PluginSlug = "rolls"
PluginSlug names this plugin in the registry and the health/migration bookkeeping.
Variables ¶
var ( DiceSetKeys = []string{ "jewelled", "confetti", "acrylic", "cage", "heartlight", "gothic", "obsidian", "moonstone", "oak", "nebula", "bone", "scale", "damascus", "cloisonne", "kintsugi", "leather", "runestone", "arcana", "relic", "agate", "neon", "amber", "botanical", "clockwork", } CritSuccKeys = []string{"storm", "phoenix", "dawn", "starfall", "bloom", "hoard", "koi", "prism", "festival", "encore", "fortune", "flutter"} CritFailKeys = []string{"grasp", "frost", "shatter", "hex", "crows", "abyss", "mimic", "engulf", "omen", "shackle", "melt", "dust"} )
DiceSetKeys and the two crit lists name what the browser's dice engine can draw (static/js/dice/engine.js, SET_KEYS and CRIT_KEYS). The names live in both places because the server must refuse a name the engine would never load; TestDiceKeysMatchEngine in service_test.go keeps the two lists equal.
var MigrationsFS embed.FS
MigrationsFS contains the embedded SQL migration files.
var StaticAssetsFS embed.FS
StaticAssetsFS holds the Table tab's script, served at /static/plugins/rolls/.
Functions ¶
func CustomizeTabFactory ¶
func CustomizeTabFactory(dice DiceSettingsReader) func(*campaigns.CampaignContext) campaigns.CustomizeTab
CustomizeTabFactory contributes the "Dice" tab to the campaigns plugin's Customize page (campaigns.Handler.RegisterCustomizeTab). The campaigns plugin only hosts the tab; the rolls plugin owns what is in it and where it saves, and says what is chosen now from its own settings. The Customize page is the owner's, so every viewer is a Director. A nil reader gives the tab no summary.
func RegisterRoutes ¶
func RegisterRoutes(e *echo.Echo, h *Handler, campaignSvc campaigns.CampaignService, authSvc auth.AuthService)
RegisterRoutes mounts the Table tab's routes. Every member may read the log and roll; the service narrows what each one sees.
Types ¶
type Audience ¶
Audience says who a live roll may reach. Exactly one shape is used: Everyone; DirectorsOnly; or Users, which Directors also receive unless Strict is set.
type CharacterSource ¶
type CharacterSource interface {
MayRollAs(ctx context.Context, campaignID string, v Viewer, characterID, mode string) (bool, error)
}
CharacterSource says whether a viewer may roll as a page: a live page of the campaign that the viewer has claimed, or any of them for a Director. For a public roll the page must also be one every player may see, because Foundry shows the linked actor's name and picture to the whole table. It is implemented in internal/app over the entities plugin, so this plugin never reads pages itself.
type CritSource ¶
CritSource gives the rules of the campaign's game system. It is implemented in internal/app over the enabled system's manifest, so this package never reads another plugin's data directly. A nil source means the default rules.
type DiceSettingsReader ¶
type DiceSettingsReader interface {
Settings(ctx context.Context, campaignID string) (*Settings, error)
}
DiceSettingsReader is the slice of the rolls service the Dice tab's summary reads: the campaign's dice.
type Die ¶
type Die struct {
Sides int `json:"sides"`
Value int `json:"value"`
Crit string `json:"crit,omitempty"`
}
Die is one die as it fell. Crit is the server's judgement under the campaign's game system ("succ", "fail" or empty); it is stored with the die and never taken from a client.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler serves the Table tab's rolls as JSON. It binds and calls the service; who may see and do what is the service's.
func (*Handler) Power ¶
Power returns the campaign's power roll and the viewer's own hero's characteristics: only the caller's claimed character, never anyone else's.
func (*Handler) SetPowerRolls ¶
func (h *Handler) SetPowerRolls(src PowerRollSource)
SetPowerRolls gives the handler the game system's power roll; without it the Table never shows a power roll line.
func (*Handler) SetSettings ¶
SetSettings changes the campaign's dice. The body is partial: only the keys it carries change. The service refuses anyone but a Director.
type LogInput ¶
type LogInput struct {
Source string
RollerID string
RollerName string
Label string
Formula string
Dice []Die
Modifier int
Total int
Outcome string
Why string
From string
Mode string
}
LogInput is a roll another plugin made on the server and adds to the log.
type PowerRollLine ¶
type PowerRollLine struct {
Declared bool `json:"declared"`
Dice string `json:"dice,omitempty"`
Edge int `json:"edge,omitempty"`
Bane int `json:"bane,omitempty"`
Tiers []int `json:"tiers,omitempty"`
Characteristics []PowerStat `json:"characteristics,omitempty"`
Hero string `json:"hero,omitempty"`
// HeroID is the hero's page id, which the Table sends back with a roll
// of its numbers so the roll is the hero's.
HeroID string `json:"heroId,omitempty"`
}
PowerRollLine is the Table's power roll line for one viewer: the game system's declared roll (2d10 plus a characteristic), and the viewer's own hero's numbers when they are known. Declared is false for a campaign whose system declares no power roll, and the Table then shows no line.
type PowerRollSource ¶
type PowerRollSource interface {
PowerRoll(ctx context.Context, campaignID, userID string, canSeeGM bool) (PowerRollLine, error)
}
PowerRollSource reads a campaign's power roll and the viewer's own hero. It is implemented in internal/app over the game system and the claimed character, so this plugin reaches neither directly. canSeeGM is whether the viewer sees GM content; without it, a number the GM marked GM-only is left out.
type PowerStat ¶
type PowerStat struct {
Key string `json:"key"`
Label string `json:"label"`
Value *int `json:"value"`
}
PowerStat is one characteristic of the power roll. Value is nil when the viewer has no hero or the hero's sheet holds no number there.
type Publisher ¶
Publisher sends a roll to the campaign's live connections. It is implemented in internal/app over the WebSocket hub, which drops the message for anyone outside the audience before it is sent.
type ReportInput ¶
type ReportInput struct {
Origin string
OriginID string
RollerID string
RollerName string
Label string
Formula string
Dice []Die
Total int
Mode string
}
ReportInput is a roll another app decided and reports (Foundry). RollerID is resolved by the caller from the app's own user, or empty.
type Repository ¶
type Repository interface {
// Insert adds a roll. A roll whose origin id is already recorded is not
// added again: it returns that roll's id and duplicate=true.
Insert(ctx context.Context, r Roll) (id string, duplicate bool, err error)
// List returns up to limit rolls the viewer may see, newest first, made
// before the roll before (all when empty).
List(ctx context.Context, campaignID string, v Viewer, before string, limit int) ([]Roll, error)
// Settings returns the campaign's dice settings: the stored row, or the
// defaults (empty names, players may pick) when none was saved. Look is ""
// until a Director sets it.
Settings(ctx context.Context, campaignID string) (Settings, error)
// SaveSettings writes s, but on an existing row only the named settings
// (keys of the Settings JSON, e.g. "diceSet"), so concurrent changes to
// different settings both land.
SaveSettings(ctx context.Context, campaignID string, s Settings, changed []string) error
}
Repository owns the campaign_rolls and campaign_roll_settings tables. Which rolls a viewer may read is decided in the SQL of List, so a read for a player never selects another person's hidden roll.
func NewRepository ¶
func NewRepository(db *sql.DB) Repository
NewRepository creates the MariaDB repository for rolls.
type Roll ¶
type Roll struct {
ID string `json:"id"`
CampaignID string `json:"-"`
Origin string `json:"origin"`
OriginID string `json:"originId,omitempty"`
Source string `json:"source"`
RollerID string `json:"rollerId,omitempty"`
RollerName string `json:"rollerName"`
Label string `json:"label"`
Formula string `json:"formula"`
Dice []Die `json:"dice,omitempty"`
Modifier int `json:"modifier"`
Total int `json:"total"`
Outcome string `json:"outcome,omitempty"`
Why string `json:"why,omitempty"`
From string `json:"from,omitempty"`
Mode string `json:"mode"`
// CharacterID is the hero the roll was made for, when it came from a
// hero's own numbers (the sheet, or the Table's power roll line). It
// travels on the live message only, so Foundry can speak as the hero's
// actor; the log does not keep it.
CharacterID string `json:"characterId,omitempty"`
CreatedAt time.Time `json:"createdAt"`
}
Roll is one roll in the log.
type Service ¶
type Service interface {
// Table makes a roll asked for at the Table tab.
Table(ctx context.Context, campaignID string, v Viewer, in TableInput) (*View, error)
// Report records a roll another app decided. A replay of the same origin
// id records nothing and returns the recorded roll's id with duplicate.
Report(ctx context.Context, campaignID string, in ReportInput) (id string, duplicate bool, err error)
// Log adds a roll another plugin made on the server.
Log(ctx context.Context, campaignID string, in LogInput) error
// Record is Log that also returns the new roll's id, for a caller that
// tells its own client which log row its roll became.
Record(ctx context.Context, campaignID string, in LogInput) (string, error)
// List is a page of the log as the viewer may see it, oldest first.
List(ctx context.Context, campaignID string, v Viewer, before string, limit int) (*Page, error)
// Settings is the campaign's dice: the table's look and the 3D dice picks.
Settings(ctx context.Context, campaignID string) (*Settings, error)
// SetSettings changes the campaign's dice, partially: only the fields sent
// change. Directors only.
SetSettings(ctx context.Context, campaignID string, v Viewer, in UpdateSettingsInput) (*Settings, error)
}
Service is the roll log's business logic. The rules, in one place:
- Chronicle's own rolls are decided here, never by a browser.
- Public rolls are everyone's. A Director-only roll is the Directors' and the roller's. A blind roll is the Directors'; its roller is told only that it was made. A self roll is the roller's alone, Directors included.
- The log a viewer reads and the live messages they receive follow the same rule (the repository's visibleTo and audienceOf below).
func NewService ¶
func NewService(repo Repository, pub Publisher, crits CritSource, chars CharacterSource) Service
NewService creates the roll log service. pub may be nil (nothing is sent live); crits may be nil (the default crit rules apply); chars may be nil (no roll is ever made as a hero).
type Settings ¶
type Settings struct {
Look string `json:"look"`
DiceSet string `json:"diceSet"`
DiceColour string `json:"diceColour"`
CritSucc string `json:"critSucc"`
CritFail string `json:"critFail"`
Quality string `json:"quality"`
PlayersPick bool `json:"playersPick"`
// DiceOffered is the sets a player may pick from, in catalogue order. It
// is empty when every set is on offer. The campaign's own set is always
// among them.
DiceOffered []string `json:"diceOffered"`
}
Settings is the campaign's dice: the table's look (what a player who hasn't picked starts on) and the 3D dice every roll is drawn with. An empty string in a dice field means "the default", so the browser's own defaults apply.
type TableInput ¶
type TableInput struct {
Formula string `json:"formula"`
Label string `json:"label"`
Mode string `json:"mode"`
CharacterID string `json:"characterId"`
}
TableInput is a roll asked for at the Table tab. CharacterID names the hero whose numbers the roll uses, when it has one; it is kept only when the roller plays that hero or is a Director (CharacterSource).
type UpdateSettingsInput ¶
type UpdateSettingsInput struct {
Look patch.Field[string] `json:"look"`
DiceSet patch.Field[string] `json:"diceSet"`
DiceColour patch.Field[string] `json:"diceColour"`
CritSucc patch.Field[string] `json:"critSucc"`
CritFail patch.Field[string] `json:"critFail"`
Quality patch.Field[string] `json:"quality"`
PlayersPick patch.Field[bool] `json:"playersPick"`
// DiceOffered replaces the whole list; an empty list offers every set.
DiceOffered patch.Field[[]string] `json:"diceOffered"`
}
UpdateSettingsInput is a partial change to Settings: a key left out keeps what is stored. An empty string on a dice field puts it back to the default; an explicit null keeps it, since none of these columns can hold NULL.
type View ¶
type View struct {
ID string `json:"id"`
Origin string `json:"origin"`
OriginID string `json:"originId,omitempty"`
Source string `json:"source"`
RollerID string `json:"rollerId,omitempty"`
RollerName string `json:"rollerName"`
Label string `json:"label"`
Formula string `json:"formula"`
Dice []Die `json:"dice,omitempty"`
Modifier *int `json:"modifier,omitempty"`
Total *int `json:"total,omitempty"`
Outcome string `json:"outcome,omitempty"`
Why string `json:"why,omitempty"`
From string `json:"from,omitempty"`
Mode string `json:"mode"`
Hidden bool `json:"hidden,omitempty"`
Mine bool `json:"mine,omitempty"`
// CharacterID is Roll.CharacterID; it is absent from the log.
CharacterID string `json:"characterId,omitempty"`
CreatedAt time.Time `json:"createdAt"`
}
View is a roll as one viewer is told it. A blind roll reaches its roller with Hidden set and no dice, modifier, total or outcome.