Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors when creating new macros" #632

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/combat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,30 @@ const MACRO_NAME = "Script Autoattack Macro";
* @returns {number} The macro ID.
*/
export function getMacroId(name = MACRO_NAME): number {
const macroMatches = xpath(
visitUrl("account_combatmacros.php"),
`//select[@name="macroid"]/option[text()="${name}"]/@value`,
);
const query = `//select[@name="macroid"]/option[text()="${name}"]/@value`;
const macroText = visitUrl("account_combatmacros.php");
let macroMatches = xpath(macroText, query);

if (macroMatches.length === 0) {
visitUrl("account_combatmacros.php?action=new");
const newMacroText = visitUrl(
`account_combatmacros.php?macroid=0&name=${name}&macrotext=abort&action=save`,
);
return parseInt(
xpath(newMacroText, `//input[@name=${name}]/@value`)[0],
10,
);
} else {
return parseInt(macroMatches[0], 10);
macroMatches = xpath(newMacroText, query);
}

if (macroMatches.length === 0) {
// We may have hit the macro cap
if (xpath(macroText, '//select[@name="macroid"]/option').length >= 100) {
throw new InvalidMacroError(
`Please delete at least one existing macro to make some space for Libram`,
);
}
// Otherwise who knows why it failed
throw new InvalidMacroError(`Could not find or create macro ${name}`);
}

return parseInt(macroMatches[0], 10);
}

type ItemOrName = Item | string;
Expand Down