Skip to content

Commit

Permalink
let's test
Browse files Browse the repository at this point in the history
  • Loading branch information
washbin committed Oct 15, 2022
1 parent f57e3c8 commit 12873d9
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 11 deletions.
24 changes: 15 additions & 9 deletions src/api/hero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,22 @@ pub struct HeroResponse {
pub list: Vec<Hero>,
}

pub async fn get_all_heros(database: &Database) -> Option<Vec<Hero>> {
Some(
database
.request::<HeroResponse>(
"https://contributors.novu.co/contributors",
"heros",
database.config.cache_heros_ttl,
)
.await
.ok()?
.list,
)
}

pub async fn get_random_hero(database: &Database) -> Option<Hero> {
let hero_list = database
.request::<HeroResponse>(
"https://contributors.novu.co/contributors",
"heros",
database.config.cache_heros_ttl,
)
.await
.ok()?
.list;
let hero_list = get_all_heros(database).await?;

Some(
hero_list
Expand Down
22 changes: 21 additions & 1 deletion src/commands/hero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ use serenity::model::prelude::component::ButtonStyle;
use serenity::model::prelude::interaction::application_command::{
ApplicationCommandInteraction, CommandDataOptionValue,
};
use serenity::model::prelude::interaction::InteractionResponseType;
use serenity::model::prelude::interaction::autocomplete::AutocompleteInteraction;
use serenity::model::prelude::interaction::{Interaction, InteractionResponseType};
use serenity::model::prelude::ReactionType;
use serenity::model::Timestamp;
use serenity::prelude::Context;
use serenity::utils::Colour;

use crate::api::hero::{get_hero, get_random_hero, Hero, Pulls};
use crate::database::Database;
use crate::fuzzy;
use crate::utils::embeds::error_embed;

fn link_button(name: &str, link: String, emoji: ReactionType) -> CreateButton {
Expand Down Expand Up @@ -171,6 +173,23 @@ pub async fn hero(ctx: Context, command: ApplicationCommandInteraction) {
}
}

pub async fn handle_autocomplete(
ctx: Context,
command: &AutocompleteInteraction,
_interaction: Interaction,
) {
let ctx_cloned = ctx.clone();
let data = ctx_cloned.data.read().await;
let database = data.get::<Database>().unwrap();

let search = fuzzy::search_hero(database, command.data.options[0].value.clone()).await;

command
.create_autocomplete_response(ctx.http, |response| response.set_choices(search))
.await
.unwrap();
}

pub async fn random_hero(ctx: Context, command: ApplicationCommandInteraction) {
let ctx_cloned = ctx.clone();
let data = ctx_cloned.data.read().await;
Expand All @@ -190,6 +209,7 @@ pub fn register_hero(command: &mut CreateApplicationCommand) -> &mut CreateAppli
.name("github_username")
.description("The github username of the contributor to look for")
.kind(CommandOptionType::String)
.set_autocomplete(true)
.required(true)
})
}
Expand Down
3 changes: 3 additions & 0 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ impl EventHandler for Handler {
"team" => {
commands::team::handle_autocomplete(ctx, command, interaction.to_owned()).await
}
"hero" => {
commands::hero::handle_autocomplete(ctx, command, interaction.to_owned()).await
}
other_commands => println!("No autocompletions for {}", other_commands),
},
other_interactions => println!("Unhandled interaction {:?}", other_interactions),
Expand Down
68 changes: 67 additions & 1 deletion src/fuzzy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use serde_json::{json, Value};
use simsearch::{SearchOptions, SimSearch};

use crate::{
api::team::{get_leaderboard, Team},
api::{
hero::{get_all_heros, Hero},
team::{get_leaderboard, Team},
},
database::Database,
};

Expand Down Expand Up @@ -75,3 +78,66 @@ pub async fn search_teams(database: &Database, query: Option<Value>) -> Value {
json!({})
}
}

pub async fn search_hero(database: &Database, query: Option<Value>) -> Value {
if let Some(query) = query {
let hero_list = match get_all_heros(database).await {
Some(hero_list) => hero_list,
None => {
return json!({
"error": "Failed to get hero list"
})
}
};

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

for hero in &hero_list {
engine.insert(hero.github.clone(), &hero.github);
}

let query: String = serde_json::from_value(query.clone()).unwrap();

let mut res = engine.search(&query);

if res.is_empty() {
for hero in &hero_list {
res.push(hero.github.clone())
}

if query.is_empty() {}

res.retain(|x| x.starts_with(&query));
}

res.truncate(10);

let iter = res.iter_mut();

let mut suggestions: Vec<Hero> = Vec::new();

for (_index, slug) in iter.enumerate() {
let hero = hero_list
.iter()
.find(|&p| p.github == slug.clone())
.cloned();

if let Some(hero) = hero {
suggestions.push(hero);
}
}

let suggestions: Vec<Suggestion> = suggestions
.iter()
.map(|x| Suggestion {
value: x.github.clone(),
name: x.github.clone(),
})
.collect();

json!(suggestions)
} else {
json!({})
}
}

0 comments on commit 12873d9

Please sign in to comment.