feat(progression): fancy notifications for quest/path progress + completion (#552)

* feat(progression): fancy notifications for quest/path progress + completion (#551)

Surface achievement feedback as in-app toasts when the player advances or
finishes a daily/weekly quest, and when they progress or level up an
instrument path.

- progression-core.js: _diff() now emits two partial-advance events —
  quest-progressed (a still-incomplete quest whose count rose) and
  path-progressed (a challenge toward the next level completed without a
  level-up). Both are guarded so the increment that COMPLETES a quest /
  the level-up itself stays a single quest-completed / path-level-up event
  (no double toast). Period rollovers and brand-new quest ids emit nothing.
  New events added to the capability owner's declared events list.
- notifications.js (new): reusable window.fbNotify toast surface (stacked,
  animated, auto-dismiss; animation + accent via inline styles so no new
  Tailwind utilities) + progression wiring — subtle toasts for advances,
  celebratory toasts for quest completion, path level-up, and rank-up.
- index.html: load notifications.js after progression-core.
- tests: progression_progress_events (diff emission + guards) and
  progression_notifications (toast rendering + wiring) — 11 cases.

No backend change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(progression): unwrap CustomEvent .detail in notification handlers

Codex P2: window.slopsmith.on delivers a CustomEvent (bus.on →
addEventListener), so the progression payload is e.detail — not the raw
argument. All five notifications.js handlers read the arg directly, so in
the browser every field was undefined (e.g. rank-changed never toasted).
Unwrap e.detail in each handler, matching every other sm.on consumer.

The test harness masked this by invoking handlers with raw payloads; it now
wraps them as {detail: payload} like the real bus, so the unwrap is actually
exercised (the tests fail without the fix).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Byron Gamatos
2026-06-21 21:18:28 +02:00
committed by GitHub
co-authored by Claude Opus 4.8
parent a0867f8bfd
commit 63eb7a4ffc
5 changed files with 449 additions and 4 deletions
+52 -4
View File
@@ -14,9 +14,11 @@
*
* Lifecycle events are emitted on the capability surface and mirrored on
* window.slopsmith as `progression:*` for non-capability consumers:
* challenge-completed, quest-completed, path-level-up, rank-changed,
* db-changed, calibration-completed, cosmetic-equipped (+ progression:updated
* whenever fresh state lands).
* challenge-completed, quest-completed, quest-progressed, path-level-up,
* path-progressed, rank-changed, db-changed, calibration-completed,
* cosmetic-equipped (+ progression:updated whenever fresh state lands).
* quest-progressed / path-progressed are the partial-advance counterparts to
* the *-completed / *-level-up events (the achievement-toast feed).
*
* Vanilla JS, no framework (constitution P-II).
*/
@@ -37,6 +39,19 @@
}
}
// Index quests by "period:id" so a refresh can be diffed against the last
// state (period_type isn't on the per-quest payload, so carry it here).
function _questIndex(state) {
const out = {};
const quests = (state && state.quests) || {};
['daily', 'weekly'].forEach((period) => {
(((quests[period] || {}).quests) || []).forEach((item) => {
if (item && item.id != null) out[period + ':' + item.id] = { period, item };
});
});
return out;
}
function _diff(prev, next) {
if (!prev || !next) return;
if (prev.mastery_rank !== next.mastery_rank) {
@@ -45,6 +60,38 @@
const before = (prev.wallet || {}).balance;
const after = (next.wallet || {}).balance;
if (before !== after) _emit('db-changed', { from: before, to: after, wallet: next.wallet });
// Quest "advance" — a still-incomplete quest whose count rose since the
// last state. The increment that COMPLETES a quest is intentionally left
// to quest-completed (emitted from notify()'s summary) so a finished
// quest surfaces once, not twice. A period rollover (count resets to 0,
// or a brand-new quest id) produces no event.
const prevQuests = _questIndex(prev);
const nextQuests = _questIndex(next);
Object.keys(nextQuests).forEach((key) => {
const pq = prevQuests[key];
const nq = nextQuests[key];
if (pq && !nq.item.completed && Number(nq.item.count) > Number(pq.item.count)) {
_emit('quest-progressed', Object.assign({ period_type: nq.period }, nq.item));
}
});
// Path "progress" — a challenge toward the next level completed
// (next.completed rose) WITHOUT a level-up. The level-up itself is
// emitted as path-level-up from notify()'s summary, so it surfaces once.
const prevPaths = {};
(prev.paths || []).forEach((p) => { if (p && p.id != null) prevPaths[p.id] = p; });
(next.paths || []).forEach((np) => {
const pp = prevPaths[np && np.id];
if (!pp || !np.next || !pp.next) return;
if (np.level === pp.level && Number(np.next.completed) > Number(pp.next.completed)) {
_emit('path-progressed', {
id: np.id, name: np.name, level: np.level,
next_level: np.next.level,
completed: np.next.completed, required: np.next.required,
});
}
});
}
async function refresh() {
@@ -135,7 +182,8 @@
ownership: 'exclusive-owner',
safety: 'safe',
commands: ['inspect', 'record-event', 'list-shop', 'buy-item', 'equip-item'],
events: ['challenge-completed', 'quest-completed', 'path-level-up', 'rank-changed',
events: ['challenge-completed', 'quest-completed', 'quest-progressed',
'path-level-up', 'path-progressed', 'rank-changed',
'db-changed', 'calibration-completed', 'cosmetic-equipped'],
description: 'Owns player progression: mastery rank, instrument-path challenges, daily/weekly quests, the Decibels wallet, and the cosmetics shop.',
handlers: {