Merge branch 'christof/de_unwrap1' into 'master'

replace an unwrap by returning an Option

See merge request veloren/veloren!1983
This commit is contained in:
Joshua Yanovski 2021-03-24 08:18:53 +00:00
commit 97912d2d0f
2 changed files with 46 additions and 42 deletions

View File

@ -530,29 +530,31 @@ impl LoadoutBuilder {
Merchant => { Merchant => {
let mut backpack = let mut backpack =
Item::new_from_asset_expect("common.items.armor.misc.back.backpack"); Item::new_from_asset_expect("common.items.armor.misc.back.backpack");
let mut coins = Item::new_from_asset_expect("common.items.utility.coins"); let mut coins = economy
coins .map(|e| e.unconsumed_stock.get(&Good::Coin))
.set_amount( .flatten()
(economy .copied()
.map(|e| e.unconsumed_stock.get(&Good::Coin)) .unwrap_or_default()
.flatten() .round() as u32;
.copied()
.unwrap_or_default()
.round() as u32)
.max(1),
)
.expect("coins should be stackable");
backpack.slots_mut()[0] = Some(coins);
let armor = economy let armor = economy
.map(|e| e.unconsumed_stock.get(&Good::Armor)) .map(|e| e.unconsumed_stock.get(&Good::Armor))
.flatten() .flatten()
.copied() .copied()
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 1..18 { for s in backpack.slots_mut() {
backpack.slots_mut()[i] = Some(Item::new_from_asset_expect( if coins > 0 {
&TradePricing::random_item(Good::Armor, armor), 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( let mut bag1 = Item::new_from_asset_expect(
"common.items.armor.misc.bag.reliable_backpack", "common.items.armor.misc.bag.reliable_backpack",
@ -563,10 +565,10 @@ impl LoadoutBuilder {
.copied() .copied()
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 0..16 { for i in bag1.slots_mut() {
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), *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",
@ -577,10 +579,12 @@ impl LoadoutBuilder {
.copied() .copied()
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 0..16 { for i in bag2.slots_mut() {
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)
)); {
*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",
@ -591,10 +595,10 @@ impl LoadoutBuilder {
.copied() .copied()
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 0..16 { for i in bag3.slots_mut() {
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), *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",
@ -605,10 +609,10 @@ impl LoadoutBuilder {
.copied() .copied()
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 0..16 { for i in bag4.slots_mut() {
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), *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)
} }
@ -359,10 +359,10 @@ mod tests {
info!("init"); info!("init");
TradePricing::instance().print_sorted(); TradePricing::instance().print_sorted();
info!("Armor 5 {}", TradePricing::random_item(Good::Armor, 5.0)); for _ in 0..5 {
info!("Armor 5 {}", TradePricing::random_item(Good::Armor, 5.0)); if let Some(item_id) = TradePricing::random_item(Good::Armor, 5.0) {
info!("Armor 5 {}", TradePricing::random_item(Good::Armor, 5.0)); info!("Armor 5 {}", item_id);
info!("Armor 5 {}", TradePricing::random_item(Good::Armor, 5.0)); }
info!("Armor 5 {}", TradePricing::random_item(Good::Armor, 5.0)); }
} }
} }