replace an unwrap by returning an Option

This commit is contained in:
Christof Petig 2021-03-23 23:32:26 +01:00
parent 6021a746b0
commit a83a74d216
2 changed files with 21 additions and 19 deletions

View File

@ -550,9 +550,9 @@ impl LoadoutBuilder {
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 1..18 { for i in 1..18 {
backpack.slots_mut()[i] = Some(Item::new_from_asset_expect( if let Some(item_id) = TradePricing::random_item(Good::Armor, armor) {
&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( let mut bag1 = Item::new_from_asset_expect(
"common.items.armor.misc.bag.reliable_backpack", "common.items.armor.misc.bag.reliable_backpack",
@ -564,9 +564,9 @@ impl LoadoutBuilder {
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 0..16 { for i in 0..16 {
bag1.slots_mut()[i] = Some(Item::new_from_asset_expect( if let Some(item_id) = TradePricing::random_item(Good::Tools, weapon) {
&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( let mut bag2 = Item::new_from_asset_expect(
"common.items.armor.misc.bag.reliable_backpack", "common.items.armor.misc.bag.reliable_backpack",
@ -578,9 +578,11 @@ impl LoadoutBuilder {
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 0..16 { for i in 0..16 {
bag2.slots_mut()[i] = Some(Item::new_from_asset_expect( if let Some(item_id) =
&TradePricing::random_item(Good::Ingredients, ingredients), 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( let mut bag3 = Item::new_from_asset_expect(
"common.items.armor.misc.bag.reliable_backpack", "common.items.armor.misc.bag.reliable_backpack",
@ -592,9 +594,9 @@ impl LoadoutBuilder {
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 0..16 { for i in 0..16 {
bag3.slots_mut()[i] = Some(Item::new_from_asset_expect( if let Some(item_id) = TradePricing::random_item(Good::Food, food) {
&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( let mut bag4 = Item::new_from_asset_expect(
"common.items.armor.misc.bag.reliable_backpack", "common.items.armor.misc.bag.reliable_backpack",
@ -606,9 +608,9 @@ impl LoadoutBuilder {
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 0..16 { for i in 0..16 {
bag4.slots_mut()[i] = Some(Item::new_from_asset_expect( if let Some(item_id) = TradePricing::random_item(Good::Potions, potions) {
&TradePricing::random_item(Good::Potions, potions), bag4.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id));
)); }
} }
LoadoutBuilder::new() LoadoutBuilder::new()
.active_item(active_item) .active_item(active_item)

View File

@ -283,9 +283,9 @@ impl TradePricing {
result result
} }
fn random_item_impl(&self, good: Good, amount: f32) -> String { fn random_item_impl(&self, good: Good, amount: f32) -> Option<String> {
if good == Good::Coin { if good == Good::Coin {
TradePricing::COIN_ITEM.into() Some(TradePricing::COIN_ITEM.into())
} else { } else {
let table = self.get_list(good); let table = self.get_list(good);
let upper = table.len(); let upper = table.len();
@ -297,11 +297,11 @@ impl TradePricing {
.unwrap_or(upper - 1); .unwrap_or(upper - 1);
let index = (rand::random::<f32>() * ((upper - lower) as f32)).floor() as usize + lower; let index = (rand::random::<f32>() * ((upper - lower) as f32)).floor() as usize + lower;
//.gen_range(lower..upper); //.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<String> {
TRADE_PRICING.random_item_impl(good, amount) TRADE_PRICING.random_item_impl(good, amount)
} }