Skip to content

Implement Mochi Limit#2286

Open
ExistentialEnso wants to merge 7 commits intomainfrom
iza.add-mochi-limit
Open

Implement Mochi Limit#2286
ExistentialEnso wants to merge 7 commits intomainfrom
iza.add-mochi-limit

Conversation

@ExistentialEnso
Copy link
Contributor

@ExistentialEnso ExistentialEnso commented Jan 7, 2026

Rough implementation of adding a 3 mochi limit to each kami. This is reliant on hardcoded indices for the mochi and could be cleaner. However, it should work as expected. Per @chlwys, MOCHI_USED data entities track the number of mochi used by that kami.

Also the diff seems to be acting as if some lines were modified that weren't? Perhaps an encoding issue. Will look at it more deeply soon.

Summary by CodeRabbit

  • New Features

    • Mochi items now have a maximum usage limit of 3 per character; a fourth use will be rejected.
    • The limit is enforced per character for each Mochi item.
  • Tests

    • Added tests verifying the 3-use limit and that the limit is tracked separately per character.

✏️ Tip: You can customize this high-level summary in your review settings.

@vercel
Copy link

vercel bot commented Jan 7, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
kamigotchi-client Ready Ready Preview, Comment Jan 8, 2026 11:08pm

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 7, 2026

Walkthrough

Whitespace-only change in KamiUseItemSystem.sol. Tests and test setup were expanded: new Mochi-related allocations added for specific item indices and two tests verifying per‑Kami Mochi usage limits were introduced.

Changes

Cohort / File(s) Summary
Core Logic (whitespace-only)
packages/contracts/src/systems/KamiUseItemSystem.sol
Only formatting/whitespace edits (removed an empty line after an import and added a trailing newline). No functional, signature, or control‑flow changes.
Test Coverage
packages/contracts/test/systems/Items/ItemKami.t.sol
Added LibData import and two tests: testMochiLimitPerKami and testMochiLimitIsPerKami, which exercise Mochi usage counts per Kami and assert limits.
Setup Utilities
packages/contracts/test/utils/SetupTemplate.t.sol
_createFood() now marks four specific item indices (11110, 11120, 11130, 11140) with MOCHI flags and emits a MOCHI_USED allo/requirement block for those items.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I nibble code and count each bite,

Three mochis per Kami — that’s the right,
Tests hop in to check the score,
Fourth attempt? I close the door,
Tiny rabbit cheers: enforce the lore! 🥠✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Implement Mochi Limit' directly and clearly describes the main objective of the pull request, which is to implement a per-kami limit on mochi item usage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ExistentialEnso
Copy link
Contributor Author

@CodeRabbit review this plz

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 7, 2026

@ExistentialEnso, I'll review the changes in this PR right away!

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

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 (3)
packages/contracts/test/utils/SetupTemplate.t.sol (1)

853-860: Consider a more maintainable approach for identifying Mochi items.

The hardcoded index checks create a maintenance burden. Any new Mochi item would require updates in multiple locations (here and in KamiUseItemSystem._isMochi).

Consider alternatives such as:

  • Using an item flag (e.g., IS_MOCHI) that can be checked via LibItem
  • Using a type-based approach if all mochi share a common subtype
  • Maintaining a registry or mapping of mochi indices

This aligns with the TODO comment you added in KamiUseItemSystem.sol line 71.

Alternative approach using item flags

In _createFood, instead of checking specific indices:

-    if (
-      index == 11110 || // Gaokerena Mochi
-      index == 11120 || // Sunset Apple Mochi
-      index == 11130 || // Kami Mochi
-      index == 11140 // Mana Mochi
-    ) {
-      __ItemRegistrySystem.addAlloBasic(abi.encode(index, "USE", "MOCHI_USED", 0, 1));
-    }

You could add a flag when creating mochi items and check it instead:

// When creating a mochi item, add a flag
__ItemRegistrySystem.addFlag(index, "IS_MOCHI");

Then in KamiUseItemSystem._isMochi:

function _isMochi(uint32 itemIndex) internal view returns (bool) {
  uint256 itemID = LibItem.getByIndex(components, itemIndex);
  return LibFlag.has(components, itemID, "IS_MOCHI");
}

This would eliminate the need for hardcoded indices in both locations.

packages/contracts/src/systems/KamiUseItemSystem.sol (1)

42-45: Extract the magic number to a constant.

The mochi limit of 3 is hardcoded on line 44. Consider extracting it to a named constant for better maintainability and clarity.

♻️ Suggested refactor
 uint256 constant ID = uint256(keccak256("system.kami.use.item"));
 string constant MOCHI_USED_TYPE = "MOCHI_USED";
+uint256 constant MOCHI_LIMIT = 3;

 // eat one snack
 contract KamiUseItemSystem is System {
   ...
   
   if (_isMochi(itemIndex)) {
     uint256 used = LibData.get(components, kamiID, 0, MOCHI_USED_TYPE);
-    require(used < 3, "mochi limit reached");
+    require(used < MOCHI_LIMIT, "mochi limit reached");
   }
packages/contracts/test/systems/Items/ItemKami.t.sol (1)

83-100: Test correctly verifies mochi limit behavior.

The test logic is sound:

  • Exercises the happy path (3 successful uses)
  • Verifies the limit enforcement (4th use reverts)
  • Confirms the counter value (MOCHI_USED equals 3)
Optional improvements for more comprehensive coverage

1. Use the constant instead of string literal (line 98):

+import { MOCHI_USED_TYPE } from "systems/KamiUseItemSystem.sol";
 
 contract ItemKamiTest is ItemTemplate {
   ...
   
-  uint256 used = LibData.get(components, kamiID, 0, "MOCHI_USED");
+  uint256 used = LibData.get(components, kamiID, 0, MOCHI_USED_TYPE);
   assertEq(used, 3);
 }

2. Consider testing that the limit is per-Kami:

function testMochiLimitIsPerKami() public {
  uint32 mochiA = 11110;
  _createFood(mochiA, "Gaokerena Mochi", "desc", 10, 0, "");

  uint256 kamiID1 = _mintKami(alice);
  uint256 kamiID2 = _mintKami(alice);
  _giveItem(alice, mochiA, 6);

  vm.startPrank(alice.operator);
  // Use 3 mochi on first kami
  _KamiUseItemSystem.executeTyped(kamiID1, mochiA);
  _KamiUseItemSystem.executeTyped(kamiID1, mochiA);
  _KamiUseItemSystem.executeTyped(kamiID1, mochiA);
  
  // Should still be able to use 3 on second kami
  _KamiUseItemSystem.executeTyped(kamiID2, mochiA);
  _KamiUseItemSystem.executeTyped(kamiID2, mochiA);
  _KamiUseItemSystem.executeTyped(kamiID2, mochiA);
  vm.stopPrank();

  assertEq(LibData.get(components, kamiID1, 0, "MOCHI_USED"), 3);
  assertEq(LibData.get(components, kamiID2, 0, "MOCHI_USED"), 3);
}
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 597a0de and bc479fa.

⛔ Files ignored due to path filters (2)
  • packages/contracts/deployment/world/data/items/allos.csv is excluded by !**/*.csv
  • packages/contracts/deployment/world/data/items/items.csv is excluded by !**/*.csv
📒 Files selected for processing (3)
  • packages/contracts/src/systems/KamiUseItemSystem.sol
  • packages/contracts/test/systems/Items/ItemKami.t.sol
  • packages/contracts/test/utils/SetupTemplate.t.sol
🔇 Additional comments (1)
packages/contracts/src/systems/KamiUseItemSystem.sol (1)

71-78: Hardcoded indices are acceptable for now but need careful synchronization in future refactoring.

Your TODO comment appropriately flags a maintenance concern. The indices (11110, 11120, 11130, 11140) are currently synchronized across three locations: KamiUseItemSystem._isMochi(), SetupTemplate._createFood(), and test files. These must remain in sync when adding new mochi items, so this temporary implementation is acceptable given the explicit TODO.

For future refactoring, consider extracting these indices into a central constant or enum that all locations can reference.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

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 (1)
packages/contracts/test/utils/SetupTemplate.t.sol (1)

853-862: LGTM! The mochi limit logic correctly enforces 3 uses per kami.

The threshold value of 2 with the CURR_MAX check correctly allows exactly 3 mochi uses (checks pass when MOCHI_USED is 0, 1, or 2, blocking the 4th use when MOCHI_USED reaches 3).

While the hardcoded indices align with your noted implementation approach, consider extracting the mochi indices and limit threshold as constants for easier maintenance:

♻️ Optional refactor for maintainability
// At the top of the contract or in a constants file
uint32 constant GAOKERENA_MOCHI_INDEX = 11110;
uint32 constant SUNSET_APPLE_MOCHI_INDEX = 11120;
uint32 constant KAMI_MOCHI_INDEX = 11130;
uint32 constant MANA_MOCHI_INDEX = 11140;
uint32 constant MOCHI_LIMIT_THRESHOLD = 2; // Allows 3 uses (0, 1, 2 pass; 3 blocked)

Then update the condition:

-    if (
-      index == 11110 || // Gaokerena Mochi
-      index == 11120 || // Sunset Apple Mochi
-      index == 11130 || // Kami Mochi
-      index == 11140 // Mana Mochi
-    ) {
+    if (
+      index == GAOKERENA_MOCHI_INDEX ||
+      index == SUNSET_APPLE_MOCHI_INDEX ||
+      index == KAMI_MOCHI_INDEX ||
+      index == MANA_MOCHI_INDEX
+    ) {
       __ItemRegistrySystem.addFlag(index, "IS_MOCHI");
       __ItemRegistrySystem.addAlloBasic(abi.encode(index, "USE", "MOCHI_USED", 0, 1));
-      __ItemRegistrySystem.addRequirement(abi.encode(index, "USE", "MOCHI_USED", "CURR_MAX", 0, 2, ""));
+      __ItemRegistrySystem.addRequirement(abi.encode(index, "USE", "MOCHI_USED", "CURR_MAX", 0, MOCHI_LIMIT_THRESHOLD, ""));
     }
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bc479fa and e9a4758.

⛔ Files ignored due to path filters (2)
  • packages/contracts/deployment/world/data/items/items.csv is excluded by !**/*.csv
  • packages/contracts/deployment/world/data/items/requirements.csv is excluded by !**/*.csv
📒 Files selected for processing (3)
  • packages/contracts/src/systems/KamiUseItemSystem.sol
  • packages/contracts/test/systems/Items/ItemKami.t.sol
  • packages/contracts/test/utils/SetupTemplate.t.sol
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/contracts/src/systems/KamiUseItemSystem.sol
  • packages/contracts/test/systems/Items/ItemKami.t.sol

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.

1 participant