Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store ruleset in RatingAdjustment and publish to database #83

Merged
merged 6 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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