/** * Rules for completing stock training missions reported by `cln_update` (`missionAwards`): * persists `pveProgress/missions` completion flags and grants first-completion rewards * (decal unlock/resource, shared first-basic-tutorial Golden Eagles unlock, and per-difficulty * first-win Silver Lions) once per account. */ import type { BlkBlock } from '@wto/blk'; import type { TrainingMissionEntry, TrainingMissionsData } from '../game-data/datasets/training-missions.ts'; import { addGoldenEagles, addSilverLions } from '../profile/profile-currency.ts'; import { hasCompletedMissionDifficulty, recordMissionCompleted } from '../profile/profile-mission-progress.ts'; import { addResource, ownsResource } from '../profile/profile-resources.ts'; import { addUnlock, earnedUnlocks } from '../profile/profile-unlocks.ts'; export type TrainingDifficulty = 0 | 1 | 2; export interface TrainingCompletionReport { readonly chapter: string; readonly mission: string; readonly difficulty: TrainingDifficulty; } const DIFFICULTIES: readonly TrainingDifficulty[] = [0, 1, 2]; export class TrainingCatalogue { readonly #byFullName: ReadonlyMap; constructor(data: TrainingMissionsData) { this.#byFullName = new Map(data.missions.map((entry) => [entry.fullName, entry])); } /** * Persist a completed training mission and grant its one-time rewards. Repeated reports keep * existing completion flags intact without paying any reward twice. */ applyCompletion(profile: BlkBlock, report: TrainingCompletionReport): void { const entry = this.#byFullName.get(`${report.chapter}/${report.mission}`); if (!entry) return; let silverLions = 0; for (const difficulty of DIFFICULTIES) { if (difficulty <= report.difficulty && !hasCompletedMissionDifficulty(profile, entry.fullName, difficulty)) { silverLions += entry.silverLionsByDifficulty[difficulty]; } } recordMissionCompleted(profile, entry.fullName, report.difficulty); if (silverLions > 0) addSilverLions(profile, silverLions); grantMissionUnlocks(profile, entry); } } function grantMissionUnlocks(profile: BlkBlock, entry: TrainingMissionEntry): void { if (!entry.decal && !entry.firstCompletion) return; const earned = earnedUnlocks(profile); const grantUnlock = (id: string): void => { if (earned.has(id)) return; addUnlock(profile, id); earned.add(id); }; if (entry.decal) { grantUnlock(entry.decal); if (!ownsResource(profile, entry.decal)) { addResource(profile, entry.decal, { silverLions: 0, goldenEagles: 0 }); } } const first = entry.firstCompletion; if (first && !earned.has(first.id) && !first.blockedBy.some((id) => earned.has(id))) { addGoldenEagles(profile, first.goldenEagles); grantUnlock(first.id); } }