fix: consume resources when installing incarnon genesis #687

Merged
Sainan merged 1 commits from incarnon-costs into main 2025-01-02 15:48:54 -08:00
Owner
No description provided.
coderabbitai[bot] commented 2025-01-02 15:17:07 -08:00 (Migrated from github.com)
Author
Owner

Walkthrough

The pull request introduces changes to the evolveWeaponController located in src/controllers/api/evolveWeaponController.ts. The modifications involve importing two functions: addMiscItems from the inventoryService and getRecipe from the itemDataService.

The controller's logic now includes retrieving a recipe using the getRecipe function with the payload.Recipe parameter. Subsequently, the addMiscItems function is called to update the inventory by removing items based on the recipe's ingredients. This process involves transforming the ingredients into an object format that specifies item types and their negative quantities, effectively removing these items from the inventory.

The existing weapon evolution logic, including checks and updates to the EvolutionProgress array, remains unchanged in this modification.


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 changes to the `evolveWeaponController` located in `src/controllers/api/evolveWeaponController.ts`. The modifications involve importing two functions: `addMiscItems` from the `inventoryService` and `getRecipe` from the `itemDataService`. The controller's logic now includes retrieving a recipe using the `getRecipe` function with the `payload.Recipe` parameter. Subsequently, the `addMiscItems` function is called to update the inventory by removing items based on the recipe's ingredients. This process involves transforming the ingredients into an object format that specifies item types and their negative quantities, effectively removing these items from the inventory. The existing weapon evolution logic, including checks and updates to the `EvolutionProgress` array, remains unchanged in this modification. <!-- walkthrough_end --> <!-- internal state start --> <!-- FAHghAtBAEAqAWBLAztF0CGA7TBXALgPYQDmAplmQE4b5kAm0AxoQLasX4Bc0ADldEIAjAFZkm+RADcyqIQE9mhetQxChifADoMiaFAB8wY+CjHYmgDZkeAM0QAPHiyzJcHaFVmFcVJrOgAd3gKNFd8DEtLRCwSMKYMKixCHHJKZBRgADl3IWoeADYADgB2YABVZHzoAGVdLGxjQxNIGApGQls8IlIKVTpGFnZOHn5BUXFJGTlFFhUadU0dPWbTGAR0dGxu4jT+hiVhrG5PDEDoN3ZExQUlebUNbV19CCMWs2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wyBjkoyGTv2Y69mOMAAJld8XjbNi2rZt8z7dN9pXaAA --> <!-- 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-02 15:18:23 -08:00
coderabbitai[bot] (Migrated from github.com) left a comment
Author
Owner

Actionable comments posted: 1

🔭 Outside diff range comments (1)
src/controllers/api/evolveWeaponController.ts (1)

Recipe-weapon compatibility validation needed

Based on the code analysis, there is indeed no explicit validation between the recipe being consumed (payload.Recipe) and the weapon being evolved (req.query.ItemId). The code only:

  1. Consumes the recipe ingredients from inventory
  2. Updates the weapon's features
  3. Tracks evolution progress using payload.EvoType

This could potentially lead to using incorrect recipes for weapons. You should add validation to ensure:

  • The recipe's intended weapon type matches the weapon being evolved
  • The recipe is a valid evolution recipe for the specific weapon
🔗 Analysis chain

Line range hint 14-24: Verify recipe-weapon compatibility

Please ensure that the consumed recipe corresponds to the weapon being evolved. Consider adding validation to check that payload.Recipe is compatible with the weapon identified by req.query.ItemId.

Let's check for other weapon evolution implementations to ensure consistency:


Let me gather more information about the recipe validation.


Let me search for more information about the relationship between recipes and weapons.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other weapon evolution implementations
rg -A 5 "EWA_INSTALL|EvolutionProgress" --type ts

Length of output: 5651


Script:

#!/bin/bash
# Search for recipe-related code and validation
ast-grep --pattern 'function getRecipe($_) {
  $$$
}'

# Search for recipe type definitions
rg "Recipe.*Blueprint" --type ts -A 3

Length of output: 848


Script:

#!/bin/bash
# Search for EvoType usage and validation
rg "EvoType.*Lotus/Weapons" --type ts -A 5

# Search for recipe validation patterns
rg "payload\.(Recipe|EvoType)" --type ts -A 5

Length of output: 1469

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b8ceb78c98 and f43d39d017.

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

3-3: LGTM: Import additions align with resource consumption requirement

The new imports for addMiscItems and getRecipe are appropriately structured and necessary for implementing the resource consumption logic.

Also applies to: 5-5

**Actionable comments posted: 1** <details> <summary>🔭 Outside diff range comments (1)</summary><blockquote> <details> <summary>src/controllers/api/evolveWeaponController.ts (1)</summary><blockquote> **Recipe-weapon compatibility validation needed** Based on the code analysis, there is indeed no explicit validation between the recipe being consumed (`payload.Recipe`) and the weapon being evolved (`req.query.ItemId`). The code only: 1. Consumes the recipe ingredients from inventory 2. Updates the weapon's features 3. Tracks evolution progress using `payload.EvoType` This could potentially lead to using incorrect recipes for weapons. You should add validation to ensure: - The recipe's intended weapon type matches the weapon being evolved - The recipe is a valid evolution recipe for the specific weapon <details> <summary>🔗 Analysis chain</summary> Line range hint `14-24`: **Verify recipe-weapon compatibility** Please ensure that the consumed recipe corresponds to the weapon being evolved. Consider adding validation to check that `payload.Recipe` is compatible with the weapon identified by `req.query.ItemId`. Let's check for other weapon evolution implementations to ensure consistency: --- Let me gather more information about the recipe validation. --- Let me search for more information about the relationship between recipes and weapons. </details> <details> <summary>🏁 Scripts executed</summary> The following scripts were executed for the analysis: Script: ```shell #!/bin/bash # Search for other weapon evolution implementations rg -A 5 "EWA_INSTALL|EvolutionProgress" --type ts ``` Length of output: 5651 --- Script: ```shell #!/bin/bash # Search for recipe-related code and validation ast-grep --pattern 'function getRecipe($_) { $$$ }' # Search for recipe type definitions rg "Recipe.*Blueprint" --type ts -A 3 ``` Length of output: 848 --- Script: ```shell #!/bin/bash # Search for EvoType usage and validation rg "EvoType.*Lotus/Weapons" --type ts -A 5 # Search for recipe validation patterns rg "payload\.(Recipe|EvoType)" --type ts -A 5 ``` Length of output: 1469 </details> </blockquote></details> </blockquote></details> <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 b8ceb78c98c310f83f5225f1489eb78505997c5d and f43d39d01794aeddf568efb1ce7fe57e1c9535a2. </details> <details> <summary>📒 Files selected for processing (1)</summary> * `src/controllers/api/evolveWeaponController.ts` (2 hunks) </details> <details> <summary>🔇 Additional comments (1)</summary><blockquote> <details> <summary>src/controllers/api/evolveWeaponController.ts (1)</summary> `3-3`: **LGTM: Import additions align with resource consumption requirement** The new imports for `addMiscItems` and `getRecipe` are appropriately structured and necessary for implementing the resource consumption logic. Also applies to: 5-5 </details> </blockquote></details> </details> <!-- This is an auto-generated comment by CodeRabbit for review status -->
coderabbitai[bot] (Migrated from github.com) commented 2025-01-02 15:18:22 -08:00
Author
Owner

⚠️ Potential issue

Add error handling and validation for recipe consumption

The current implementation has several potential issues that could lead to data inconsistency:

  1. The non-null assertion on getRecipe could cause runtime errors if the recipe doesn't exist
  2. There's no validation of ingredient quantities before attempting to consume them
  3. No transaction handling if the save operation fails after consuming ingredients

Consider implementing this safer approach:

-    const recipe = getRecipe(payload.Recipe)!;
-    addMiscItems(
-        inventory,
-        recipe.ingredients.map(x => ({ ItemType: x.ItemType, ItemCount: x.ItemCount * -1 }))
-    );
+    const recipe = getRecipe(payload.Recipe);
+    if (!recipe) {
+        return res.status(404).json({ error: 'Recipe not found' });
+    }
+    
+    // Validate that player has all required ingredients
+    const hasAllIngredients = recipe.ingredients.every(ingredient => {
+        const playerHasAmount = inventory.getMiscItemCount?.(ingredient.ItemType) ?? 0;
+        return playerHasAmount >= ingredient.ItemCount;
+    });
+    
+    if (!hasAllIngredients) {
+        return res.status(400).json({ error: 'Missing required ingredients' });
+    }
+    
+    try {
+        addMiscItems(
+            inventory,
+            recipe.ingredients.map(x => ({ ItemType: x.ItemType, ItemCount: x.ItemCount * -1 }))
+        );

Committable suggestion skipped: line range outside the PR's diff.

_:warning: Potential issue_ **Add error handling and validation for recipe consumption** The current implementation has several potential issues that could lead to data inconsistency: 1. The non-null assertion on `getRecipe` could cause runtime errors if the recipe doesn't exist 2. There's no validation of ingredient quantities before attempting to consume them 3. No transaction handling if the save operation fails after consuming ingredients Consider implementing this safer approach: ```diff - const recipe = getRecipe(payload.Recipe)!; - addMiscItems( - inventory, - recipe.ingredients.map(x => ({ ItemType: x.ItemType, ItemCount: x.ItemCount * -1 })) - ); + const recipe = getRecipe(payload.Recipe); + if (!recipe) { + return res.status(404).json({ error: 'Recipe not found' }); + } + + // Validate that player has all required ingredients + const hasAllIngredients = recipe.ingredients.every(ingredient => { + const playerHasAmount = inventory.getMiscItemCount?.(ingredient.ItemType) ?? 0; + return playerHasAmount >= ingredient.ItemCount; + }); + + if (!hasAllIngredients) { + return res.status(400).json({ error: 'Missing required ingredients' }); + } + + try { + addMiscItems( + inventory, + recipe.ingredients.map(x => ({ ItemType: x.ItemType, ItemCount: x.ItemCount * -1 })) + ); ``` > Committable suggestion skipped: line range outside the PR's diff. <!-- This is an auto-generated comment by CodeRabbit -->
Sign in to join this conversation.
No description provided.