mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
remove the remaining unwraps
This commit is contained in:
parent
43dec7c103
commit
a897a5aa68
@ -360,78 +360,82 @@ fn plan_trade_for_site(
|
|||||||
// === the actual planning is here ===
|
// === the actual planning is here ===
|
||||||
for (g, (_, a)) in missing_goods.iter() {
|
for (g, (_, a)) in missing_goods.iter() {
|
||||||
let mut amount = *a;
|
let mut amount = *a;
|
||||||
for (s, (price, supply)) in good_price.get_mut(g).unwrap().iter_mut() {
|
if let Some(site_price_stock) = good_price.get_mut(g) {
|
||||||
// how much to buy, limit by supply and transport budget
|
for (s, (price, supply)) in site_price_stock.iter_mut() {
|
||||||
let mut buy_target = amount.min(*supply);
|
// how much to buy, limit by supply and transport budget
|
||||||
let effort = transportation_effort(*g);
|
let mut buy_target = amount.min(*supply);
|
||||||
let collect = buy_target * effort;
|
let effort = transportation_effort(*g);
|
||||||
let mut potential_balance: f32 = 0.0;
|
let collect = buy_target * effort;
|
||||||
if collect > collect_capacity && effort > 0.0 {
|
let mut potential_balance: f32 = 0.0;
|
||||||
let transportable_amount = collect_capacity / effort;
|
if collect > collect_capacity && effort > 0.0 {
|
||||||
let missing_trade = buy_target - transportable_amount;
|
let transportable_amount = collect_capacity / effort;
|
||||||
potential_trade[*g] += missing_trade;
|
let missing_trade = buy_target - transportable_amount;
|
||||||
potential_balance += missing_trade * *price;
|
potential_trade[*g] += missing_trade;
|
||||||
buy_target = transportable_amount; // (buy_target - missing_trade).max(0.0); // avoid negative buy target caused by numeric inaccuracies
|
potential_balance += missing_trade * *price;
|
||||||
missing_collect += collect - collect_capacity;
|
buy_target = transportable_amount; // (buy_target - missing_trade).max(0.0); // avoid negative buy target caused by numeric inaccuracies
|
||||||
|
missing_collect += collect - collect_capacity;
|
||||||
|
debug!(
|
||||||
|
"missing capacity {:?}/{:?} {:?}",
|
||||||
|
missing_trade, amount, potential_balance,
|
||||||
|
);
|
||||||
|
amount = (amount - missing_trade).max(0.0); // you won't be able to transport it from elsewhere either, so don't count multiple times
|
||||||
|
}
|
||||||
|
let mut balance: f32 = *price * buy_target;
|
||||||
debug!(
|
debug!(
|
||||||
"missing capacity {:?}/{:?} {:?}",
|
"buy {:?} at {:?} amount {:?} balance {:?}",
|
||||||
missing_trade, amount, potential_balance,
|
*g,
|
||||||
|
s.id(),
|
||||||
|
buy_target,
|
||||||
|
balance,
|
||||||
);
|
);
|
||||||
amount = (amount - missing_trade).max(0.0); // you won't be able to transport it from elsewhere either, so don't count multiple times
|
if let Some(neighbor_orders) = neighbor_orders.get_mut(s) {
|
||||||
}
|
// find suitable goods in exchange
|
||||||
let mut balance: f32 = *price * buy_target;
|
let mut acute_missing_dispatch: f32 = 0.0; // only count the highest priority (not multiple times)
|
||||||
debug!(
|
for (g2, (_, price2)) in good_payment[s].iter() {
|
||||||
"buy {:?} at {:?} amount {:?} balance {:?}",
|
let mut amount2 = extra_goods[*g2];
|
||||||
*g,
|
// good available for trading?
|
||||||
s.id(),
|
if amount2 > 0.0 {
|
||||||
buy_target,
|
amount2 = amount2.min(balance / price2); // pay until balance is even
|
||||||
balance,
|
let effort2 = transportation_effort(*g2);
|
||||||
);
|
let mut dispatch = amount2 * effort2;
|
||||||
// find suitable goods in exchange
|
// limit by separate transport budget (on way back)
|
||||||
let mut acute_missing_dispatch: f32 = 0.0; // only count the highest priority (not multiple times)
|
if dispatch > dispatch_capacity && effort2 > 0.0 {
|
||||||
for (g2, (_, price2)) in good_payment[s].iter() {
|
let transportable_amount = dispatch_capacity / effort2;
|
||||||
let mut amount2 = extra_goods[*g2];
|
let missing_trade = amount2 - transportable_amount;
|
||||||
// good available for trading?
|
amount2 = transportable_amount;
|
||||||
if amount2 > 0.0 {
|
if acute_missing_dispatch == 0.0 {
|
||||||
amount2 = amount2.min(balance / price2); // pay until balance is even
|
acute_missing_dispatch = missing_trade * effort2;
|
||||||
let effort2 = transportation_effort(*g2);
|
}
|
||||||
let mut dispatch = amount2 * effort2;
|
debug!(
|
||||||
// limit by separate transport budget (on way back)
|
"can't carry payment {:?} {:?} {:?}",
|
||||||
if dispatch > dispatch_capacity && effort2 > 0.0 {
|
g2, dispatch, dispatch_capacity
|
||||||
let transportable_amount = dispatch_capacity / effort2;
|
);
|
||||||
let missing_trade = amount2 - transportable_amount;
|
dispatch = dispatch_capacity;
|
||||||
amount2 = transportable_amount;
|
}
|
||||||
if acute_missing_dispatch == 0.0 {
|
|
||||||
acute_missing_dispatch = missing_trade * effort2;
|
|
||||||
}
|
|
||||||
debug!(
|
|
||||||
"can't carry payment {:?} {:?} {:?}",
|
|
||||||
g2, dispatch, dispatch_capacity
|
|
||||||
);
|
|
||||||
dispatch = dispatch_capacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
extra_goods[*g2] -= amount2;
|
extra_goods[*g2] -= amount2;
|
||||||
debug!("pay {:?} {:?} = {:?}", g2, amount2, balance);
|
debug!("pay {:?} {:?} = {:?}", g2, amount2, balance);
|
||||||
balance -= amount2 * price2;
|
balance -= amount2 * price2;
|
||||||
neighbor_orders.get_mut(s).unwrap()[*g2] -= amount2;
|
neighbor_orders[*g2] -= amount2;
|
||||||
dispatch_capacity = (dispatch_capacity - dispatch).max(0.0);
|
dispatch_capacity = (dispatch_capacity - dispatch).max(0.0);
|
||||||
if balance == 0.0 {
|
if balance == 0.0 {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
missing_dispatch += acute_missing_dispatch;
|
||||||
|
// adjust order if we are unable to pay for it
|
||||||
|
buy_target -= balance / *price;
|
||||||
|
buy_target = buy_target.min(amount);
|
||||||
|
collect_capacity = (collect_capacity - buy_target * effort).max(0.0);
|
||||||
|
neighbor_orders[*g] += buy_target;
|
||||||
|
amount -= buy_target;
|
||||||
|
debug!(
|
||||||
|
"deal amount {:?} end_balance {:?} price {:?} left {:?}",
|
||||||
|
buy_target, balance, *price, amount
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
missing_dispatch += acute_missing_dispatch;
|
|
||||||
// adjust order if we are unable to pay for it
|
|
||||||
buy_target -= balance / *price;
|
|
||||||
buy_target = buy_target.min(amount);
|
|
||||||
collect_capacity = (collect_capacity - buy_target * effort).max(0.0);
|
|
||||||
neighbor_orders.get_mut(s).unwrap()[*g] += buy_target;
|
|
||||||
amount -= buy_target;
|
|
||||||
debug!(
|
|
||||||
"deal amount {:?} end_balance {:?} price {:?} left {:?}",
|
|
||||||
buy_target, balance, *price, amount
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// if site_id.id() == 1 {
|
// if site_id.id() == 1 {
|
||||||
@ -550,49 +554,48 @@ fn trade_at_site(
|
|||||||
);
|
);
|
||||||
let mut good_delivery = MapVec::from_default(0.0);
|
let mut good_delivery = MapVec::from_default(0.0);
|
||||||
for (g, amount, price) in sorted_sell.iter() {
|
for (g, amount, price) in sorted_sell.iter() {
|
||||||
if order_stock_ratio[*g].is_none() {
|
if let Some(order_stock_ratio) = order_stock_ratio[*g] {
|
||||||
continue;
|
let allocated_amount = *amount / order_stock_ratio.max(1.0);
|
||||||
}
|
let mut balance = allocated_amount * *price;
|
||||||
let allocated_amount = *amount / order_stock_ratio[*g].unwrap().max(1.0);
|
for (g2, avail, price2) in sorted_buy.iter_mut() {
|
||||||
let mut balance = allocated_amount * *price;
|
let amount2 = (-*avail).min(balance / *price2);
|
||||||
for (g2, avail, price2) in sorted_buy.iter_mut() {
|
assert!(amount2 >= 0.0);
|
||||||
let amount2 = (-*avail).min(balance / *price2);
|
economy.stocks[*g2] += amount2;
|
||||||
assert!(amount2 >= 0.0);
|
balance = (balance - amount2 * *price2).max(0.0);
|
||||||
economy.stocks[*g2] += amount2;
|
*avail += amount2; // reduce (negative) brought stock
|
||||||
balance = (balance - amount2 * *price2).max(0.0);
|
debug!("paid with {:?} {} {}", *g2, amount2, *price2);
|
||||||
*avail += amount2; // reduce (negative) brought stock
|
if balance == 0.0 {
|
||||||
debug!("paid with {:?} {} {}", *g2, amount2, *price2);
|
break;
|
||||||
if balance == 0.0 {
|
}
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
let paid_amount = allocated_amount - balance / *price;
|
||||||
|
if paid_amount / allocated_amount < 0.95 {
|
||||||
|
debug!(
|
||||||
|
"Client {} is broke on {:?} : {} {} severity {}",
|
||||||
|
o.customer.id(),
|
||||||
|
*g,
|
||||||
|
paid_amount,
|
||||||
|
allocated_amount,
|
||||||
|
order_stock_ratio,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
debug!("bought {:?} {} {}", *g, paid_amount, *price);
|
||||||
|
}
|
||||||
|
good_delivery[*g] += paid_amount;
|
||||||
|
if economy.stocks[*g] - paid_amount < 0.0 {
|
||||||
|
info!(
|
||||||
|
"BUG {:?} {:?} {} TO {:?} OSR {:?} ND {:?}",
|
||||||
|
economy.stocks[*g],
|
||||||
|
*g,
|
||||||
|
paid_amount,
|
||||||
|
total_orders[*g],
|
||||||
|
order_stock_ratio,
|
||||||
|
next_demand[*g]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert!(economy.stocks[*g] - paid_amount >= 0.0);
|
||||||
|
economy.stocks[*g] -= paid_amount;
|
||||||
}
|
}
|
||||||
let paid_amount = allocated_amount - balance / *price;
|
|
||||||
if paid_amount / allocated_amount < 0.95 {
|
|
||||||
debug!(
|
|
||||||
"Client {} is broke on {:?} : {} {} severity {}",
|
|
||||||
o.customer.id(),
|
|
||||||
*g,
|
|
||||||
paid_amount,
|
|
||||||
allocated_amount,
|
|
||||||
order_stock_ratio[*g].unwrap(),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
debug!("bought {:?} {} {}", *g, paid_amount, *price);
|
|
||||||
}
|
|
||||||
good_delivery[*g] += paid_amount;
|
|
||||||
if economy.stocks[*g] - paid_amount < 0.0 {
|
|
||||||
info!(
|
|
||||||
"BUG {:?} {:?} {} TO {:?} OSR {:?} ND {:?}",
|
|
||||||
economy.stocks[*g],
|
|
||||||
*g,
|
|
||||||
paid_amount,
|
|
||||||
total_orders[*g],
|
|
||||||
order_stock_ratio[*g].unwrap(),
|
|
||||||
next_demand[*g]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
assert!(economy.stocks[*g] - paid_amount >= 0.0);
|
|
||||||
economy.stocks[*g] -= paid_amount;
|
|
||||||
}
|
}
|
||||||
for (g, amount, _) in sorted_buy.drain(..) {
|
for (g, amount, _) in sorted_buy.drain(..) {
|
||||||
if amount < 0.0 {
|
if amount < 0.0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user