Skip to content

Stack items with expiration dates - #303

Merged
AngeloTadeucci merged 1 commit into
masterfrom
dev
Feb 9, 2025
Merged

Stack items with expiration dates#303
AngeloTadeucci merged 1 commit into
masterfrom
dev

Conversation

@AngeloTadeucci

@AngeloTadeucci AngeloTadeucci commented Feb 7, 2025

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Refactor

    • Streamlined the inventory’s item stacking process to ensure consistent behavior regardless of item expiration details.
    • Improved stacking validation by aligning grouping conditions based on expiration time intervals.
  • Style

    • Made minor clarity enhancements to item processing outputs for better overall reliability.

@coderabbitai

coderabbitai Bot commented Feb 7, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The changes adjust the stacking logic in the inventory and item collection classes. In the InventoryManager, the Add method now always passes true for stacking by removing the conditional check on the item’s expiry time. In the ItemCollection, the Add method’s return syntax is simplified and the CanStack method is enhanced to include a check that compares item expiry times at a minute granularity. Both modifications maintain their original method signatures while streamlining internal logic.

Changes

File Change Summary
Maple2.Server.Game/.../InventoryManager.cs Updated the Add method to always call items.Add with stacking enabled, removing the conditional check on expiry time.
Maple2.Server.Game/.../ItemCollection.cs Changed return array syntax in Add method and updated CanStack to include a new condition checking expiry times in minute granularity.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant InventoryManager
    participant ItemCollection
    User->>InventoryManager: Call Add(item, notifyNew, commit)
    InventoryManager->>ItemCollection: Call Add(item, stack=true)
    Note right of ItemCollection: Evaluate stacking with updated expiry check
    ItemCollection-->>InventoryManager: Return result
    InventoryManager-->>User: Return result
Loading

Poem

Oh, I’m a rabbit, hopping with glee,
Code paths cleared and logic set free.
Stacking true like a row of carrots,
Expiry checks refined in minute merits.
With every hop, the code feels light,
Celebrating changes deep into the night!
🐰 Happy coding, let’s bounce to new heights!


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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
Maple2.Server.Game/Manager/Items/ItemCollection.cs (1)

321-322: Consider edge cases in expiry time comparison.

The changes correctly implement stacking based on expiry times at minute granularity. However, consider these improvements:

  1. Integer division might truncate seconds differently for times close to minute boundaries
  2. No null checks for ExpiryTime property
  3. No validation that ExpiryTime is non-negative

Consider this safer implementation:

-               && Equals(item.Transfer, stack.Transfer)
-               && item.ExpiryTime / 60 == stack.ExpiryTime / 60; // Expiry time is in seconds, convert to minutes
+               && Equals(item.Transfer, stack.Transfer)
+               && (item.ExpiryTime == null && stack.ExpiryTime == null || // Both items have no expiry
+                   item.ExpiryTime != null && stack.ExpiryTime != null && // Both items have expiry
+                   item.ExpiryTime >= 0 && stack.ExpiryTime >= 0 && // Validate non-negative
+                   Math.Abs(item.ExpiryTime.Value - stack.ExpiryTime.Value) < 60); // Within same minute
Maple2.Server.Game/Manager/Items/InventoryManager.cs (1)

203-207: Consider adding test coverage for stacking items with different expiry times.

The current test file only covers the loyalty discount calculation. Consider adding test cases to verify:

  • Stacking items with different expiry times
  • Stacking items with no expiry time
  • Edge cases around item stacking limits

Would you like me to generate test cases for these scenarios?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a10c122 and 2063254.

📒 Files selected for processing (2)
  • Maple2.Server.Game/Manager/Items/InventoryManager.cs (1 hunks)
  • Maple2.Server.Game/Manager/Items/ItemCollection.cs (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (2)
Maple2.Server.Game/Manager/Items/ItemCollection.cs (1)

146-146: Verify C# version compatibility for collection expressions.

The code uses C# 12's collection expressions feature ([] syntax). Ensure that the project targets a compatible .NET version.

✅ Verification successful

🏁 Script executed:

#!/bin/bash
# Description: Check .NET SDK version and C# language version in the project file

# Find and check all .csproj files
fd -e csproj -x cat {} \; | grep -E "TargetFramework|LangVersion"

Length of output: 1042


C# Version Compatibility Verified for Collection Expressions

The project targets .NET 8.0 with LangVersion set to 12, which fully supports the C# 12 collection expressions syntax used at line 146. No compatibility issues were found.

Maple2.Server.Game/Manager/Items/InventoryManager.cs (1)

203-203: Verify the implications of always allowing item stacking.

The change to always pass true for stacking could affect how items with different expiry times are handled. This might lead to unexpected behavior if items with different expiry times are stacked together.

✅ Verification successful

🏁 Script executed:

#!/bin/bash
# Description: Search for any special handling of item expiry times in stacking logic
# Look for references to ExpiryTime in relation to stacking

# Search for ExpiryTime usage in stacking context
ast-grep --pattern 'ExpiryTime'

# Search for stack-related methods that might be affected
rg -A 5 'CanStack|GetStackResult'

Length of output: 8324


Verify potential expiry time discrepancies when forcing stacking behavior.

The change to always pass stack: true means that stacking will always be attempted. However, the underlying logic in ItemCollection.cs (specifically in the CanStack method) still verifies that items can only stack if their expiry times (when converted to minutes) match, along with other properties like Id, Rarity, and Transfer. In other words, even with stacking always enabled at the InventoryManager level, items with mismatched expiry times won’t stack together.

  • Confirm that the strict expiry time check (item.ExpiryTime / 60 == stack.ExpiryTime / 60) continues to prevent unintended stacking of items with different expiry values.
  • It may be beneficial to add or review tests covering edge cases where items have slightly different expiry times to ensure that they are correctly handled (i.e., allocated to separate slots rather than being forced to stack).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants