From da93205f238d1bb6dd47f08559f3110327b55698 Mon Sep 17 00:00:00 2001 From: Jacobtread Date: Mon, 18 Sep 2023 16:05:03 +1200 Subject: [PATCH] Moved challenge points compute fn out of player_data --- src/database/entities/player_data.rs | 15 +-------------- src/services/leaderboard/mod.rs | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/database/entities/player_data.rs b/src/database/entities/player_data.rs index 16c4834d..097f7aca 100644 --- a/src/database/entities/player_data.rs +++ b/src/database/entities/player_data.rs @@ -1,3 +1,4 @@ +use crate::{database::DbResult, utils::types::PlayerID}; use sea_orm::{ entity::prelude::*, sea_query::OnConflict, @@ -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")] @@ -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 { - 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) - } } diff --git a/src/services/leaderboard/mod.rs b/src/services/leaderboard/mod.rs index d3ff6677..44ee43a1 100644 --- a/src/services/leaderboard/mod.rs +++ b/src/services/leaderboard/mod.rs @@ -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}; @@ -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(), @@ -222,3 +223,13 @@ fn compute_cp_player(db: DatabaseConnection, player: Player) -> Lf { }) }) } + +async fn get_challenge_points(db: &DatabaseConnection, player_id: PlayerID) -> Option { + 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) +}