Split user and announcer osu clients

This commit is contained in:
Natsu Kagami 2021-01-22 14:08:15 +09:00
parent 901d55814d
commit 145cb01bd0
Signed by: nki
GPG key ID: 7306B3D3C3AD6E51
3 changed files with 108 additions and 89 deletions

View file

@ -1,5 +1,5 @@
use super::db::{OsuSavedUsers, OsuUser};
use super::{embeds::score_embed, BeatmapWithMode, OsuClient};
use super::{embeds::score_embed, BeatmapWithMode};
use crate::{
discord::beatmap_cache::BeatmapMetaCache,
discord::cache::save_beatmap,
@ -21,7 +21,15 @@ use youmubot_prelude::*;
pub const ANNOUNCER_KEY: &'static str = "osu";
/// The announcer struct implementing youmubot_prelude::Announcer
pub struct Announcer;
pub struct Announcer {
client: Osu,
}
impl Announcer {
pub fn new(client: Osu) -> Self {
Self { client }
}
}
#[async_trait]
impl youmubot_prelude::Announcer for Announcer {
@ -39,6 +47,7 @@ impl youmubot_prelude::Announcer for Announcer {
let d = d.clone();
let channels = &channels;
let c = c.clone();
let s = &self;
async move {
let channels = channels.channels_of(c.clone(), user_id).await;
if channels.is_empty() {
@ -47,7 +56,7 @@ impl youmubot_prelude::Announcer for Announcer {
let pp = match (&[Mode::Std, Mode::Taiko, Mode::Catch, Mode::Mania])
.into_iter()
.map(|m| {
handle_user_mode(
s.handle_user_mode(
c.clone(),
&osu_user,
user_id,
@ -86,8 +95,10 @@ impl youmubot_prelude::Announcer for Announcer {
}
}
impl Announcer {
/// Handles an user/mode scan, announces all possible new scores, return the new pp value.
async fn handle_user_mode(
&self,
c: Arc<CacheAndHttp>,
osu_user: &OsuUser,
user_id: UserId,
@ -96,10 +107,9 @@ async fn handle_user_mode(
d: AppData,
) -> Result<Option<f64>, Error> {
let (scores, user) = {
let data = d.read().await;
let osu = data.get::<OsuClient>().unwrap();
let scores = scan_user(osu, osu_user, mode).await?;
let user = osu
let scores = self.scan_user(osu_user, mode).await?;
let user = self
.client
.user(UserID::ID(osu_user.id), |f| f.mode(mode))
.await?
.ok_or(Error::msg("user not found"))?;
@ -117,7 +127,8 @@ async fn handle_user_mode(
let oppai = data.get::<BeatmapCache>().unwrap();
let beatmap = cache.get_beatmap_default(score.beatmap_id).await?;
let content = oppai.get_beatmap(beatmap.beatmap_id).await?;
let r: Result<_> = Ok((rank, score, BeatmapWithMode(beatmap, mode), content));
let r: Result<_> =
Ok((rank, score, BeatmapWithMode(beatmap, mode), content));
r
}
})
@ -158,8 +169,9 @@ async fn handle_user_mode(
Ok(pp)
}
async fn scan_user(osu: &Osu, u: &OsuUser, mode: Mode) -> Result<Vec<(u8, Score)>, Error> {
let scores = osu
async fn scan_user(&self, u: &OsuUser, mode: Mode) -> Result<Vec<(u8, Score)>, Error> {
let scores = self
.client
.user_best(UserID::ID(u.id), |f| f.mode(mode).limit(25))
.await?;
let scores = scores
@ -170,3 +182,4 @@ async fn scan_user(osu: &Osu, u: &OsuUser, mode: Mode) -> Result<Vec<(u8, Score)
.collect();
Ok(scores)
}
}

View file

@ -66,9 +66,12 @@ pub fn setup(
// API client
let http_client = data.get::<HTTPClient>().unwrap().clone();
let osu_client = Arc::new(OsuHttpClient::new(
let make_client = || {
OsuHttpClient::new(
std::env::var("OSU_API_KEY").expect("Please set OSU_API_KEY as osu! api key."),
));
)
};
let osu_client = Arc::new(make_client());
data.insert::<OsuClient>(osu_client.clone());
data.insert::<oppai_cache::BeatmapCache>(oppai_cache::BeatmapCache::new(http_client));
data.insert::<beatmap_cache::BeatmapMetaCache>(beatmap_cache::BeatmapMetaCache::new(
@ -76,7 +79,10 @@ pub fn setup(
));
// Announcer
announcers.add(announcer::ANNOUNCER_KEY, announcer::Announcer);
announcers.add(
announcer::ANNOUNCER_KEY,
announcer::Announcer::new(make_client()),
);
Ok(())
}

View file

@ -13,7 +13,7 @@ use std::convert::TryInto;
use youmubot_prelude::{ratelimit::Ratelimit, *};
/// The number of requests per minute to the osu! server.
const REQUESTS_PER_MINUTE: usize = 200;
const REQUESTS_PER_MINUTE: usize = 60;
/// Client is the client that will perform calls to the osu! api server.
pub struct Client {