fix: purchase of multiple booster packs #671

Merged
Sainan merged 1 commits from multiple-packs into main 2024-12-30 16:39:46 -08:00
Owner
No description provided.
coderabbitai[bot] commented 2024-12-30 14:30:45 -08:00 (Migrated from github.com)
Author
Owner

Walkthrough

The pull request modifies the handleBoosterPackPurchase function in the purchase service to support purchasing multiple booster packs in a single transaction. The function's signature is expanded to include a quantity parameter, allowing users to specify the number of booster packs they want to purchase.

The implementation now iterates through the specified quantity, rolling for rewards for each booster pack individually. This change enhances the purchase system's flexibility by enabling bulk purchases of booster packs. The handleTypesPurchase function is correspondingly updated to pass the quantity parameter when calling handleBoosterPackPurchase, ensuring consistent handling of multiple pack purchases across the service.

The core functionality remains consistent, with the primary modification being the ability to process multiple booster pack purchases in a single method call.


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 modifies the `handleBoosterPackPurchase` function in the purchase service to support purchasing multiple booster packs in a single transaction. The function's signature is expanded to include a `quantity` parameter, allowing users to specify the number of booster packs they want to purchase. The implementation now iterates through the specified quantity, rolling for rewards for each booster pack individually. This change enhances the purchase system's flexibility by enabling bulk purchases of booster packs. The `handleTypesPurchase` function is correspondingly updated to pass the quantity parameter when calling `handleBoosterPackPurchase`, ensuring consistent handling of multiple pack purchases across the service. The core functionality remains consistent, with the primary modification being the ability to process multiple booster pack purchases in a single method call. <!-- walkthrough_end --> <!-- internal state start --> <!-- FAHghAtBAEAqAWBLAztF0CGA7TBXALgPYQDmAplmQE4b5kAm0AxoQLasX4Bc0ADldEIAjAFZkm+RADcyqIQE9mhetQxChifADoMiaFAB8wY+CjHYmgDZkeAM0QAPHr1xUm8DMjKDb0VrktJXmtoIUJCZDoBXgwmAGtkYAA5XFYhah4ANgB2AEZgAFUvKh4AZV0sbGNDE0gYCkZCXwwCYnJKGjpGFnZOZwFhMQlpWVDFFhUadU0dPRrTGAR0dGw8IlIKVS6lXqxuaBoAd2hkVNY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ykyrAoGQIAYm7aoAEqyMrfJkAYGqDoaXw/GayCApjapglQEJkFCyAaoASYSZnbkS5T5UqrVQLtKhXXie1pZA+xwkcB2SQc8ahezhyP0ckBmonfgWPCXmk1Bx9ACdsMnqfp14WfPryXj5/qmZGia1el+XelWhfNr18AMAG0bjBuqbXqERb/qBjbhHv1GYGaDblgXyTsojd13mQfu2kh5kHHiHKe9AY4AG4C4GjPiXVkV8QpVxrnXRumZf6uH/roB27c/Kd3Ab3SBXtZAwLgZPMO9B57xR/Mg1Bp9i7WnNFQnBZo8HwkRA/Z4rx6jAOXC0dY7QjzdDYFhfYRx8TsEJMNCYqhpgPDmMIhYOwk74AAPqIAgnowivxQIMD0ZEC4ZUaigDqNAMgAAOJgABmAAnNkMgnjnEAFYAAMAAmDA0ZfEOMyLYXx9AmDZH8dkbx/iyCZAACy5FyBgTI9BbCuMyM4p4LxtG8U0AYoxJjEBmPoHohouSgA --> <!-- 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 2024-12-30 14:32:03 -08:00
coderabbitai[bot] (Migrated from github.com) left a comment
Author
Owner

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/services/purchaseService.ts (2)

267-271: Validate the quantity parameter to avoid edge cases
If quantity is zero or negative, no booster packs should be granted or an error should be thrown. Consider adding a safeguard to handle such scenarios and clarify usage.

 const handleBoosterPackPurchase = async (
     typeName: string,
     accountId: string,
     quantity: number
 ): Promise<IPurchaseResponse> => {
+    if (quantity <= 0) {
+        throw new Error("Quantity must be at least 1.");
+    }
     const pack = ExportBoosterPacks[typeName];
     if (!pack) {
         throw new Error(`unknown booster pack: ${typeName}`);

280-291: Consider accumulating booster items in an array
Repeated string concatenation with += may complicate parsing and hamper flexibility if changes are needed later (e.g., adding metadata or JSON encoding). An alternative approach is to store the items in an array for easier post-processing.

 for (let i = 0; i != quantity; ++i) {
     for (const weights of pack.rarityWeightsPerRoll) {
         const result = getRandomWeightedReward(pack.components, weights);
         if (result) {
             logger.debug(`booster pack rolled`, result);
-            purchaseResponse.BoosterPackItems += result.type.split("/Lotus/").join("/Lotus/StoreItems/") + ',{"lvl":0};';
+            const itemString = result.type.split("/Lotus/").join("/Lotus/StoreItems/") + ',{"lvl":0};';
+            // Option 1: Keep a separate array
+            // purchaseResponse.BoosterPackItemsArray ??= [];
+            // purchaseResponse.BoosterPackItemsArray.push(itemString);
             combineInventoryChanges(
                 purchaseResponse.InventoryChanges,
                 (await addItem(accountId, result.type, result.itemCount)).InventoryChanges
             );
         }
     }
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b8ef39bada and e8c397eee3.

📒 Files selected for processing (1)
  • src/services/purchaseService.ts (3 hunks)
🔇 Additional comments (1)
src/services/purchaseService.ts (1)

309-309: Function call consistency verified
Using an explicit quantity parameter in handleBoosterPackPurchase is consistent with the new signature. Looks good!

**Actionable comments posted: 0** <details> <summary>🧹 Nitpick comments (2)</summary><blockquote> <details> <summary>src/services/purchaseService.ts (2)</summary><blockquote> `267-271`: **Validate the `quantity` parameter to avoid edge cases** If `quantity` is zero or negative, no booster packs should be granted or an error should be thrown. Consider adding a safeguard to handle such scenarios and clarify usage. ```diff const handleBoosterPackPurchase = async ( typeName: string, accountId: string, quantity: number ): Promise<IPurchaseResponse> => { + if (quantity <= 0) { + throw new Error("Quantity must be at least 1."); + } const pack = ExportBoosterPacks[typeName]; if (!pack) { throw new Error(`unknown booster pack: ${typeName}`); ``` --- `280-291`: **Consider accumulating booster items in an array** Repeated string concatenation with `+=` may complicate parsing and hamper flexibility if changes are needed later (e.g., adding metadata or JSON encoding). An alternative approach is to store the items in an array for easier post-processing. ```diff for (let i = 0; i != quantity; ++i) { for (const weights of pack.rarityWeightsPerRoll) { const result = getRandomWeightedReward(pack.components, weights); if (result) { logger.debug(`booster pack rolled`, result); - purchaseResponse.BoosterPackItems += result.type.split("/Lotus/").join("/Lotus/StoreItems/") + ',{"lvl":0};'; + const itemString = result.type.split("/Lotus/").join("/Lotus/StoreItems/") + ',{"lvl":0};'; + // Option 1: Keep a separate array + // purchaseResponse.BoosterPackItemsArray ??= []; + // purchaseResponse.BoosterPackItemsArray.push(itemString); combineInventoryChanges( purchaseResponse.InventoryChanges, (await addItem(accountId, result.type, result.itemCount)).InventoryChanges ); } } } ``` </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 b8ef39badad6309e5dd48ad798d212a5ab3cf80e and e8c397eee3502aad086f0dc72752e6411a6df963. </details> <details> <summary>📒 Files selected for processing (1)</summary> * `src/services/purchaseService.ts` (3 hunks) </details> <details> <summary>🔇 Additional comments (1)</summary> <details> <summary>src/services/purchaseService.ts (1)</summary> `309-309`: **Function call consistency verified** Using an explicit `quantity` parameter in `handleBoosterPackPurchase` is consistent with the new signature. Looks good! </details> </details> </details> <!-- This is an auto-generated comment by CodeRabbit for review status -->
Sign in to join this conversation.
No description provided.