feat(webui): add "add missing mods" #804

Merged
Sainan merged 7 commits from addmods into main 2025-01-18 16:57:39 -08:00
2 changed files with 50 additions and 0 deletions

View File

@ -199,6 +199,12 @@
</table>
</div>
</div>
<div class="card mb-3">
<h5 class="card-header">Bulk Actions</h5>
<div class="card-body">
<button class="btn btn-primary" onclick="doAddAllMods();">Add Missing Mods</button>
</div>
</div>
</div>
</div>
</div>

View File

@ -1077,6 +1077,50 @@ function doHelminthUnlockAll() {
});
}
function doAddAllMods() {
let modsAll = new Set();
for (const child of document.getElementById("datalist-mods").children) {
modsAll.add(child.getAttribute("data-key"));
}
revalidateAuthz(() => {
const req = $.get("/api/inventory.php?" + window.authz + "&xpBasedLevelCapDisabled=1");
req.done(data => {
for (const modOwned of data.RawUpgrades) {
if (modOwned.ItemCount ?? 1 > 0) {
modsAll.delete(modOwned.ItemType);
}
}
modsAll = Array.from(modsAll);
if (
modsAll.length != 0 &&
window.confirm("Are you sure you want to add " + modsAll.length + " mods to your account?")
) {
// Batch to avoid PayloadTooLargeError
const batches = [];
for (let i = 0; i < modsAll.length; i += 1000) {
batches.push(modsAll.slice(i, i + 1000));
}
batches.forEach(batch => {
$.post({
url: "/api/missionInventoryUpdate.php?" + window.authz,
contentType: "text/plain",
data: JSON.stringify({
RawUpgrades: batch.map(mod => ({
ItemType: mod,
ItemCount: 21 // To fully upgrade certain arcanes
}))
})
}).done(function () {
updateInventory();
});
});
}
});
});
coderabbitai[bot] commented 2025-01-18 02:07:46 -08:00 (Migrated from github.com)
Review

🛠️ Refactor suggestion

Add error handling for network failures and batch operations.

The current implementation lacks explicit error handling for network requests and batch operations.

Add error handling and progress feedback:

 revalidateAuthz(() => {
     const req = $.get("/api/inventory.php?" + window.authz + "&xpBasedLevelCapDisabled=1");
+    req.fail((jqXHR, textStatus, errorThrown) => {
+        console.error('Failed to fetch inventory:', textStatus, errorThrown);
+        alert('Failed to fetch inventory. Please try again.');
+    });
     req.done(data => {
         // ... existing code ...
+        let completedBatches = 0;
         batches.forEach(batch => {
             $.post({
                 url: "/api/missionInventoryUpdate.php?" + window.authz,
                 contentType: "text/plain",
                 data: JSON.stringify({
                     RawUpgrades: batch.map(mod => ({
                         ItemType: mod,
                         ItemCount: 21
                     }))
                 })
             }).done(function () {
+                completedBatches++;
+                if (completedBatches === batches.length) {
                     updateInventory();
+                }
+            }).fail((jqXHR, textStatus, errorThrown) => {
+                console.error('Batch update failed:', textStatus, errorThrown);
+                alert('Failed to add some mods. Please try again.');
             });
         });
     });
 });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

    revalidateAuthz(() => {
        const req = $.get("/api/inventory.php?" + window.authz + "&xpBasedLevelCapDisabled=1");
        req.fail((jqXHR, textStatus, errorThrown) => {
            console.error('Failed to fetch inventory:', textStatus, errorThrown);
            alert('Failed to fetch inventory. Please try again.');
        });
        req.done(data => {
            for (const modOwned of data.RawUpgrades) {
                if (modOwned.ItemCount ?? 1 > 0) {
                    modsAll.delete(modOwned.ItemType);
                }
            }

            modsAll = Array.from(modsAll);
            if (
                modsAll.length != 0 &&
                window.confirm("Are you sure you want to add " + modsAll.length + " mods to your account?")
            ) {
                // Batch to avoid PayloadTooLargeError
                const batches = [];
                for (let i = 0; i < modsAll.length; i += 1000) {
                    batches.push(modsAll.slice(i, i + 1000));
                }
                let completedBatches = 0;
                batches.forEach(batch => {
                    $.post({
                        url: "/api/missionInventoryUpdate.php?" + window.authz,
                        contentType: "text/plain",
                        data: JSON.stringify({
                            RawUpgrades: batch.map(mod => ({
                                ItemType: mod,
                                ItemCount: 21 // To fully upgrade certain arcanes
                            }))
                        })
                    }).done(function () {
                        completedBatches++;
                        if (completedBatches === batches.length) {
                            updateInventory();
                        }
                    }).fail((jqXHR, textStatus, errorThrown) => {
                        console.error('Batch update failed:', textStatus, errorThrown);
                        alert('Failed to add some mods. Please try again.');
                    });
                });
            }
        });
    });
🧰 Tools
🪛 eslint

[error] 1087-1087: '$' is not defined.

(no-undef)


[error] 1106-1106: '$' is not defined.

(no-undef)

_:hammer_and_wrench: Refactor suggestion_ **Add error handling for network failures and batch operations.** The current implementation lacks explicit error handling for network requests and batch operations. Add error handling and progress feedback: ```diff revalidateAuthz(() => { const req = $.get("/api/inventory.php?" + window.authz + "&xpBasedLevelCapDisabled=1"); + req.fail((jqXHR, textStatus, errorThrown) => { + console.error('Failed to fetch inventory:', textStatus, errorThrown); + alert('Failed to fetch inventory. Please try again.'); + }); req.done(data => { // ... existing code ... + let completedBatches = 0; batches.forEach(batch => { $.post({ url: "/api/missionInventoryUpdate.php?" + window.authz, contentType: "text/plain", data: JSON.stringify({ RawUpgrades: batch.map(mod => ({ ItemType: mod, ItemCount: 21 })) }) }).done(function () { + completedBatches++; + if (completedBatches === batches.length) { updateInventory(); + } + }).fail((jqXHR, textStatus, errorThrown) => { + console.error('Batch update failed:', textStatus, errorThrown); + alert('Failed to add some mods. Please try again.'); }); }); }); }); ``` <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. `````suggestion revalidateAuthz(() => { const req = $.get("/api/inventory.php?" + window.authz + "&xpBasedLevelCapDisabled=1"); req.fail((jqXHR, textStatus, errorThrown) => { console.error('Failed to fetch inventory:', textStatus, errorThrown); alert('Failed to fetch inventory. Please try again.'); }); req.done(data => { for (const modOwned of data.RawUpgrades) { if (modOwned.ItemCount ?? 1 > 0) { modsAll.delete(modOwned.ItemType); } } modsAll = Array.from(modsAll); if ( modsAll.length != 0 && window.confirm("Are you sure you want to add " + modsAll.length + " mods to your account?") ) { // Batch to avoid PayloadTooLargeError const batches = []; for (let i = 0; i < modsAll.length; i += 1000) { batches.push(modsAll.slice(i, i + 1000)); } let completedBatches = 0; batches.forEach(batch => { $.post({ url: "/api/missionInventoryUpdate.php?" + window.authz, contentType: "text/plain", data: JSON.stringify({ RawUpgrades: batch.map(mod => ({ ItemType: mod, ItemCount: 21 // To fully upgrade certain arcanes })) }) }).done(function () { completedBatches++; if (completedBatches === batches.length) { updateInventory(); } }).fail((jqXHR, textStatus, errorThrown) => { console.error('Batch update failed:', textStatus, errorThrown); alert('Failed to add some mods. Please try again.'); }); }); } }); }); ````` </details> <!-- suggestion_end --> <details> <summary>🧰 Tools</summary> <details> <summary>🪛 eslint</summary> [error] 1087-1087: '$' is not defined. (no-undef) --- [error] 1106-1106: '$' is not defined. (no-undef) </details> </details> <!-- This is an auto-generated comment by CodeRabbit -->
}
// Powersuit Route
single.getRoute("#powersuit-route").on("beforeload", function () {