This commit is contained in:
2025-01-20 16:16:43 -05:00
parent de03fa8741
commit 76a4610903
3 changed files with 28 additions and 13 deletions

View File

@@ -248,11 +248,12 @@ impl RoyaltyTree {
}
}
// We really need two methods: remove an order and redistribute, remove some and return captured amount
fn remove_order_id(&mut self, id: usize) { // And zero out in the tree
fn remove_order_id(&mut self, id: usize) -> FiNum { // And zero out in the tree
if let Some(pos)=self.order_finder.remove(&id) {
let weight=self.weight_here(pos);
self.sub_weight(pos,weight);
self.sub_weight(pos,weight)
}
else { FiNum::zero() }
}
fn random_order_id(&self, rng: &mut rand::rngs::StdRng) -> Option<usize> {
if self.tree.len()==0 { return None }
@@ -273,12 +274,23 @@ impl RoyaltyTree {
fn capture_partial(&mut self, id: usize, weight: FiNum) -> FiNum {
FiNum::zero()
}
fn sub_weight(&mut self, index: usize, mut weight: FiNum) {
fn sub_weight(&mut self, index: usize, mut weight: FiNum) -> FiNum {
self.get_royalty(index);
let delta_count;
let wh=self.weight_here(index);
let redist=if weight>=wh { delta_count=if wh>FiNum::zero() { 1 } else { 0 }; weight=wh; self.tree[index].acc }
else { delta_count=0; self.tree[index].acc*weight/wh };
let dist;
let redist;
if weight>=wh {
delta_count=if wh>FiNum::zero() { 1 } else { 0 };
weight=wh;
dist=self.tree[index].acc;
redist=FiNum::zero();
}
else {
delta_count=0;
dist=self.tree[index].acc*weight/wh;
redist=self.tree[index].acc-dist;
}
self.tree[index].acc-=redist;
let mut index=index;
let ff=self.forefather();
@@ -289,6 +301,7 @@ impl RoyaltyTree {
index=wt_parent(index);
}
self.add_royalty(redist);
dist
}
fn set_location(&mut self, id: usize, index: usize) {
self.tree[index].order_id=id;
@@ -574,6 +587,7 @@ impl Market {
fn retract_order(&mut self, order_id:usize) -> Result { // Still need to credit back funds!
let pair=self.order_finder.get(&order_id).unwrap();
let sell_type=pair.0;
let buy_type=pair.1;
let queue=self.orders.get_mut(pair).unwrap();
let queue_index=*queue.order_finder.get(&order_id).unwrap();
let order=&queue.v[queue_index];
@@ -582,7 +596,8 @@ impl Market {
println!("Order owner is {}",order.owner);
// Remove from royalties
let rt=self.royalties.get_mut(&pair.0).unwrap();
rt.remove_order_id(order_id);
let dist=rt.remove_order_id(order_id);
self.traders[order.owner].add_balance(buy_type,dist);
// Remove from order_finder
self.order_finder.remove(&order_id);
// Remove from orders
@@ -644,7 +659,8 @@ impl Market {
//if !top_order.commission_remain.is_zero() { println!("Little commission? {} ",top_order.commission_remain); }
let rq3=top_order.royalty_remain;
let cq3=top_order.commission_remain;
self.royalties.get_mut(&buy_type).unwrap().remove_order_id(top_order.order_id);
let dist=self.royalties.get_mut(&buy_type).unwrap().remove_order_id(top_order.order_id);
self.traders[elt.owner].add_balance(sell_type,dist);
self.order_finder.remove(&top_order.order_id);
self.traders[top_order.owner].order_finder.remove(&top_order.order_id);
self.orders.get_mut(&ap).unwrap().pop(); // This removes id (which is stored as part of the Order) from self.order's finder
@@ -920,7 +936,7 @@ fn royalty_stuff() {
let node=rng.gen_range(0..10);
match rng.gen_range(0..10) {
0 => rt.add_weight(node,FiNum::new_i32(rng.gen_range(0..101))),
1 => rt.sub_weight(node,FiNum::new_i32(rng.gen_range(0..101))),
1 => { rt.sub_weight(node,FiNum::new_i32(rng.gen_range(0..101))); },
2 => rt.add_royalty(FiNum::new_i32(rng.gen_range(0..10))),
// 3 => println!("Random Order ID: {:?}",rt.random_order_id(&mut rng)),
_ => (),