From a83a74d216eeaf752922ce2572f19fef9b5c57bd Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Tue, 23 Mar 2021 23:32:26 +0100 Subject: [PATCH 1/3] replace an unwrap by returning an Option --- common/src/comp/inventory/loadout_builder.rs | 32 +++++++++++--------- common/src/comp/inventory/trade_pricing.rs | 8 ++--- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/common/src/comp/inventory/loadout_builder.rs b/common/src/comp/inventory/loadout_builder.rs index 72e7f90a3e..e4040e079b 100644 --- a/common/src/comp/inventory/loadout_builder.rs +++ b/common/src/comp/inventory/loadout_builder.rs @@ -550,9 +550,9 @@ impl LoadoutBuilder { .unwrap_or_default() / 10.0; for i in 1..18 { - backpack.slots_mut()[i] = Some(Item::new_from_asset_expect( - &TradePricing::random_item(Good::Armor, armor), - )); + if let Some(item_id) = TradePricing::random_item(Good::Armor, armor) { + backpack.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + } } let mut bag1 = Item::new_from_asset_expect( "common.items.armor.misc.bag.reliable_backpack", @@ -564,9 +564,9 @@ impl LoadoutBuilder { .unwrap_or_default() / 10.0; for i in 0..16 { - bag1.slots_mut()[i] = Some(Item::new_from_asset_expect( - &TradePricing::random_item(Good::Tools, weapon), - )); + if let Some(item_id) = TradePricing::random_item(Good::Tools, weapon) { + bag1.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + } } let mut bag2 = Item::new_from_asset_expect( "common.items.armor.misc.bag.reliable_backpack", @@ -578,9 +578,11 @@ impl LoadoutBuilder { .unwrap_or_default() / 10.0; for i in 0..16 { - bag2.slots_mut()[i] = Some(Item::new_from_asset_expect( - &TradePricing::random_item(Good::Ingredients, ingredients), - )); + if let Some(item_id) = + TradePricing::random_item(Good::Ingredients, ingredients) + { + bag2.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + } } let mut bag3 = Item::new_from_asset_expect( "common.items.armor.misc.bag.reliable_backpack", @@ -592,9 +594,9 @@ impl LoadoutBuilder { .unwrap_or_default() / 10.0; for i in 0..16 { - bag3.slots_mut()[i] = Some(Item::new_from_asset_expect( - &TradePricing::random_item(Good::Food, food), - )); + if let Some(item_id) = TradePricing::random_item(Good::Food, food) { + bag3.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + } } let mut bag4 = Item::new_from_asset_expect( "common.items.armor.misc.bag.reliable_backpack", @@ -606,9 +608,9 @@ impl LoadoutBuilder { .unwrap_or_default() / 10.0; for i in 0..16 { - bag4.slots_mut()[i] = Some(Item::new_from_asset_expect( - &TradePricing::random_item(Good::Potions, potions), - )); + if let Some(item_id) = TradePricing::random_item(Good::Potions, potions) { + bag4.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + } } LoadoutBuilder::new() .active_item(active_item) diff --git a/common/src/comp/inventory/trade_pricing.rs b/common/src/comp/inventory/trade_pricing.rs index b18321eb7c..1670ab27a8 100644 --- a/common/src/comp/inventory/trade_pricing.rs +++ b/common/src/comp/inventory/trade_pricing.rs @@ -283,9 +283,9 @@ impl TradePricing { result } - fn random_item_impl(&self, good: Good, amount: f32) -> String { + fn random_item_impl(&self, good: Good, amount: f32) -> Option { if good == Good::Coin { - TradePricing::COIN_ITEM.into() + Some(TradePricing::COIN_ITEM.into()) } else { let table = self.get_list(good); let upper = table.len(); @@ -297,11 +297,11 @@ impl TradePricing { .unwrap_or(upper - 1); let index = (rand::random::() * ((upper - lower) as f32)).floor() as usize + lower; //.gen_range(lower..upper); - table.get(index).unwrap().0.clone() + table.get(index).map(|i| i.0.clone()) } } - pub fn random_item(good: Good, amount: f32) -> String { + pub fn random_item(good: Good, amount: f32) -> Option { TRADE_PRICING.random_item_impl(good, amount) } From acba98366a615353a01fae875c41180fc5452674 Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Wed, 24 Mar 2021 00:15:55 +0100 Subject: [PATCH 2/3] loop over slots_mut instead of using an index --- common/src/comp/inventory/loadout_builder.rs | 50 ++++++++++---------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/common/src/comp/inventory/loadout_builder.rs b/common/src/comp/inventory/loadout_builder.rs index e4040e079b..9b91368886 100644 --- a/common/src/comp/inventory/loadout_builder.rs +++ b/common/src/comp/inventory/loadout_builder.rs @@ -530,28 +530,30 @@ impl LoadoutBuilder { Merchant => { let mut backpack = Item::new_from_asset_expect("common.items.armor.misc.back.backpack"); - let mut coins = Item::new_from_asset_expect("common.items.utility.coins"); - coins - .set_amount( - (economy - .map(|e| e.unconsumed_stock.get(&Good::Coin)) - .flatten() - .copied() - .unwrap_or_default() - .round() as u32) - .max(1), - ) - .expect("coins should be stackable"); - backpack.slots_mut()[0] = Some(coins); + let mut coins = economy + .map(|e| e.unconsumed_stock.get(&Good::Coin)) + .flatten() + .copied() + .unwrap_or_default() + .round() as u32; let armor = economy .map(|e| e.unconsumed_stock.get(&Good::Armor)) .flatten() .copied() .unwrap_or_default() / 10.0; - for i in 1..18 { - if let Some(item_id) = TradePricing::random_item(Good::Armor, armor) { - backpack.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + for s in backpack.slots_mut() { + if coins > 0 { + let mut coin_item = + Item::new_from_asset_expect("common.items.utility.coins"); + coin_item + .set_amount(coins) + .expect("coins should be stackable"); + *s = Some(coin_item); + coins = 0; + } else if let Some(item_id) = TradePricing::random_item(Good::Armor, armor) + { + *s = Some(Item::new_from_asset_expect(&item_id)); } } let mut bag1 = Item::new_from_asset_expect( @@ -563,9 +565,9 @@ impl LoadoutBuilder { .copied() .unwrap_or_default() / 10.0; - for i in 0..16 { + for i in bag1.slots_mut() { if let Some(item_id) = TradePricing::random_item(Good::Tools, weapon) { - bag1.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + *i = Some(Item::new_from_asset_expect(&item_id)); } } let mut bag2 = Item::new_from_asset_expect( @@ -577,11 +579,11 @@ impl LoadoutBuilder { .copied() .unwrap_or_default() / 10.0; - for i in 0..16 { + for i in bag2.slots_mut() { if let Some(item_id) = TradePricing::random_item(Good::Ingredients, ingredients) { - bag2.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + *i = Some(Item::new_from_asset_expect(&item_id)); } } let mut bag3 = Item::new_from_asset_expect( @@ -593,9 +595,9 @@ impl LoadoutBuilder { .copied() .unwrap_or_default() / 10.0; - for i in 0..16 { + for i in bag3.slots_mut() { if let Some(item_id) = TradePricing::random_item(Good::Food, food) { - bag3.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + *i = Some(Item::new_from_asset_expect(&item_id)); } } let mut bag4 = Item::new_from_asset_expect( @@ -607,9 +609,9 @@ impl LoadoutBuilder { .copied() .unwrap_or_default() / 10.0; - for i in 0..16 { + for i in bag4.slots_mut() { if let Some(item_id) = TradePricing::random_item(Good::Potions, potions) { - bag4.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + *i = Some(Item::new_from_asset_expect(&item_id)); } } LoadoutBuilder::new() From aa6a75808f7e33257a717a0d7a75f69789018aad Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Wed, 24 Mar 2021 07:37:09 +0100 Subject: [PATCH 3/3] adapt unit test --- common/src/comp/inventory/trade_pricing.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/common/src/comp/inventory/trade_pricing.rs b/common/src/comp/inventory/trade_pricing.rs index 1670ab27a8..f4094aadb8 100644 --- a/common/src/comp/inventory/trade_pricing.rs +++ b/common/src/comp/inventory/trade_pricing.rs @@ -359,10 +359,10 @@ mod tests { info!("init"); TradePricing::instance().print_sorted(); - info!("Armor 5 {}", TradePricing::random_item(Good::Armor, 5.0)); - info!("Armor 5 {}", TradePricing::random_item(Good::Armor, 5.0)); - info!("Armor 5 {}", TradePricing::random_item(Good::Armor, 5.0)); - info!("Armor 5 {}", TradePricing::random_item(Good::Armor, 5.0)); - info!("Armor 5 {}", TradePricing::random_item(Good::Armor, 5.0)); + for _ in 0..5 { + if let Some(item_id) = TradePricing::random_item(Good::Armor, 5.0) { + info!("Armor 5 {}", item_id); + } + } } }