Skip to content

Commit

Permalink
chore: rename teams to leaderboard in command and some places through…
Browse files Browse the repository at this point in the history
…out the codebase
  • Loading branch information
washbin committed Oct 12, 2022
1 parent 0aa3c35 commit 3dd3a38
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 27 deletions.
16 changes: 12 additions & 4 deletions REQUIREMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@
# TODO

- [x] Teams
### HackSquad
- [x] Individual Team
- [x] Score
- [x] Total PRs
- [x] Total PRs Deleted
- [ ] Users
- [ ] Search
- [ ] Info (no email)
- [x] Leaderboard
- [x] Total Score
- [x] All PRs
- [x] Deleted PRs

### Novu
- [x] Contibutor / Hero
- [x] Info
- [x] Search
- [x] Random Contibutor
6 changes: 3 additions & 3 deletions src/api/team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use crate::{CONFIG, DATABASE};

#[derive(Deserialize, Debug, Serialize)]
pub struct TeamsResponse {
pub struct LeaderboardResponse {
pub teams: Vec<Team>,
}

Expand Down Expand Up @@ -34,10 +34,10 @@ pub struct PR {
pub url: String,
}

pub async fn get_teams() -> Vec<Team> {
pub async fn get_leaderboard() -> Vec<Team> {
let db = DATABASE.lock().await;

db.request::<TeamsResponse>(
db.request::<LeaderboardResponse>(
"https://www.hacksquad.dev/api/leaderboard",
"leaderboard",
CONFIG.lock().await.cache_leaderboard_ttl,
Expand Down
10 changes: 5 additions & 5 deletions src/commands/teams.rs → src/commands/leaderboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ use serenity::model::prelude::interaction::Interaction;
use serenity::prelude::Context;
use serenity::utils::Color;

use crate::api::team::get_teams;
use crate::api::team::get_leaderboard;
use crate::pagination::Pagination;
use crate::PAGINATION;

pub async fn run(ctx: Context, command: ApplicationCommandInteraction) {
let teams = get_teams().await;
let leaderboard = get_leaderboard().await;

let mut pages = Vec::new();
for team_list in teams.chunks(8) {
for team_list in leaderboard.chunks(8) {
let mut description = String::new();
for team in team_list {
description += &format!(
"**[{}](https://hacksquad.dev/team/{})**\n<:reply_multi:1029067132572549142>`🥇`Rank: `{}`\n<:reply:1029065416905076808>Points: `{}`\n",
team.name.clone(),
team.slug,
teams.iter().position(|r| r.slug == team.slug).unwrap() + 1,
leaderboard.iter().position(|r| r.slug == team.slug).unwrap() + 1,
team.score
)
}
Expand Down Expand Up @@ -93,5 +93,5 @@ pub async fn handle_interaction(
}

pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command.name("teams").description("Leaderboard")
command.name("leaderboard").description("Get the HackSquad Leaderboard")
}
2 changes: 1 addition & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod hero;
pub mod team;
pub mod teams;
pub mod leaderboard;
8 changes: 4 additions & 4 deletions src/commands/team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serenity::model::prelude::ReactionType;
use serenity::prelude::Context;
use serenity::utils::Colour;

use crate::api::team::{get_team, get_teams, PR};
use crate::api::team::{get_team, get_leaderboard, PR};
use crate::fuzzy::search_teams;

fn link_button(name: &str, link: String, emoji: ReactionType) -> CreateButton {
Expand All @@ -34,7 +34,7 @@ pub async fn run(ctx: Context, command: ApplicationCommandInteraction) {

if let CommandDataOptionValue::String(team_id) = option {
let team = get_team(team_id).await;
let teams = get_teams().await;
let leaderboard = get_leaderboard().await;

let mut pull_req = String::new();
let mut user_list = String::new();
Expand Down Expand Up @@ -94,7 +94,7 @@ pub async fn run(ctx: Context, command: ApplicationCommandInteraction) {
let data = format!(
"`ℹ️` **Information**\n<:reply_multi:1029067132572549142>**Name:** {}\n<:reply_multi:1029067132572549142>**Rank:**`{}`\n<:reply_multi:1029067132572549142>**Score:** `{}`\n<:reply_multi:1029067132572549142>**Total PRs:** `{}`\n<:reply:1029065416905076808>**Total PRs Deleted:** `{}`\n\n`🏆` **Team Members**\n{}\n`🔗` **Last 3 PRs**\n{}",
team.name,
teams.iter().position(|r| r.slug == team.slug).unwrap() + 1,
leaderboard.iter().position(|r| r.slug == team.slug).unwrap() + 1,
team.score,
team.score + deleted,
deleted,
Expand Down Expand Up @@ -128,7 +128,7 @@ pub async fn run(ctx: Context, command: ApplicationCommandInteraction) {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|message| {
message.content("Please provide a valid teams")
message.content("Please provide a valid team")
})
})
.await
Expand Down
10 changes: 5 additions & 5 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ impl EventHandler for Handler {
match interaction {
Interaction::ApplicationCommand(ref command) => match command.data.name.as_str() {
"team" => commands::team::run(ctx, command.to_owned()).await,
"teams" => commands::teams::run(ctx, command.to_owned()).await,
"leaderboard" => commands::leaderboard::run(ctx, command.to_owned()).await,
"hero" => commands::hero::hero(ctx, command.to_owned()).await,
"randomhero" => commands::hero::random_hero(ctx, command.to_owned()).await,
other_commands => println!("Unknown command {}", other_commands),
},
Interaction::MessageComponent(ref component) => match component.message.interaction {
Some(ref message_interaction) => match message_interaction.name.as_str() {
"teams" => {
commands::teams::handle_interaction(
"leaderboard" => {
commands::leaderboard::handle_interaction(
ctx,
component.to_owned(),
interaction.to_owned(),
)
.await
}
_ => println!("We only handle component interaction in teams command"),
_ => println!("We only handle component interaction in leaderboard command"),
},
None => println!("No interaction"),
},
Expand All @@ -57,7 +57,7 @@ impl EventHandler for Handler {

let commands = GuildId::set_application_commands(&guild_id, &ctx.http, |commands| {
commands.create_application_command(|command| commands::team::register(command));
commands.create_application_command(|command| commands::teams::register(command));
commands.create_application_command(|command| commands::leaderboard::register(command));
commands.create_application_command(|command| {
commands::hero::register_random_hero(command)
});
Expand Down
10 changes: 5 additions & 5 deletions src/fuzzy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::Serialize;
use serde_json::{json, Value};
use simsearch::{SearchOptions, SimSearch};

use crate::api::team::{get_teams, Team};
use crate::api::team::{get_leaderboard, Team};

#[derive(Serialize, Debug)]
struct Suggestion {
Expand All @@ -12,12 +12,12 @@ struct Suggestion {

pub async fn search_teams(query: Option<Value>) -> Value {
if let Some(query) = query {
let teams = get_teams().await;
let leaderboard = get_leaderboard().await;

let mut engine =
SimSearch::new_with(SearchOptions::new().case_sensitive(false).threshold(0.82));

for team in &teams {
for team in &leaderboard {
engine.insert(team.slug.clone(), &team.name);
}

Expand All @@ -26,7 +26,7 @@ pub async fn search_teams(query: Option<Value>) -> Value {
let mut res = engine.search(&query);

if res.is_empty() {
for team in &teams {
for team in &leaderboard {
res.push(team.slug.clone())
}

Expand All @@ -42,7 +42,7 @@ pub async fn search_teams(query: Option<Value>) -> Value {
let mut suggestions: Vec<Team> = Vec::new();

for (_index, slug) in iter.enumerate() {
let team = teams.iter().find(|&p| p.slug == slug.clone()).cloned();
let team = leaderboard.iter().find(|&p| p.slug == slug.clone()).cloned();

if let Some(team) = team {
suggestions.push(team);
Expand Down

0 comments on commit 3dd3a38

Please sign in to comment.