Skip to content

Commit

Permalink
renames and bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
B-2U committed Aug 14, 2024
1 parent b943abf commit 6c9d6ee
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 56 deletions.
8 changes: 4 additions & 4 deletions src/cmds/clan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tokio::join;

use crate::{
dc_utils::{
auto_complete::{self},
autocomplete::{self},
Args, ContextAddon, EasyEmbed,
},
structs::{ClanMember, ClanStatsSeason, PartialClan, StatisticValueType},
Expand All @@ -42,14 +42,14 @@ pub fn clan_hybrid() -> poise::Command<Data, Error> {
pub async fn clan(
ctx: Context<'_>,
#[description = "clan's tag or name, default: yourself"]
#[autocomplete = "auto_complete::clan"]
#[autocomplete = "autocomplete::clan"]
clan: Option<String>,
#[description = "specify season of Clan Battle, -1 for the latest season"] season: Option<i32>,
) -> Result<(), Error> {
let api = WowsApi::new(&ctx);
let partial_clan = if let Some(clan_input) = clan {
let auto_complete_clan = parse::parse_region_clan(&clan_input)?;
cache_methods::clan(&api, auto_complete_clan).await?
let autocomplete_clan = parse::parse_region_clan(&clan_input)?;
cache_methods::clan(&api, autocomplete_clan).await?
} else {
let author = ctx
.data()
Expand Down
12 changes: 6 additions & 6 deletions src/cmds/recent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use poise::{
};

use crate::{
dc_utils::{auto_complete, Args, ContextAddon, UserAddon},
dc_utils::{autocomplete, Args, ContextAddon, UserAddon},
structs::{
AutoCompletePlayer, Mode, PartialPlayer, Player, PlayerSnapshots, PlayerSnapshotsType,
Ship, ShipId, ShipModeStatsPair, ShipStatsCollection,
Expand Down Expand Up @@ -40,20 +40,20 @@ pub async fn recent(
ctx: Context<'_>,
#[description = "last 1~30 (90 for patreons) days of stats, default: 1"] days: Option<u64>,
#[description = "player's ign, default: yourself"]
#[autocomplete = "auto_complete::player"]
#[autocomplete = "autocomplete::player"]
player: Option<AutoCompletePlayer>, // the String is a Serialized PartialPlayer struct
#[description = "@ping / discord user's ID, default: yourself"]
#[rename = "user"]
discord_user: Option<String>,
#[description = "specific warship, default: all ships' recent"]
#[rename = "warship"]
#[autocomplete = "auto_complete::ship"]
#[autocomplete = "autocomplete::ship"]
ship_name: Option<String>,
#[description = "battle type, default: pvp"] battle_type: Option<Mode>,
) -> Result<(), Error> {
let partial_player = if let Some(auto_complete_player) = player {
auto_complete_player.save_user_search_history(&ctx).await;
auto_complete_player
let partial_player = if let Some(autocomplete_player) = player {
autocomplete_player.save_user_search_history(&ctx).await;
autocomplete_player
.fetch_partial_player(&WowsApi::new(&ctx))
.await?
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/server_top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use itertools::Itertools;
use poise::{serenity_prelude::CreateAttachment, CreateReply};

use crate::{
dc_utils::{auto_complete, Args, ContextAddon, UserAddon},
dc_utils::{autocomplete, Args, ContextAddon, UserAddon},
structs::{PlayerSnapshots, Ship},
template_data::{Render, ServerTopPlayer, ServerTopTemplate},
utils::{wws_api::WowsApi, IsacError, IsacInfo},
Expand Down Expand Up @@ -31,7 +31,7 @@ pub async fn server_top(
ctx: Context<'_>,
#[description = "warship's name"]
#[rename = "warship"]
#[autocomplete = "auto_complete::ship"]
#[autocomplete = "autocomplete::ship"]
ship_name: String,
) -> Result<(), Error> {
let ship = ctx.data().ships.read().search_name(&ship_name, 1)?.first();
Expand Down
18 changes: 6 additions & 12 deletions src/cmds/setting.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{
dc_utils::{auto_complete, UserAddon},
structs::Region,
utils::{
cache_methods, parse::parse_region_ign, wws_api::WowsApi, IsacError, IsacInfo,
LoadSaveFromJson,
},
dc_utils::{autocomplete, UserAddon},
structs::{AutoCompletePlayer, Region},
utils::{wws_api::WowsApi, IsacError, IsacInfo, LoadSaveFromJson},
Context, Data, Error,
};
use poise;
Expand All @@ -21,15 +18,12 @@ pub fn link_hybrid() -> poise::Command<Data, Error> {
#[poise::command(slash_command)]
pub async fn link(
ctx: Context<'_>,
#[autocomplete = "auto_complete::player"]
#[autocomplete = "autocomplete::player"]
#[description = "your game server & ign"]
player: String, // the String is a Serialized PartialPlayer struct
player: AutoCompletePlayer, // the String is a Serialized PartialPlayer struct
) -> Result<(), Error> {
let api = WowsApi::new(&ctx);
let partial_player = {
let (region, ign) = parse_region_ign(&player)?;
cache_methods::player(&api, &region, &ign).await?
};
let partial_player = { player.fetch_partial_player(&api).await? };
let player = partial_player.full_player(&api).await?;
{
let mut guard = ctx.data().link.write().await;
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use regex::Regex;
use scraper::{node::Element, ElementRef, Html, Selector};

use crate::{
dc_utils::{auto_complete, Args, ContextAddon, UserAddon},
dc_utils::{autocomplete, Args, ContextAddon, UserAddon},
structs::{
color::ColorStats, Region, Ship, ShipLeaderboardPlayer, ShipLeaderboardShip, StatisticValue,
},
Expand Down Expand Up @@ -40,7 +40,7 @@ pub async fn top(
ctx: Context<'_>,
#[description = "warship's name"]
#[rename = "warship"]
#[autocomplete = "auto_complete::ship"]
#[autocomplete = "autocomplete::ship"]
ship_name: String,
#[description = "specific region, default: depend on server's default"] region: Option<Region>,
) -> Result<(), Error> {
Expand Down
12 changes: 6 additions & 6 deletions src/cmds/wws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use poise::{
};

use crate::{
dc_utils::{auto_complete, Args, ContextAddon, UserAddon},
dc_utils::{autocomplete, Args, ContextAddon, UserAddon},
structs::{
AutoCompletePlayer, Mode, PartialPlayer, Ship, ShipClass, ShipTier, Statistic,
StatisticValueType,
Expand All @@ -38,19 +38,19 @@ pub async fn wws(
ctx: Context<'_>,
#[description = "specific warship, default: account's overall stats"]
#[rename = "warship"]
#[autocomplete = "auto_complete::ship"]
#[autocomplete = "autocomplete::ship"]
ship_name: Option<String>,
#[description = "player's ign, default: yourself"]
#[autocomplete = "auto_complete::player"]
#[autocomplete = "autocomplete::player"]
player: Option<AutoCompletePlayer>,
#[description = "@ping / discord user's ID, default: yourself"]
#[rename = "user"]
discord_user: Option<String>,
#[description = "battle type, default: pvp"] battle_type: Option<Mode>,
) -> Result<(), Error> {
let partial_player = if let Some(auto_complete_player) = player {
auto_complete_player.save_user_search_history(&ctx).await;
auto_complete_player
let partial_player = if let Some(autocomplete_player) = player {
autocomplete_player.save_user_search_history(&ctx).await;
autocomplete_player
.fetch_partial_player(&WowsApi::new(&ctx))
.await?
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/dc_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pub use args::Args;
mod user;
pub use user::UserAddon;

pub mod auto_complete;
pub mod autocomplete;
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub async fn player(ctx: Context<'_>, input: &str) -> Vec<AutocompleteChoice> {
}) else {
return match ctx.data().cache.lock().await.get(&ctx.author().id).await {
Some(cache) => cache
.auto_complete_player
.autocomplete_player
.iter()
.map(|p| AutocompleteChoice::new(p.clone(), p.clone()))
.collect(),
Expand All @@ -49,8 +49,8 @@ pub async fn player(ctx: Context<'_>, input: &str) -> Vec<AutocompleteChoice> {
candidates
.into_iter()
.map(|vortex_p| {
let auto_complete_p = vortex_p.to_auto_complete_player(region);
AutocompleteChoice::new(auto_complete_p.clone(), auto_complete_p.clone())
let autocomplete_p = vortex_p.to_autocomplete_player(region);
AutocompleteChoice::new(autocomplete_p.clone(), autocomplete_p.clone())
})
.collect()
}
Expand Down
2 changes: 1 addition & 1 deletion src/structs/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl Default for BannerData {
}
}

// a intermediate struct for receiving formatted String from `auto_complete::player()`
// a intermediate struct for receiving formatted String from `autocomplete::player()`
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct AutoCompletePlayer {
pub region: Region,
Expand Down
8 changes: 4 additions & 4 deletions src/structs/user_search_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tokio::io::AsyncWriteExt;

use crate::structs::{lru_vector::LruVector, AutoCompletePlayer};

/// users searching history in auto_complete::player()
/// users searching history in autocomplete::player()
/// user's data is saved when getting evicted or bot shutting down
#[derive(Debug)]
pub struct SearchCache {
Expand Down Expand Up @@ -91,15 +91,15 @@ impl SearchCache {
#[derive(Debug, Serialize, Deserialize)]
pub struct UserSearchCache {
pub user_id: UserId,
pub auto_complete_player: LruVector<AutoCompletePlayer>,
pub autocomplete_player: LruVector<AutoCompletePlayer>,
}

impl UserSearchCache {
pub fn new(user_id: UserId) -> Self {
let auto_complete_player = LruVector::new(15);
let autocomplete_player = LruVector::new(15);
UserSearchCache {
user_id,
auto_complete_player,
autocomplete_player,
}
}
/// load the player's recent data, return None if he is not inside
Expand Down
27 changes: 14 additions & 13 deletions src/utils/cache_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ use once_cell::sync::Lazy;
use parking_lot::Mutex;

use crate::{
dc_utils::auto_complete::AutoCompleteClan,
structs::{user_search_history::UserSearchCache, PartialClan, PartialPlayer, Region},
dc_utils::autocomplete::AutoCompleteClan,
structs::{
user_search_history::UserSearchCache, AutoCompletePlayer, PartialClan, PartialPlayer,
Region,
},
utils::{wws_api::WowsApi, IsacError, IsacInfo},
Context,
};
Expand Down Expand Up @@ -53,41 +56,39 @@ pub async fn player(
/// [`IsacInfo::ClanNotFound`]
pub async fn clan(
api: &WowsApi<'_>,
auto_complete_clan: AutoCompleteClan,
autocomplete_clan: AutoCompleteClan,
) -> Result<PartialClan, IsacError> {
static CACHE: Lazy<Mutex<LruCache<AutoCompleteClan, PartialClan>>> =
Lazy::new(|| Mutex::new(LruCache::new(NonZeroUsize::new(30).unwrap())));

let cache_result = {
let mut lock = CACHE.lock();
lock.get(&auto_complete_clan).cloned()
lock.get(&autocomplete_clan).cloned()
};
if let Some(cached_clan) = cache_result {
Ok(cached_clan)
} else {
let mut candidates = api
.clans(&auto_complete_clan.region, &auto_complete_clan.tag)
.clans(&autocomplete_clan.region, &autocomplete_clan.tag)
.await?;
let first_candidate = match candidates.is_empty() {
true => Err(IsacInfo::ClanNotFound {
clan: auto_complete_clan.tag.to_string(),
region: auto_complete_clan.region,
clan: autocomplete_clan.tag.to_string(),
region: autocomplete_clan.region,
})?,
false => candidates.swap_remove(0),
};
CACHE
.lock()
.put(auto_complete_clan, first_candidate.clone());
CACHE.lock().put(autocomplete_clan, first_candidate.clone());

Ok(first_candidate)
}
}

/// save the result into ctx::cache::auto_complete_player
/// save the result into ctx::cache::autocomplete_player
pub async fn save_user_search_history(ctx: &Context<'_>, region: Region, ign: String) {
let mut mg = ctx.data().cache.lock().await;
mg.get_or_insert_mut(&ctx.author().id, || UserSearchCache::new(ctx.author().id))
.await
.auto_complete_player
.put((region, ign));
.autocomplete_player
.put(AutoCompletePlayer { region, ign });
}
2 changes: 1 addition & 1 deletion src/utils/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use once_cell::sync::Lazy;
use regex::Regex;

use crate::{
dc_utils::auto_complete::AutoCompleteClan,
dc_utils::autocomplete::AutoCompleteClan,
structs::{ClanTag, Region},
utils::{IsacError, IsacInfo},
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/wws_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ impl Display for VortexPlayerSearch {

impl VortexPlayerSearch {
/// for turning it into [`AutocompleteChoice`]
pub fn to_auto_complete_player(self, region: Region) -> AutoCompletePlayer {
pub fn to_autocomplete_player(self, region: Region) -> AutoCompletePlayer {
AutoCompletePlayer {
region,
ign: self.name,
Expand Down

0 comments on commit 6c9d6ee

Please sign in to comment.