diff --git a/apps/server/src/char/actions/action-table.ts b/apps/server/src/char/actions/action-table.ts index 8b2ad8d..6fca14d 100644 --- a/apps/server/src/char/actions/action-table.ts +++ b/apps/server/src/char/actions/action-table.ts @@ -12,6 +12,7 @@ import type { EquipmentCatalogue } from '../../modifications/equipment-catalogue import type { PlayerProfiles } from '../../profile/player-profiles.ts'; import type { ResearchRules } from '../../research/research-rules.ts'; import type { StoreCatalogue } from '../../store/store-catalogue.ts'; +import type { TrainingCatalogue } from '../../training/training-completion.ts'; import type { UnlockShop } from '../../unlocks/unlock-purchase.ts'; import type { CharActionHandler } from '../char-action.ts'; import { acceptEula } from './agreement-actions.ts'; @@ -94,6 +95,7 @@ export interface ActionDependencies { readonly store: StoreCatalogue; readonly inventory: InventoryCatalogue; readonly trophies: TrophyCatalogue; + readonly training?: TrainingCatalogue; readonly clans?: ClanActionDependencies; readonly cloudSettings?: CloudSettingsStore; readonly battle?: BattleActionDependencies; @@ -118,7 +120,7 @@ function coreActionEntries(deps: ActionDependencies): Array<[string, CharActionH ['cln_get_initial_meta', initialMeta(deps.accounts, deps.cloudSettings)], ['cln_upd_entitlements_full', entitlementsFull(deps.profiles, deps.accounts)], ['cln_legacy_get_userdata', legacyUserData(deps.profiles, deps.accounts)], - ['cln_update', clientUpdateAction(deps.profiles, deps.accounts)], + ['cln_update', clientUpdateAction(deps.profiles, deps.accounts, deps.training)], ['cln_accept_eula', acceptEula(deps.profiles, deps.accounts)], ['cln_set_starting_info', setStartingInfo(deps.profiles, deps.accounts)], ['cln_bulk_train_aircraft', bulkTrainCrews(deps.profiles, deps.accounts, deps.crew)], diff --git a/apps/server/src/char/actions/client-update-actions.ts b/apps/server/src/char/actions/client-update-actions.ts index f23b9b2..cb818f4 100644 --- a/apps/server/src/char/actions/client-update-actions.ts +++ b/apps/server/src/char/actions/client-update-actions.ts @@ -2,19 +2,48 @@ * `cln_update` (aces.exe 2.61.0.37 0x29f3ee, job type 3 -> `onUpdateProfile`): * triggered by `save_online_single_job` when the client marks seen user logs as disabled * (`scripts/userLog/userlogUtils.nut` `disableSeenUserlogs`, tag 223) or updates hangar - * auto-repair / auto-refill / auto-buy-modifications and map preferences. Answers the whole - * updated profile. + * auto-repair / auto-refill / auto-buy-modifications and map preferences. On returning from + * a stock training mission, the client also reports completion in `missionAwards` while + * `pveProgress` is empty. Answers the whole updated profile. */ +import { findBlock, findParam, type BlkBlock } from '@wto/blk'; import type { AccountStore } from '../../accounts/account-store.ts'; +import { boolField, stringField } from '../../game-data/datasets/blk-fields.ts'; import { applyClientProfileUpdate } from '../../profile/profile-client-settings.ts'; import type { PlayerProfiles } from '../../profile/player-profiles.ts'; import { ACCEPTED } from '../../research/rule-outcome.ts'; +import type { TrainingCatalogue, TrainingCompletionReport } from '../../training/training-completion.ts'; import type { CharActionHandler } from '../char-action.ts'; import { ruleAction } from './rule-answer.ts'; -export function clientUpdateAction(profiles: PlayerProfiles, accounts: AccountStore): CharActionHandler { - return ruleAction(profiles, accounts, (payload) => (profile) => { - applyClientProfileUpdate(profile, payload); - return ACCEPTED; +export function clientUpdateAction( + profiles: PlayerProfiles, + accounts: AccountStore, + training?: TrainingCatalogue, +): CharActionHandler { + return ruleAction(profiles, accounts, (payload) => { + const completion = training ? readTrainingCompletion(payload) : undefined; + return (profile) => { + applyClientProfileUpdate(profile, payload); + if (completion && training) training.applyCompletion(profile, completion); + return ACCEPTED; + }; }); } + +function readTrainingCompletion(request: BlkBlock): TrainingCompletionReport | undefined { + const awards = findBlock(request, 'missionAwards'); + if (!awards + || stringField(awards, 'gameModeName') !== 'training' + || boolField(awards, 'success') !== true + || boolField(awards, 'early') !== false + || boolField(awards, 'onlineGame') !== false) return undefined; + + const chapter = stringField(awards, 'chapterName'); + const mission = stringField(awards, 'missionName'); + const difficulty = findParam(awards, 'difficulty')?.value; + if (!chapter || !mission || difficulty?.type !== 'int' + || (difficulty.value !== 0 && difficulty.value !== 1 && difficulty.value !== 2)) return undefined; + + return { chapter, mission, difficulty: difficulty.value }; +} diff --git a/apps/server/src/game-data/datasets/training-missions.ts b/apps/server/src/game-data/datasets/training-missions.ts new file mode 100644 index 0000000..c04a45a --- /dev/null +++ b/apps/server/src/game-data/datasets/training-missions.ts @@ -0,0 +1,117 @@ +/** + * Dataset: stock training missions and their first-completion rewards from char.vromfs.bin: + * - config/mislist.blk ("training" block): chapter/mission full names accepted for progress; + * - config/pve_awards.blk ("training" block): per-mission decal, shared first-completion unlock, + * and per-difficulty Silver Lions rewards (`wpEarnedWinDiff0..2`); + * - config/unlocks.blk: decal unlock verification and `oneTimeAwardUnlock` Golden Eagles amount + * and mutually exclusive `char_unlocked` conditions. + */ +import { findParam, type BlkBlock } from '@wto/blk'; +import { blockAt, boolField, stringField, stringList } from './blk-fields.ts'; +import type { Dataset, DatasetSource } from './dataset.ts'; + +const CHAR_PACK = 'char.vromfs.bin'; +const MISLIST = 'config/mislist.blk'; +const PVE_AWARDS = 'config/pve_awards.blk'; +const UNLOCKS = 'config/unlocks.blk'; +const TRAINING_SECTION = 'training'; + +export interface TrainingFirstCompletionAward { + readonly id: string; + readonly goldenEagles: number; + readonly blockedBy: readonly string[]; +} + +export interface TrainingMissionEntry { + readonly fullName: string; + readonly decal?: string; + readonly firstCompletion?: TrainingFirstCompletionAward; + /** First-win Silver Lions rewards (`wpEarnedWinDiff0..2`) for arcade, realistic, simulator. */ + readonly silverLionsByDifficulty: readonly [number, number, number]; +} + +export interface TrainingMissionsData { + readonly missions: readonly TrainingMissionEntry[]; +} + +export const trainingMissions: Dataset = { + id: 'training-missions', + version: 1, + extract(source: DatasetSource): TrainingMissionsData { + const misSection = blockAt(source.readBlk(CHAR_PACK, MISLIST), TRAINING_SECTION); + if (!misSection) throw new Error(`${MISLIST} has no "${TRAINING_SECTION}" block`); + const awardSection = blockAt(source.readBlk(CHAR_PACK, PVE_AWARDS), TRAINING_SECTION); + if (!awardSection) throw new Error(`${PVE_AWARDS} has no "${TRAINING_SECTION}" block`); + + const awardsByMission = new Map(awardSection.blocks.map((entry) => [entry.name, entry])); + const unlocksById = new Map(); + for (const entry of source.readBlk(CHAR_PACK, UNLOCKS).blocks) { + const id = stringField(entry, 'id'); + if (id) unlocksById.set(id, entry); + } + + const seen = new Set(); + const missions: TrainingMissionEntry[] = []; + for (const chapter of misSection.blocks) { + for (const mission of stringList(chapter, 'm')) { + const fullName = `${chapter.name}/${mission}`; + if (seen.has(fullName)) continue; + seen.add(fullName); + missions.push(buildMissionEntry(fullName, mission, awardsByMission.get(mission), unlocksById)); + } + } + return { missions }; + }, +}; + +function buildMissionEntry( + fullName: string, + mission: string, + award: BlkBlock | undefined, + unlocks: ReadonlyMap, +): TrainingMissionEntry { + const decalId = stringField(award, 'decal'); + const decal = decalId && stringField(unlocks.get(decalId), 'type') === 'decal' ? decalId : undefined; + const firstCompletion = award ? readFirstCompletionAward(award, mission, unlocks) : undefined; + return { + fullName, + ...(decal ? { decal } : {}), + ...(firstCompletion ? { firstCompletion } : {}), + silverLionsByDifficulty: [ + readWinSilverLions(award, 0), + readWinSilverLions(award, 1), + readWinSilverLions(award, 2), + ], + }; +} + +function readFirstCompletionAward( + award: BlkBlock, + mission: string, + unlocks: ReadonlyMap, +): TrainingFirstCompletionAward | undefined { + const id = stringField(award, 'oneTimeAwardUnlock'); + if (!id) return undefined; + const unlock = unlocks.get(id); + if (stringField(unlock, 'type') !== 'award') return undefined; + const amount = unlock && findParam(unlock, 'amount_gold')?.value; + const mode = blockAt(unlock, 'mode'); + if (amount?.type !== 'int' || amount.value <= 0 + || stringField(mode, 'type') !== 'char_mission_list' + || !stringList(mode, 'name').includes(mission)) return undefined; + + const blocked = mode?.blocks.flatMap((condition) => + condition.name === 'condition' + && stringField(condition, 'type') === 'char_unlocked' + && boolField(condition, 'positive') === false + ? stringList(condition, 'unlock') + : []) ?? []; + const hideIfEarned = stringField(award, 'hideOneTimeAwardIfUnlockIsOpen'); + if (hideIfEarned) blocked.push(hideIfEarned); + return { id, goldenEagles: amount.value, blockedBy: [...new Set(blocked)] }; +} + +function readWinSilverLions(award: BlkBlock | undefined, difficulty: 0 | 1 | 2): number { + const value = award && findParam(award, `wpEarnedWinDiff${difficulty}`)?.value; + return value?.type === 'int' && value.value > 0 ? value.value : 0; +} diff --git a/apps/server/src/profile/profile-mission-progress.ts b/apps/server/src/profile/profile-mission-progress.ts index 7590add..86d4f45 100644 --- a/apps/server/src/profile/profile-mission-progress.ts +++ b/apps/server/src/profile/profile-mission-progress.ts @@ -17,12 +17,20 @@ * script function get_mission_progress (0x179c900 -> 0xebad60) looks up. It returns the * highest difficulty completed (0, 1, 2), or 3 or more when there is none. */ -import { ensureBlock, setParam, type BlkBlock } from '@wto/blk'; +import { ensureBlock, findBlock, findParam, setParam, type BlkBlock } from '@wto/blk'; const PVE_PROGRESS = 'pveProgress'; const MISSIONS = 'missions'; const DIFFICULTIES = [0, 1, 2] as const; +export function hasCompletedMissionDifficulty(profile: BlkBlock, fullMissionName: string, difficulty: 0 | 1 | 2): boolean { + const pve = findBlock(profile, PVE_PROGRESS); + const missions = pve && findBlock(pve, MISSIONS); + const mission = missions && findBlock(missions, fullMissionName); + const flag = mission && findParam(mission, `diff_${difficulty}`)?.value; + return flag?.type === 'bool' && flag.value; +} + /** Record the mission as completed on every difficulty up to and including `highest` (0, 1 or 2). */ export function recordMissionCompleted(profile: BlkBlock, fullMissionName: string, highest: 0 | 1 | 2): void { const entry = ensureBlock(ensureBlock(ensureBlock(profile, PVE_PROGRESS), MISSIONS), fullMissionName); diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 6f90f2b..1a76546 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -35,6 +35,7 @@ import { itemCatalogue } from './game-data/datasets/item-catalogue.ts'; import { multiplayerMissions, type MultiplayerMissions } from './game-data/datasets/multiplayer-missions.ts'; import { shopCountries } from './game-data/datasets/shop-countries.ts'; import { storeBundles } from './game-data/datasets/store-bundles.ts'; +import { trainingMissions } from './game-data/datasets/training-missions.ts'; import { uiLabels, type UiLabels } from './game-data/datasets/ui-labels.ts'; import { unitNames, type UnitNamesMap } from './game-data/datasets/unit-names.ts'; import { unlockPrices } from './game-data/datasets/unlock-prices.ts'; @@ -57,6 +58,7 @@ import { Router } from './transport/router.ts'; import { loadOrCreateCredentials } from './transport/tls/tls-certificate.ts'; import { StoreCatalogue } from './store/store-catalogue.ts'; import { StoreService } from './store/store-service.ts'; +import { TrainingCatalogue } from './training/training-completion.ts'; import { TrafficLog } from './traffic-log/traffic-log.ts'; import { loadTuning, type Tuning } from './tuning/tuning.ts'; import { UnlockShop } from './unlocks/unlock-purchase.ts'; @@ -309,6 +311,7 @@ function loadDomainCatalogues( store: new StoreCatalogue(vehicles, datasets.load(storeBundles), localizedUnitNames, labels, tuning.store), inventory: new InventoryCatalogue(vehicles, localizedUnitNames, datasets.load(externalItemSources), classicItemDefs, tuning), trophies: new TrophyCatalogue(vehicles, classicItemDefs, tuning.trophies), + training: new TrainingCatalogue(datasets.load(trainingMissions)), clanEconomy: datasets.load(clanEconomy), }; } diff --git a/apps/server/src/training/training-completion.ts b/apps/server/src/training/training-completion.ts new file mode 100644 index 0000000..eebc521 --- /dev/null +++ b/apps/server/src/training/training-completion.ts @@ -0,0 +1,73 @@ +/** + * 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); + } +}