Skip to content

Commit

Permalink
Merge pull request #83 from osu-tournament-rating/feature/store-ra-ru…
Browse files Browse the repository at this point in the history
…leset

Store ruleset in `RatingAdjustment` and publish to database
  • Loading branch information
hburn7 authored Dec 27, 2024
2 parents 0302fae + a9f46f2 commit 607c720
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/database/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl DbClient {
/// Save all rating adjustments in a single batch query
async fn save_rating_adjustments(&self, adjustment_mapping: &HashMap<i32, Vec<RatingAdjustment>>) {
// Prepare the base query
let base_query = "INSERT INTO rating_adjustments (player_id, player_rating_id, match_id, \
let base_query = "INSERT INTO rating_adjustments (player_id, ruleset, player_rating_id, match_id, \
rating_before, rating_after, volatility_before, volatility_after, timestamp, adjustment_type) \
VALUES ";

Expand All @@ -303,8 +303,9 @@ impl DbClient {
let match_id = adjustment.match_id.map_or("NULL".to_string(), |id| id.to_string());

let value_tuple = format!(
"({}, {}, {}, {}, {}, {}, {}, '{}', {})",
"({}, {}, {}, {}, {}, {}, {}, {}, '{}', {})",
adjustment.player_id,
adjustment.ruleset as i32,
player_rating_id,
match_id,
adjustment.rating_before,
Expand Down
1 change: 1 addition & 0 deletions src/database/db_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub struct PlayerRating {
#[derive(Debug, Clone, Serialize)]
pub struct RatingAdjustment {
pub player_id: i32,
pub ruleset: Ruleset,
pub match_id: Option<i32>,
pub rating_before: f64,
pub rating_after: f64,
Expand Down
1 change: 1 addition & 0 deletions src/model/decay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl DecayTracker {

decay_ratings.push(RatingAdjustment {
player_id,
ruleset,
match_id: None,
rating_before: old_rating,
rating_after: new_rating,
Expand Down
1 change: 1 addition & 0 deletions src/model/otr_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ impl OtrModel {
// Create the adjustment
let adjustment = RatingAdjustment {
player_id: *k,
ruleset: match_.ruleset,
match_id: Some(match_.id),
rating_before: player_rating.rating,
rating_after: v.mu,
Expand Down
1 change: 1 addition & 0 deletions src/model/rating_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn create_initial_ratings(player: &Player) -> Vec<PlayerRating> {
let rating = initial_rating(player, &ruleset);
let adjustment = RatingAdjustment {
player_id: player.id,
ruleset,
match_id: None,
rating_before: 0.0,
rating_after: rating,
Expand Down
16 changes: 8 additions & 8 deletions src/model/structures/rating_adjustment_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use std::convert::TryFrom;
#[repr(u8)]
pub enum RatingAdjustmentType {
Initial = 0,
Match = 1,
Decay = 2
Decay = 1,
Match = 2
}

impl TryFrom<i32> for RatingAdjustmentType {
type Error = ();
fn try_from(v: i32) -> Result<Self, Self::Error> {
match v {
0 => Ok(RatingAdjustmentType::Initial),
1 => Ok(RatingAdjustmentType::Match),
2 => Ok(RatingAdjustmentType::Decay),
1 => Ok(RatingAdjustmentType::Decay),
2 => Ok(RatingAdjustmentType::Match),
_ => Err(())
}
}
Expand All @@ -32,13 +32,13 @@ mod tests {
}

#[test]
fn test_convert_match() {
assert_eq!(RatingAdjustmentType::try_from(1), Ok(RatingAdjustmentType::Match));
fn test_convert_decay() {
assert_eq!(RatingAdjustmentType::try_from(1), Ok(RatingAdjustmentType::Decay));
}

#[test]
fn test_convert_decay() {
assert_eq!(RatingAdjustmentType::try_from(2), Ok(RatingAdjustmentType::Decay));
fn test_convert_match() {
assert_eq!(RatingAdjustmentType::try_from(2), Ok(RatingAdjustmentType::Match));
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/utils/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn generate_player_rating(

adjustments.push(RatingAdjustment {
player_id,
ruleset,
adjustment_type,
match_id: None,
rating_before,
Expand Down

0 comments on commit 607c720

Please sign in to comment.