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

feat: Global leaderboard #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
88 changes: 88 additions & 0 deletions aoc-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,94 @@ impl AocClient {
Ok(())
}

fn get_global_leaderboard_html(&self) -> AocResult<String> {
debug!("🦌 Fetching global leaderboard for {}", self.year);

let url = format!("https://adventofcode.com/{}/leaderboard", self.year);
let response = http_client(&self.session_cookie, "text/html")?
.get(url)
.send()?;
if response.status() == StatusCode::NOT_FOUND {
// A 404 reponse means the leaderboard for
// the requested year is not yet available
return Err(AocError::InvalidEventYear(self.year));
}
let contents = response.error_for_status()?.text()?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to handle NOT FOUND which can only happen if the year is invalid (like it's done in get_calendar_html):

        if response.status() == StatusCode::NOT_FOUND {
            // A 404 reponse means the leaderboard for
            // the requested year is not yet available
            return Err(AocError::InvalidEventYear(self.year));
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! Added :)


let main = Regex::new(r"(?i)(?s)<main>(?P<main>.*)</main>")
.unwrap()
.captures(&contents)
.ok_or(AocError::AocResponseError)?
.name("main")
.unwrap()
.as_str()
.to_string();

Ok(main)
}

pub fn show_global_leaderboard(&self) -> AocResult<()> {
let leaderboard_html = self.get_global_leaderboard_html()?;
let leadboard_text = self.html2text(&leaderboard_html);
const MAX_RANK_LEN: usize = 3; //"100"

let empty_str = "Nothing to show on the leaderboard... yet.";
if leadboard_text.contains(empty_str) {
println!("{}", empty_str);
return Ok(());
}

let colored_leaderboard = leadboard_text
.replace("(AoC++)", &"(AoC++)".color(GOLD).to_string())
.replace(
"(Sponsor)",
&"(Sponsor)".color(Color::BrightBlue).to_string(),
);

for line in colored_leaderboard
.lines()
.skip_while(|line| !line.is_empty())
.skip(1)
.take_while(|line| !line.starts_with("You"))
{
println!("{}", line);
}

let rank_re = Regex::new(
r"\s*(?P<rank>[0-9]+)\)\s*(?P<score>[0-9]+)\s*(?P<name>.+)",
)
.unwrap();
let no_rank_re =
Regex::new(r"\s*(?P<score>[0-9]+)\s*(?P<name>.+)").unwrap();

for line in colored_leaderboard
.lines()
.skip_while(|line| !line.is_empty())
.skip(1)
.skip_while(|line| !line.contains(')'))
{
if line.is_empty() {
continue;
}
if let Some(rank_caps) = rank_re.captures(line) {
// The longest ranking is "100)", want every other ranking to
// line up it's ")" with the ) in "100)"
let rank_len = rank_caps
.name("rank")
.ok_or(AocError::AocResponseError)?
.as_str()
.len();
let padding = " ".repeat(MAX_RANK_LEN - rank_len);
println!("{}{}", padding, line);
} else if let Some(_no_rank_caps) = no_rank_re.captures(line) {
let padding = " ".repeat(MAX_RANK_LEN + 1); // + 1 for ")"
println!("{} {}", padding, line);
}
}

Ok(())
}

fn get_private_leaderboard(
&self,
leaderboard_id: LeaderboardId,
Expand Down
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ pub enum Command {
#[command(visible_alias = "d")]
Download,

/// Show Advent of Code global leaderboard
#[command(visible_alias = "g")]
GlobalLeaderboard,

/// Read puzzle statement (the default command)
#[command(visible_alias = "r")]
Read,
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ fn run(args: &Args, client: AocClient) -> AocResult<()> {
}
Ok(())
}
Some(Command::GlobalLeaderboard) => client.show_global_leaderboard(),
Some(Command::Submit { part, answer }) => {
client.submit_answer_and_show_outcome(part, answer)
}
Expand Down