Implemented Elo Function Algorithm and wrote unit test for it#15
Merged
GoSTEAN merged 11 commits intoNetwalls:mainfrom May 27, 2025
Merged
Implemented Elo Function Algorithm and wrote unit test for it#15GoSTEAN merged 11 commits intoNetwalls:mainfrom
GoSTEAN merged 11 commits intoNetwalls:mainfrom
Conversation
…ooknet_contract into feat/elo_function
Contributor
Author
|
Hey @GoSTEAN I've made the PR now. |
Contributor
|
@Cyber-Mitch What is the update on this? |
Contributor
Author
I don't know whats causing the issue. My codes are okay. I'm still verifying though. maybe it's something that has to do with dependencies |
Contributor
|
@Cyber-Mitch resolve conflicts |
Contributor
Author
oh okay. I'll do that now then |
Contributor
Author
|
I've resloved the conflicts @GoSTEAN |
GoSTEAN
requested changes
May 2, 2025
Contributor
The ci is failing @Cyber-Mitch
|
Contributor
Author
is this a CI issue? @GoSTEAN |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Elo Rating System Features and Unit Tests
This section describes the newly implemented Elo rating system features for the Snooket game smart contract, a Dojo-based project on StarkNet that enhances competitive gameplay by tracking player skill levels. It covers the Elo rating functionality, its integration into the game, and the unit tests added to verify correctness. The features are implemented in
src/systems/Snooknet.cairoandsrc/model/game_model.cairo, with tests insrc/tests/test_world.cairo. Fixes for issues likeu32_sub Overflowand model registration errors are also included.Implemented Features
1. PlayerRating Model
src/model/game_model.cairo:ContractAddressas the key and au32rating (default: 1500).2. Rating Initialization
ensure_player_ratingfunction insrc/systems/Snooknet.cairo:create_matchto ensure both players have valid ratings before the game starts.3. Elo Rating Calculation
src/systems/Snooknet.cairowithin theInternalImplblock:elo_function: Calculates new ratings for win/loss scenarios.PlayerRatingmodels.calculate_expected.safe_subtractto prevent underflow when score < expected_score.elo_function_draw: Handles rating updates for draws.safe_subtractand clamping to 100.u32underflow by returning 0 whena < b.4. RatingUpdated Event
RatingUpdatedevent whenever a player's rating changes after a match.src/systems/Snooknet.cairo:end_matchafter updating ratings for both players.5. Integration with
end_matchend_matchfunction to incorporate Elo rating updates after a game concludes.src/systems/Snooknet.cairoto callelo_functionfor wins/losses orelo_function_drawfor draws:Fixes and Optimizations
1. Model Registration Issue
PlayerRatingmodel.namespace = "Snooknet"to thePlayerRatingmodel insrc/model/game_model.cairo:test_world.cairoto registerPlayerRatingandRatingUpdated:ENTRYPOINT_FAILEDerrors by ensuring proper model registration in the Dojo world.2. u32_sub Overflow Error
test_end_gameandtest_rating_update_after_winfailed withując3_sub Overflowdue to unsafe subtractions inelo_function(e.g.,score1 - expected1whenscore1 = 0).safe_subtractand modifiedelo_functionandelo_function_drawto handle negative adjustments:safe_subtractto compute deltas without underflow.Unit Tests
The following unit tests were added to
src/tests/test_world.cairoto verify the Elo rating system features, covering initialization, rating updates, and edge cases.Test Setup
src/tests/test_world.cairoPlayerRatingmodel registration:TestResource::Model(m_PlayerRating::TEST_CLASS_HASH).RatingUpdatedevent registration:TestResource::Event(Snooknet::e_RatingUpdated::TEST_CLASS_HASH).namespace_defsnippet:Tests Added
Test:
test_player_rating_initializationTest:
test_rating_update_after_winTest:
test_rating_update_after_drawTest:
test_end_game_already_endedTest:
test_invalid_winnerTesting Instructions
To run the tests and verify the Elo rating system:
scarb).sozo testSummary
The Elo rating system enhances the Snooket game by introducing skill-based player rankings, implemented through the
PlayerRatingmodel,elo_function,elo_function_draw, andRatingUpdatedevent. The system handles Cairo’s unsigned integer constraints withsafe_subtractand minimum rating clamping. Fixes for model registration andu32overflow issues ensure reliability, and the test suite validates initialization, rating updates, and edge cases, ensuring a robust competitive experience.This closes #9