Skip to content

Commit

Permalink
Added automatic config fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
TYTheBeast committed Oct 9, 2024
1 parent f7ef9fb commit bfcf160
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/args/item_search_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Display;
use clap::{Parser, ValueEnum};

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
#[command(author, version, about, long_about)]
pub struct ItemSearchArgs {
/// Apparel type
#[arg(short, long)]
Expand Down
16 changes: 16 additions & 0 deletions src/config/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,26 @@ pub struct ThresholdFifth {
}

pub async fn load_config(path: impl AsRef<Path>) -> Result<Config, String> {
// Check if the file exists
if !path.as_ref().exists() {
// Fetch the default config from https://raw.githubusercontent.com/TYTheBeast/WynnBuilderTools-Rekindled/refs/heads/master/config/config.toml
let url = "https://raw.githubusercontent.com/TYTheBeast/WynnBuilderTools-Rekindled/refs/heads/master/config/config.toml";
let request = reqwest::get(url)
.await
.unwrap()
.text()
.await
.unwrap();

// Write the default config to the file
tokio::fs::write(path.as_ref(), request).await.unwrap();
}

let mut f = match File::open(path).await {
Ok(ok) => Ok(ok),
Err(err) => Err(err.to_string()),
}?;

let mut buffer = Vec::new();

match f.read_to_end(&mut buffer).await {
Expand Down
24 changes: 16 additions & 8 deletions src/items/api_items/api_to_internal_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
use crate::items::Items;
use crate::Item;

use super::{ApiItems, StatOrInt};
use super::{ApiItems, StatOrInt, ApiItem};

fn string_to_i32_hash(s: &str) -> i32 {
let mut hasher = DefaultHasher::new();
Expand All @@ -28,8 +28,19 @@ impl From<ApiItems> for Items {
fn from(api_items: ApiItems) -> Self {
let mut items = Items { items: Vec::new() };

for (name, api_item) in api_items {
let ids = api_item.identifications.as_ref();
for (_, api_item) in api_items {
let item: Item = api_item.into();

items.items.push(item);
}

items
}
}

impl From<ApiItem> for Item {
fn from(api_item: ApiItem) -> Self {
let ids = api_item.identifications.as_ref();

let max_or_int = |option: Option<StatOrInt>| match option {
Some(v) => match v {
Expand Down Expand Up @@ -137,9 +148,6 @@ impl From<ApiItems> for Items {
xpb: ids.and_then(|ids| max_or_int(ids.xp_bonus)),
};

items.items.push(item);
}

items
item
}
}
}

0 comments on commit bfcf160

Please sign in to comment.