-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 455902c
Showing
9 changed files
with
708 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/target | ||
/Cargo.lock | ||
/.vscode |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "skillratings" | ||
version = "0.1.0" | ||
edition = "2021" | ||
description = "Calculate a player's skill level using elo and glicko-2 algorithms." | ||
readme= "README.md" | ||
repository = "https://github.com/atomflunder/skillratings" | ||
license = "MIT" | ||
keywords = ["elo", "glicko-2", "glicko2", "skill", "rating"] | ||
categories = ["game-development", "algorithms"] | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 atomflunder | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# skillratings | ||
|
||
Calculate a player's skill level using [Elo](https://en.wikipedia.org/wiki/Elo_rating_system) and [Glicko-2](https://en.wikipedia.org/wiki/Glicko_rating_system#Glicko-2_algorithm) algorithms known from their usage in chess and other games. | ||
|
||
## Usage | ||
|
||
For a detailed guide on how to use this crate, head over to the documentation. | ||
|
||
### Elo rating system | ||
```rust | ||
extern crate skillratings; | ||
|
||
use skillratings; | ||
|
||
let player_one = skillratings::rating::EloRating { rating: 1000.0 }; | ||
let player_two = skillratings::rating::EloRating { rating: 1000.0 }; | ||
|
||
// The outcome is from the perspective of player one. | ||
let outcome = skillratings::outcomes::Outcomes::WIN; | ||
|
||
let (player_one_new, player_two_new) = skillratings::elo::elo(player_one, player_two, outcome, 32.0); | ||
assert_eq!(player_one_new.rating, 1016.0); | ||
assert_eq!(player_two_new.rating, 984.0); | ||
``` | ||
|
||
### Glicko-2 rating system | ||
|
||
```rust | ||
extern crate skillratings; | ||
|
||
use skillratings; | ||
|
||
let player_one = skillratings::rating::GlickoRating { | ||
rating: 1500.0, | ||
deviation: 350.0, | ||
volatility: 0.06 | ||
}; | ||
let player_two = skillratings::rating::GlickoRating { | ||
rating: 1500.0, | ||
deviation: 350.0, | ||
volatility: 0.06 | ||
}; | ||
|
||
let outcome = skillratings::outcomes::Outcomes::WIN; | ||
|
||
let (player_one_new, player_two_new) = skillratings::glicko2::glicko2(player_one, player_two, outcome, 0.5); | ||
|
||
assert_eq!(player_one_new.rating.round(), 1662.0); | ||
assert_eq!(player_one_new.deviation.round(), 290.0); | ||
|
||
assert_eq!(player_two_new.rating.round(), 1338.0); | ||
assert_eq!(player_two_new.deviation.round(), 290.0); | ||
``` | ||
|
||
# License | ||
|
||
This project is licensed under the [MIT License](/LICENSE). |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
use crate::outcomes::Outcomes; | ||
use crate::rating::EloRating; | ||
|
||
/// Calculates the elo scores of two players based on their ratings and the outcome of the game. | ||
/// | ||
/// Takes in two players, the outcome of the game and the k-value. | ||
/// | ||
/// The outcome of the match is in the perspective of player_one. | ||
/// This means `Outcomes::WIN` is a win for player_one and `Outcomes::LOSS` is a win for player_two. | ||
/// | ||
/// The k-value is the maximum amount of rating change from a single match. | ||
/// In chess, k-values from 40 to 10 are used, with the most common being 32, 24 or 16. | ||
/// The higher the number, the more volatile the ranking. | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// use skillratings; | ||
/// | ||
/// let player_one = skillratings::rating::EloRating { rating: 1000.0 }; | ||
/// let player_two = skillratings::rating::EloRating { rating: 1000.0 }; | ||
/// | ||
/// let outcome = skillratings::outcomes::Outcomes::WIN; | ||
/// | ||
/// let (player_one_new, player_two_new) = skillratings::elo::elo(player_one, player_two, outcome, 32.0); | ||
/// assert_eq!(player_one_new.rating, 1016.0); | ||
/// assert_eq!(player_two_new.rating, 984.0); | ||
/// ``` | ||
/// | ||
/// # More | ||
/// [Wikipedia Article on the Elo system](https://en.wikipedia.org/wiki/Elo_rating_system). | ||
pub fn elo( | ||
player_one: EloRating, | ||
player_two: EloRating, | ||
outcome: Outcomes, | ||
k: f64, | ||
) -> (EloRating, EloRating) { | ||
let (one_expected, two_expected) = expected_score(player_one.rating, player_two.rating); | ||
|
||
let o = match outcome { | ||
Outcomes::WIN => 1.0, | ||
Outcomes::LOSS => 0.0, | ||
Outcomes::DRAW => 0.5, | ||
}; | ||
|
||
let one_new_elo = player_one.rating + k * (o - one_expected); | ||
let two_new_elo = player_two.rating + k * ((1.0 - o) - two_expected); | ||
|
||
( | ||
EloRating { | ||
rating: one_new_elo, | ||
}, | ||
EloRating { | ||
rating: two_new_elo, | ||
}, | ||
) | ||
} | ||
|
||
/// Calculates the expected score of two players based on their elo rating. | ||
/// Meant for usage in the elo function, but you can also use it to predict games yourself. | ||
/// | ||
/// Takes in two elo scores and returns the expected score of each player. | ||
/// A score of 1.0 means certain win, a score of 0.0 means certain loss, and a score of 0.5 is a draw. | ||
/// | ||
/// #Example | ||
/// ``` | ||
/// use skillratings; | ||
/// | ||
/// let (exp_one, exp_two) = skillratings::elo::expected_score(1500.0, 1210.0); | ||
/// assert_eq!((exp_one * 100.0).round(), 84.0); | ||
/// assert_eq!((exp_two * 100.0).round(), 16.0); | ||
/// ``` | ||
pub fn expected_score(player_one_elo: f64, player_two_elo: f64) -> (f64, f64) { | ||
( | ||
1.0 / (1.0 + 10_f64.powf((player_two_elo - player_one_elo) / 400.0)), | ||
1.0 / (1.0 + 10_f64.powf((player_one_elo - player_two_elo) / 400.0)), | ||
) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_elo() { | ||
let (winner_new_elo, loser_new_elo) = elo( | ||
EloRating { rating: 1000.0 }, | ||
EloRating { rating: 1000.0 }, | ||
Outcomes::WIN, | ||
32.0, | ||
); | ||
assert_eq!(winner_new_elo.rating, 1016.0); | ||
assert_eq!(loser_new_elo.rating, 984.0); | ||
|
||
let (winner_new_elo, loser_new_elo) = elo( | ||
EloRating { rating: 1000.0 }, | ||
EloRating { rating: 1000.0 }, | ||
Outcomes::LOSS, | ||
32.0, | ||
); | ||
assert_eq!(winner_new_elo.rating, 984.0); | ||
assert_eq!(loser_new_elo.rating, 1016.0); | ||
|
||
let (winner_new_elo, loser_new_elo) = elo( | ||
EloRating { rating: 1000.0 }, | ||
EloRating { rating: 1000.0 }, | ||
Outcomes::DRAW, | ||
32.0, | ||
); | ||
assert_eq!(winner_new_elo.rating, 1000.0); | ||
assert_eq!(loser_new_elo.rating, 1000.0); | ||
|
||
let (winner_new_elo, loser_new_elo) = elo( | ||
EloRating { rating: 500.0 }, | ||
EloRating { rating: 1500.0 }, | ||
Outcomes::WIN, | ||
32.0, | ||
); | ||
assert_eq!(winner_new_elo.rating.round(), 532.0); | ||
assert_eq!(loser_new_elo.rating.round(), 1468.0); | ||
} | ||
|
||
#[test] | ||
fn test_expected_score() { | ||
let (winner_expected, loser_expected) = expected_score(1000.0, 1000.0); | ||
assert_eq!(winner_expected, 0.5); | ||
assert_eq!(loser_expected, 0.5); | ||
} | ||
} |
Oops, something went wrong.