feat: archon shard removal #724

Merged
Sainan merged 1 commits from helminth-x into main 2025-01-05 04:40:19 -08:00

View File

@ -40,6 +40,25 @@ export const infestedFoundryController: RequestHandler = async (req, res) => {
break; break;
} }
case "x": {
// shard removal
const request = getJSONfromString(String(req.body)) as IShardUninstallRequest;
const inventory = await getInventory(accountId);
const suit = inventory.Suits.find(suit => suit._id.toString() == request.SuitId.$oid)!;
suit.ArchonCrystalUpgrades![request.Slot] = {};
const bile = inventory.InfestedFoundry!.Resources!.find(
x => x.ItemType == "/Lotus/Types/Items/InfestedFoundry/HelminthBile"
)!;
bile.Count -= 300;
await inventory.save();
res.json({
InventoryChanges: {
InfestedFoundry: inventory.toJSON().InfestedFoundry
}
});
break;
}
coderabbitai[bot] commented 2025-01-05 03:35:18 -08:00 (Migrated from github.com)
Review

⚠️ Potential issue

Handle insufficient HelminthBile gracefully

The code subtracts 300 from the HelminthBile count without checking if there is enough HelminthBile in the inventory, potentially resulting in a negative count. Consider adding a defensive check to prevent negative inventory:

 const bile = inventory.InfestedFoundry!.Resources!.find(
   x => x.ItemType == "/Lotus/Types/Items/InfestedFoundry/HelminthBile"
 )!;
- bile.Count -= 300;
+ if (bile.Count < 300) {
+   // Not enough HelminthBile to remove
+   return res.status(400).json({
+     error: "Not enough HelminthBile to remove"
+   });
+ }
+ bile.Count -= 300;
📝 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.

        case "x": {
            // shard removal
            const request = getJSONfromString(String(req.body)) as IShardUninstallRequest;
            const inventory = await getInventory(accountId);
            const suit = inventory.Suits.find(suit => suit._id.toString() == request.SuitId.$oid)!;
            suit.ArchonCrystalUpgrades![request.Slot] = {};
            const bile = inventory.InfestedFoundry!.Resources!.find(
                x => x.ItemType == "/Lotus/Types/Items/InfestedFoundry/HelminthBile"
            )!;
            if (bile.Count < 300) {
                // Not enough HelminthBile to remove
                return res.status(400).json({
                    error: "Not enough HelminthBile to remove"
                });
            }
            bile.Count -= 300;
            await inventory.save();
            res.json({
                InventoryChanges: {
                    InfestedFoundry: inventory.toJSON().InfestedFoundry
                }
            });
            break;
        }

🛠️ Refactor suggestion

Validate slot range to avoid out-of-bounds indexing

The code sets suit.ArchonCrystalUpgrades![request.Slot] without validating whether request.Slot is within the expected range of 0–4 (since there are five archon crystal upgrade slots). This could cause runtime errors or unexpected behavior if request.Slot is out of range.

- suit.ArchonCrystalUpgrades![request.Slot] = {};
+ if (request.Slot < 0 || request.Slot >= suit.ArchonCrystalUpgrades!.length) {
+   return res.status(400).json({ error: "Invalid shard slot" });
+ }
+ 
+ suit.ArchonCrystalUpgrades![request.Slot] = {};
📝 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.

        case "x": {
            // shard removal
            const request = getJSONfromString(String(req.body)) as IShardUninstallRequest;
            const inventory = await getInventory(accountId);
            const suit = inventory.Suits.find(suit => suit._id.toString() == request.SuitId.$oid)!;
            if (request.Slot < 0 || request.Slot >= suit.ArchonCrystalUpgrades!.length) {
              return res.status(400).json({ error: "Invalid shard slot" });
            }
            
            suit.ArchonCrystalUpgrades![request.Slot] = {};
            const bile = inventory.InfestedFoundry!.Resources!.find(
                x => x.ItemType == "/Lotus/Types/Items/InfestedFoundry/HelminthBile"
            )!;
            bile.Count -= 300;
            await inventory.save();
            res.json({
                InventoryChanges: {
                    InfestedFoundry: inventory.toJSON().InfestedFoundry
                }
            });
            break;
        }
_:warning: Potential issue_ **Handle insufficient HelminthBile gracefully** The code subtracts 300 from the HelminthBile count without checking if there is enough HelminthBile in the inventory, potentially resulting in a negative count. Consider adding a defensive check to prevent negative inventory: ```diff const bile = inventory.InfestedFoundry!.Resources!.find( x => x.ItemType == "/Lotus/Types/Items/InfestedFoundry/HelminthBile" )!; - bile.Count -= 300; + if (bile.Count < 300) { + // Not enough HelminthBile to remove + return res.status(400).json({ + error: "Not enough HelminthBile to remove" + }); + } + bile.Count -= 300; ``` <!-- 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 case "x": { // shard removal const request = getJSONfromString(String(req.body)) as IShardUninstallRequest; const inventory = await getInventory(accountId); const suit = inventory.Suits.find(suit => suit._id.toString() == request.SuitId.$oid)!; suit.ArchonCrystalUpgrades![request.Slot] = {}; const bile = inventory.InfestedFoundry!.Resources!.find( x => x.ItemType == "/Lotus/Types/Items/InfestedFoundry/HelminthBile" )!; if (bile.Count < 300) { // Not enough HelminthBile to remove return res.status(400).json({ error: "Not enough HelminthBile to remove" }); } bile.Count -= 300; await inventory.save(); res.json({ InventoryChanges: { InfestedFoundry: inventory.toJSON().InfestedFoundry } }); break; } ````` </details> <!-- suggestion_end --> --- _:hammer_and_wrench: Refactor suggestion_ **Validate slot range to avoid out-of-bounds indexing** The code sets `suit.ArchonCrystalUpgrades![request.Slot]` without validating whether `request.Slot` is within the expected range of 0–4 (since there are five archon crystal upgrade slots). This could cause runtime errors or unexpected behavior if `request.Slot` is out of range. ```diff - suit.ArchonCrystalUpgrades![request.Slot] = {}; + if (request.Slot < 0 || request.Slot >= suit.ArchonCrystalUpgrades!.length) { + return res.status(400).json({ error: "Invalid shard slot" }); + } + + suit.ArchonCrystalUpgrades![request.Slot] = {}; ``` <!-- 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 case "x": { // shard removal const request = getJSONfromString(String(req.body)) as IShardUninstallRequest; const inventory = await getInventory(accountId); const suit = inventory.Suits.find(suit => suit._id.toString() == request.SuitId.$oid)!; if (request.Slot < 0 || request.Slot >= suit.ArchonCrystalUpgrades!.length) { return res.status(400).json({ error: "Invalid shard slot" }); } suit.ArchonCrystalUpgrades![request.Slot] = {}; const bile = inventory.InfestedFoundry!.Resources!.find( x => x.ItemType == "/Lotus/Types/Items/InfestedFoundry/HelminthBile" )!; bile.Count -= 300; await inventory.save(); res.json({ InventoryChanges: { InfestedFoundry: inventory.toJSON().InfestedFoundry } }); break; } ````` </details> <!-- suggestion_end --> <!-- This is an auto-generated comment by CodeRabbit -->
case "n": { case "n": {
// name the beast // name the beast
const request = getJSONfromString(String(req.body)) as IHelminthNameRequest; const request = getJSONfromString(String(req.body)) as IHelminthNameRequest;
@ -251,6 +270,11 @@ interface IShardInstallRequest {
Color: string; Color: string;
} }
interface IShardUninstallRequest {
SuitId: IOid;
Slot: number;
}
interface IHelminthNameRequest { interface IHelminthNameRequest {
newName: string; newName: string;
} }