chore: handle addMiscItems & addMods resulting in account having 0 or less of a type #821

Merged
Sainan merged 2 commits from zero-to-remove into main 2025-01-19 03:29:19 -08:00
Owner
No description provided.
coderabbitai[bot] commented 2025-01-18 19:55:11 -08:00 (Migrated from github.com)
Author
Owner

Walkthrough

The pull request introduces changes to the src/services/inventoryService.ts file, focusing on inventory management logic. The modifications primarily affect the addMiscItems and addMods functions, enhancing their item count handling mechanism. The key updates include implementing a new control flow that removes items from the respective arrays (MiscItems and RawUpgrades) when their count becomes zero or negative.

A new warning logging system has been introduced to alert when item counts drop below zero. The changes also remove the previous inventory.markModified call, suggesting a shift in how inventory state modifications are tracked. These alterations aim to improve the robustness of the inventory management system by ensuring more precise handling of item quantities and providing better error tracking for inventory-related operations.

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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 `src/services/inventoryService.ts` file, focusing on inventory management logic. The modifications primarily affect the `addMiscItems` and `addMods` functions, enhancing their item count handling mechanism. The key updates include implementing a new control flow that removes items from the respective arrays (`MiscItems` and `RawUpgrades`) when their count becomes zero or negative. A new warning logging system has been introduced to alert when item counts drop below zero. The changes also remove the previous `inventory.markModified` call, suggesting a shift in how inventory state modifications are tracked. These alterations aim to improve the robustness of the inventory management system by ensuring more precise handling of item quantities and providing better error tracking for inventory-related operations. <!-- walkthrough_end --> <!-- internal state start --> <!-- FAHghAtBAEAqAWBLAztF0CGA7TBXALgPYQDmAplmQE4b5kAm0AxoQLasX4Bc0ADldEIAjAFZkm+RADcyqIQE9mhetQxChifADoMiaFAB8wY+CjHYmgDZkeTeISo3o8bPWuZ69ALIomASTpWVAAyD29lVEdkXEtJLBI0HAwmFlwsfGcMKUR46AAGQQFrZFRCADNMaHx5XjJgADlcViFqHgAOACYARmAAVWRW6ABlXSxsY0MTSBgKRnK8IlIKVTpGFnZOHn5BUXFJGTl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ilhe/9pQVP7gfBwIofh6WUeIJK3zUKK3JDv18pIqiRI6liAuanXupkrKWXUpSEwwnCMxhnzjysisHLrBl3C2SRnIAslILHKoZyQpc3fXO8rCaAA+og9DIGvjjZGQpf0GvBOAhkkygNM0BtHkGAAGwdB0AAsD8YF0Qg3wAnDfyRlAArK2GAAHYhAdCYF0egACuhlHfg/DoDA2hkHft0aEsJl4j3Xpvbeu9i4HzXrMZBQA== --> <!-- internal state end --> <!-- finishing_touch_checkbox_start --> ## Finishing Touches - [ ] <!-- {"checkboxId": "7962f53c-55bc-4827-bfbf-6a18da830691"} --> 📝 Generate Docstrings (Beta) <!-- finishing_touch_checkbox_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-18 19:56:42 -08:00
coderabbitai[bot] (Migrated from github.com) left a comment
Author
Owner

Actionable comments posted: 0

🧹 Nitpick comments (3)
src/services/inventoryService.ts (3)

664-670: Good addition of cleanup logic for zero/negative item counts.

The implementation correctly handles edge cases by:

  1. Removing items when count reaches zero
  2. Logging warnings for negative counts

However, consider adding a debug log when removing items to help with troubleshooting:

 if (MiscItems[itemIndex].ItemCount <= 0) {
     if (MiscItems[itemIndex].ItemCount == 0) {
+        logger.debug(`Removing ${ItemType} from MiscItems as count reached 0`);
         MiscItems.splice(itemIndex, 1);
     } else {
         logger.warn(`account now owns a negative amount of ${ItemType}`);
     }
 }

732-738: Consistent implementation of cleanup logic across item types.

The implementation mirrors the cleanup logic in addMiscItems, maintaining consistency across the codebase.

Consider the same logging enhancement for consistency:

 if (RawUpgrades[itemIndex].ItemCount <= 0) {
     if (RawUpgrades[itemIndex].ItemCount == 0) {
+        logger.debug(`Removing ${ItemType} from RawUpgrades as count reached 0`);
         RawUpgrades.splice(itemIndex, 1);
     } else {
         logger.warn(`account now owns a negative amount of ${ItemType}`);
     }
 }

664-670: Consider extracting common cleanup logic into a utility function.

Both addMiscItems and addMods now share identical cleanup logic. Consider extracting this into a reusable utility function to promote DRY principles and ensure consistent behavior.

const cleanupItemWithZeroCount = (
    items: Array<{ ItemCount: number, ItemType: string }>,
    itemIndex: number,
    category: string
): void => {
    if (items[itemIndex].ItemCount <= 0) {
        const itemType = items[itemIndex].ItemType;
        if (items[itemIndex].ItemCount === 0) {
            logger.debug(`Removing ${itemType} from ${category} as count reached 0`);
            items.splice(itemIndex, 1);
        } else {
            logger.warn(`account now owns a negative amount of ${itemType}`);
        }
    }
};

Usage:

- if (MiscItems[itemIndex].ItemCount <= 0) {
-     if (MiscItems[itemIndex].ItemCount == 0) {
-         MiscItems.splice(itemIndex, 1);
-     } else {
-         logger.warn(`account now owns a negative amount of ${ItemType}`);
-     }
- }
+ cleanupItemWithZeroCount(MiscItems, itemIndex, 'MiscItems');

Also applies to: 732-738

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ae832d0125 and 80a62244a1.

📒 Files selected for processing (1)
  • src/services/inventoryService.ts (2 hunks)
**Actionable comments posted: 0** <details> <summary>🧹 Nitpick comments (3)</summary><blockquote> <details> <summary>src/services/inventoryService.ts (3)</summary><blockquote> `664-670`: **Good addition of cleanup logic for zero/negative item counts.** The implementation correctly handles edge cases by: 1. Removing items when count reaches zero 2. Logging warnings for negative counts However, consider adding a debug log when removing items to help with troubleshooting: ```diff if (MiscItems[itemIndex].ItemCount <= 0) { if (MiscItems[itemIndex].ItemCount == 0) { + logger.debug(`Removing ${ItemType} from MiscItems as count reached 0`); MiscItems.splice(itemIndex, 1); } else { logger.warn(`account now owns a negative amount of ${ItemType}`); } } ``` --- `732-738`: **Consistent implementation of cleanup logic across item types.** The implementation mirrors the cleanup logic in `addMiscItems`, maintaining consistency across the codebase. Consider the same logging enhancement for consistency: ```diff if (RawUpgrades[itemIndex].ItemCount <= 0) { if (RawUpgrades[itemIndex].ItemCount == 0) { + logger.debug(`Removing ${ItemType} from RawUpgrades as count reached 0`); RawUpgrades.splice(itemIndex, 1); } else { logger.warn(`account now owns a negative amount of ${ItemType}`); } } ``` --- `664-670`: **Consider extracting common cleanup logic into a utility function.** Both `addMiscItems` and `addMods` now share identical cleanup logic. Consider extracting this into a reusable utility function to promote DRY principles and ensure consistent behavior. ```typescript const cleanupItemWithZeroCount = ( items: Array<{ ItemCount: number, ItemType: string }>, itemIndex: number, category: string ): void => { if (items[itemIndex].ItemCount <= 0) { const itemType = items[itemIndex].ItemType; if (items[itemIndex].ItemCount === 0) { logger.debug(`Removing ${itemType} from ${category} as count reached 0`); items.splice(itemIndex, 1); } else { logger.warn(`account now owns a negative amount of ${itemType}`); } } }; ``` Usage: ```diff - if (MiscItems[itemIndex].ItemCount <= 0) { - if (MiscItems[itemIndex].ItemCount == 0) { - MiscItems.splice(itemIndex, 1); - } else { - logger.warn(`account now owns a negative amount of ${ItemType}`); - } - } + cleanupItemWithZeroCount(MiscItems, itemIndex, 'MiscItems'); ``` Also applies to: 732-738 </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 ae832d01255bd58b23bfbe5ca8eb0294563c8b7f and 80a62244a1b696acf5eea7b2c1d71f942ed8e921. </details> <details> <summary>📒 Files selected for processing (1)</summary> * `src/services/inventoryService.ts` (2 hunks) </details> </details> <!-- This is an auto-generated comment by CodeRabbit for review status -->
Sign in to join this conversation.
No description provided.