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

modify shopping list printout to include ( N x stacks + remainder) when ingredient_count > 250 #1

Closed
solstice-gw2 opened this issue Mar 18, 2021 · 3 comments

Comments

@solstice-gw2
Copy link
Contributor

Hi!

I can program in several languages, but Rust isn't one of them. I'm trying to modify the shopping list printout so that instead of
output like this:

============
Shopping list for 1298 x Sheet of Fine Paper = 3.29.81g profit (7 / step)
============
1234 Cotton Scrap
681 Bolt of Cotton
3894 Seasoned Wood Log

I instead get output something like this:

============
Shopping list for 1298 x Sheet of Fine Paper = 3.29.81g profit (7 / step)
============
1234 (4 stacks + 234) Cotton Scrap
681 (2 stacks + 181) Bolt of Cotton
3894 (15 stacks + 144) Seasoned Wood Log

In other words, make it report the number of stacks + remainder in the case when the buy order is larger than the stack size. I don't care if it says "N stacks + ###" or "Nx250 + ###" or something else, just that it does the math for me and makes it clear how many full stacks plus partial amount is required.

I made a clumsy attempt to do this myself by adding the following to your area containing constants:

// GW2 uses a "stack size" of 250
const ITEM_STACK_SIZE: i32 = 250;

and then as a first attempt I changed the recipe-printing code to:

        println!("============");
        for (ingredient_id, ingredient_count) in &purchased_ingredients {
            if ingredient_count > ITEM_STACK_SIZE {
                let num_stacks = ingredient_count / ITEM_STACK_SIZE;
                let remainder  = ingredient_count % ITEM_STACK_SIZE;
                println!(
                    "{} ({}x{} +{}) {}",
                    ingredient_count.ceil().to_integer(),
                    num_stacks,
                    ITEM_STACK_SIZE,
                    remainder,
                    items_map
                        .get(ingredient_id)
                        .map(|item| item.name.as_ref())
                        .unwrap_or("???")
                );
            } else {
                println!(
                    "{} {}",
                    ingredient_count.ceil().to_integer(),
                    items_map
                        .get(ingredient_id)
                        .map(|item| item.name.as_ref())
                        .unwrap_or("???")
                );
            }
        }

        return Ok(());

Yes, it's an ugly way to do that (based on a couple hours of research I'm guessing using fmt!() to pre-format each line and then
output it would be better), but as I said, this is my first time trying to modify Rust code. I'm trying to get the basics working before making it clean code.

I'm running into an issue because of Rust's strong type system, though. I can't compare ingredient_count to ITEM_STACK_SIZE, I'm getting a mismatched types error:

error[E0308]: mismatched types
   --> src/main.rs:466:26
    |
466 |             if ingredient_count > ITEM_STACK_SIZE {
    |                                   ^^^^^^^^^^^^^^^ expected `&Ratio<i32>`, found `i32`
    |
    = note: expected reference `&Ratio<i32>`
                    found type `i32`

error: aborting due to previous error

Any suggestions on how to pacify the type system?

Thanks!

solstice.1847

t-mw added a commit that referenced this issue Mar 18, 2021
@t-mw
Copy link
Owner

t-mw commented Mar 18, 2021

Hi, it's a great idea and you almost have it working. The problem is that the ingredient count at that stage is a decimal type (from https://github.com/rust-num/num-rational) because that's what's used for the calculation internally, so it needs to be converted to an integer first. Here's a commit with a fix 0bb2d6b, and a type annotation on the hashmap to make it clear what it contains. If you want to create a PR so that you get credited I'll happily merge it!

t-mw added a commit that referenced this issue Mar 18, 2021
@solstice-gw2
Copy link
Contributor Author

Thanks Tobias!

You did the real work to add the feature, so I don't have any need for any credit. I'm just happy you shared the tool (and that not everyone is using it, so I can make a little 💰 periodically 😃 ).

@t-mw t-mw closed this as completed in 35b6e0c Mar 19, 2021
@t-mw
Copy link
Owner

t-mw commented Mar 19, 2021

Ok! Added in 35b6e0c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants