neon-sprawl/bruno/neon-sprawl-server/scripts/bruno-dev-fixture-helper.js

134 lines
4.3 KiB
JavaScript

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 = [
// Keep in sync with PrototypeE7M1QuestCatalogRules.ExpectedQuestIds (server/NeonSprawl.Server/Game/Quests/PrototypeE7M1QuestCatalogRules.cs).
"prototype_quest_combat_intro",
"prototype_quest_gather_intro",
"prototype_quest_grid_contract",
"prototype_quest_operator_chain",
"prototype_quest_refine_intro",
];
const PROTOTYPE_FACTION_IDS = [
"prototype_faction_grid_operators",
"prototype_faction_rust_collective",
];
async function resetPrototypeFactionStanding(bru) {
const { baseUrl, playerId, jsonHeaders } = resolveConfig(bru);
const response = await axios.post(
`${baseUrl}/game/players/${playerId}/__dev/quest-fixture`,
{ schemaVersion: 1, resetFactionIds: PROTOTYPE_FACTION_IDS },
jsonHeaders,
);
if (response.status !== 200 || response.data?.applied !== true) {
throw new Error(
`quest-fixture resetFactionIds failed: ${response.status} ${JSON.stringify(response.data)}`,
);
}
}
async function resetContractInstances(bru, contractInstanceIds) {
const { baseUrl, playerId, jsonHeaders } = resolveConfig(bru);
const response = await axios.post(
`${baseUrl}/game/players/${playerId}/__dev/quest-fixture`,
{ schemaVersion: 1, resetContractInstanceIds: contractInstanceIds },
jsonHeaders,
);
if (response.status !== 200 || response.data?.applied !== true) {
throw new Error(
`quest-fixture resetContractInstanceIds failed: ${response.status} ${JSON.stringify(response.data)}`,
);
}
}
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,
resetPrototypeFactionStanding,
resetContractInstances,
clearInventory,
assertAllPrototypeQuestsNotStarted,
ALL_PROTOTYPE_QUEST_IDS,
PROTOTYPE_FACTION_IDS,
};