feat: simple musicDB for title

This commit is contained in:
mokurin000
2025-08-01 02:03:47 +08:00
parent 9b046036c9
commit 6fd7361ca1
7 changed files with 121 additions and 3 deletions

View File

@@ -43,3 +43,4 @@ bincode = { version = "2.0.1", optional = true }
# magic macro
crabtime = { git = "https://github.com/wdanilo/crabtime.git", rev = "2ed856f5" }
rustc-hash = "2.1.1"

View File

@@ -10,6 +10,5 @@ pub fn level_name(level: u32) -> &'static str {
}
}
// TODO: MusicDB lazy load
// struct MusicDB;
// static MUSIC_DB: LazyLock<MusicDB> = LazyLock::new(|| unimplemented!());
mod music_db;
pub use music_db::{MUSIC_DB, MusicInfo,};

View File

@@ -0,0 +1,32 @@
use std::{fs::OpenOptions, sync::LazyLock};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use spdlog::{info, warn};
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MusicInfo {
pub id: u32,
pub name: String,
pub version: i64,
}
type MusicDB = FxHashMap<u32, MusicInfo>;
pub static MUSIC_DB: LazyLock<Option<MusicDB>> = LazyLock::new(|| {
info!("loading musicDB...");
let json = OpenOptions::new()
.read(true)
.create(false)
.open("musicDB.json")
.inspect_err(|e| warn!("failed to load musicDB: {e}"))
.ok()?;
let db: Vec<MusicInfo> = serde_json::from_reader(json)
.inspect_err(|e| warn!("failed to load musicDB: {e}"))
.ok()?;
Some(db.into_iter().map(|entry| (entry.id, entry)).collect())
});

View File

@@ -3,6 +3,7 @@ use std::fmt::Display;
use serde::Deserialize;
use serde::Serialize;
use crate::helper::MUSIC_DB;
use crate::helper::level_name;
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -109,6 +110,10 @@ impl Display for GetUserRatingApiResp {
impl Display for MusicRating {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("歌曲ID: \t{}\n", self.music_id))?;
if let Some(info) = MUSIC_DB.as_ref().map(|db| db.get(&self.music_id)).flatten() {
let title = &info.name;
f.write_fmt(format_args!("曲目标题: \t{title}\n"))?;
}
f.write_fmt(format_args!(
"谱面版本: \t{}\n",
match (self.music_id / 10000) % 10 {