Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rank to leaderboard_total and change rendering of stars #5

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use demoji_rs::remove_emoji;
use poise::serenity_prelude as serenity;
use serde::Deserialize;
use std::cmp;
use std::fmt::Write;

struct Data {} // User data, which is stored and accessible in all command invocations
type Error = Box<dyn std::error::Error + Send + Sync>;
Expand All @@ -22,6 +23,7 @@ pub struct TotalEntry {
pub name: Option<String>,
pub score: i32,
pub stars: [i32; 25],
pub rank: i32,
}

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -63,19 +65,14 @@ async fn leaderboard_total(ctx: Context<'_>) -> Result<(), Error> {
})
.max()
.unwrap();
let max_stars: usize = aoch_data
.total
.iter()
.map(|x| x.stars.clone().iter().sum::<i32>())
.max()
.unwrap() as usize;
let row_size: usize = start_width + 11 + max_stars;

let row_size: usize = start_width + 18 + 50;
let row_count = 1900 / row_size;

let mut table_rows = vec![];
table_rows.push(format!(
"{1:0$} | {2:5} | {3:5}",
start_width, "Name", "Score", "Stars"
"{1:4} | {2:0$} | {3:5} | {4:5}",
start_width, "Rank", "Name", "Score", "01020304050607080910111213141516171819202122232425"
));
table_rows.push(format!("{:-^row_size$}", ""));

Expand All @@ -86,13 +83,15 @@ async fn leaderboard_total(ctx: Context<'_>) -> Result<(), Error> {
.map(|x| {
let name: String = remove_emoji(&x.name.unwrap_or_else(|| ANON_USER.to_string()));

let star_count = x.stars.iter().sum();
let mut stars: String = String::new();
for _ in 0..star_count {
stars += "*";
}
let stars = x.stars.iter().fold(String::new(), |mut acc, &s| {
let _ = write!(acc, "{:<2}", "*".repeat(s as usize));
acc
});

format!("{name:0$} | {1:5} | {stars} ", start_width, x.score)
format!(
"{2:>3}) | {name:0$} | {1:5} | {stars} ",
start_width, x.score, x.rank
)
})
.collect::<Vec<String>>()
.as_mut(),
Expand Down Expand Up @@ -126,7 +125,7 @@ async fn leaderboard_today(ctx: Context<'_>) -> Result<(), Error> {

let mut table_rows = vec![];
table_rows.push(format!(
"{4:5} | {1:0$} | {2:5} | {3:5}",
"{4:4} | {1:0$} | {2:5} | {3:5}",
start_width, "Name", "Stars", "Score", "Rank"
));
table_rows.push(format!("{:-^row_size$}", ""));
Expand All @@ -147,7 +146,7 @@ async fn leaderboard_today(ctx: Context<'_>) -> Result<(), Error> {
}

format!(
"{2:>4}) | {name:0$} | {stars:<5} | {1:>5}",
"{2:>3}) | {name:0$} | {stars:<5} | {1:>5}",
start_width, x.score, x.rank
)
})
Expand Down
Loading