Skip to content

Commit

Permalink
attempt to ensure .osu file md5s
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunyoku committed Jun 2, 2024
1 parent 9732002 commit df50b0d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ improved-miss-penalty = { package = "akatsuki-pp", git = "https://github.com/osu
accuracy-fun = { package = "akatsuki-pp", git = "https://github.com/osuAkatsuki/akatsuki-pp-rs", rev = "879fd8caa2cb082a5b9c0761fc2ddca3b283b226", features = [
"async_tokio",
] }
md5 = "0.7.0"

[profile.release]
lto = "fat"
Expand Down
17 changes: 13 additions & 4 deletions src/api/routes/calculate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub fn router() -> Router {
#[derive(serde::Serialize, serde::Deserialize)]
pub struct CalculateRequest {
pub beatmap_id: i32,
pub beatmap_md5: String,
pub mode: i32,
pub mods: i32,
pub max_combo: i32,
Expand All @@ -36,8 +37,12 @@ async fn calculate_relax_pp(
request: &CalculateRequest,
context: Arc<Context>,
) -> anyhow::Result<CalculateResponse> {
let beatmap_bytes =
usecases::beatmaps::fetch_beatmap_osu_file(request.beatmap_id, context).await?;
let beatmap_bytes = usecases::beatmaps::fetch_beatmap_osu_file(
request.beatmap_id,
&request.beatmap_md5,
context,
)
.await?;
let beatmap = Beatmap::from_bytes(&beatmap_bytes).await?;

let result = akatsuki_pp_rs::osu_2019::OsuPP::new(&beatmap)
Expand Down Expand Up @@ -72,8 +77,12 @@ async fn calculate_rosu_pp(
request: &CalculateRequest,
context: Arc<Context>,
) -> anyhow::Result<CalculateResponse> {
let beatmap_bytes =
usecases::beatmaps::fetch_beatmap_osu_file(request.beatmap_id, context).await?;
let beatmap_bytes = usecases::beatmaps::fetch_beatmap_osu_file(
request.beatmap_id,
&request.beatmap_md5,
context,
)
.await?;
let beatmap = Beatmap::from_bytes(&beatmap_bytes).await?;

let result = beatmap
Expand Down
29 changes: 19 additions & 10 deletions src/deploy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use tokio::sync::Mutex;
#[derive(serde::Serialize, serde::Deserialize)]
pub struct CalculateRequest {
pub beatmap_id: i32,
pub beatmap_md5: String,
pub mode: i32,
pub mods: i32,
pub max_combo: i32,
Expand Down Expand Up @@ -44,8 +45,12 @@ async fn calculate_special_pp(
.unwrap()
.clone()
} else {
let beatmap_bytes =
usecases::beatmaps::fetch_beatmap_osu_file(request.beatmap_id, context.clone()).await?;
let beatmap_bytes = usecases::beatmaps::fetch_beatmap_osu_file(
request.beatmap_id,
&request.beatmap_md5,
context.clone(),
)
.await?;
let beatmap = Beatmap::from_bytes(&beatmap_bytes).await?;

recalc_mutex
Expand Down Expand Up @@ -91,8 +96,12 @@ async fn calculate_rosu_pp(
.unwrap()
.clone()
} else {
let beatmap_bytes =
usecases::beatmaps::fetch_beatmap_osu_file(request.beatmap_id, context.clone()).await?;
let beatmap_bytes = usecases::beatmaps::fetch_beatmap_osu_file(
request.beatmap_id,
&request.beatmap_md5,
context.clone(),
)
.await?;
let beatmap = Beatmap::from_bytes(&beatmap_bytes).await?;

recalc_mutex
Expand Down Expand Up @@ -139,6 +148,7 @@ async fn recalculate_score(
) -> anyhow::Result<()> {
let request = CalculateRequest {
beatmap_id: score.beatmap_id,
beatmap_md5: score.beatmap_md5,
mode: score.play_mode,
mods: score.mods,
max_combo: score.max_combo,
Expand Down Expand Up @@ -427,12 +437,11 @@ async fn recalculate_user(
.execute(ctx.database.get().await?.deref_mut())
.await?;

let (country, user_privileges): (String, i32) = sqlx::query_as(
"SELECT country, privileges FROM users WHERE id = ?",
)
.bind(user_id)
.fetch_one(ctx.database.get().await?.deref_mut())
.await?;
let (country, user_privileges): (String, i32) =
sqlx::query_as("SELECT country, privileges FROM users WHERE id = ?")
.bind(user_id)
.fetch_one(ctx.database.get().await?.deref_mut())
.await?;

let last_score_time: Option<i32> = sqlx::query_scalar(&format!(
"SELECT max(time) FROM {} INNER JOIN beatmaps USING(beatmap_md5)
Expand Down
21 changes: 14 additions & 7 deletions src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ async fn calculate_conceptual_pp(
context: Arc<Context>,
) -> anyhow::Result<f32> {
let beatmap_bytes =
usecases::beatmaps::fetch_beatmap_osu_file(score.beatmap_id, context).await?;
usecases::beatmaps::fetch_beatmap_osu_file(score.beatmap_id, &score.beatmap_md5, context)
.await?;
let beatmap = ConceptualBeatmap::from_bytes(&beatmap_bytes).await?;

let result = beatmap
Expand Down Expand Up @@ -74,7 +75,8 @@ async fn calculate_skill_rebalance_pp(
context: Arc<Context>,
) -> anyhow::Result<f32> {
let beatmap_bytes =
usecases::beatmaps::fetch_beatmap_osu_file(score.beatmap_id, context).await?;
usecases::beatmaps::fetch_beatmap_osu_file(score.beatmap_id, &score.beatmap_md5, context)
.await?;
let beatmap = SkillRebalanceBeatmap::from_bytes(&beatmap_bytes).await?;

let result = beatmap
Expand Down Expand Up @@ -105,7 +107,8 @@ async fn calculate_cursordance_pp(
context: Arc<Context>,
) -> anyhow::Result<f32> {
let beatmap_bytes =
usecases::beatmaps::fetch_beatmap_osu_file(score.beatmap_id, context).await?;
usecases::beatmaps::fetch_beatmap_osu_file(score.beatmap_id, &score.beatmap_md5, context)
.await?;
let beatmap = CdBeatmap::from_bytes(&beatmap_bytes).await?;

let result = cursordance::osu_2019::OsuPP::new(&beatmap)
Expand All @@ -128,7 +131,8 @@ async fn calculate_no_accuracy_pp(
context: Arc<Context>,
) -> anyhow::Result<f32> {
let beatmap_bytes =
usecases::beatmaps::fetch_beatmap_osu_file(score.beatmap_id, context).await?;
usecases::beatmaps::fetch_beatmap_osu_file(score.beatmap_id, &score.beatmap_md5, context)
.await?;
let beatmap = NoAccuracyBeatmap::from_bytes(&beatmap_bytes).await?;

let result = no_accuracy::osu_2019::OsuPP::new(&beatmap)
Expand All @@ -151,7 +155,8 @@ async fn calculate_simplfy_relax_pp(
context: Arc<Context>,
) -> anyhow::Result<f32> {
let beatmap_bytes =
usecases::beatmaps::fetch_beatmap_osu_file(score.beatmap_id, context).await?;
usecases::beatmaps::fetch_beatmap_osu_file(score.beatmap_id, &score.beatmap_md5, context)
.await?;
let beatmap = SimplifyRelaxBeatmap::from_bytes(&beatmap_bytes).await?;

let result = simplify_relax::osu_2019::OsuPP::new(&beatmap)
Expand All @@ -174,7 +179,8 @@ async fn calculate_improved_miss_penalty_pp(
context: Arc<Context>,
) -> anyhow::Result<f32> {
let beatmap_bytes =
usecases::beatmaps::fetch_beatmap_osu_file(score.beatmap_id, context).await?;
usecases::beatmaps::fetch_beatmap_osu_file(score.beatmap_id, &score.beatmap_md5, context)
.await?;
let beatmap = ImprovedMissPenaltyBeatmap::from_bytes(&beatmap_bytes).await?;

let result = improved_miss_penalty::osu_2019::OsuPP::new(&beatmap)
Expand All @@ -197,7 +203,8 @@ async fn calculate_accuracy_fun_pp(
context: Arc<Context>,
) -> anyhow::Result<f32> {
let beatmap_bytes =
usecases::beatmaps::fetch_beatmap_osu_file(score.beatmap_id, context).await?;
usecases::beatmaps::fetch_beatmap_osu_file(score.beatmap_id, &score.beatmap_md5, context)
.await?;
let beatmap = AccuracyFunBeatmap::from_bytes(&beatmap_bytes).await?;

let result = accuracy_fun::osu_2019::OsuPP::new(&beatmap)
Expand Down
14 changes: 12 additions & 2 deletions src/usecases/beatmaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::context::Context;

pub async fn fetch_beatmap_osu_file(
beatmap_id: i32,
beatmap_md5: &str,
context: Arc<Context>,
) -> anyhow::Result<Vec<u8>> {
let beatmap_path = &format!("beatmaps/{beatmap_id}.osu");
Expand All @@ -15,8 +16,17 @@ pub async fn fetch_beatmap_osu_file(
Err(S3Error::Http(status_code, _)) if status_code == 404 => Ok(None),
Err(e) => Err(e),
}?;
if existing_file.is_some() {
return Ok(existing_file.unwrap().to_vec());

match existing_file {
Some(existing_file) => {
let osu_file_data = existing_file.to_vec();
let osu_file_data_md5 = format!("{:x}", md5::compute(&osu_file_data));

if osu_file_data_md5 == beatmap_md5 {
return Ok(osu_file_data);
}
}
None => {}
}

let osu_response = reqwest::get(&format!("https://old.ppy.sh/osu/{beatmap_id}"))
Expand Down

0 comments on commit df50b0d

Please sign in to comment.