feedBack/static/v3/progression-core.js
Bret Mogilefsky af2949677a
rename: slopsmith → feedBack, byron → got-feedBack (#537)
* Update GitHub repo references from feedback* to feedBack*

* rename: slopsmith -> feedBack, byron -> got-feedBack

Renames across the entire codebase:
- slopsmith/Slopsmith/SLOPSMITH/SlopSmith -> feedBack/FeedBack/FEEDBACK/FeedBack
- byron/Byron/Byrongamatos -> got-feedBack/got-feedBack/got-feedBack
- /home/byron/ -> /opt/got-feedBack/
- byron@ougsoft.com -> hi@got-feedBack.org
- github.com/byrongamatos/ -> github.com/got-feedback/
- com.byron. -> com.got-feedback.
- SLOPSMITH_ env vars -> FEEDBACK_ with backward-compat fallback
- Protocol/storage strings migrated with read-old/write-new pattern
- window.slopsmith JS API -> window.feedBack (canonical) + backward-compat alias

Refs: #rename-slopsmith

* rename: complete regen against current main + fix backward-compat alias

Regenerated the slopsmith->feedBack / byron->got-feedBack rename on top of
current main (3 commits had landed since the branch: #572/#554/#574),
resolving the four content conflicts in favour of main's newer content
(autoplay/auto-exit, accuracy-badge, Virtuoso re-home, feedpak badge).

Completion fixes on top of the mechanical rename:
- Re-apply rename to post-branch content the original rename never saw:
  window.slopsmith(.Tour) consumers in lessons.js / notifications.js /
  onboarding-tour.js, and the matching JS + python tests (autoplay_exit,
  progression_*, test_feedpak_extension FEEDBACK_* env vars). The test env
  vars now match server.py (which reads FEEDBACK_SYNC_STARTUP /
  FEEDBACK_SKIP_STARTUP_TASKS), so the sync-startup test exercises the real
  path again.
- Restore the window.slopsmith backward-compat alias dropped during conflict
  resolution, and move the bus aliases to AFTER the _feedBackExisting merge
  block so they reference the fully-assembled object (also fixes the
  loop_api.test.js API-surface regex, which the original PR latently broke).
- Drop the stray empty data/web_library.db (runtime DB lives in CONFIG_DIR)
  and gitignore it.
- Fix stale tone-source test: feed[dB]ack -> fee[dB]ack to match shipped
  source labels.

Verified locally (org CI billing-blocked): JS 819/819 pass; pytest 1669
passed / 1683 collected with 0 import errors; zero residual slopsmith/byron
except the two intentional window.slopsmith aliases.

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

* rename: implement advertised backward-compat + prune dead community plugins

Address gaps where PR #537's "Backward compatibility" section was advertised
but not implemented, and clean up the community plugin list.

Env vars (FEEDBACK_* canonical, legacy SLOPSMITH_* honoured):
- New lib/env_compat.py (getenv_compat / env_flag_compat) + tests. server.py
  (_env_flag + all FEEDBACK_* reads), diagnostics_hardware, gp2midi and
  tailwind_rebuild now resolve the legacy alias, so existing SLOPSMITH_UI /
  SLOPSMITH_PLUGINS_DIR / etc. deployments keep working.
- Fix the rename collapsing plugins/__init__.py and minigames/routes.py from
  `FEEDBACK_PLUGINS_DIR or SLOPSMITH_PLUGINS_DIR` into a redundant
  `FEEDBACK_ or FEEDBACK_` (the fallback was silently lost).

Storage (app.js update-channel):
- Read feedBack-update-channel, fall back to legacy slopsmith-update-channel,
  and clear the legacy key on write — so a user's update-channel preference
  survives the rename instead of resetting to "stable".

Community plugin list (README): the rename rewrote third-party repo URLs we
don't own. Probed every one; their owners never renamed, so:
- Restore the 13 live community plugins to their real slopsmith-* names.
- Prune 6 that are 404 to the public (topkoa splitscreen/stems, OmikronApex
  tuner, Jafz2001 nam-rig-builder, DeathlySin song-preview, Erikcb91 shuffle).
- Fix a pre-existing Guitar Theory clone-command typo (nam-tone -> guitar-theory).

Verified: env_compat 7/7, JS 819/819, pytest 1690 collected / 0 import errors,
rename-sensitive + startup suites green.

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

---------

Co-authored-by: byrongamatos <xasiklas@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 11:03:01 +02:00

250 lines
12 KiB
JavaScript

/*
* fee[dB]ack v0.3.0 — progression core (spec 010).
*
* Shared client state for /api/progression (mastery rank, paths/challenges,
* quests, Decibels wallet) AND the owner of the `progression` capability
* domain (kind: command, safety: safe), so plugins coordinate through the
* capability pipeline instead of private globals:
*
* inspect → the full progression state
* record-event {type, payload} → whitelisted event intake (minigame_run);
* song_completed is server-derived and denied
* list-shop / buy-item / equip-item
* (buy/equip require authorization:'user-action')
*
* Lifecycle events are emitted on the capability surface and mirrored on
* window.feedBack as `progression:*` for non-capability consumers:
* 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).
*/
(function () {
'use strict';
const sm = window.feedBack = window.feedBack || {};
let _state = null; // last /api/progression payload
let _fetching = null; // in-flight refresh (coalesced)
function _emit(name, detail) {
const capabilities = sm.capabilities;
if (capabilities && capabilities.version === 1 && typeof capabilities.emitEvent === 'function') {
try { capabilities.emitEvent('progression', name, detail || {}); } catch (e) { /* non-fatal */ }
}
if (typeof sm.emit === 'function') {
try { sm.emit('progression:' + name, detail || {}); } catch (e) { /* non-fatal */ }
}
}
// 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) {
_emit('rank-changed', { from: prev.mastery_rank, to: next.mastery_rank });
}
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() {
if (_fetching) return _fetching;
_fetching = (async () => {
try {
const r = await fetch('/api/progression');
if (r.ok) {
const prev = _state;
_state = await r.json();
_diff(prev, _state);
_contributeDiagnostics();
if (typeof sm.emit === 'function') sm.emit('progression:updated', _state);
}
} catch (e) { /* offline — keep last-known state */ }
_fetching = null;
return _state;
})();
return _fetching;
}
// Fan a record-event / stats outcome summary out as lifecycle events, then
// refresh the cached state. Anything that receives a summary payload
// (stats-recorder, the minigames hub, the events command) feeds this.
function notify(summary) {
if (summary && typeof summary === 'object') {
(summary.challenges_completed || []).forEach((c) => _emit('challenge-completed', c));
(summary.quests_completed || []).forEach((q) => _emit('quest-completed', q));
(summary.level_ups || []).forEach((l) => _emit('path-level-up', l));
if (summary.calibration_completed) {
// Capture the pre-completion status NOW, before refresh() below
// flips the cached calibration_status to 'completed'. The handler
// prefers detail.prior_calibration_status over the cache, so this
// keeps the success modal correct even if the event is delivered
// after a refresh has already landed.
const prior = (_state && _state.onboarding && _state.onboarding.calibration_status) || null;
_emit('calibration-completed', { prior_calibration_status: prior });
}
}
return refresh(); // rank-changed / db-changed fall out of the diff
}
async function _post(url, body) {
const r = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body || {}),
});
let data = {};
try { data = await r.json(); } catch (e) { /* empty body */ }
return { ok: r.ok, status: r.status, data };
}
// ── Diagnostics (redaction-safe: counts + totals only, no song names) ────
function _contributeDiagnostics() {
const diagnostics = sm.diagnostics;
if (!diagnostics || typeof diagnostics.contribute !== 'function' || !_state) return;
try {
diagnostics.contribute('progression', {
schema: 'feedBack.progression.diag.v1',
mastery_rank: _state.mastery_rank,
calibration_status: (_state.onboarding || {}).calibration_status,
paths: (_state.paths || []).map((p) => ({ id: p.id, level: p.level, max_level: p.max_level })),
quests: {
daily: ((_state.quests || {}).daily || {}).quests
? _state.quests.daily.quests.filter((q) => q.completed).length + '/' + _state.quests.daily.quests.length
: null,
weekly: ((_state.quests || {}).weekly || {}).quests
? _state.quests.weekly.quests.filter((q) => q.completed).length + '/' + _state.quests.weekly.quests.length
: null,
},
wallet: _state.wallet || null,
});
} catch (e) { /* diagnostics must never break progression */ }
}
// ── Capability domain owner ──────────────────────────────────────────────
const _handled = (payload) => ({ outcome: 'handled', payload: payload || {} });
const _denied = (reason, payload) => ({ outcome: 'denied', reason, payload: payload || {} });
const _failed = (reason) => ({ outcome: 'failed', reason });
function _registerOwner() {
const capabilities = sm.capabilities;
if (!capabilities || capabilities.version !== 1 || typeof capabilities.registerOwner !== 'function') return;
capabilities.registerOwner('progression', {
pluginId: 'core.progression',
kind: 'command',
ownership: 'exclusive-owner',
safety: 'safe',
commands: ['inspect', 'record-event', 'list-shop', 'buy-item', 'equip-item'],
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: {
inspect: async () => _handled((await refresh()) || {}),
'record-event': async (ctx) => {
const payload = (ctx && ctx.payload) || {};
try {
const r = await _post('/api/progression/events',
{ type: payload.type, payload: payload.payload || {} });
if (!r.ok) return _denied(r.data.error || ('HTTP ' + r.status));
notify(r.data.progression);
return _handled(r.data.progression);
} catch (e) { return _failed('progression event intake unreachable'); }
},
'list-shop': async () => {
try {
const r = await fetch('/api/shop');
if (!r.ok) return _failed('HTTP ' + r.status);
return _handled(await r.json());
} catch (e) { return _failed('shop unreachable'); }
},
'buy-item': async (ctx) => {
if (!ctx || ctx.authorization !== 'user-action') {
return _denied('buy-item requires authorization: user-action');
}
try {
const r = await _post('/api/shop/buy', { item_id: (ctx.payload || {}).item_id });
if (!r.ok) return _denied(r.data.error || ('HTTP ' + r.status), r.data);
refresh();
return _handled(r.data);
} catch (e) { return _failed('shop unreachable'); }
},
'equip-item': async (ctx) => {
if (!ctx || ctx.authorization !== 'user-action') {
return _denied('equip-item requires authorization: user-action');
}
const payload = ctx.payload || {};
try {
const r = await _post('/api/shop/equip',
{ slot: payload.slot, item_id: payload.item_id == null ? null : payload.item_id });
if (!r.ok) return _denied(r.data.error || ('HTTP ' + r.status), r.data);
_emit('cosmetic-equipped', { slot: payload.slot, item_id: payload.item_id == null ? null : payload.item_id });
return _handled(r.data);
} catch (e) { return _failed('shop unreachable'); }
},
},
});
}
// ── Public API + boot ────────────────────────────────────────────────────
window.v3Progression = {
refresh,
notify,
get: () => _state,
};
_registerOwner();
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => { refresh(); }, { once: true });
} else {
refresh();
}
})();