feedBack/static/v3/stats-recorder.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

179 lines
8.7 KiB
JavaScript

/*
* fee[dB]ack v0.3.0 — song-stats recorder (core glue).
*
* Bridges the highway note-detection scorer to the core song_stats store
* (POST /api/stats). The scorer lives in the OPTIONAL external plugin
* feedBack-plugin-notedetect, which emits `note:hit` / `note:miss` per note
* on window.feedBack — note-detection is a DEFERRED capability domain, so we
* use those legacy events directly (design/05-capability-pipelines.md). If the
* plugin isn't installed, no note events fire and nothing is recorded
* (graceful degrade — the dashboard simply shows no accuracy).
*
* Two paths:
* • Scored session: tally hits/misses (or accept an explicit
* `note_detect:session-ended` summary), then POST on song end.
* • Resume position: a lightweight POST of the play position (as the
* `lastPlayPosition` field, which /api/stats accepts alongside
* `last_position`) on pause/stop so Continue-Playing works for non-scored
* plays.
*
* Score/accuracy formula mirrors lib/song_score.py so badge == server.
*/
(function () {
'use strict';
const sm = window.feedBack;
if (!sm || typeof sm.on !== 'function') return;
let cur = null; // active session
let recordedThisSession = false;
function reset(filename, arrangement) {
cur = {
filename: filename || null,
arrangement: Number.isFinite(arrangement) ? arrangement : 0,
hits: 0, misses: 0, streak: 0, bestStreak: 0,
scored: false, lastTime: 0,
};
recordedThisSession = false;
}
// accuracy = hits / max(1, hits+misses); score = round(hits*100*accuracy)
function accuracyOf(hits, misses) { return hits / Math.max(1, hits + misses); }
function scoreOf(hits, misses) { return Math.round(hits * 100 * accuracyOf(hits, misses)); }
async function post(body) {
try {
const r = await fetch('/api/stats', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
try { return await r.json(); } catch (e) { return null; }
} catch (e) { return null; /* offline / endpoint absent — non-fatal */ }
}
// Fan the scored-POST outcome out to the progression core (challenge/quest
// completion events + state refresh). Summary may be null (older server).
// `posted` is the stats body we sent; `natural` is true when the song ran
// to its end (vs. the user stopping early).
async function notifyProgression(response, posted, natural) {
const summary = response && response.progression;
if (window.v3Progression && typeof window.v3Progression.notify === 'function') {
// Await the notify promise (which wraps a refresh()) so the profile
// badge renders post-award rank/dB rather than stale cached values.
try { await window.v3Progression.notify(summary); } catch (e) { /* non-fatal */ }
}
// Calibration attempt feedback (spec 010): the diagnostic sloppak was
// played to the end with scoring but below 100% — surface it so the UI
// can offer a retry. Early quits don't prompt (the player bailed on
// purpose), and neither do replays once calibration is completed.
try {
const state = window.v3Progression && window.v3Progression.get && window.v3Progression.get();
const onboarding = (state && state.onboarding) || {};
if (natural && posted && posted.filename &&
posted.filename === onboarding.diagnostic_filename &&
onboarding.calibration_status !== 'completed' &&
!(summary && summary.calibration_completed) &&
typeof posted.accuracy === 'number' && posted.accuracy < 1) {
sm.emit('progression:calibration-attempt', { accuracy: posted.accuracy });
}
} catch (e) { /* feedback must never break stats recording */ }
}
function finalizeScored(position, natural) {
if (!cur || !cur.filename || recordedThisSession) return;
if (!cur.scored || (cur.hits + cur.misses) <= 0) return; // no real scoring this session
recordedThisSession = true;
const body = {
filename: cur.filename,
arrangement: cur.arrangement,
score: scoreOf(cur.hits, cur.misses),
accuracy: accuracyOf(cur.hits, cur.misses),
hits: cur.hits,
misses: cur.misses,
bestStreak: cur.bestStreak,
lastPlayPosition: Number.isFinite(position) ? position : cur.lastTime,
};
post(body).then(async (response) => {
await notifyProgression(response, body, !!natural);
// Refresh the profile badge AFTER the progression state moved so
// the rank/dB it renders are post-award values.
if (window.v3Profile && typeof window.v3Profile.refresh === 'function') {
window.v3Profile.refresh();
}
// Tell the library the song's best score may have changed so its
// card/list badge refreshes without waiting for a restart.
sm.emit('stats:recorded', { filename: body.filename, arrangement: body.arrangement });
});
}
function touchPosition(position) {
if (!cur || !cur.filename) return;
// Allow 0: restarting a song and stopping at the very beginning must be
// able to clear a stale Continue offset. Only negatives are invalid.
if (!Number.isFinite(position) || position < 0) return;
post({ filename: cur.filename, arrangement: cur.arrangement, lastPlayPosition: position });
}
// ── Session lifecycle ─────────────────────────────────────────────────--
sm.on('song:loading', (e) => {
const d = (e && e.detail) || {};
reset(d.filename, d.arrangement == null ? 0 : Number(d.arrangement));
});
sm.on('song:arrangement-changed', (e) => {
const d = (e && e.detail) || {};
// Arrangement switch restarts scoring — treat as a fresh session.
reset(d.filename || (cur && cur.filename), d.arrangement == null ? 0 : Number(d.arrangement));
});
// ── Per-note tally (from the note_detect plugin) ──────────────────────--
sm.on('note:hit', () => {
if (!cur) reset(null, 0);
cur.scored = true; cur.hits++; cur.streak++;
if (cur.streak > cur.bestStreak) cur.bestStreak = cur.streak;
});
sm.on('note:miss', () => {
if (!cur) reset(null, 0);
cur.scored = true; cur.misses++; cur.streak = 0;
});
// Track latest position for finalize fallbacks.
sm.on('song:position-changed', (e) => {
const t = e && e.detail && e.detail.time;
if (cur && Number.isFinite(t)) cur.lastTime = t;
});
// ── Authoritative explicit summary (if the plugin emits one) ──────────--
sm.on('note_detect:session-ended', (e) => {
const d = (e && e.detail) || {};
if (!d.filename || recordedThisSession) return;
recordedThisSession = true;
const body = {
filename: d.filename,
arrangement: d.arrangement == null ? 0 : Number(d.arrangement),
score: d.score, accuracy: d.accuracy,
hits: d.hits, misses: d.misses, bestStreak: d.bestStreak,
lastPlayPosition: d.lastPlayPosition,
};
post(body).then(async (response) => {
// The plugin's explicit summary is an authoritative session end —
// treat it as a natural finish for calibration-retry feedback.
await notifyProgression(response, body, true);
if (window.v3Profile && typeof window.v3Profile.refresh === 'function') window.v3Profile.refresh();
sm.emit('stats:recorded', { filename: body.filename, arrangement: body.arrangement });
});
});
// ── Finalize / resume-position ────────────────────────────────────────--
sm.on('song:ended', (e) => finalizeScored(e && e.detail && e.detail.time, true));
sm.on('song:pause', (e) => touchPosition(e && e.detail && e.detail.time));
sm.on('song:stop', (e) => {
// Record the scored session if it wasn't already (e.g. user closed the
// player before the track ended), then persist the resume position.
// Not a natural end — no calibration-retry prompt for deliberate quits.
const t = e && e.detail && e.detail.time;
finalizeScored(t, false);
touchPosition(t);
});
})();