Skip to content

Comments

added helper's experience#107

Closed
Mmesolove wants to merge 4 commits intoAkatsukiLabs:mainfrom
Mmesolove:main
Closed

added helper's experience#107
Mmesolove wants to merge 4 commits intoAkatsukiLabs:mainfrom
Mmesolove:main

Conversation

@Mmesolove
Copy link

Pull Request Overview

📝 Summary

created a utility helper which provides the core level progression system for the game

Related Issues

Type of Change

Mark with an x all the applicable checkboxes (like [x]).

  • 📝 Documentation (updates to README, docs, or comments)
  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • 👌 Enhancement (non-breaking change which adds functionality)
  • [ X] 💥 Breaking change (fix or feature that would cause existing functionality to change)

🔄 Changes Made

What's Changed

Provide a clear and concise description of what you changed and why.
_I created a new file, experience_utils.cairo in the helpers folder
and implemented the ExperienceCalculator trait to
-Calculate the experience needed for a level
-Determine if a level up should occur
-Calculate the remaining experience after level up.

Implementation Details

__I created a new file, experience_utils.cairo, in the helpers folder
and implemented the ExperienceCalculator

Technical Notes

Include any technical details that reviewers should be aware of.

🔧 Tests Results

Describe the tests you performed to verify your changes.

Test Coverage

  • Unit Tests
  • Integration Tests
  • Manual Testing

Evidence

Provide relevant evidence of testing (screenshots, test outputs, etc.).

Testing Notes

Include any special testing considerations or edge cases checked.

🔜 Next Steps

Indicate actions or improvements to be taken after this PR, if applicable.

@@ -0,0 +1,55 @@
#[starknet::interface]
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for this implementation but use this code instead please:

use core::traits::Into;

#[generate_trait]
pub impl ExperienceCalculator of ExperienceCalculatorTrait {
    
    fn calculate_exp_needed_for_level(level: u8) -> u16 {
        let level_u16: u16 = level.into();
        level_u16 * level_u16 * 10
    }
    
    fn should_level_up(current_level: u8, current_exp: u16) -> bool {
        let exp_needed = Self::calculate_exp_needed_for_level(current_level);
        current_exp >= exp_needed
    }
    
    fn remaining_exp_after_level_up(current_level: u8, current_exp: u16) -> u16 {
        let exp_needed = Self::calculate_exp_needed_for_level(current_level);
        if current_exp >= exp_needed {
            current_exp - exp_needed
        } else {
            current_exp
        }
    }
}

#[cfg(test)]
mod experience_calculator_tests {
    use super::{ExperienceCalculator};

    #[test]
    #[available_gas(1000000)]
    fn test_calculate_exp_needed_for_level() {
        // Test values for different levels to verify the formula level² × 10
        
        // Level 1: 1² × 10 = 10
        let exp_level_1 = ExperienceCalculator::calculate_exp_needed_for_level(1);
        assert(exp_level_1 == 10, 'Level 1 should need 10 exp');
        
        // Level 2: 2² × 10 = 40
        let exp_level_2 = ExperienceCalculator::calculate_exp_needed_for_level(2);
        assert(exp_level_2 == 40, 'Level 2 should need 40 exp');
        
        // Level 5: 5² × 10 = 250
        let exp_level_5 = ExperienceCalculator::calculate_exp_needed_for_level(5);
        assert(exp_level_5 == 250, 'Level 5 should need 250 exp');
        
        // Level 10: 10² × 10 = 1000
        let exp_level_10 = ExperienceCalculator::calculate_exp_needed_for_level(10);
        assert(exp_level_10 == 1000, 'Level 10 should need 1000 exp');
        
        // Level 20: 20² × 10 = 4000
        let exp_level_20 = ExperienceCalculator::calculate_exp_needed_for_level(20);
        assert(exp_level_20 == 4000, 'Level 20 should need 4000 exp');
    }
    
    #[test]
    #[available_gas(1000000)]
    fn test_should_level_up() {
        // Level 1 (needs 10 exp)
        
        // 9 exp: should not level up
        let should_level_up_1_9 = ExperienceCalculator::should_level_up(1, 9);
        assert(!should_level_up_1_9, 'Level 1 with 9 exp not level up');
        
        // 10 exp: should level up
        let should_level_up_1_10 = ExperienceCalculator::should_level_up(1, 10);
        assert(should_level_up_1_10, 'Level 1 with 10 exp level up');
        
        // 15 exp: should level up
        let should_level_up_1_15 = ExperienceCalculator::should_level_up(1, 15);
        assert(should_level_up_1_15, 'Level 1 with 15 exp level up');
        
        // Level 5 (needs 250 exp)
        
        // 249 exp: should not level up
        let should_level_up_5_249 = ExperienceCalculator::should_level_up(5, 249);
        assert(!should_level_up_5_249, 'Level 5 249 exp not level up');
        
        // 250 exp: should level up
        let should_level_up_5_250 = ExperienceCalculator::should_level_up(5, 250);
        assert(should_level_up_5_250, 'Level 5 with 250 exp level up');
        
        // 300 exp: should level up
        let should_level_up_5_300 = ExperienceCalculator::should_level_up(5, 300);
        assert(should_level_up_5_300, 'Level 5 with 300 exp level up');
    }
    
    #[test]
    #[available_gas(1000000)]
    fn test_remaining_exp_after_level_up() {
        // Level 1 (needs 10 exp)
        
        // 9 exp: should remain at 9 (no level up)
        let remaining_1_9 = ExperienceCalculator::remaining_exp_after_level_up(1, 9);
        assert(remaining_1_9 == 9, 'Level 1 with 9 exp keep 9');
        
        // 10 exp: should remain at 0 (level up)
        let remaining_1_10 = ExperienceCalculator::remaining_exp_after_level_up(1, 10);
        assert(remaining_1_10 == 0, 'Level 1 with 10 exp 0 remaining');
        
        // 15 exp: should remain at 5 (level up)
        let remaining_1_15 = ExperienceCalculator::remaining_exp_after_level_up(1, 15);
        assert(remaining_1_15 == 5, 'Level 1 with 15 exp 5 remaining');
        
        // Level 5 (needs 250 exp)
    
        // 300 exp: should remain at 50 (level up)
        let remaining_5_300 = ExperienceCalculator::remaining_exp_after_level_up(5, 300);
        assert(remaining_5_300 == 50, 'Level 5 300 exp 50 remaining');
    }
    
    #[test]
    #[available_gas(1000000)]
    fn test_multiple_level_ups_simulation() {
        // Simulate a scenario where a beast levels up multiple times
        
        // Beast level 1 with 0 initial exp
        let mut current_level = 1_u8;
        let mut current_exp = 0_u16;
        
        // Earned 20 exp
        current_exp += 20;
        
        // Validate if it should level up
        let should_level = ExperienceCalculator::should_level_up(current_level, current_exp);
        assert(should_level, 'Level up after more 20 exp');
        
        // Level up and update remaining exp
        if should_level {
            current_exp = ExperienceCalculator::remaining_exp_after_level_up(current_level, current_exp);
            current_level += 1;
        }
        
        // Verify final values
        assert(current_level == 2, 'Should now be level 2');
        assert(current_exp == 10, 'Should have 10 exp remaining');
        
        // Earned 50 more exp
        current_exp += 50;
        
        // Validate if it should level up again
        let should_level_again = ExperienceCalculator::should_level_up(current_level, current_exp);
        assert(should_level_again, 'Level up 60 exp at level 2');
        
        // Level up again
        if should_level_again {
            current_exp = ExperienceCalculator::remaining_exp_after_level_up(current_level, current_exp);
            current_level += 1;
        }
        
        // Verify final values
        assert(current_level == 3, 'Should now be level 3');
        assert(current_exp == 20, 'Should have 20 exp remaining');
        
        // Should not level up to level 4 yet
        let not_enough_for_level4 = ExperienceCalculator::should_level_up(current_level, current_exp);
        assert(!not_enough_for_level4, 'Should not reach level 4 yet');
    }
}

@coxmars
Copy link
Contributor

coxmars commented May 5, 2025

I'm so sorry for the miss up, I have deleted it @coxmars

But I'm still seeing the files, delete them and update your PR

@Mmesolove
Copy link
Author

I had some issues with my laptop, I have deleted it now @coxmars

@Mmesolove Mmesolove requested a review from coxmars May 5, 2025 07:43
@coxmars
Copy link
Contributor

coxmars commented May 5, 2025

I had some issues with my laptop, I have deleted it now @coxmars

@Mmesolove you remove the incorrect file, the ONLY file that we need should be located in helper folder which is the implementation.

@Mmesolove
Copy link
Author

Please check @coxmars

Copy link
Contributor

Choose a reason for hiding this comment

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

@Mmesolove delete just this file

@coxmars coxmars closed this May 28, 2025
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.

📝 [FEAT]: Implement experience_utils.cairo helper for level progression

2 participants