mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-19 00:38:54 +00:00
Getting pp info when a new user is saved. (#42)
This commit is contained in:
parent
1066f249b0
commit
f3c062f417
3 changed files with 40 additions and 3 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -3181,6 +3181,7 @@ dependencies = [
|
|||
"bitflags 1.3.2",
|
||||
"chrono",
|
||||
"dashmap",
|
||||
"futures-util",
|
||||
"lazy_static",
|
||||
"osuparse",
|
||||
"rand",
|
||||
|
|
|
@ -22,6 +22,7 @@ serde = { version = "1.0.137", features = ["derive"] }
|
|||
serenity = "0.12"
|
||||
zip = "0.6.2"
|
||||
rand = "0.8"
|
||||
futures-util = "0.3.30"
|
||||
|
||||
youmubot-db = { path = "../youmubot-db" }
|
||||
youmubot-db-sql = { path = "../youmubot-db-sql" }
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use std::{str::FromStr, sync::Arc};
|
||||
|
||||
use futures_util::join;
|
||||
use rand::seq::IteratorRandom;
|
||||
use serenity::{
|
||||
builder::{CreateMessage, EditMessage},
|
||||
|
@ -80,7 +81,6 @@ impl TypeMapKey for OsuEnv {
|
|||
/// This does NOT automatically enable:
|
||||
/// - Commands on the "osu" prefix
|
||||
/// - Hooks. Hooks are completely opt-in.
|
||||
///
|
||||
pub async fn setup(
|
||||
data: &mut TypeMap,
|
||||
prelude: youmubot_prelude::Env,
|
||||
|
@ -364,14 +364,49 @@ pub async fn forcesave(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
|
|||
}
|
||||
|
||||
async fn add_user(target: serenity::model::id::UserId, user: User, env: &OsuEnv) -> Result<()> {
|
||||
let pp_fut = async {
|
||||
[Mode::Std, Mode::Taiko, Mode::Catch, Mode::Mania]
|
||||
.into_iter()
|
||||
.map(|mode| async move {
|
||||
env.client
|
||||
.user(UserID::ID(user.id), |f| f.mode(mode))
|
||||
.await
|
||||
.unwrap_or_else(|err| {
|
||||
eprintln!("{}", err);
|
||||
None
|
||||
})
|
||||
.and_then(|u| u.pp)
|
||||
})
|
||||
.collect::<stream::FuturesOrdered<_>>()
|
||||
.collect::<Vec<_>>()
|
||||
.await
|
||||
};
|
||||
|
||||
let std_weight_map_length_fut = async {
|
||||
let scores = env
|
||||
.client
|
||||
.user_best(UserID::ID(user.id), |f| f.mode(Mode::Std).limit(100))
|
||||
.await
|
||||
.unwrap_or_else(|err| {
|
||||
eprintln!("{}", err);
|
||||
vec![]
|
||||
});
|
||||
|
||||
calculate_weighted_map_length(&scores, &env.beatmaps, Mode::Std)
|
||||
.await
|
||||
.pls_ok()
|
||||
};
|
||||
|
||||
let (pp, std_weight_map_length) = join!(pp_fut, std_weight_map_length_fut);
|
||||
|
||||
let u = OsuUser {
|
||||
user_id: target,
|
||||
username: user.username.into(),
|
||||
id: user.id,
|
||||
failures: 0,
|
||||
last_update: chrono::Utc::now(),
|
||||
pp: [None, None, None, None],
|
||||
std_weighted_map_length: None,
|
||||
pp: pp.try_into().unwrap(),
|
||||
std_weighted_map_length: std_weight_map_length,
|
||||
};
|
||||
env.saved_users.new_user(u).await?;
|
||||
Ok(())
|
||||
|
|
Loading…
Add table
Reference in a new issue