playerchanges

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: 24 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 playerchanges is "What players changed": a record of every change a player makes to a page (a sheet field, a page's title or text, a removed page), by hand or through their AI, and the owner's page for searching it and undoing a change.

The last month lives one row per change and answers at once. Older months are packed into one compressed blob per campaign and month and kept as long as the site's Long-term trash setting said when they were packed; a search that reaches back unpacks them. Recording never fails the write it describes: errors are logged and the write stands.

Index

Constants

View Source
const (
	RangeMonth = "month"
	RangeLong  = "long"
)

Ranges the time switch offers.

View Source
const (
	KindField   = "field"   // a sheet field
	KindText    = "text"    // the page's title or text
	KindRemoved = "removed" // the page went to the Trash
)

Kinds of change.

View Source
const (
	SourceWeb = "web"
	SourceAI  = "ai"
)

Sources of a change, as the page names them. They mirror the change source an entity write carried.

View Source
const (
	// LiveDays is how long a change stays one row of its own, answering at
	// once, before it is packed with the rest of its month.
	LiveDays = 30

	// MaxResults is the most changes one search returns.
	MaxResults = 300
)

Limits.

View Source
const DefaultKeepMonths = 6

DefaultKeepMonths is the Long-term trash length until the admin sets one.

View Source
const PluginSlug = "player_changes"

PluginSlug names this plugin in the migration bookkeeping and the health registry, so a failed migration here turns off only "What players changed".

Variables

View Source
var ErrPageGone = apperror.NewNotFound("that page is gone, so the change can't be undone")

ErrPageGone is what Undo says when the change's page no longer exists.

View Source
var KeepChoices = []int{0, 3, 6, 12, 24}

KeepChoices are the Long-term trash lengths the site admin may pick, in months; 0 is off.

View Source
var MigrationsFS embed.FS

MigrationsFS holds the plugin's SQL migrations, built into the binary.

View Source
var StaticAssetsFS embed.FS

StaticAssetsFS holds the page's script, served at /static/plugins/player_changes/.

Functions

func RegisterRoutes

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

RegisterRoutes mounts the page on the campaign group, owner only.

func TextOf

func TextOf(v any) string

TextOf is textOf for the recorder in internal/app.

func ValueJSON

func ValueJSON(v any) string

ValueJSON is a field value as Undo stores it; "" when it can't be kept.

Types

type Change

type Change struct {
	ID         int64      `json:"id"`
	CampaignID string     `json:"-"`
	EntityID   string     `json:"e"`
	PageName   string     `json:"p"`
	UserID     string     `json:"u"`
	UserName   string     `json:"un"`
	Kind       string     `json:"k"`
	FieldKey   string     `json:"fk,omitempty"`
	FieldLabel string     `json:"fl,omitempty"`
	OldText    string     `json:"ot,omitempty"`
	NewText    string     `json:"nt,omitempty"`
	OldJSON    string     `json:"oj,omitempty"`
	NewJSON    string     `json:"nj,omitempty"`
	Source     string     `json:"s,omitempty"`
	App        string     `json:"a,omitempty"`
	CreatedAt  time.Time  `json:"t"`
	UndoneAt   *time.Time `json:"ud,omitempty"`
	UndoneBy   string     `json:"ub,omitempty"`
	// Packed is true for a change read from a packed month.
	Packed bool `json:"-"`
}

Change is one thing a player changed.

func (Change) ByAI

func (c Change) ByAI() bool

ByAI reports whether the player's AI made the change.

func (Change) What

func (c Change) What() string

What names what changed, as a line starts: the field, "Page text" or "Name".

type DeskProvider

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

DeskProvider fills the desk's "what players changed" line.

func NewDeskProvider

func NewDeskProvider(svc Service) *DeskProvider

NewDeskProvider returns the desk provider for the changes log.

func (*DeskProvider) Facts

Facts counts the last week's changes. The desk route is owner-only and this reads only the one campaign the request names.

func (*DeskProvider) Slot

func (p *DeskProvider) Slot() campaigns.DeskSlot

Slot is the one desk line this provider fills.

type Handler

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

Handler serves the owner's "What players changed" page.

func NewHandler

func NewHandler(svc Service, keepMonths func(c *echo.Context) int, zone func(c *echo.Context) *time.Location) *Handler

NewHandler creates the handler. keepMonths says how far back the long range reaches; zone is the owner's time zone (UTC when nil).

func (*Handler) Page

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

Page renders the page.

func (*Handler) Results

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

Results answers the search box.

func (*Handler) Undo

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

Undo puts one change back.

type Pack

type Pack struct {
	PackKey
	Rows      []Change
	KeepUntil time.Time
}

Pack is one packed month, unpacked.

type PackKey

type PackKey struct {
	CampaignID string
	Month      time.Time // the first of the month, UTC
}

PackKey names one packed month.

type PageGroup

type PageGroup struct {
	EntityID string
	Name     string
	Changes  []Change
}

PageGroup is the changes to one page, newest first.

type Pages

type Pages interface {
	// FieldValue is a sheet field's stored value; ok is false when the page
	// or field is gone.
	FieldValue(ctx context.Context, campaignID, entityID, key string) (value any, ok bool, err error)
	// SetField writes one sheet field back, as the owner.
	SetField(ctx context.Context, campaignID, entityID, key string, value any, ownerID string) error
	// RestorePage brings a removed page back from the Trash.
	RestorePage(ctx context.Context, campaignID, entityID string) error
	// TextDiff names the words a page's text lost and gained between two
	// plain-text versions, each run joined with " … ".
	TextDiff(older, newer string) (removed, added string)
}

Pages is what Undo needs from the pages a change was made on. It is implemented over the entities service in internal/app.

type Query

type Query struct {
	// Pages are page ids; a change on any of them matches.
	Pages []string
	// Fields are field keys or "name"/"text"; a change to any matches.
	Fields []string
	// AI keeps only changes the player's AI made.
	AI bool
	// Removed keeps only removed pages.
	Removed bool
	// Words must all appear somewhere in the change.
	Words []string
	// Since is how far back to look.
	Since time.Time
}

Query is what the owner asked the history for. Every part narrows.

type Repository

type Repository interface {
	Insert(ctx context.Context, c Change) error
	// LastFor returns the newest live, not undone change by the same person
	// to the same page, kind, field and source made at or after since, or
	// nil. Record folds quick successive saves into it.
	LastFor(ctx context.Context, c Change, since time.Time) (*Change, error)
	// UpdateNew rewrites a live change's page name, texts, new value and
	// time after a fold.
	UpdateNew(ctx context.Context, c Change) error
	// Delete removes a live change whose fold came back to where it started.
	Delete(ctx context.Context, campaignID string, id int64) error
	// Live returns the campaign's live changes since q.Since that match the
	// query's page, AI and removed parts, newest first, at most liveScan.
	Live(ctx context.Context, campaignID string, q Query) ([]Change, error)
	// Get returns one live change of the campaign, or nil.
	Get(ctx context.Context, campaignID string, id int64) (*Change, error)
	// MarkUndone records that the owner undid a live change; false when it
	// was already undone or is not there.
	MarkUndone(ctx context.Context, campaignID string, id int64, by string, at time.Time) (bool, error)
	// Recent lists the pages and fields the campaign's live changes touched,
	// for the search box's suggestions.
	Recent(ctx context.Context, campaignID string, since time.Time) (pages, fields []Suggestion, err error)

	// DueMonths lists the campaign months that hold live rows older than
	// cutoff.
	DueMonths(ctx context.Context, cutoff time.Time) ([]PackKey, error)
	// PackMonth moves the month's live rows older than cutoff into its pack,
	// keeping the pack until keepUntil (or longer, if it already was).
	// keepUntil zero drops them instead.
	PackMonth(ctx context.Context, k PackKey, cutoff time.Time, keepUntil time.Time) (int, error)
	// Packs returns the campaign's packed months from since on.
	Packs(ctx context.Context, campaignID string, since time.Time) ([]Pack, error)
	// RewritePack replaces one packed month's rows (after an Undo in it).
	RewritePack(ctx context.Context, k PackKey, rows []Change) error
	// PrunePacks removes packs, and kept pages, whose keep_until has passed.
	PrunePacks(ctx context.Context, today time.Time) (int, error)
	// MarkPageRemovalsUndone marks every open removal of the page undone, by
	// the person (empty when unknown), so the page stops being kept.
	MarkPageRemovalsUndone(ctx context.Context, campaignID, entityID, by string, at time.Time) error
	// KeptPages lists the pages players removed that the Trash must keep:
	// every live, not undone removal, and every kept page whose keep_until
	// is today or later.
	KeptPages(ctx context.Context, today time.Time) ([]string, error)
	// MonthlyBytes is about how much one month of packed changes takes on
	// the site: the packs' average, or the live rows' size shrunk by what
	// packing usually saves. known is false with nothing to go by.
	MonthlyBytes(ctx context.Context) (bytes int64, known bool, err error)
	// ForgetPage stops keeping a removed page (it was brought back).
	ForgetPage(ctx context.Context, campaignID, entityID string) error
	// CountSince counts the campaign's live changes made at or after since
	// that were not undone, and how many different people made them. One
	// aggregate query, so the owner's desk is not capped by liveScan.
	CountSince(ctx context.Context, campaignID string, since time.Time) (changes, people int, err error)
}

Repository owns player_changes and player_change_packs. It decides nothing about who may see or undo a change; the service does.

func NewRepository

func NewRepository(db *sql.DB) Repository

NewRepository creates the MariaDB repository.

type Result

type Result struct {
	Changes []Change
	// Truncated is true when more matched than MaxResults.
	Truncated bool
	// Unpacked is how many packed months were read.
	Unpacked int
}

Result is a search's answer.

type ResultsView

type ResultsView struct {
	CampaignID string
	Range      string
	KeepMonths int
	Groups     []PageGroup
	Count      int
	Truncated  bool
	Zone       *time.Location
	Words      []string
	Now        time.Time
}

ResultsView is one search's answer as the page shows it.

type Service

type Service interface {
	// Record keeps one change. Errors are logged by the caller and never
	// fail the write the change describes.
	Record(ctx context.Context, c Change) error
	// Search answers a query. withPacked also unpacks older months.
	Search(ctx context.Context, campaignID string, q Query, withPacked bool) (*Result, error)
	// Suggestions are the tags the search box offers.
	Suggestions(ctx context.Context, campaignID string) ([]Suggestion, error)
	// Undo puts a change back, as the owner, and marks it undone.
	Undo(ctx context.Context, campaignID, ownerID string, id int64) (*Change, error)
	// PackDue packs live rows older than LiveDays and removes packs whose
	// time is up.
	PackDue(ctx context.Context) error
	// StartPacker runs PackDue now and then every hour until ctx ends.
	StartPacker(ctx context.Context)
	// KeptPages lists the pages players removed that the campaign Trash
	// keeps past its days (Long-term trash).
	KeptPages(ctx context.Context) ([]string, error)
	// PageRestored closes a page's open removals once it is back out of the
	// Trash by any route, so the Trash no longer keeps it.
	PageRestored(ctx context.Context, campaignID, entityID string)
	// MonthlyBytes estimates one packed month's size, for the Site admin
	// setting.
	MonthlyBytes(ctx context.Context) (int64, bool, error)
	// CountSince counts the campaign's live changes since a time and how many
	// different people made them, for the owner's desk.
	CountSince(ctx context.Context, campaignID string, since time.Time) (changes, people int, err error)
}

Service records and searches what players changed.

func NewService

func NewService(repo Repository, pages Pages, keepMonths func(ctx context.Context) int) Service

NewService creates the service. keepMonths may be nil.

type Suggestion

type Suggestion struct {
	Kind  string `json:"kind"` // "page", "field", "ai", "removed"
	Value string `json:"value"`
	Label string `json:"label"`
}

Suggestion is a tag the search box offers.

type View

type View struct {
	CampaignID  string
	Suggestions []Suggestion
	// KeepMonths is how long changes are kept; 0 means only the last month.
	KeepMonths int
}

View is what the page shows.

Jump to

Keyboard shortcuts

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