test: add dx rating calculate check
This commit is contained in:
@@ -15,4 +15,5 @@ serde_json = { workspace = true }
|
|||||||
spdlog-rs = { workspace = true, optional = true }
|
spdlog-rs = { workspace = true, optional = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
default = ["log"]
|
||||||
log = ["dep:spdlog-rs"]
|
log = ["dep:spdlog-rs"]
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use std::{fs::OpenOptions, io::BufReader, sync::LazyLock};
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
use rust_decimal::{Decimal, dec, serde::DecimalFromString};
|
use rust_decimal::{Decimal, dec, serde::DecimalFromString};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
@@ -41,18 +41,7 @@ pub fn query_music_level(music_id: u32, level: u32) -> Option<&'static Level> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub static MUSIC_DB: LazyLock<Option<MusicDB>> = LazyLock::new(|| {
|
pub static MUSIC_DB: LazyLock<Option<MusicDB>> = LazyLock::new(|| {
|
||||||
let json = OpenOptions::new()
|
let db: Vec<MusicInfo> = serde_json::from_slice(include_bytes!("musicDB.json"))
|
||||||
.read(true)
|
|
||||||
.create(false)
|
|
||||||
.open("musicDB.json")
|
|
||||||
.inspect_err(|_e| {
|
|
||||||
#[cfg(feature = "log")]
|
|
||||||
spdlog::warn!("failed to load musicDB: {_e}")
|
|
||||||
})
|
|
||||||
.ok()?;
|
|
||||||
let buf_reader = BufReader::new(json);
|
|
||||||
|
|
||||||
let db: Vec<MusicInfo> = serde_json::from_reader(buf_reader)
|
|
||||||
.inspect_err(|_e| {
|
.inspect_err(|_e| {
|
||||||
#[cfg(feature = "log")]
|
#[cfg(feature = "log")]
|
||||||
spdlog::warn!("failed to load musicDB: {_e}")
|
spdlog::warn!("failed to load musicDB: {_e}")
|
||||||
@@ -78,8 +67,11 @@ impl Level {
|
|||||||
let difficulty_rank: Decimal = self.difficulty.value;
|
let difficulty_rank: Decimal = self.difficulty.value;
|
||||||
let achievement = Decimal::new(achievement as _, 4);
|
let achievement = Decimal::new(achievement as _, 4);
|
||||||
|
|
||||||
|
#[cfg(feature = "log")]
|
||||||
|
spdlog::info!("factor: {factor}, achievement: {achievement}");
|
||||||
|
|
||||||
// when ach > 100.5%, calculate as 100.5%
|
// when ach > 100.5%, calculate as 100.5%
|
||||||
let rating: u32 = (factor * difficulty_rank * achievement.min(dec!(100.5)))
|
let rating: u32 = (factor * difficulty_rank * achievement)
|
||||||
.floor()
|
.floor()
|
||||||
.try_into()
|
.try_into()
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
@@ -112,3 +104,14 @@ const RANKS: [(&'static str, i32, Decimal); 23] = [
|
|||||||
("SSS", 1004999, dec!(0.222)),
|
("SSS", 1004999, dec!(0.222)),
|
||||||
("SSS+", 1005000, dec!(0.224)),
|
("SSS+", 1005000, dec!(0.224)),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::query_music_level;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rating_calculate() {
|
||||||
|
let level = query_music_level(11696, 3).expect("not found");
|
||||||
|
assert_eq!(level.dx_rating(953184), ("AAA", 184));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user