diff --git a/readme.md b/readme.md index 35a2942..1b53925 100644 --- a/readme.md +++ b/readme.md @@ -8,7 +8,7 @@ Add the following to your `Cargo.toml`: ```toml [dependencies] -glicko_2 = "1.0.0" +glicko_2 = "1.1.0" ``` ## Sample Usage @@ -33,7 +33,7 @@ let mut opponent_4 = Rating::new(&tuning); /// Rate our team against a vector of matchup results algorithm::rate( &mut team_to_update, - vec![(Outcome::Win, &mut opponent_1), + &mut [(Outcome::Win, &mut opponent_1), (Outcome::Loss, &mut opponent_2), (Outcome::Draw, &mut opponent_3), ] @@ -43,7 +43,7 @@ algorithm::rate( opponent_4.decay(); /// Print our updated rating -println!("{:?}", team_to_update); // { mu: 1500.0, phi: 255.40, sigma: 0.0059, is_scaled: false } +println!("{:?}", team_to_update); // Rating(μ=1500.0, φ=255.40, σ=0.0059)0059 } ``` ### To get the odds one team will beat another @@ -60,7 +60,7 @@ let mut rating_2 = Rating::new(&tuning); /// Get odds (percent chance team_1 beats team_2) let odds = game::odds(&mut rating_1, &mut rating_2); -println!("{}", odds); // 0.5, perfect odds since both teams have the same rating +println!("{odds}"); // 0.5, perfect odds since both teams have the same rating ``` ### To determine the quality of a matchup @@ -77,7 +77,7 @@ let mut rating_2 = Rating::new(&tuning); /// Get odds (the advantage team 1 has over team 2) let quality = game::quality(&mut rating_1, &mut rating_2); -println!("{}", quality); // 1.0, perfect matchup since both teams have the same rating +println!("{quality}"); // 1.0, perfect matchup since both teams have the same rating ``` ### To update both team's ratings for a single matchup @@ -96,8 +96,8 @@ let mut rating_2 = Rating::new(&tuning); game::compete(&mut rating_1, &mut rating_2, false); /// Print our updated ratings -println!("{:?}", rating_1); // { mu: 1646.47, phi: 307.84, sigma: 0.0059, is_scaled: false } -println!("{:?}", rating_2); // { mu: 1383.42, phi: 306.83, sigma: 0.0059, is_scaled: false } +println!("{rating_1}"); // Rating(μ=1646.47, φ=307.84, σ=0.0059) +println!("{rating_2}"); // Rating(μ=1383.42, φ=306.83, σ=0.0059) ``` ## Rating diff --git a/src/glicko2/algorithm.rs b/src/glicko2/algorithm.rs index 09de1d8..f2341ea 100644 --- a/src/glicko2/algorithm.rs +++ b/src/glicko2/algorithm.rs @@ -90,7 +90,7 @@ fn determine_sigma(rating: &Rating, difference: &f64, variance: &f64) -> f64 { /// use glicko_2::{Rating, Tuning, game::Outcome}; /// /// let tuning = Tuning::default(); -/// +/// /// let mut team_to_update = Rating::new(&tuning); /// let mut opponent_1 = Rating::new(&tuning); /// let mut opponent_2 = Rating::new(&tuning); @@ -98,13 +98,13 @@ fn determine_sigma(rating: &Rating, difference: &f64, variance: &f64) -> f64 { /// /// glicko_2::algorithm::rate( /// &mut team_to_update, -/// vec![(Outcome::Win, &mut opponent_1), +/// &mut [(Outcome::Win, &mut opponent_1), /// (Outcome::Loss, &mut opponent_2), /// (Outcome::Draw, &mut opponent_3), /// ] /// ); /// ``` -pub fn rate(rating: &mut Rating, outcomes: Vec<(Outcome, &mut Rating)>) { +pub fn rate(rating: &mut Rating, outcomes: &mut [(Outcome, &mut Rating)]) { // Outcome is a list of outcomes for a set of games between two teams, i.e. // a vector tuples like [(WIN, rating2), ...] diff --git a/src/glicko2/game.rs b/src/glicko2/game.rs index 5965320..61b9626 100644 --- a/src/glicko2/game.rs +++ b/src/glicko2/game.rs @@ -21,11 +21,11 @@ use crate::glicko2::{algorithm, constants, rating::Rating}; pub fn compete(winner: &mut Rating, loser: &mut Rating, drawn: bool) { // drawn is false if Team 1 beat Team 2 if drawn { - algorithm::rate(winner, vec![(Outcome::Draw, loser)]); - algorithm::rate(loser, vec![(Outcome::Draw, winner)]); + algorithm::rate(winner, &mut [(Outcome::Draw, loser)]); + algorithm::rate(loser, &mut [(Outcome::Draw, winner)]); } else { - algorithm::rate(winner, vec![(Outcome::Win, loser)]); - algorithm::rate(loser, vec![(Outcome::Loss, winner)]); + algorithm::rate(winner, &mut [(Outcome::Win, loser)]); + algorithm::rate(loser, &mut [(Outcome::Loss, winner)]); }; } @@ -94,7 +94,7 @@ impl Outcome { /// let loss = Outcome::Loss; /// let loss_val = loss.val(); /// ``` - pub fn val(&self) -> f64 { + pub const fn val(&self) -> f64 { match self { Outcome::Win => constants::WIN, Outcome::Draw => constants::DRAW, diff --git a/src/glicko2/rating.rs b/src/glicko2/rating.rs index de4a4f0..689d24f 100644 --- a/src/glicko2/rating.rs +++ b/src/glicko2/rating.rs @@ -10,11 +10,11 @@ pub struct Rating<'a> { pub mu: f64, pub phi: f64, pub sigma: f64, - pub is_scaled: bool, + pub(crate) is_scaled: bool, pub(crate) tuning: &'a Tuning, } -impl<'a> Rating<'a> { +impl Rating<'_> { /// Create a new instance of a Rating based on the provided tuning parameters. /// /// # Example @@ -60,10 +60,10 @@ impl<'a> Rating<'a> { /// # Example /// ``` /// use glicko_2::{Rating, Tuning}; - /// + /// /// let tuning = Tuning::default(); /// let mut new_rating = Rating::new(&tuning); - /// + /// /// new_rating.decay(); /// ``` pub fn decay(&mut self) { @@ -75,3 +75,13 @@ impl<'a> Rating<'a> { self.scale_up(); } } + +impl std::fmt::Display for Rating<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "Rating(μ={:.2}, φ={:.2}, σ={:.4})", + self.mu, self.phi, self.sigma + ) + } +} diff --git a/src/lib.rs b/src/lib.rs index 718dcc1..0d02236 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -176,6 +176,13 @@ mod rating_tests { assert!(!new_rating.is_scaled); } + #[test] + fn create_rating_string() { + let new_rating = Rating::new(&TUNING); + let rating_string = format!("{new_rating}"); + assert_eq!(rating_string, "Rating(μ=1500.00, φ=350.00, σ=0.0060)"); + } + #[test] fn scale_down() { let mut new_rating = Rating::new(&TUNING); @@ -283,7 +290,7 @@ mod algorithm_tests { }; algorithm::rate( &mut new_rating, - vec![(game::Outcome::Win, &mut other_rating)], + &mut [(game::Outcome::Win, &mut other_rating)], ); println!("{:?}", new_rating); assert_eq!(new_rating.mu, 1643.2419919603035);