|
| 1 | +/* |
| 2 | +Toolbox Aid |
| 3 | +David Quesenberry |
| 4 | +06/03/2026 |
| 5 | +engineV2ConfigDrivenProofScene.js |
| 6 | +*/ |
| 7 | + |
| 8 | +import { createFirstManifestDrivenPlayableScene } from "./firstManifestDrivenPlayableScene.js"; |
| 9 | +import { resolveEngineV2Inventory } from "./engineV2InventorySystem.js"; |
| 10 | +import { processRuntimeDamage } from "./runtimeDamageProcessing.js"; |
| 11 | +import { processEngineV2Objectives } from "./engineV2ObjectiveSystem.js"; |
| 12 | +import { evaluateRuntimeOutcomes } from "./runtimeOutcomeProcessing.js"; |
| 13 | +import { resolveEngineV2GameUi } from "./engineV2UiRuntime.js"; |
| 14 | +import { validateEngineV2SaveLoadFlow } from "./engineV2SaveLoadValidation.js"; |
| 15 | + |
| 16 | +export const ENGINE_V2_PROOF_SCENE_ERRORS = Object.freeze({ |
| 17 | + MANIFEST_INVALID: "ENGINE_V2_PROOF_SCENE_MANIFEST_INVALID", |
| 18 | + PLAYABLE_SCENE_FAILED: "ENGINE_V2_PROOF_SCENE_PLAYABLE_SCENE_FAILED", |
| 19 | + EXTENSIONS_MISSING: "ENGINE_V2_PROOF_SCENE_EXTENSIONS_MISSING", |
| 20 | + INVENTORY_FAILED: "ENGINE_V2_PROOF_SCENE_INVENTORY_FAILED", |
| 21 | + COMBAT_FAILED: "ENGINE_V2_PROOF_SCENE_COMBAT_FAILED", |
| 22 | + OBJECTIVES_FAILED: "ENGINE_V2_PROOF_SCENE_OBJECTIVES_FAILED", |
| 23 | + OUTCOMES_FAILED: "ENGINE_V2_PROOF_SCENE_OUTCOMES_FAILED", |
| 24 | + UI_FAILED: "ENGINE_V2_PROOF_SCENE_UI_FAILED", |
| 25 | + SAVE_LOAD_FAILED: "ENGINE_V2_PROOF_SCENE_SAVE_LOAD_FAILED", |
| 26 | +}); |
| 27 | + |
| 28 | +export function runEngineV2ConfigDrivenProofScene({ manifest, inputEvents, saveDefinition }) { |
| 29 | + const errors = []; |
| 30 | + |
| 31 | + if (!isRecord(manifest)) { |
| 32 | + errors.push(createProofSceneError(ENGINE_V2_PROOF_SCENE_ERRORS.MANIFEST_INVALID, "Config-driven proof scene requires manifest object.", "manifest")); |
| 33 | + } |
| 34 | + |
| 35 | + if (!Array.isArray(inputEvents)) { |
| 36 | + errors.push(createProofSceneError(ENGINE_V2_PROOF_SCENE_ERRORS.MANIFEST_INVALID, "Config-driven proof scene requires inputEvents array.", "inputEvents")); |
| 37 | + } |
| 38 | + |
| 39 | + if (!isRecord(saveDefinition)) { |
| 40 | + errors.push(createProofSceneError(ENGINE_V2_PROOF_SCENE_ERRORS.MANIFEST_INVALID, "Config-driven proof scene requires saveDefinition object.", "saveDefinition")); |
| 41 | + } |
| 42 | + |
| 43 | + if (errors.length > 0) { |
| 44 | + return createProofSceneResult({ scene: null, frame: null, validation: null, errors }); |
| 45 | + } |
| 46 | + |
| 47 | + const playableResult = createFirstManifestDrivenPlayableScene(manifest, inputEvents); |
| 48 | + |
| 49 | + if (!playableResult.valid) { |
| 50 | + return createProofSceneResult({ |
| 51 | + scene: null, |
| 52 | + frame: null, |
| 53 | + validation: null, |
| 54 | + errors: remapErrors(playableResult.errors, ENGINE_V2_PROOF_SCENE_ERRORS.PLAYABLE_SCENE_FAILED), |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + const extensions = manifest.engineV2; |
| 59 | + |
| 60 | + if (!isRecord(extensions)) { |
| 61 | + return createProofSceneResult({ |
| 62 | + scene: null, |
| 63 | + frame: null, |
| 64 | + validation: null, |
| 65 | + errors: [createProofSceneError(ENGINE_V2_PROOF_SCENE_ERRORS.EXTENSIONS_MISSING, "Proof scene manifest requires engineV2 extension data.", "manifest.engineV2")], |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + const inventoryResult = resolveEngineV2Inventory(extensions.inventory); |
| 70 | + |
| 71 | + if (!inventoryResult.valid) { |
| 72 | + return createProofSceneResult({ scene: null, frame: null, validation: null, errors: remapErrors(inventoryResult.errors, ENGINE_V2_PROOF_SCENE_ERRORS.INVENTORY_FAILED) }); |
| 73 | + } |
| 74 | + |
| 75 | + const damageResult = processRuntimeDamage(extensions.combat); |
| 76 | + |
| 77 | + if (!damageResult.valid) { |
| 78 | + return createProofSceneResult({ scene: null, frame: null, validation: null, errors: remapErrors(damageResult.errors, ENGINE_V2_PROOF_SCENE_ERRORS.COMBAT_FAILED) }); |
| 79 | + } |
| 80 | + |
| 81 | + const objectiveRuntimeState = { |
| 82 | + ...extensions.objectives.runtimeState, |
| 83 | + scores: { |
| 84 | + ...extensions.objectives.runtimeState.scores, |
| 85 | + ...extensions.outcomes.runtimeState.scores, |
| 86 | + }, |
| 87 | + }; |
| 88 | + const objectiveResult = processEngineV2Objectives({ |
| 89 | + ...extensions.objectives, |
| 90 | + runtimeState: objectiveRuntimeState, |
| 91 | + }); |
| 92 | + |
| 93 | + if (!objectiveResult.valid) { |
| 94 | + return createProofSceneResult({ scene: null, frame: null, validation: null, errors: remapErrors(objectiveResult.errors, ENGINE_V2_PROOF_SCENE_ERRORS.OBJECTIVES_FAILED) }); |
| 95 | + } |
| 96 | + |
| 97 | + const outcomeResult = evaluateRuntimeOutcomes(extensions.outcomes); |
| 98 | + |
| 99 | + if (!outcomeResult.valid) { |
| 100 | + return createProofSceneResult({ scene: null, frame: null, validation: null, errors: remapErrors(outcomeResult.errors, ENGINE_V2_PROOF_SCENE_ERRORS.OUTCOMES_FAILED) }); |
| 101 | + } |
| 102 | + |
| 103 | + const uiRuntimeState = { |
| 104 | + ...extensions.ui.runtimeState, |
| 105 | + inventoryStates: inventoryResult.inventoryStates, |
| 106 | + healthRecords: damageResult.healthRecords, |
| 107 | + scores: extensions.outcomes.runtimeState.scores, |
| 108 | + }; |
| 109 | + const uiResult = resolveEngineV2GameUi({ |
| 110 | + uiDefinitions: extensions.ui.uiDefinitions, |
| 111 | + runtimeState: uiRuntimeState, |
| 112 | + }); |
| 113 | + |
| 114 | + if (!uiResult.valid) { |
| 115 | + return createProofSceneResult({ scene: null, frame: null, validation: null, errors: remapErrors(uiResult.errors, ENGINE_V2_PROOF_SCENE_ERRORS.UI_FAILED) }); |
| 116 | + } |
| 117 | + |
| 118 | + const runtimeState = { |
| 119 | + ...extensions.saveLoad.runtimeState, |
| 120 | + inventory: inventoryResult.inventoryStates, |
| 121 | + state: { |
| 122 | + ...extensions.saveLoad.runtimeState.state, |
| 123 | + objectives: objectiveResult.objectiveStates, |
| 124 | + health: damageResult.healthRecords, |
| 125 | + runtime: { |
| 126 | + ...extensions.saveLoad.runtimeState.state.runtime, |
| 127 | + matchedOutcomes: outcomeResult.matchedOutcomes, |
| 128 | + }, |
| 129 | + }, |
| 130 | + }; |
| 131 | + const saveLoadResult = validateEngineV2SaveLoadFlow({ |
| 132 | + saveDefinition, |
| 133 | + runtimeState, |
| 134 | + shutdownState: extensions.saveLoad.shutdownState, |
| 135 | + loadRequest: extensions.saveLoad.loadRequest, |
| 136 | + continueRequest: extensions.saveLoad.continueRequest, |
| 137 | + }); |
| 138 | + |
| 139 | + if (!saveLoadResult.valid) { |
| 140 | + return createProofSceneResult({ scene: null, frame: null, validation: null, errors: remapErrors(saveLoadResult.errors, ENGINE_V2_PROOF_SCENE_ERRORS.SAVE_LOAD_FAILED) }); |
| 141 | + } |
| 142 | + |
| 143 | + return createProofSceneResult({ |
| 144 | + scene: playableResult.scene, |
| 145 | + frame: playableResult.frame, |
| 146 | + validation: Object.freeze({ |
| 147 | + inventory: inventoryResult, |
| 148 | + combat: damageResult, |
| 149 | + objectives: objectiveResult, |
| 150 | + outcomes: outcomeResult, |
| 151 | + ui: uiResult, |
| 152 | + saveLoad: saveLoadResult, |
| 153 | + }), |
| 154 | + errors, |
| 155 | + }); |
| 156 | +} |
| 157 | + |
| 158 | +function createProofSceneResult({ scene, frame, validation, errors }) { |
| 159 | + return Object.freeze({ |
| 160 | + valid: errors.length === 0, |
| 161 | + scene, |
| 162 | + frame, |
| 163 | + validation, |
| 164 | + errors: Object.freeze(errors), |
| 165 | + }); |
| 166 | +} |
| 167 | + |
| 168 | +function remapErrors(errors, code) { |
| 169 | + return errors.map((error) => Object.freeze({ code, message: error.message, path: error.path })); |
| 170 | +} |
| 171 | + |
| 172 | +function createProofSceneError(code, message, path) { |
| 173 | + return Object.freeze({ code, message, path }); |
| 174 | +} |
| 175 | + |
| 176 | +function isRecord(value) { |
| 177 | + return value !== null && typeof value === "object" && !Array.isArray(value); |
| 178 | +} |
0 commit comments