Skip to content

Multiplayer Rating System as solidity library (inspired by openskill)

License

Notifications You must be signed in to change notification settings

Wertusser/solskill

Repository files navigation

LibSkill

Add to your project (requires foundry):

forge install Wertusser/solskill

Usage

import {LibSkill, Rating} from "solskill/LibSkill.sol";

contract ExampleGame {
    mapping(address => Rating) public ratingOf;

    function init() public {
        ratingOf[msg.sender] = LibSkill.rating();
    }

    function fight(address opponent) public {
        require(ratingOf[opponent].mu > 0, "opponent not initialized");

        bool isWin = uint256(blockhash(block.number - 1)) % 2 == 0;

        Rating[] memory ratings = new Rating[](2);
        ratings[0] = ratingOf[msg.sender];
        ratings[1] = ratingOf[opponent];

        uint256[] memory ranks = new uint256[](2);
        ranks[0] = isWin ? 1 : 0;
        ranks[1] = isWin ? 0 : 1;

        Rating[] memory nextRatings = LibSkill.updateRatings(ratings, ranks);
        ratingOf[msg.sender] = nextRatings[0];
        ratingOf[opponent] = nextRatings[1];
    }
}

About

Multiplayer Rating System as solidity library (inspired by openskill)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published