Documentation menu

Documentation / Learning engine / gamification

Gamification

Package
@wabbit/tome-gamification
Version
v0.4.0
npm install @wabbit/tome-gamification
First install from our registry?
  1. Get a registry token from your credentials page. You need a purchase that includes this package, or a Craft Library membership.

  2. Add the registry and your token to the .npmrc at the root of your project, with your token in place of YOUR_TOKEN:

    @wabbit:registry=https://npm.wabbit.com/
    //npm.wabbit.com/:_authToken=YOUR_TOKEN
Learning engineStable

Generic gamification substrate — points ledger, badges, and achievements; extracted from @wabbit/tome-lms, consumed directly by @wabbit/tome-rpg for XP derivation.

Overview

Generic gamification substrate — a points ledger, badges, and achievements — for the Tome platform. domain layer per root ARCHITECTURE.md. Extracted from @wabbit/tome-lms on 2026-06-17; tome-lms re-exports these symbols for back-compat, but new code should depend on this package directly. @wabbit/tome-rpg is the current real consumer of the server subpath (@wabbit/tome-gamification/server's getPointsBalance/getPointsSince back the RPG layer's XP derivation).

Peer dependencies

PeerRangeNotes
payload>=3.67.0required
@wabbit/tome-core>=1.14.0 <2.0.0optional — the layer boots without it; you just lose the admin-nav/layer-registry manifest entry

server-only is a regular dependency (used to guard the @wabbit/tome-gamification/server subpath).

60-second quickstart

// payload.config.ts
import { createGamificationLayer } from '@wabbit/tome-gamification'

const { collections } = createGamificationLayer({ courseSlug: 'courses' })
// Spread `collections` into your Payload config as-is: the factory already
// orders them Badge → Achievement → Points (Achievement relates to Badge).
// Omit `courseSlug` outside an LMS and the course fields are dropped.
// Award points from a server hook or action. Writes one Points row, then
// re-runs the badge walk with the member's new total.
import { awardPoints } from '@wabbit/tome-gamification'

await awardPoints(memberId, 25, 'custom', null, 'Finished onboarding', payload)

awardPoints(studentId, amount, reason, courseId, description, payload, slugs?) takes six positional arguments plus an optional slugs object and resolves void. checkAndAwardBadges(studentId, trigger, payload, slugs?) resolves the array of badges it awarded on this call (already-earned badges are skipped). Both swallow every error silently — a failed write never throws to the caller, so check the ledger if an award seems missing.

// Server Component / route handler — real usage pattern from @wabbit/tome-rpg's
// server subpath
import { getPointsBalance, getPointsSince } from '@wabbit/tome-gamification/server'

const balance = await getPointsBalance(memberId, payload)

Public API

ExportSubpathDescription
createGamificationLayer(config?)rootBuilds the 3 collections (Badge, Achievement, Points) in dependency order, registers with layerRegistry (silently skipped when @wabbit/tome-core is absent), and returns { collections, utilities: { awardPoints, checkAndAwardBadges } }. registerGamificationLayer is the same function, @deprecated and sunset at 1.0
createPointsCollection, PointsCollection, createBadgeCollection, BadgeCollection, createAchievementCollection, AchievementCollectionrootCollection factories / defaults
awardPoints, checkAndAwardBadgesrootLedger + badge-trigger utilities. Default to the points/badges/achievements slugs; pass slugs (or use the copies on createGamificationLayer(config).utilities, which are bound to the config's slugs) when the collections are renamed
GAMIFICATION_ROLE_TIERS, hasAnyRole, isAdmin, isDirector, getUserId, publicRead, adminOnly, systemOrAdmin, ownRecordByField, pointsRead, pointsWrite, achievementRead, achievementWriterootAccess predicates + role helpers
GamificationCollectionConfig, BadgeTriggerContext, GamificationSlugs, GamificationRoleTier, GamificationLayerrootType surface
getPointsBalance(memberId, payload, opts?)@wabbit/tome-gamification/serverSums the member's Points ledger rows (positive entries add, negative subtract). Reads at most 10,000 rows. opts.pointsSlug overrides the default 'points' slug
getPointsSince(memberId, since, payload, opts?)@wabbit/tome-gamification/serverSame, filtered to rows created on/after since (ISO string or Date)

Config (`GamificationCollectionConfig`) — all optional: memberSlug (default 'members'), courseSlug (no default on the layer factory — omit it and the course/relatedCourse fields are left out), mediaSlug (default 'media'), reasonOptions (default: the five LMS reasons course-completion, quiz-pass, assignment-submit, badge-earned, custom), and the three collection-slug overrides pointsSlug / badgesSlug / achievementsSlug. The fields inherited from the shared LayerFactoryConfig are accepted by the type but not applied by this layer yet. The static PointsCollection and AchievementCollection defaults are built with courseSlug: 'courses', so they include the course fields.

Slug overrides and the utilities. If you rename any of the three collections, call the utilities from the layer so they write to the renamed collections:

const gamification = createGamificationLayer({ pointsSlug: 'xp-ledger' })
await gamification.utilities.awardPoints(memberId, 25, 'custom', null, undefined, payload)

The root-barrel awardPoints/checkAndAwardBadges know nothing about your config; they use the defaults unless you pass the trailing slugs argument ({ pointsSlug, badgesSlug, achievementsSlug }). The @wabbit/tome-gamification/server readers take opts.pointsSlug. reasonOptions is not carried through: checkAndAwardBadges writes bonus rows with reason badge-earned, so a custom list must keep that value.

Not exported from the root barrel: getPointsBalance/getPointsSince — the root barrel's own header comment states this explicitly: "Server-only helpers (getPointsBalance, getPointsSince) are NOT exported from this barrel. Import them from '@wabbit/tome-gamification/server' instead."

Server / client posture

No .tsx files in this package — it's a Payload backend layer with zero React. Neither entry point belongs in a Client Component, but only @wabbit/tome-gamification/server carries the server-only guard:

  • Root barrel — collection factories, access predicates, and the awardPoints/checkAndAwardBadges utilities. It does not evaluate server-only, so it loads under plain Node, tsx, a plain test runner, and Payload CLI commands such as payload generate:types and payload migrate that load your payload.config.ts. The utilities share the ledger reader with @wabbit/tome-gamification/server through an internal module rather than importing @wabbit/tome-gamification/server. They still run privileged Payload writes, so keep them on the server.
  • `@wabbit/tome-gamification/server` — guarded with import 'server-only' at the top of its barrel (getPointsBalance, getPointsSince). Both query payload.find() directly with overrideAccess: true, so importing this subpath into a client bundle would both fail the server-only build guard and leak a privileged query path if it somehow didn't.

Testing

pnpm --filter @wabbit/tome-gamification test runs the Vitest suite (tests/: layer-version, role-resolution, and root-barrel/slug-override checks) in a plain Node environment, including a check that the root barrel loads there. Only code that imports @wabbit/tome-gamification/server needs server-only stubbed (for example with a Vitest alias to its empty.js).

Extending

A new ledger-derived metric follows getPointsBalance's shape: query the Points collection with overrideAccess: true, sum/filter in application code (not a Payload aggregation pipeline — the collection is small enough per-member that this is the deliberate simplicity trade-off). New badge trigger conditions extend checkAndAwardBadges's BadgeTriggerContext input, not the collection schema.

Design history

  • Extracted from @wabbit/tome-lms's points/badges logic into its own layer, then adopted by @wabbit/tome-rpg as the authoritative XP source of truth (RPG derives level/XP from this package's points ledger; the level/xpTotal columns on a character sheet are a denormalized cache for leaderboard sorting, not the source).
  • docs/claude-gotchas.md → Module / Exports Contracts section — "registerLayer MUST use lazy require() in try/catch, never static import" governs registerGamificationLayer (deprecated alias — use createGamificationLayer)'s registration call

Exports

Import path
@wabbit/tome-gamification
@wabbit/tome-gamification/server

Changelog

0.4.xMinor

  • getPointsBalance and getPointsSince take optional reasons and excludeReasons filters, and a new getPointsDaysSince lists the calendar days a member earned points.
    • Unset filters behave exactly as before (every reason counts).
    • getPointsDaysSince(memberId, since, payload, { timeZone }) returns sorted YYYY-MM-DD strings in the given time zone (default UTC).
    • New GAMIFICATION_REASONS constant. The default reason options gain artifact-produced, gate-passed, quest-complete, daily-complete and hearth-save, so layers can write those rows without registering them first. A site passing its own reasonOptions still controls the list.

0.3.xMinor

  • Role checks now understand a roles RELATIONSHIP, not just flat strings — unblocking admin gates that were silently shut.

    Three packages read req.user.roles by collecting only entries where typeof entry === 'string', then comparing them to literal tier names ('admin', 'instructor', …). On a site whose roles are a relationship to a Roles collection, that read produced [] and every tier check returned false.

    In tome-lms that closed enrollmentCreate, so a site's own super admin had no "Create" button on Course Enrollments; in tome-gamification it closed the Points/Badge/Achievement write gates; in tome-ai it scoped an admin to only their own credentials. The failure is silent — an access denial renders as a missing button, not an error.

    Two things made it worse than a simple shape mismatch:

    • Payload binds `req.user` at `collection.auth.depth`, which defaults to `0`, so a relationship arrives as raw ID strings. A site that also installs a custom auth strategy may populate it deeper — meaning the SAME deployment presents different shapes on different login paths. Widening the synchronous read alone would have fixed one path and left the other silently broken.
    • `super-admin` matched nothing. The tier lists hold literal role names, and super-admin is not one of them, so the highest-privilege role failed every check.

    Fixed in tome-lms and tome-gamification:

    • readRoles accepts flat names, populated Role docs ({slug}), the _populatedRoles enricher shape, and a flat singular role field.
    • super-admin now satisfies every tier, matching the platform-wide implicit '*' grant.
    • New resolveRoleSlugs(req) / hasAnyRoleAsync / isAdminAsync / isDirectorAsync / isInstructorRoleAsync / isMaintainerRoleAsync hydrate unresolved IDs through req.payload, memoized on req.context so a request running many access checks fetches at most once. Hydration never throws: a flat-name site keeps its synchronous result, so this is a strict widening for every shape.
    • Every collection access gate in both packages now uses the async resolvers. The synchronous helpers remain exported unchanged for hook call sites that already hold a populated user.

    Fixed in tome-ai: AiCredentials' admin check accepts populated Role docs and _populatedRoles, and recognizes the canonical super-admin slug (it previously matched only camelCase superAdmin). It stays synchronous by design — a field-level credential gate is the wrong place for a per-check DB round-trip.

    No behavior change for sites already using flat role strings: every previously-passing check still passes.

    Also pays the test-floor debt for all three packages: each gains its first suite — 35 cases covering every user shape, the super-admin rule, hydration, single-fetch memoization, failure tolerance and anonymous denial — and is removed from the assert-test-floor allowlist.

9 fixes (0.3.1 – 0.3.6)
  • 0.3.6
    The changelog, README and license text now use American spellings.

    No code or behavior changes.

  • 0.3.5

    README: subpath and file mentions are written package-qualified (for example @wabbit/tome-x/render) or as code text, so crawlers no longer resolve them as site URLs.

  • 0.3.4
    Published packages now contain compiled JavaScript and type declarations under a one-line license banner, and no longer include source maps.

    What you install: one compiled .js (ESM) and .cjs (CommonJS) file per source module, its .d.ts / .d.cts declarations, and the stylesheets, fonts and other assets a package already shipped. Every JavaScript module opens with a comment naming the package and its license: /*! @wabbit/<package> — © Wabbit, LLC. Wabbit Tome Commercial License (see LICENSE.md). Not for redistribution. */.

    The .map files and the sourceMappingURL comments that pointed at them are gone, which roughly halves the size of each tarball.

    Debugging: the code is still unbundled and unminified, one readable file per module, so a stack trace points at real code with real names. Line numbers in a stack trace are one higher than before, because of the banner line.

    A 'use client' directive stays the first statement of its module (the banner is a comment above it), so React Server Component boundaries are unchanged.

    No API change, no runtime behavior change, and nothing to do on upgrade.

    In @wabbit/tome-blocks-gallery, the source snapshots extractGallerySource writes from an installed pack leave out the license banner line, so a component or config snapshot starts at the code and a paid block's preview shows its first 15 lines of real code.

  • 0.3.3
    Internal refactor: collection slugs are now typed through one package-internal helper instead of inline casts scattered across the source.

    No API or behavior change.

  • 0.3.3
    The root barrel now loads under plain Node (Payload CLI), and the award utilities honor renamed collections.

    The root barrel no longer evaluates server-only, so a payload.config.ts that imports this package loads under plain Node (Payload CLI generate:types, migrate). awardPoints and checkAndAwardBadges now honor renamed collections: both take an optional trailing slugs argument (GamificationSlugs), and the copies on createGamificationLayer(config).utilities are bound to the config's pointsSlug/badgesSlug/achievementsSlug.

    ./server is unchanged and still guarded.

  • 0.3.2
    One LayerFactoryConfig every layer factory's config extends, and one factory verb.

    Fourteen layer packages end in the same one call a consumer writes into payload.config.ts, and no two agreed on what config may contain: full seam vocabulary in three (org, lms, ledger), partial in six, NONE in six (2026-09-01 sale-readiness audit §5.3). A site that learned adminGroup from org and hooks from sc discovered, package by package, that six factories accept neither — not because the seam had been rejected, but because nothing said it existed.

    New in core (a NEW exports-map subpath, hence the minor): @wabbit/tome-core/utilities/layerFactoryConfig exports the LayerFactoryConfig interface — adminGroup, access (per-collection override map), hooks (appended via mergeHooks, never replacing), extraFields, fieldOverrides, omitFields, fieldOrder, slugs — and applyLayerFactoryConfig(collections, config), which honors the whole vocabulary in one call and one fixed order (adminGroup → access → hooks → field shape, the last delegated to fields/fieldShape's applyFieldShape so the order cannot drift between layers). Pure: new array, new objects, identity return on an empty config.

    It is a separate subpath from ./utilities/layerRegistry deliberately — that module is in core's sideEffects array, and a pure type/vocabulary module should not drag a declared side-effecting module into every factory's type graph.

    The slug convention is documented rather than forced, because both live shapes are right for what they do: a typed slugs?: Partial<XSlugs> map for the slugs a layer OWNS (org, sc, accounts — the typed key set makes a typo a compile error, and a homomorphic mapped type satisfies the base's Record<string, string | undefined>), and named <name>Slug?: string scalars for relationship targets in OTHER layers (memberSlug, mediaSlug, eventSlug, rolesSlug) — those are pointers out of a layer, not entries in its key set.

    *Every `createLayer config now extends it.** Twelve extend LayerFactoryConfig directly and APPLY it through applyLayerFactoryConfig (accounts, catalog, crm, crowdfund, deals, fulfillment, lms, marketing, org, sc) or through a targeted application (chrome). Additive in every case: for the six that accepted none of the seams (deals, economy, gamification, marketing, plus forms/intake, see below), the fields are new; for the rest, adminGroup` and friends keep their existing meaning and the applier is a no-op when they are omitted.

    Two packages accept the vocabulary but do NOT yet apply it, and say so in their type's JSDoc in the required form ("accepted, not yet applied — trigger: …"). economy and gamification both declare @wabbit/tome-core as an OPTIONAL peer and hold zero runtime imports of it — gamification reaches registerLayer through a lazy require() in a try/catch for exactly this reason.

    applyLayerFactoryConfig is a runtime VALUE, so importing it at module scope would convert an optional peer into a required one and break every site that installs those packages without core; copying the applier locally is barred by assert:no-forked-primitives. The trigger is stated: the day core becomes a required peer, delete the note and add one line.

    Both take the type via import type, which is erased at runtime.

    Two packages drop seams EXPLICITLY rather than accept-and-ignore. chrome extends Omit<LayerFactoryConfig, 'access' | 'hooks' | 'extraFields' | 'fieldOverrides' | 'omitFields' | 'fieldOrder' | 'slugs'> because it returns Payload GLOBALS, not collections — those seven are keyed by collection slug and typed against CollectionConfig, and chrome's slugs already have direct per-surface knobs (header.slug, footer.slug) a parallel map could contradict.

    The one seam it keeps, adminGroup, IS applied: globals carry admin.group exactly as collections do. rpg extends Omit<LayerFactoryConfig, 'access'> because CharacterSheetsConfig is a single collection's config that doubles as the layer factory's config, and its own access already means "this collection's access object" — one level shallower than the base's slug-keyed map.

    Two meanings under one name is the confusion this interface exists to end.

    Factory-verb convergence. Three verbs were live. createWorkflowLayer(config?) is new in @wabbit/tome-workflow (a new export — hence the minor) and returns a spreadable, deliberately EMPTY CollectionConfig[]: this layer is an engine, not a collection set, so the empty array is the honest answer and lets ...createWorkflowLayer() compose exactly like every sibling.

    Its WorkflowLayerConfig omits every seam for the same reason, and exists as the stable place a real option will land. createGamificationLayer and createRpgLayer are pure aliases of registerGamificationLayer / registerRpgLayer.

    initWorkflow, registerGamificationLayer and registerRpgLayer are all @deprecated with sunset at each package's next major; none is removed.

    Forcing function: scripts/assert-layer-factory-contract.mjs + pnpm assert:layer-factory-contract, wired into platform-discipline.yml after assert:layer-version (source reading only, pre-build). Every exported create*Layer must take a config parameter whose type resolves to LayerFactoryConfig — through extends, an intersection, or an explicit Omit<…> — with verb aliases followed to their register*/init* target.

    Before this change it reported 12 violations and 0 conforming; it now reports 15 conforming, 0 violations. Deliberately NOT checked: whether a factory actually applies what it accepts, because a machine cannot tell a documented deferral from an accident, and a gate that forced silent application would be worse than one that forces a stated deferral. docs/guides/create-a-new-layer-package.md gains a "The factory contract" section stating the rule and the three permitted responses.

    Three factories are ALLOWLISTED with a reason each: createAiLayer returns credential wiring and owns no collections, so every seam is meaningless to it; createFormsLayer and createIntakeLayer are owned by the forms+intake access wave running in parallel, whose changes rewrite the same files.

    Peer floors: accounts, catalog, chrome, crm, deals, economy, fulfillment, gamification, marketing and rpg raise @wabbit/tome-core to >=1.14.0 <2.0.0. The new subpaths do not exist below that, and a too-low floor is how ERR_PACKAGE_PATH_NOT_EXPORTED reached crowdfund's consumers once already.

    These are marked patch because the config widening is purely additive; the raised required-peer floor is the reason a release manager may prefer to cut them as minors instead.

  • 0.3.2
    Pin each layer's registered version to package.json instead of a hand-typed literal.

    registerLayer(name, { version }) is the contract a consumer reads back through hasLayer/getLayer to gate on a layer's capability. Eight packages passed a literal that nobody compared to the manifest, so an up-to-date install advertised an old contract and every gate keyed on it failed silently — nothing throws when a version string is stale.

    PackageRegisteredActual
    @wabbit/tome-rpg'0.1.2'0.2.2
    @wabbit/tome-gamification'0.1.0'0.3.1
    @wabbit/tome-crm'0.3.0'0.5.0
    @wabbit/tome-ai'0.1.0'0.4.0
    @wabbit/tome-formsTOME_FORMS_VERSION = '0.1.0'0.3.2
    @wabbit/tome-intakeTOME_INTAKE_VERSION = '0.1.0'0.3.1
    @wabbit/tome-marketing'0.1.0'0.4.0
    @wabbit/tome-chrome'0.6.0'0.8.5

    Each package now carries a leaf src/version.ts exporting <NAME>_LAYER_VERSION, read by its registerLayer call — the shape nine sibling packages (accounts, catalog, crowdfund, deals, economy, fulfillment, ledger, lms, org, workflow) already used and stayed accurate with. Forms' and intake's module-local TOME_*_VERSION consts move into that module: a _named_ constant was never the guarantee, a _pinned_ one is.

    The forcing function ships with the fix. pnpm assert:layer-version (new, wired into platform-discipline.yml pre-build) parses every registerLayer call in the repo, resolves its version argument through literals and consts, and fails on any disagreement with the manifest — so this cannot recur in a package that never gets around to writing the test.

    Seven of these eight were found by the 2026-09-01 sale-readiness audit; chrome was found by the assert itself on its first run.

    crm, forms, intake, marketing and rpg gained their first test suite in the process (tests/layer-version.test.ts) and were removed from the assert:test-floor starting-debt allowlist.

    No runtime behavior changes for a consumer already on a current install — the version a layer reports simply becomes true.

  • 0.3.2
    Manifest metadata: homepage, bugs, engines.

    All 46 publishable manifests were missing the three fields a consumer sees before any code (2026-09-01 sale-readiness audit §6). Metadata only — no source, no build, no runtime change.

    • homepage deep-links to that package README on GitHub (.../tree/main/packages/<dir>#readme). Without it a registry page links to the monorepo root and the reader has to guess which of 46 folders they want.
    • bugs.url points at the repo issue tracker, so a paying customer has a place to report a defect that is not email.
    • engines.node is >=22, matching the root engines and .nvmrc set the same day. This is a real floor, not decoration: CI on Node 20 could not expand the glob the block packs use for node --test, and a package installed on Node 20 fails at a runtime the installer cannot connect back to the version.

    The forcing function ships with the change: scripts/assert-manifest-metadata.mjs (root pnpm assert:manifest-metadata, wired into platform-discipline.yml beside assert:license-metadata) fails when any publishable manifest lacks description, repository.directory matching its own folder, homepage, bugs, engines.node equal to the repo floor, license, files or sideEffects. It reported 138 violations before this change and 0 after.

  • 0.3.1

    Wave 3 LMS absorbs + platform fixes. lms: certificate-helper enum repairs (valid vs active — verification could never succeed), own-record access resolves MEMBER id, effectiveAwardStatus predicates, award-chain gate + converging reconciler job, certificate expiry sweep (fixes the stalled T-7/T-0 progression), instructorRoles via rolesSlug knob, createdBy/owner authority split, prerequisiteStrictness (G1), ojt_signoff lessonType with approveOJT guard enforced, packaging guards wired. gamification: dist ships extensioned specifiers (raw-Node loadable; lms barrel dependency).

0.2.x

2 fixes (0.2.1)
  • 0.2.1
    Every package now declares an explicit sideEffects field (38 added; motion/engine/forms already correct).

    Registration-bearing modules (render files' registerRenderer, blocks/*/index.ts defineBlock self-registration, widget register.ts files, productHooks, permission self-registrations, print templates, chrome built-in variants) are listed so bundlers can tree-shake everything else WITHOUT dropping import-time registrations — previously the field was unset, which blocked cross-module tree-shaking through the barrels entirely. Never blanket false on a package with registration or CSS.

  • 0.2.1

    DRY adoption sweep (the audit's "adoption, not extraction" rule): crm/deals capability presets delegate to core's sessionHasCapabilityOrLegacyAdmin; new core buildOwnershipWhere/ownershipOrBypass (via ./access) adopted by core's vendorScoped, catalog's vendor-scoping, and org's ownOrScoped (public APIs unchanged); slugField() adopted at 7 sites where semantics matched exactly (core lms collections + createMemberCollection — replacing a third independent slugify), with ~25 sites honestly skipped for named semantic divergences (auto-regenerate-on-clear vs allow-empty, collection-level hook pattern) now listed as core-enhancement candidates; new formatDisplayDate in blocks-core utilities (UTC-pinned, hydration-safe) adopted at 5 verified-identical sites; lms-ui consolidates its two certificate date formatters locally; useMediaQuery/useIsMobile published from tome-ui and adopted by AppShell + admin's SidebarProvider; gamification's awardPoints now uses the authoritative getPointsBalance (fixes a divergent 1000-row scan cap vs the correct 10000).

Full changelog across all packages