Documentation menu
Documentation / Learning engine / gamification
Gamification
npm install @wabbit/tome-gamificationFirst install from our registry?
Get a registry token from your credentials page. You need a purchase that includes this package, or a Craft Library membership.
Add the registry and your token to the
.npmrcat the root of your project, with your token in place ofYOUR_TOKEN:@wabbit:registry=https://npm.wabbit.com/ //npm.wabbit.com/:_authToken=YOUR_TOKEN
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
| Peer | Range | Notes |
|---|---|---|
payload | >=3.67.0 | required |
@wabbit/tome-core | >=1.14.0 <2.0.0 | optional — 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
| Export | Subpath | Description |
|---|---|---|
createGamificationLayer(config?) | root | Builds 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, AchievementCollection | root | Collection factories / defaults |
awardPoints, checkAndAwardBadges | root | Ledger + 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, achievementWrite | root | Access predicates + role helpers |
GamificationCollectionConfig, BadgeTriggerContext, GamificationSlugs, GamificationRoleTier, GamificationLayer | root | Type surface |
getPointsBalance(memberId, payload, opts?) | @wabbit/tome-gamification/server | Sums 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/server | Same, 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/checkAndAwardBadgesutilities. It does not evaluateserver-only, so it loads under plain Node,tsx, a plain test runner, and Payload CLI commands such aspayload generate:typesandpayload migratethat load yourpayload.config.ts. The utilities share the ledger reader with@wabbit/tome-gamification/serverthrough 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 querypayload.find()directly withoverrideAccess: true, so importing this subpath into a client bundle would both fail theserver-onlybuild 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-rpgas the authoritative XP source of truth (RPG derives level/XP from this package's points ledger; thelevel/xpTotalcolumns 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 lazyrequire()in try/catch, never static import" governsregisterGamificationLayer(deprecated alias — usecreateGamificationLayer)'s registration call
Exports
| Import path |
|---|
@wabbit/tome-gamification |
@wabbit/tome-gamification/server |
Changelog
0.4.xMinor
getPointsBalanceandgetPointsSincetake optionalreasonsandexcludeReasonsfilters, and a newgetPointsDaysSincelists the calendar days a member earned points.- Unset filters behave exactly as before (every reason counts).
getPointsDaysSince(memberId, since, payload, { timeZone })returns sortedYYYY-MM-DDstrings in the given time zone (default UTC).- New
GAMIFICATION_REASONSconstant. The default reason options gainartifact-produced,gate-passed,quest-complete,daily-completeandhearth-save, so layers can write those rows without registering them first. A site passing its ownreasonOptionsstill controls the list.
0.3.xMinor
Role checks now understand a
rolesRELATIONSHIP, not just flat strings — unblocking admin gates that were silently shut.Three packages read
req.user.rolesby collecting only entries wheretypeof 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-adminis not one of them, so the highest-privilege role failed every check.
Fixed in tome-lms and tome-gamification:
readRolesaccepts flat names, populated Role docs ({slug}), the_populatedRolesenricher shape, and a flat singularrolefield.super-adminnow satisfies every tier, matching the platform-wide implicit'*'grant.- New
resolveRoleSlugs(req)/hasAnyRoleAsync/isAdminAsync/isDirectorAsync/isInstructorRoleAsync/isMaintainerRoleAsynchydrate unresolved IDs throughreq.payload, memoized onreq.contextso 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 canonicalsuper-adminslug (it previously matched only camelCasesuperAdmin). 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-floorallowlist.
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.ctsdeclarations, 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
.mapfiles and thesourceMappingURLcomments 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 snapshotsextractGallerySourcewrites 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 apayload.config.tsthat imports this package loads under plain Node (Payload CLIgenerate:types,migrate).awardPointsandcheckAndAwardBadgesnow honor renamed collections: both take an optional trailingslugsargument (GamificationSlugs), and the copies oncreateGamificationLayer(config).utilitiesare bound to the config'spointsSlug/badgesSlug/achievementsSlug../serveris unchanged and still guarded. - 0.3.2
One
LayerFactoryConfigevery 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 whatconfigmay 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 learnedadminGroupfrom org andhooksfrom 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/layerFactoryConfigexports theLayerFactoryConfiginterface —adminGroup,access(per-collection override map),hooks(appended viamergeHooks, never replacing),extraFields,fieldOverrides,omitFields,fieldOrder,slugs— andapplyLayerFactoryConfig(collections, config), which honors the whole vocabulary in one call and one fixed order (adminGroup → access → hooks → field shape, the last delegated tofields/fieldShape'sapplyFieldShapeso the order cannot drift between layers). Pure: new array, new objects, identity return on an empty config.It is a separate subpath from
./utilities/layerRegistrydeliberately — that module is in core'ssideEffectsarray, 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'sRecord<string, string | undefined>), and named<name>Slug?: stringscalars 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 extendLayerFactoryConfigdirectly and APPLY it throughapplyLayerFactoryConfig(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-coreas an OPTIONAL peer and hold zero runtime imports of it — gamification reachesregisterLayerthrough a lazyrequire()in a try/catch for exactly this reason.applyLayerFactoryConfigis 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 byassert: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 againstCollectionConfig, 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 carryadmin.groupexactly as collections do. rpg extendsOmit<LayerFactoryConfig, 'access'>becauseCharacterSheetsConfigis a single collection's config that doubles as the layer factory's config, and its ownaccessalready 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 EMPTYCollectionConfig[]: 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
WorkflowLayerConfigomits every seam for the same reason, and exists as the stable place a real option will land.createGamificationLayerandcreateRpgLayerare pure aliases ofregisterGamificationLayer/registerRpgLayer.initWorkflow,registerGamificationLayerandregisterRpgLayerare all@deprecatedwith sunset at each package's next major; none is removed.Forcing function:
scripts/assert-layer-factory-contract.mjs+pnpm assert:layer-factory-contract, wired intoplatform-discipline.ymlafterassert:layer-version(source reading only, pre-build). Every exportedcreate*Layermust take a config parameter whose type resolves toLayerFactoryConfig— throughextends, an intersection, or an explicitOmit<…>— with verb aliases followed to theirregister*/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.mdgains a "The factory contract" section stating the rule and the three permitted responses.Three factories are ALLOWLISTED with a reason each:
createAiLayerreturns credential wiring and owns no collections, so every seam is meaningless to it;createFormsLayerandcreateIntakeLayerare 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-coreto>=1.14.0 <2.0.0. The new subpaths do not exist below that, and a too-low floor is howERR_PACKAGE_PATH_NOT_EXPORTEDreached crowdfund's consumers once already.These are marked
patchbecause 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.jsoninstead of a hand-typed literal.registerLayer(name, { version })is the contract a consumer reads back throughhasLayer/getLayerto 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.Package Registered Actual @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.tsexporting<NAME>_LAYER_VERSION, read by itsregisterLayercall — 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-localTOME_*_VERSIONconsts 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 intoplatform-discipline.ymlpre-build) parses everyregisterLayercall in the repo, resolves itsversionargument 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 theassert:test-floorstarting-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.
homepagedeep-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.urlpoints at the repo issue tracker, so a paying customer has a place to report a defect that is not email.engines.nodeis>=22, matching the rootenginesand.nvmrcset the same day. This is a real floor, not decoration: CI on Node 20 could not expand the glob the block packs use fornode --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(rootpnpm assert:manifest-metadata, wired intoplatform-discipline.ymlbesideassert:license-metadata) fails when any publishable manifest lacksdescription,repository.directorymatching its own folder,homepage,bugs,engines.nodeequal to the repo floor,license,filesorsideEffects. 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
sideEffectsfield (38 added; motion/engine/forms already correct).Registration-bearing modules (render files'
registerRenderer,blocks/*/index.tsdefineBlockself-registration, widgetregister.tsfiles, 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 blanketfalseon 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 corebuildOwnershipWhere/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; newformatDisplayDatein blocks-core utilities (UTC-pinned, hydration-safe) adopted at 5 verified-identical sites; lms-ui consolidates its two certificate date formatters locally;useMediaQuery/useIsMobilepublished from tome-ui and adopted by AppShell + admin's SidebarProvider; gamification'sawardPointsnow uses the authoritativegetPointsBalance(fixes a divergent 1000-row scan cap vs the correct 10000).