Skip to content

Commit

Permalink
Merge pull request #5 from WISVCH/feature/leaderboard-total-rank
Browse files Browse the repository at this point in the history
Add rank to leaderboard_total and change rendering of stars
  • Loading branch information
julian9499 authored Dec 20, 2024
2 parents 9182711 + be911b6 commit 486b9d2
Showing 1 changed file with 16 additions and 17 deletions.
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

0 comments on commit 486b9d2

Please sign in to comment.