fix: removing an archon shard doesn't refund it #729
| @ -52,14 +52,33 @@ export const infestedFoundryController: RequestHandler = async (req, res) => { | ||||
|             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)!; | ||||
| 
 | ||||
|             // refund shard
 | ||||
|             const shard = Object.entries(colorToShard).find( | ||||
|                 ([color]) => color == suit.ArchonCrystalUpgrades![request.Slot].Color | ||||
|             )![1]; | ||||
|  | ||||
|             const miscItemChanges = [ | ||||
|                 { | ||||
|                     ItemType: shard, | ||||
|                     ItemCount: 1 | ||||
|                 } | ||||
|             ]; | ||||
|             addMiscItems(inventory, miscItemChanges); | ||||
| 
 | ||||
|             // remove from suit
 | ||||
|             suit.ArchonCrystalUpgrades![request.Slot] = {}; | ||||
| 
 | ||||
|             // remove bile
 | ||||
|             const bile = inventory.InfestedFoundry!.Resources!.find( | ||||
|                 x => x.ItemType == "/Lotus/Types/Items/InfestedFoundry/HelminthBile" | ||||
|             )!; | ||||
|             bile.Count -= 300; | ||||
| 
 | ||||
|   🛠️ Refactor suggestion Avoid negative resource counts 📝 Committable suggestion
 _:hammer_and_wrench: Refactor suggestion_
**Avoid negative resource counts**  
Subtracting 300 from `bile.Count` without checking if there's enough HelminthBile could produce negative counts. This might lead to inconsistencies in the user’s resource inventory.  
```diff
- bile.Count -= 300;
+ if (bile.Count < 300) {
+   bile.Count = 0; 
+   // or handle insufficient HelminthBile scenario
+ } else {
+   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
            // remove bile
            const bile = inventory.InfestedFoundry!.Resources!.find(
                x => x.ItemType == "/Lotus/Types/Items/InfestedFoundry/HelminthBile"
            )!;
            if (bile.Count < 300) {
                bile.Count = 0;
                // or handle insufficient HelminthBile scenario
            } else {
                bile.Count -= 300;
            }
`````
</details>
<!-- suggestion_end -->
<!-- This is an auto-generated comment by CodeRabbit --> | ||||
|             await inventory.save(); | ||||
| 
 | ||||
|             res.json({ | ||||
|                 InventoryChanges: { | ||||
|                     MiscItems: miscItemChanges, | ||||
|                     InfestedFoundry: inventory.toJSON().InfestedFoundry | ||||
|                 } | ||||
|             }); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	
⚠️ Potential issue
Validate that a matching shard color is always found
If
find()returnsundefined(due to an unexpected color string), the!non-null assertion may cause a runtime error. Consider adding a fallback or error handling to avoid potential crashes.