Skip to content

Commit

Permalink
osu: Implement map length on server ranks
Browse files Browse the repository at this point in the history
  • Loading branch information
natsukagami committed Mar 5, 2024
1 parent a50f44a commit 11f32cc
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 71 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ All PRs welcome.
- `youmubot-core`: Core commands: admin, fun, community
- `youmubot-osu`: osu!-related commands.

## Working with `sqlx`

### Regenerate compiler information

From within `./youmubot-db-sql` run
```bash
cargo sqlx prepare --database-url "sqlite:$(realpath ..)/youmubot.db"
```

## License

Basically MIT.

This file was deleted.

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

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Add migration script here

ALTER TABLE osu_users
ADD COLUMN std_weighted_map_length DOUBLE NULL DEFAULT NULL;

23 changes: 16 additions & 7 deletions youmubot-db-sql/src/models/osu_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub struct OsuUser {
pub pp_catch: Option<f64>,
/// Number of consecutive update failures
pub failures: u8,

pub std_weighted_map_length: Option<f64>,
}

impl OsuUser {
Expand All @@ -30,7 +32,8 @@ impl OsuUser {
id as "id: i64",
last_update as "last_update: DateTime",
pp_std, pp_taiko, pp_mania, pp_catch,
failures as "failures: u8"
failures as "failures: u8",
std_weighted_map_length
FROM osu_users WHERE user_id = ?"#,
user_id
)
Expand All @@ -52,7 +55,8 @@ impl OsuUser {
id as "id: i64",
last_update as "last_update: DateTime",
pp_std, pp_taiko, pp_mania, pp_catch,
failures as "failures: u8"
failures as "failures: u8",
std_weighted_map_length
FROM osu_users WHERE id = ?"#,
osu_id
)
Expand All @@ -74,7 +78,8 @@ impl OsuUser {
id as "id: i64",
last_update as "last_update: DateTime",
pp_std, pp_taiko, pp_mania, pp_catch,
failures as "failures: u8"
failures as "failures: u8",
std_weighted_map_length
FROM osu_users"#,
)
.fetch_many(conn)
Expand All @@ -90,8 +95,8 @@ impl OsuUser {
{
query!(
r#"INSERT
INTO osu_users(user_id, username, id, last_update, pp_std, pp_taiko, pp_mania, pp_catch, failures)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)
INTO osu_users(user_id, username, id, last_update, pp_std, pp_taiko, pp_mania, pp_catch, failures, std_weighted_map_length)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (user_id) WHERE id = ? DO UPDATE
SET
last_update = excluded.last_update,
Expand All @@ -100,7 +105,8 @@ impl OsuUser {
pp_taiko = excluded.pp_taiko,
pp_mania = excluded.pp_mania,
pp_catch = excluded.pp_catch,
failures = excluded.failures
failures = excluded.failures,
std_weighted_map_length = excluded.std_weighted_map_length
"#,
self.user_id,
self.username,
Expand All @@ -111,7 +117,10 @@ impl OsuUser {
self.pp_mania,
self.pp_catch,
self.failures,
self.user_id).execute(conn).await?;
self.std_weighted_map_length,

self.user_id,
).execute(conn).await?;
Ok(())
}

Expand Down
32 changes: 32 additions & 0 deletions youmubot-osu/src/discord/announcer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::db::{OsuSavedUsers, OsuUser};
use super::OsuClient;
use super::{embeds::score_embed, BeatmapWithMode};
use crate::{
discord::beatmap_cache::BeatmapMetaCache,
Expand All @@ -19,6 +20,7 @@ use serenity::{
};
use std::{convert::TryInto, sync::Arc};
use youmubot_prelude::announcer::CacheAndHttp;
use youmubot_prelude::stream::{FuturesUnordered, TryStreamExt};
use youmubot_prelude::*;

/// osu! announcer's unique announcer key.
Expand Down Expand Up @@ -83,7 +85,12 @@ impl youmubot_prelude::Announcer for Announcer {
.unwrap();
osu_user.username = v.into_iter().next().unwrap().username.into();
osu_user.last_update = now;
osu_user.std_weighted_map_length =
Self::std_weighted_map_length(&ctx, &osu_user)
.await
.pls_ok();
let id = osu_user.id;
println!("{:?}", osu_user);
ctx.data
.read()
.await
Expand Down Expand Up @@ -185,6 +192,31 @@ impl Announcer {
.collect();
Ok(scores)
}

async fn std_weighted_map_length(ctx: &Context, u: &OsuUser) -> Result<f64> {
let data = ctx.data.read().await;
let client = data.get::<OsuClient>().unwrap().clone();
let cache = data.get::<BeatmapMetaCache>().unwrap();
let scores = client
.user_best(UserID::ID(u.id), |f| f.mode(Mode::Std).limit(100))
.await?;
scores
.into_iter()
.enumerate()
.map(|(i, s)| async move {
let beatmap = cache.get_beatmap_default(s.beatmap_id).await?;
const SCALING_FACTOR: f64 = 0.975;
Ok(beatmap
.difficulty
.apply_mods(s.mods, 0.0 /* dont care */)
.drain_length
.as_secs_f64()
* (SCALING_FACTOR.powi(i as i32)))
})
.collect::<FuturesUnordered<_>>()
.try_fold(0.0, |a, b| future::ready(Ok(a + b)))
.await
}
}

#[derive(Clone)]
Expand Down
3 changes: 3 additions & 0 deletions youmubot-osu/src/discord/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub struct OsuUser {
pub id: u64,
pub last_update: DateTime<Utc>,
pub pp: [Option<f64>; 4],
pub std_weighted_map_length: Option<f64>,
/// More than 5 failures => gone
pub failures: u8,
}
Expand All @@ -160,6 +161,7 @@ impl From<OsuUser> for model::OsuUser {
pp_taiko: u.pp[Mode::Taiko as usize],
pp_catch: u.pp[Mode::Catch as usize],
pp_mania: u.pp[Mode::Mania as usize],
std_weighted_map_length: u.std_weighted_map_length,
failures: u.failures,
}
}
Expand All @@ -178,6 +180,7 @@ impl From<model::OsuUser> for OsuUser {
Mode::Catch => u.pp_catch,
Mode::Mania => u.pp_mania,
}),
std_weighted_map_length: u.std_weighted_map_length,
failures: u.failures,
}
}
Expand Down
1 change: 1 addition & 0 deletions youmubot-osu/src/discord/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ async fn add_user(
failures: 0,
last_update: chrono::Utc::now(),
pp: [None, None, None, None],
std_weighted_map_length: None,
};
data.get::<OsuSavedUsers>().unwrap().new_user(u).await?;
Ok(())
Expand Down
Loading

0 comments on commit 11f32cc

Please sign in to comment.