diff --git a/Cargo.lock b/Cargo.lock index 6d8554d..8bd6231 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -574,7 +574,7 @@ checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" [[package]] name = "ruborute" -version = "0.1.2" +version = "0.1.3" dependencies = [ "clap", "clap_derive", diff --git a/Cargo.toml b/Cargo.toml index 5e9c2e3..7932416 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ authors = ["RinChanNOW "] description = "ruborute is a command-line tool to get asphyxia@sdvx gaming data." edition = "2018" name = "ruborute" -version = "0.1.2" +version = "0.1.3" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/storage/record.rs b/src/storage/record.rs index 4cb2c72..72b15eb 100644 --- a/src/storage/record.rs +++ b/src/storage/record.rs @@ -313,14 +313,14 @@ impl std::ops::Add for LevelStat { pub struct RecordStore { /// music records of current user. /// Vec contained music records of different levels. - records: HashMap>, + records: HashMap>, } impl RecordStore { /// open db file and load all music data to memory pub fn open(user: String, path: impl Into, music_store: &MusicStore) -> Result { let path = path.into(); - let mut records: HashMap> = HashMap::new(); + let mut records: HashMap> = HashMap::new(); // load data let mut reader = BufReader::new(File::open(&path)?); let mut stream = serde_json::Deserializer::from_reader(&mut reader).into_iter::(); @@ -334,12 +334,20 @@ impl RecordStore { let music = music_store.get_music(music_record.music_id); let full_record = FullRecord::from_record_with_music(&music_record, music); if let Some(rec) = records.get_mut(&full_record.get_music_id()) { - if !rec.iter().any(|r| r.level == full_record.get_level()) { - rec.push(full_record.clone()); + let level = full_record.get_level(); + if !rec.contains_key(&level) { + rec.insert(level, full_record); + } else if let Some(r) = rec.get_mut(&level) { + // record the best record + if r.get_volforce() < full_record.get_volforce() { + *r = full_record; + } } - rec.sort_by_key(|r| r.get_level()); } else { - records.insert(full_record.get_music_id(), vec![full_record.clone()]); + let mut m = HashMap::new(); + let id = full_record.get_music_id(); + m.insert(full_record.get_level(), full_record); + records.insert(id, m); } } } @@ -357,8 +365,9 @@ impl RecordStore { .iter() .filter(|(id, _)| music_id.contains(&id)) .map(|(_, rec)| rec) - .collect::>>() + .collect::>>() .into_iter() + .map(|map| map.values().collect::>()) .flatten() .collect::>() } @@ -369,21 +378,18 @@ impl RecordStore { .records .iter() .map(|(_, rec)| rec) - .collect::>>() + .collect::>>() .into_iter() + .map(|map| map.values().collect::>()) .flatten() .collect::>(); records.sort_by_key(|rec| rec.volfoce); - let mut res = Vec::with_capacity(50); - let mut count = 0; - for &rec in records.iter().rev() { - if count == 50 { - break; - } - res.push(rec); - count += 1; - } - res + records + .iter() + .rev() + .take(50) + .cloned() + .collect::>() } /// compute the complete volforce @@ -404,8 +410,9 @@ impl RecordStore { .records .iter() .map(|(_, rec)| rec) - .collect::>>() + .collect::>>() .into_iter() + .map(|map| map.values().collect::>()) .flatten() .filter(|r| match level { Some(l) => r.get_level() == l,