skillratings

Skillratings provides a collection of well-known (and lesser known) skill rating algorithms, that allow you to assess a player's skill level instantly.
You can easily calculate skill ratings instantly in 1vs1 matches, Team vs Team matches, or in tournaments / rating periods.
This library is incredibly lightweight (no dependencies by default), user-friendly, and of course, blazingly fast.

Currently supported algorithms:

Most of these are known from their usage in chess and various other games.
Click on the documentation for the modules linked above for more information about the specific rating algorithms, and their advantages and disadvantages.

Installation

If you are on Rust 1.62 or higher use cargo add to install the latest version:

cargo add skillratings

Alternatively, you can add the following to your Cargo.toml file manually:

toml [dependencies] skillratings = "0.22"

Serde support

Serde support is gated behind the serde feature. You can enable it like so:

Using cargo add:

cargo add skillratings --features serde

By editing Cargo.toml manually:

toml [dependencies] skillratings = {version = "0.22", features = ["serde"]}

Usage and Examples

Below you can find some basic examples of the use cases of this crate.
There are many more rating algorithms available with lots of useful functions that are not covered here.
For more information, please head over to the documentation.

Player-vs-Player

Every rating algorithm included here can be used to rate 1v1 games.
We use Glicko-2 in this example here.

```rust use skillratings::{ glicko2::{glicko2, Glicko2Config, Glicko2Rating}, Outcomes, };

// Initialise a new player rating. // The default values are: 1500.0, 350.0, and 0.06. let player_one = Glicko2Rating::new();

// Or you can initialise it with your own values of course. // Imagine these numbers being pulled from a database. let (somerating, somedeviation, somevolatility) = (1325.0, 230.0, 0.05932); let playertwo = Glicko2Rating { rating: somerating, deviation: somedeviation, volatility: some_volatility, };

// The outcome of the match is from the perspective of player one. let outcome = Outcomes::WIN;

// The config allows you to specify certain values in the Glicko-2 calculation. let config = Glicko2Config::new();

// The glicko2 function will calculate the new ratings for both players and return them. let (newplayerone, newplayertwo) = glicko2(&playerone, &playertwo, &outcome, &config);

// The first players rating increased by ~112 points. asserteq!(newplayer_one.rating.round(), 1612.0); ```

Team-vs-Team

Some algorithms like TrueSkill or Weng-Lin allow you to rate team-based games as well.
This example shows a 3v3 game using TrueSkill.

```rust use skillratings::{ trueskill::{trueskilltwoteams, TrueSkillConfig, TrueSkillRating}, Outcomes, };

// We initialise Team One as a Vec of multiple TrueSkillRatings. let team_one = vec![ TrueSkillRating { rating: 33.3, uncertainty: 3.3, }, TrueSkillRating { rating: 25.1, uncertainty: 1.2, }, TrueSkillRating { rating: 43.2, uncertainty: 2.0, }, ];

// Team Two will be made up of 3 new players, for simplicity. // Note that teams do not necessarily have to be the same size. let team_two = vec![ TrueSkillRating::new(), TrueSkillRating::new(), TrueSkillRating::new(), ];

// The outcome of the match is from the perspective of team one. let outcome = Outcomes::LOSS;

// The config allows you to specify certain values in the TrueSkill calculation. let config = TrueSkillConfig::new();

// The trueskilltwoteams function will calculate the new ratings for both teams and return them. let (newteamone, newteamtwo) = trueskilltwoteams(&teamone, &teamtwo, &outcome, &config);

// The rating of the first player on team one decreased by around ~1.2 points. asserteq!(newteam_one[0].rating.round(), 32.0); ```

Free-For-Alls and Multiple Teams

The Weng-Lin algorithm supports rating matches with multiple Teams.
Here is an example showing a 3-Team game with 3 players each.

```rust use skillratings::{ wenglin::{wenglinmultiteam, WengLinConfig, WengLinRating}, MultiTeamOutcome, };

// Initialise the teams as Vecs of WengLinRatings. // Note that teams do not necessarily have to be the same size. let team_one = vec![ WengLinRating { rating: 25.1, uncertainty: 5.0, }, WengLinRating { rating: 24.0, uncertainty: 1.2, }, WengLinRating { rating: 18.0, uncertainty: 6.5, }, ];

let team_two = vec![ WengLinRating { rating: 44.0, uncertainty: 1.2, }, WengLinRating { rating: 32.0, uncertainty: 2.0, }, WengLinRating { rating: 12.0, uncertainty: 3.2, }, ];

// Using the default rating for team three for simplicity. let team_three = vec![ WengLinRating::new(), WengLinRating::new(), WengLinRating::new(), ];

// Every team is assigned a rank, depending on their placement. The lower the rank, the better. // If two or more teams tie with each other, assign them the same rank. let ratinggroups = vec![ (&teamone[..], MultiTeamOutcome::new(1)), // team one takes the 1st place. (&teamtwo[..], MultiTeamOutcome::new(3)), // team two takes the 3rd place. (&teamthree[..], MultiTeamOutcome::new(2)), // team three takes the 2nd place. ];

// The wenglinmultiteam function will calculate the new ratings for all teams and return them. let newteams = wenglinmultiteam(&ratinggroups, &WengLinConfig::new());

// The rating of the first player of team one increased by around ~2.9 points. asserteq!(newteams[0][0].rating.round(), 28.0); ```

Expected outcome

Every rating algorithm has an expected_score function that you can use to predict the outcome of a game.
This example is using Glicko (not Glicko-2!) to demonstrate.

```rust use skillratings::glicko::{expected_score, GlickoRating};

// Initialise a new player rating. // The default values are: 1500.0, and 350.0. let player_one = GlickoRating::new();

// Initialising a new rating with custom numbers. let player_two = GlickoRating { rating: 1812.0, deviation: 195.0, };

// The expectedscore function will return two floats between 0 and 1 for each player. // A value of 1 means guaranteed victory, 0 means certain loss. // Values near 0.5 mean draws are likely to occur. let (expone, exptwo) = expectedscore(&playerone, &playertwo);

// The expected score for player one is ~0.25. // If these players would play 100 games, player one is expected to score around 25 points. // (Win = 1 point, Draw = 0.5, Loss = 0) asserteq!((expone * 100.0).round(), 25.0); ```

Rating period

Every rating algorithm included here has a ..._rating_period that allows you to calculate a player's new rating using a list of results.
This can be useful in tournaments, or if you only update ratings at the end of a certain rating period, as the name suggests.
We are using the Elo rating algorithm in this example.

```rust use skillratings::{ elo::{eloratingperiod, EloConfig, EloRating}, Outcomes, };

// We initialise a new Elo Rating here. let player = EloRating { rating: 1402.1 };

// We need a list of results to pass to the eloratingperiod function. let mut results = Vec::new();

// And then we populate the list with tuples containing the opponent, // and the outcome of the match from our perspective. results.push((EloRating::new(), Outcomes::WIN)); results.push((EloRating { rating: 954.0 }, Outcomes::DRAW)); results.push((EloRating::new(), Outcomes::LOSS));

// The eloratingperiod function calculates the new rating for the player and returns it. let newplayer = elorating_period(&player, &results, &EloConfig::new());

// The rating of the player decreased by around ~40 points. asserteq!(newplayer.rating.round(), 1362.0); ```

Contributing

Contributions of any kind are always welcome!

Found a bug or have a feature request? Submit a new issue.
Alternatively, open a pull request if you want to add features or fix bugs.
Leaving other feedback is of course also appreciated.

Thanks to everyone who takes their time to contribute.

License

This project is licensed under either the MIT License, or the Apache License, Version 2.0.