const axios = require("axios"); const { resetPrototypeCombatTargets } = require("./combat-targets-reset-helper"); const { clearInventory } = require("./inventory-api-helper"); function resolveConfig(bru) { return { baseUrl: bru.getEnvVar("baseUrl") || bru.getVar("baseUrl"), playerId: bru.getEnvVar("playerId") || bru.getVar("playerId"), jsonHeaders: { headers: { "Content-Type": "application/json" } }, }; } async function resetPrototypeResourceNodes(bru) { const { baseUrl, jsonHeaders } = resolveConfig(bru); const response = await axios.post( `${baseUrl}/game/__dev/resource-node-fixture`, { schemaVersion: 1 }, jsonHeaders, ); if (response.status !== 200 || response.data?.applied !== true) { throw new Error( `resource-node fixture reset failed: ${response.status} ${JSON.stringify(response.data)}`, ); } } async function resetQuestProgress(bru, questIds) { const { baseUrl, playerId, jsonHeaders } = resolveConfig(bru); const response = await axios.post( `${baseUrl}/game/players/${playerId}/__dev/quest-fixture`, { schemaVersion: 1, resetQuestIds: questIds }, jsonHeaders, ); if (response.status !== 200 || response.data?.applied !== true) { throw new Error( `quest fixture reset failed: ${response.status} ${JSON.stringify(response.data)}`, ); } } const ALL_PROTOTYPE_QUEST_IDS = [ "prototype_quest_combat_intro", "prototype_quest_gather_intro", "prototype_quest_grid_contract", "prototype_quest_operator_chain", "prototype_quest_refine_intro", ]; async function assertAllPrototypeQuestsNotStarted(bru) { const { baseUrl, playerId } = resolveConfig(bru); const response = await axios.get(`${baseUrl}/game/players/${playerId}/quest-progress`, { validateStatus: () => true, }); if (response.status !== 200) { throw new Error(`quest-progress verify GET failed: ${response.status}`); } const notStarted = []; for (const row of response.data.quests ?? []) { if (row.status !== "not_started") { notStarted.push(`${row.questId}:${row.status}`); } } if (notStarted.length > 0) { throw new Error( `quest fixture reset left non-not_started rows: ${notStarted.join(", ")}. Rebuild/restart the server with quest-fixture resetQuestIds support.`, ); } } async function resetAllPrototypeQuestProgress(bru) { await resetQuestProgress(bru, ALL_PROTOTYPE_QUEST_IDS); await assertAllPrototypeQuestsNotStarted(bru); } async function resetGatherIntroQuestProgress(bru) { await resetQuestProgress(bru, ["prototype_quest_gather_intro"]); } async function resetGatherIntroSpine(bru) { await resetPrototypeResourceNodes(bru); await clearInventory(bru); await resetGatherIntroQuestProgress(bru); } module.exports = { resetPrototypeCombatTargets, resetPrototypeResourceNodes, resetQuestProgress, resetAllPrototypeQuestProgress, resetGatherIntroQuestProgress, resetGatherIntroSpine, clearInventory, assertAllPrototypeQuestsNotStarted, ALL_PROTOTYPE_QUEST_IDS, };