Skip to content

Commit

Permalink
Moved challenge points compute fn out of player_data
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtread committed Sep 18, 2023
1 parent f53df9b commit da93205
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
15 changes: 1 addition & 14 deletions src/database/entities/player_data.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::{database::DbResult, utils::types::PlayerID};
use sea_orm::{
entity::prelude::*,
sea_query::OnConflict,
Expand All @@ -7,8 +8,6 @@ use sea_orm::{
use serde::Serialize;
use std::future::Future;

use crate::{database::DbResult, utils::types::PlayerID};

/// Structure for player data stro
#[derive(Serialize, Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "player_data")]
Expand Down Expand Up @@ -154,16 +153,4 @@ impl Model {
)
.all(db)
}

/// Parses the challenge points value which is the second
/// item in the completion list.
///
/// `db` The database connection
/// `player_id` The ID of the player to get the cp for
pub async fn get_challenge_points(db: &DatabaseConnection, player_id: PlayerID) -> Option<u32> {
let list = Self::get(db, player_id, "Completion").await.ok()??.value;
let part = list.split(',').nth(1)?;
let value: u32 = part.parse().ok()?;
Some(value)
}
}
19 changes: 15 additions & 4 deletions src/services/leaderboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use crate::{
entities::{Player, PlayerData},
DatabaseConnection, DbResult,
},
utils::parsing::{KitNameDeployed, PlayerClass},
utils::{
parsing::{KitNameDeployed, PlayerClass},
types::PlayerID,
},
};
use futures_util::future::BoxFuture;
use log::{debug, error};
Expand Down Expand Up @@ -210,9 +213,7 @@ fn compute_n7_player(db: DatabaseConnection, player: Player) -> Lf {
/// `player` The player to rank
fn compute_cp_player(db: DatabaseConnection, player: Player) -> Lf {
Box::pin(async move {
let value = PlayerData::get_challenge_points(&db, player.id)
.await
.unwrap_or(0);
let value = get_challenge_points(&db, player.id).await.unwrap_or(0);
Ok(LeaderboardEntry {
player_id: player.id,
player_name: player.display_name.into_boxed_str(),
Expand All @@ -222,3 +223,13 @@ fn compute_cp_player(db: DatabaseConnection, player: Player) -> Lf {
})
})
}

async fn get_challenge_points(db: &DatabaseConnection, player_id: PlayerID) -> Option<u32> {
let list = PlayerData::get(db, player_id, "Completion")
.await
.ok()??
.value;
let part = list.split(',').nth(1)?;
let value: u32 = part.parse().ok()?;
Some(value)
}

0 comments on commit da93205

Please sign in to comment.