display should take a lazy score future

This commit is contained in:
Natsu Kagami 2025-05-12 00:27:18 +02:00
parent 11e34e0c78
commit fcf3ed60d2
Signed by: nki
GPG key ID: 55A032EB38B49ADB
5 changed files with 30 additions and 18 deletions

View file

@ -307,7 +307,7 @@ async fn handle_listing<U: HasOsuEnv>(
let reply = ctx.clone().reply(&header).await?;
style
.display_scores(
plays.try_collect::<Vec<_>>().await?,
plays.try_collect::<Vec<_>>(),
ctx.clone().serenity_context(),
ctx.guild_id(),
(reply, ctx).with_header(header),
@ -489,7 +489,7 @@ async fn check<U: HasOsuEnv>(
style
.display_scores(
scores,
future::ok(scores),
ctx.clone().serenity_context(),
ctx.guild_id(),
(msg, ctx).with_header(header),
@ -612,7 +612,7 @@ async fn leaderboard<U: HasOsuEnv>(
let reply = ctx.reply(header).await?;
style
.display_scores(
scores.into_iter().map(|s| s.score).collect(),
future::ok(scores.into_iter().map(|s| s.score).collect()),
ctx.serenity_context(),
Some(guild.id),
(reply, ctx),

View file

@ -76,6 +76,7 @@ mod scores {
) -> Result<()> {
let env = ctx.data.read().await.get::<OsuEnv>().unwrap().clone();
let channel_id = on.get_message().await?.channel_id;
let scores = scores.await?;
if scores.is_empty() {
on.apply_edit(CreateReply::default().content("No plays found"))
.await?;
@ -85,7 +86,7 @@ mod scores {
paginate_with_first_message(
Paginate {
env,
scores: scores.await?,
scores,
guild_id,
channel_id,
},
@ -157,6 +158,7 @@ mod scores {
pub mod table {
use std::borrow::Cow;
use std::future::Future;
use pagination::paginate_with_first_message;
use serenity::all::{CreateActionRow, CreateAttachment};
@ -170,27 +172,26 @@ mod scores {
use crate::models::Score;
pub async fn display_scores_as_file(
scores: Vec<Score>,
scores: impl Future<Output = Result<Vec<Score>>>,
ctx: &Context,
mut on: impl CanEdit,
) -> Result<()> {
let header = on.headers().unwrap_or("").to_owned();
let content = format!("{}\n\nPreparing file...", header);
let preparing = on.apply_edit(CreateReply::default().content(content));
let (_, scores) = future::try_join(preparing, scores).await?;
if scores.is_empty() {
on.apply_edit(CreateReply::default().content("No plays found"))
.await?;
return Ok(());
}
let header = on.headers().unwrap_or("").to_owned();
let content = format!("{}\n\nPreparing file...", header);
let first_edit = on
.apply_edit(CreateReply::default().content(content))
.map(|v| v.pls_ok());
let p = Paginate {
env: ctx.data.read().await.get::<OsuEnv>().unwrap().clone(),
header: header.clone(),
scores,
};
let (_, content) = future::join(first_edit, p.to_table(0, p.scores.len())).await;
let content = p.to_table(0, p.scores.len()).await;
on.apply_edit(
CreateReply::default()
.content(header)
@ -201,10 +202,11 @@ mod scores {
}
pub async fn display_scores_table(
scores: Vec<Score>,
scores: impl Future<Output = Result<Vec<Score>>>,
ctx: &Context,
mut on: impl CanEdit,
) -> Result<()> {
let scores = scores.await?;
if scores.is_empty() {
on.apply_edit(CreateReply::default().content("No plays found"))
.await?;

View file

@ -109,7 +109,12 @@ pub fn handle_check_button<'a>(
let guild_id = comp.guild_id;
ScoreListStyle::Grid
.display_scores(scores, &ctx, guild_id, (comp, ctx).with_header(header))
.display_scores(
future::ok(scores),
&ctx,
guild_id,
(comp, ctx).with_header(header),
)
.await
.pls_ok();
Ok(())

View file

@ -685,7 +685,7 @@ pub async fn recent(ctx: &Context, msg: &Message, mut args: Args) -> CommandResu
let reply = msg.reply(ctx, &header).await?;
style
.display_scores(
plays.try_collect::<Vec<_>>().await?,
plays.try_collect::<Vec<_>>(),
ctx,
reply.guild_id,
(reply, ctx).with_header(header),
@ -758,7 +758,7 @@ pub async fn pins(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
let reply = msg.reply(ctx, &header).await?;
style
.display_scores(
plays.try_collect::<Vec<_>>().await?,
plays.try_collect::<Vec<_>>(),
ctx,
reply.guild_id,
(reply, ctx).with_header(header),
@ -1016,7 +1016,12 @@ pub async fn check(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
);
let reply = msg.reply(&ctx, &header).await?;
style
.display_scores(scores, ctx, msg.guild_id, (reply, ctx).with_header(header))
.display_scores(
future::ok(scores),
ctx,
msg.guild_id,
(reply, ctx).with_header(header),
)
.await?;
Ok(())
@ -1122,7 +1127,7 @@ pub async fn top(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
let reply = msg.reply(&ctx, &header).await?;
style
.display_scores(
plays.try_collect::<Vec<_>>().await?,
plays.try_collect::<Vec<_>>(),
ctx,
msg.guild_id,
(reply, ctx).with_header(header),

View file

@ -438,7 +438,7 @@ pub async fn show_leaderboard(ctx: &Context, msg: &Message, mut args: Args) -> C
let reply = msg.reply(&ctx, header).await?;
style
.display_scores(
scores.into_iter().map(|s| s.score).collect(),
future::ok(scores.into_iter().map(|s| s.score).collect()),
ctx,
Some(guild),
(reply, ctx),