feat: archon shard removal #724

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

#288 my beloved

#288 my beloved
coderabbitai[bot] commented 2025-01-05 03:33:55 -08:00 (Migrated from github.com)
Author
Owner

Walkthrough

The pull request introduces modifications to the infestedFoundryController in the API controllers, specifically adding a new case for shard removal functionality. The changes involve implementing a process to uninstall shards from a suit by handling a specific request type. The implementation includes retrieving inventory and suit information, modifying ArchonCrystalUpgrades, and adjusting resource quantities like HelminthBile. A new interface IShardUninstallRequest is defined to structure the incoming request, which includes properties for identifying the suit and the specific slot where the shard is to be removed. The changes extend the existing controller's capabilities by providing a new mechanism for managing shard-related operations within the system.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.
<!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- walkthrough_start --> ## Walkthrough The pull request introduces modifications to the `infestedFoundryController` in the API controllers, specifically adding a new case for shard removal functionality. The changes involve implementing a process to uninstall shards from a suit by handling a specific request type. The implementation includes retrieving inventory and suit information, modifying `ArchonCrystalUpgrades`, and adjusting resource quantities like `HelminthBile`. A new interface `IShardUninstallRequest` is defined to structure the incoming request, which includes properties for identifying the suit and the specific slot where the shard is to be removed. The changes extend the existing controller's capabilities by providing a new mechanism for managing shard-related operations within the system. <!-- walkthrough_end --> <!-- internal state start --> <!-- FAHghAtBAEAqAWBLAztF0CGA7TBXALgPYQDmAplmQE4b5kAm0AxoQLasX4Bc0ADldEIAjAFZkm+RADcyqIQE9mhetQxChifADoMiaFAB8wY+CjGAxOegAFAErQAyrnYYq84wjJ9cAGx/QqMgBHXFl8aEl8HwZoACIAMzJaHlcmeEIcZHhXRkDWQikMH1joAHcMVGRcIVZNOkYFCPgvXGRqR10sbEwsRnREFSxJeMQYxvxm6CxnIXaAdgAmABYtOEneX39AkLDmbtnMJiZZNsZaJq94wj9CUsQsEmgAVVsAGR4AbXh8fF5kLgA9ACSJp4NUtCxWADkLwMMcsPcRBVqDIqACHLDjgA5REYBwo6gAj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u4JhVQhHdGdjoH0aAzdwC3M6TzGBiM4B+5gW3CG4KYZmoSfgBgABZMgJmUEDEBILo5bDIGQfqLOQ4zcIsxbUyyPriykashiK6oHgC8Sk7ars2tB9LjCH6YyuCKIlfeGt86K/xbqxZA9lKSUG0siOI+l04MAlBxLiK8EJISXL0QcH5Nzfh3DEP8B4AgYGAmJVwfpLzKGghoWCD50FPj3LUfAAB9AYyBmGBEKlpBgzC0ph3gohBhZBHpLHiAANg9HMAAnI9AArGIuYABGBYYjljPQ9GQKRZBFH0EekwJYZAyBCEUeo+gsili9iYEIARGDGGaFYfQdhnDRilB4cuARQA= --> <!-- internal state end --> <!-- tips_start --> --- Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? <details> <summary>❤️ Share</summary> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </details> <details> <summary>🪧 Tips</summary> ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit <commit_id>, please review it.` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` - `@coderabbitai help me debug CodeRabbit configuration file.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (Invoked using PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai generate docstrings` to [generate docstrings](https://docs.coderabbit.ai/finishing-touches/docstrings) for this PR. ([Beta](https://docs.coderabbit.ai/early-access#beta)) - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@coderabbitai help` to get help. ### Other keywords and placeholders - Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. - Add `@coderabbitai summary` to generate the high-level summary at a specific location in the PR description. - Add `@coderabbitai` anywhere in the PR title to generate the title automatically. ### Documentation and Community - Visit our [Documentation](https://docs.coderabbit.ai) for detailed information on how to use CodeRabbit. - Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements. </details> <!-- tips_end -->
coderabbitai[bot] (Migrated from github.com) reviewed 2025-01-05 03:35:19 -08:00
coderabbitai[bot] (Migrated from github.com) left a comment
Author
Owner

Actionable comments posted: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8154f9bc36 and e34f687935.

📒 Files selected for processing (1)
  • src/controllers/api/infestedFoundryController.ts (2 hunks)
🔇 Additional comments (1)
src/controllers/api/infestedFoundryController.ts (1)

273-277: Interface looks good

This new interface correctly models the uninstall request structure. Good job!

**Actionable comments posted: 2** <details> <summary>📜 Review details</summary> **Configuration used: .coderabbit.yaml** **Review profile: CHILL** **Plan: Pro** <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 8154f9bc364f125fcfd3b5354d160f522ac2a6d2 and e34f687935671262408e9e1d3c4eeb108d54cacb. </details> <details> <summary>📒 Files selected for processing (1)</summary> * `src/controllers/api/infestedFoundryController.ts` (2 hunks) </details> <details> <summary>🔇 Additional comments (1)</summary><blockquote> <details> <summary>src/controllers/api/infestedFoundryController.ts (1)</summary> `273-277`: **Interface looks good** This new interface correctly models the uninstall request structure. Good job! </details> </blockquote></details> </details> <!-- This is an auto-generated comment by CodeRabbit for review status -->
@ -43,0 +57,4 @@
}
});
break;
}
coderabbitai[bot] (Migrated from github.com) commented 2025-01-05 03:35:18 -08:00
Author
Owner

⚠️ 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 -->
Sign in to join this conversation.
No description provided.