feat: v2.9.0 终端实时面板,学习时长与学分统计
新增 runner/tui.mjs(~470 行):前台运行时自动开一块实时面板, 框线布局显示状态 / 课程 / 学时 / 课时 / 视频进度条 / 进度 / 最近日志。 - 学习统计 runner/study.mjs:只累计视频真在播的时长(暂停、重试退避、 页面加载都不计),学分按课程去重(重试 / 刷新不重复计) - 页面侧新增 credit 快照:平台只把本门学分写在倒计时文案里, 学完文案就消失,所以边播边记 - log.mjs 增加 setConsoleSink:面板接管终端那一路,日志文件仍是纯文本 - 面板里按 q 等价 Ctrl+C,走 stop.request 同一条优雅收尾路径 - 尺寸自适应:宽度跟随终端(不封顶 120 列),无 resize 事件的平台 靠定时刷新发现尺寸变化;变窄时按旧帧在新宽度下的折行数回退光标, 折完超过窗口高就整屏重置;宽度 / 高度不足按重要性丢字段 - 非 TTY(重定向 / 管道 / 计划任务 / --check)自动降级为逐行日志, 不写 ANSI 控制字符;--no-tui / YUN_XUE_TANG_TUI=0 可手动关闭, --no-color / NO_COLOR 关颜色 - 监控轮询从 2000ms 调到 1000ms,与面板刷新对齐 - supervisor 收尾时先收面板再逐行打退出日志,并汇总本次学习时长 / 学分 - check.mjs 增加裸 CR 检查:脚本里的 config\runner 曾被写成裸 CR, 变成 config<CR>unner 导致找不到配置文件 自测:npm run test:tui(渲染 / 列宽 / 极端尺寸)、npm run test:study(假时钟口径)
This commit is contained in:
1 parent
c1e883d67f
commit
65bf63ea6a
17 files changed
+1608
-28
No files matched your search
@@ -0,0 +1,111 @@
|
||||
// 学习统计:本次运行学了多久、拿到多少学分。
|
||||
//
|
||||
// 两个口径都是用户能直接验证的,所以规则要写死在这里、别散到面板里:
|
||||
// · 学习时长 = 视频真正在播的累计时间(暂停 / 排队 / 重试退避 / 页面加载都不算)
|
||||
// · 获得学分 = 本次运行里真正刷完的课程能拿到的学分之和(按课程去重)
|
||||
//
|
||||
// 数据都来自 page.mjs 的 EXPR_SNAPSHOT:视频状态来自 <video> 元素,
|
||||
// 学分来自倒计时文案「…可完成本课程学习并获得6.52学分,加油!」。
|
||||
// 学完之后那个倒计时元素就消失了,所以学分必须边播边记进 creditOf。
|
||||
//
|
||||
// 这里不读时钟以外的任何东西,也不太碰终端——所以能脱离浏览器和 TTY 直接测。
|
||||
/** 两次快照间隔超过这个秒数就认为中间断过(浏览器重启 / 进程被挂起),不补计 */
|
||||
const MAX_GAP_SECONDS = 60;
|
||||
|
||||
/**
|
||||
* @param {object} [opts]
|
||||
* @param {Function} [opts.now] 取当前时间(毫秒),测试里可换成假时钟
|
||||
*/
|
||||
export function createStudyTracker(opts = {}) {
|
||||
const now = opts.now || (() => Date.now());
|
||||
|
||||
let studySeconds = 0; // 本次运行累计播放时长(秒)
|
||||
let creditEarned = 0; // 本次运行累计获得学分
|
||||
let credit = null; // 当前这门课学完能拿多少学分(平台只在播放期间写出来)
|
||||
let lastAt = 0; // 上一条快照的时间,用来算两次之间播了多少秒
|
||||
let lastPlaying = false;
|
||||
let lastCompleted = 0;
|
||||
let lastCourse = '';
|
||||
|
||||
const creditOf = new Map(); // 课程名 → 可得学分(学完后元素消失,只能靠缓存)
|
||||
const credited = new Set(); // 已计入的课程,防止重试 / 刷新页面重复累加
|
||||
|
||||
/**
|
||||
* 吃一条主监控快照,返回最新的统计值。
|
||||
* 传进来的快照字段可能缺失(页面刚跳转时就是半空的),所以全部按可选处理。
|
||||
*/
|
||||
function update(snap) {
|
||||
if (!snap || snap.noApi) return get();
|
||||
const at = now();
|
||||
const course = snap.course || '';
|
||||
|
||||
// ---- 学习时长:只算视频真的在播的时间 ----
|
||||
// 用「两次快照的真实间隔」而不是写死的轮询周期:CDP 往返偶尔会慢,
|
||||
// 写死一个常数会让长跑下来明显偏少或偏多。间隔过大说明中间断过,直接丢弃不补计。
|
||||
// 只有「上一帧在播、这一帧也在播」才计:宁可少算一秒,不能把
|
||||
// 暂停后挂机的时间 算成学习时长(挂机好几分钟才是常态)。
|
||||
const playing = !!(snap.video && !snap.video.paused);
|
||||
if (lastPlaying && playing && lastAt > 0) {
|
||||
const gap = (at - lastAt) / 1000;
|
||||
if (gap > 0 && gap <= MAX_GAP_SECONDS) studySeconds += gap;
|
||||
}
|
||||
lastPlaying = playing;
|
||||
lastAt = at;
|
||||
|
||||
// ---- 学分:边播边记「这门课值多少分」----
|
||||
const read = typeof snap.credit === 'number' && snap.credit > 0 ? snap.credit : null;
|
||||
if (read != null) {
|
||||
credit = read;
|
||||
if (course) creditOf.set(course, read);
|
||||
} else if (course && creditOf.has(course)) {
|
||||
credit = creditOf.get(course); // 倒计时消失(学完了)后沿用缓存值
|
||||
} else if (course && course !== lastCourse) {
|
||||
credit = null; // 换课了,新课的学分还没读出来
|
||||
}
|
||||
|
||||
// ---- 学分:只给真正刷完的课发分 ----
|
||||
const completed = Number(snap.completed) || 0;
|
||||
if (completed > lastCompleted) {
|
||||
// 完成数涨了:刚刷完的那门课此刻可能已经从快照里切走,所以用上一次记的课程名
|
||||
const justDone = lastCourse || course;
|
||||
if (justDone && creditOf.has(justDone) && !credited.has(justDone)) {
|
||||
credited.add(justDone);
|
||||
creditEarned += creditOf.get(justDone);
|
||||
}
|
||||
lastCompleted = completed;
|
||||
}
|
||||
if (course) lastCourse = course;
|
||||
|
||||
return get();
|
||||
}
|
||||
|
||||
/** 当前统计值(不会被外部改到内部状态) */
|
||||
function get() {
|
||||
// 学分保留两位小数:平台给的本来就是 0.23 / 6.52 这种,浮点累加会出现 3.7499999
|
||||
return { studySeconds, credit, creditEarned };
|
||||
}
|
||||
|
||||
/** 日志 / 收尾用的一句话(没有数据时返回空串,调用方直接跳过) */
|
||||
function summary() {
|
||||
if (studySeconds <= 0 && creditEarned <= 0) return '';
|
||||
return '视频累计播放 ' + fmt(studySeconds) + ',获得 ' + creditText() + ' 学分';
|
||||
}
|
||||
|
||||
/** 学分文本:整数不带小数点,小数保留最多两位并去掉尾随 0 */
|
||||
function creditText(value) {
|
||||
const v = value === undefined ? creditEarned : value;
|
||||
return String(Number(v.toFixed(2)));
|
||||
}
|
||||
|
||||
return { update, get, summary, creditText, reset() { studySeconds = 0; creditEarned = 0; credit = null; lastAt = 0; lastPlaying = false; lastCompleted = 0; lastCourse = ''; creditOf.clear(); credited.clear(); } };
|
||||
}
|
||||
|
||||
/** 秒 → 1:23:45 / 23:45(与面板显示保持一致,面板自己那份在 tui.mjs) */
|
||||
function fmt(sec) {
|
||||
const s = Math.max(0, Math.round(Number(sec) || 0));
|
||||
const h = Math.floor(s / 3600);
|
||||
const two = (n) => String(n).padStart(2, '0');
|
||||
const m = Math.floor((s % 3600) / 60);
|
||||
const r = s % 60;
|
||||
return h > 0 ? h + ':' + two(m) + ':' + two(r) : two(m) + ':' + two(r);
|
||||
}
|
||||
Reference in new issue
Block a user