From ebb49a9f1e09b92212da80d0a4eaa72e51b13839 Mon Sep 17 00:00:00 2001 From: Sam Gaus Date: Sat, 27 Jul 2024 23:04:13 +0100 Subject: [PATCH 1/3] Fix get macro id --- src/combat.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/combat.ts b/src/combat.ts index 7ba144ac37..bfbb443002 100644 --- a/src/combat.ts +++ b/src/combat.ts @@ -33,22 +33,22 @@ 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`; + let macroMatches = xpath(visitUrl("account_combatmacros.php"), query); + if (macroMatches.length === 0) { visitUrl("account_combatmacros.php?action=new"); const newMacroText = visitUrl( `account_combatmacros.php?macroid=0&name=${name}¯otext=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) { + throw new InvalidMacroError(`Could not find or create macro ${name}`); + } + + return parseInt(macroMatches[0], 10); } type ItemOrName = Item | string; From b62ac96653f0edbb4c67965563dba8fff290bf48 Mon Sep 17 00:00:00 2001 From: Sam Gaus Date: Sat, 27 Jul 2024 23:17:21 +0100 Subject: [PATCH 2/3] Check specific reason for macro creation failure --- src/combat.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/combat.ts b/src/combat.ts index bfbb443002..2398fd1755 100644 --- a/src/combat.ts +++ b/src/combat.ts @@ -34,7 +34,8 @@ const MACRO_NAME = "Script Autoattack Macro"; */ export function getMacroId(name = MACRO_NAME): number { const query = `//select[@name="macroid"]/option[text()="${name}"]/@value`; - let macroMatches = xpath(visitUrl("account_combatmacros.php"), query); + const macroText = visitUrl("account_combatmacros.php"); + let macroMatches = xpath(macroText, query); if (macroMatches.length === 0) { visitUrl("account_combatmacros.php?action=new"); @@ -45,6 +46,13 @@ export function getMacroId(name = MACRO_NAME): number { } if (macroMatches.length === 0) { + // We may have hit the macro cap + if (xpath(macroText, '//select[@name="macroid"]/option').length >= 128) { + 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}`); } From 0ff593f8321a0866628d7683c2856bd7b5cf1595 Mon Sep 17 00:00:00 2001 From: Sam Gaus Date: Sun, 28 Jul 2024 13:25:14 +0100 Subject: [PATCH 3/3] 100 macros max --- src/combat.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/combat.ts b/src/combat.ts index 2398fd1755..4c548de3c7 100644 --- a/src/combat.ts +++ b/src/combat.ts @@ -47,7 +47,7 @@ export function getMacroId(name = MACRO_NAME): number { if (macroMatches.length === 0) { // We may have hit the macro cap - if (xpath(macroText, '//select[@name="macroid"]/option').length >= 128) { + 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`, );