File size: 11,360 Bytes
65d68f5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | import type { SourceRef } from "./sessionData";
import type { MissionCatalog, MissionCatalogChapter, MissionSelection } from "./missionAdapter";
import {
getChapterProgress,
getMissionProgress,
isMissionComplete,
} from "./missionProgress";
import { getDueRevisionItems } from "./revisionScheduler";
/**
* Local Today Study Queue.
*
* Decides what DocDoe should suggest first when /tuition opens, using only the
* existing local data (revision scheduler, mission progress, chapter progress)
* plus the mission catalog from the metadata API. Pure — no React, no writes.
* source_refs are carried through unchanged when available.
*/
export type StudyQueueType = "revision" | "continue_mission" | "next_mission" | "chapter_complete";
export type StudyQueueItem = {
type: StudyQueueType;
priority: number;
title: string;
subtitle: string;
discipline: string;
chapterId: string;
chapterTitle: string;
chapterNo: number;
missionId: string;
missionTitle: string;
source_refs?: SourceRef[];
reason: string;
/** Selection to switch to (mission actions). */
selection?: MissionSelection;
/** Revision item id (revision actions). */
revisionItemId?: string;
};
export const STUDY_QUEUE_PRIORITY = {
overdue_revision: 100,
due_revision: 80,
continue_mission: 70,
next_mission: 50,
chapter_complete: 10,
} as const;
type MissionDescriptor = {
discipline: string;
chapterId: string;
chapterTitle: string;
chapterNo: number;
missionId: string;
missionTitle: string;
};
function missionShortTitle(title: string): string {
return title.replace(/^M\d+:\s*/i, "").trim() || title;
}
function startOfDay(now: number): number {
const date = new Date(now);
date.setHours(0, 0, 0, 0);
return date.getTime();
}
function chapterMetaById(
catalog: MissionCatalog | null,
chapterId: string,
): { discipline: string; chapterNo: number; chapterTitle: string } | null {
if (!catalog) return null;
for (const subject of catalog.subjects) {
const chapter = subject.chapters.find((item) => item.chapter_id === chapterId);
if (chapter) {
return { discipline: chapter.discipline || "", chapterNo: chapter.chapter_no, chapterTitle: chapter.chapter_title };
}
}
return null;
}
export function chapterForSelection(
catalog: MissionCatalog | null,
selection: MissionSelection,
): MissionCatalogChapter | null {
if (!catalog) return null;
const subject = catalog.subjects.find((s) => s.subject === selection.subject);
if (!subject) return null;
return (
subject.chapters.find(
(chapter) => chapter.chapter_id === selection.chapterId || (chapter.discipline === selection.discipline && chapter.chapter_no === selection.chapterNo),
) ?? null
);
}
function flatMissions(catalog: MissionCatalog | null): MissionDescriptor[] {
const out: MissionDescriptor[] = [];
for (const subject of catalog?.subjects ?? []) {
for (const chapter of subject.chapters) {
for (const mission of chapter.missions) {
out.push({
discipline: chapter.discipline || "",
chapterId: chapter.chapter_id,
chapterTitle: chapter.chapter_title,
chapterNo: chapter.chapter_no,
missionId: mission.mission_id,
missionTitle: mission.mission_title,
});
}
}
}
return out;
}
function toSelection(descriptor: MissionDescriptor, subjectName: "Social Science" | "Physics" = "Social Science"): MissionSelection {
return { board: "Kerala SSLC", classLevel: "10", subject: subjectName, discipline: descriptor.discipline, chapterNo: descriptor.chapterNo, chapterId: descriptor.chapterId, missionId: descriptor.missionId };
}
/** The mission the student currently has selected, as a descriptor. */
export function resolveCurrentMission(
catalog: MissionCatalog | null,
selection: MissionSelection,
): MissionDescriptor | null {
const chapter = chapterForSelection(catalog, selection);
if (!chapter) return null;
const mission = chapter.missions.find((item) => item.mission_id === selection.missionId);
return {
discipline: chapter.discipline || "",
chapterId: chapter.chapter_id,
chapterTitle: chapter.chapter_title,
chapterNo: chapter.chapter_no,
missionId: selection.missionId,
missionTitle: mission?.mission_title ?? selection.missionId,
};
}
/** The saved progress for the currently selected mission (or null). */
export function resolveCurrentMissionProgress(
studentId: string,
catalog: MissionCatalog | null,
selection: MissionSelection,
) {
const current = resolveCurrentMission(catalog, selection);
if (!current) return null;
return getMissionProgress(studentId, current.chapterId, current.missionId);
}
export type NextMissionResolution = MissionDescriptor & { unlocked: boolean; reason: string };
/**
* The next mission the student should do. If the current mission is in progress
* it returns the mission that comes after it ("up next"); otherwise it returns
* the first not-completed mission at or after the current one (the current
* mission itself when it is fresh, or the next chapter's first mission when the
* current chapter is complete).
*/
export function resolveNextUnlockedMission(
studentId: string,
catalog: MissionCatalog | null,
selection: MissionSelection,
): NextMissionResolution | null {
const flat = flatMissions(catalog);
if (flat.length === 0) return null;
const current = resolveCurrentMission(catalog, selection);
const currentIndex = current
? flat.findIndex((item) => item.chapterId === current.chapterId && item.missionId === current.missionId)
: -1;
const start = currentIndex >= 0 ? currentIndex : 0;
const currentProgress = current ? getMissionProgress(studentId, current.chapterId, current.missionId) : null;
const currentInProgress = Boolean(
currentProgress && currentProgress.missionStarted && !isMissionComplete(currentProgress),
);
const from = currentInProgress ? start + 1 : start;
for (let index = from; index < flat.length; index += 1) {
const descriptor = flat[index];
const chapterProgress = getChapterProgress(studentId, descriptor.chapterId);
if (chapterProgress.completedMissionIds.includes(descriptor.missionId)) continue;
const unlocked = descriptor.missionId === selection.missionId || chapterProgress.unlockedMissionIds.includes(descriptor.missionId) || isFirstMissionOfChapter(catalog, descriptor);
const sameChapter = current && descriptor.chapterId === current.chapterId;
const reason = currentInProgress
? "Up next after this mission"
: sameChapter
? "Your current mission is ready"
: "Next chapter unlocked";
return { ...descriptor, unlocked, reason };
}
return null;
}
function isFirstMissionOfChapter(catalog: MissionCatalog | null, descriptor: MissionDescriptor): boolean {
if (!catalog) return false;
for (const subject of catalog.subjects) {
const chapter = subject.chapters.find((item) => item.chapter_id === descriptor.chapterId);
if (chapter) {
return chapter.missions[0]?.mission_id === descriptor.missionId;
}
}
return false;
}
function isChapterComplete(studentId: string, chapter: MissionCatalogChapter): boolean {
if (chapter.missions.length === 0) return false;
const chapterProgress = getChapterProgress(studentId, chapter.chapter_id);
const completed = new Set(chapterProgress.completedMissionIds);
return chapter.missions.every((mission) => completed.has(mission.mission_id));
}
/** Build the ordered Today study queue. */
export function buildTodayStudyQueue(
studentId: string,
catalog: MissionCatalog | null,
selection: MissionSelection,
now: number = Date.now(),
): StudyQueueItem[] {
const items: StudyQueueItem[] = [];
const dayStart = startOfDay(now);
// 1. Due revision items (overdue first).
for (const revision of getDueRevisionItems(studentId, now)) {
const overdue = new Date(revision.dueAt).getTime() < dayStart;
const meta = chapterMetaById(catalog, revision.chapterId);
items.push({
type: "revision",
priority: overdue ? STUDY_QUEUE_PRIORITY.overdue_revision : STUDY_QUEUE_PRIORITY.due_revision,
title: `Revise: ${revision.weakAreaTag}`,
subtitle: revision.chapterTitle,
discipline: meta?.discipline ?? "",
chapterId: revision.chapterId,
chapterTitle: revision.chapterTitle,
chapterNo: meta?.chapterNo ?? 0,
missionId: revision.missionId,
missionTitle: "",
source_refs: revision.source_refs,
reason: overdue ? "Overdue revision" : "Revision due today",
revisionItemId: revision.id,
});
}
// 2. Unfinished current mission.
const current = resolveCurrentMission(catalog, selection);
if (current) {
const progress = getMissionProgress(studentId, current.chapterId, current.missionId);
if (progress && progress.missionStarted && !isMissionComplete(progress)) {
items.push({
type: "continue_mission",
priority: STUDY_QUEUE_PRIORITY.continue_mission,
title: `Continue: ${missionShortTitle(current.missionTitle)}`,
subtitle: current.chapterTitle,
discipline: current.discipline,
chapterId: current.chapterId,
chapterTitle: current.chapterTitle,
chapterNo: current.chapterNo,
missionId: current.missionId,
missionTitle: current.missionTitle,
source_refs: progress.source_refs,
reason: `Resume from the ${progress.lastStage} stage`,
selection: toSelection(current, selection.subject),
});
}
}
// 3. Next unlocked / up-next mission.
const next = resolveNextUnlockedMission(studentId, catalog, selection);
if (next) {
items.push({
type: "next_mission",
priority: STUDY_QUEUE_PRIORITY.next_mission,
title: `Start: ${missionShortTitle(next.missionTitle)}`,
subtitle: next.chapterTitle,
discipline: next.discipline,
chapterId: next.chapterId,
chapterTitle: next.chapterTitle,
chapterNo: next.chapterNo,
missionId: next.missionId,
missionTitle: next.missionTitle,
reason: next.reason,
selection: toSelection(next, selection.subject),
});
}
// 4. Completed chapter (info).
const currentChapter = chapterForSelection(catalog, selection);
if (currentChapter && isChapterComplete(studentId, currentChapter)) {
items.push({
type: "chapter_complete",
priority: STUDY_QUEUE_PRIORITY.chapter_complete,
title: `Chapter complete: ${currentChapter.chapter_title}`,
subtitle: `${currentChapter.missions.length}/${currentChapter.missions.length} missions`,
discipline: currentChapter.discipline || "",
chapterId: currentChapter.chapter_id,
chapterTitle: currentChapter.chapter_title,
chapterNo: currentChapter.chapter_no,
missionId: "",
missionTitle: "",
reason: "All missions in this chapter are completed",
});
}
return items.sort((a, b) => b.priority - a.priority);
}
/** The single highest-priority action (or null when nothing is queued). */
export function getTopStudyAction(
studentId: string,
catalog: MissionCatalog | null,
selection: MissionSelection,
now: number = Date.now(),
): StudyQueueItem | null {
return buildTodayStudyQueue(studentId, catalog, selection, now)[0] ?? null;
}
|