Totally working, best I can tell

This commit is contained in:
2025-01-24 20:20:37 -05:00
parent 2153a6ce96
commit 8fa172da17
3 changed files with 100153 additions and 29 deletions

View File

@@ -250,8 +250,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) -> FiNum { // And zero out in the tree
if let Some(pos)=self.order_finder.remove(&id) {
self.capture0(pos);
let weight=self.weight_here(pos);
self.sub_weight(pos,weight)
self.sub_weight(pos,weight);
let rval=self.tree[pos].acc;
self.tree[pos].acc=FiNum::zero();
rval
}
else { FiNum::zero() }
}
@@ -271,27 +275,25 @@ impl RoyaltyTree {
}
None
}
fn capture_partial(&mut self, id: usize, weight: FiNum) -> FiNum {
FiNum::zero()
fn capture_by_order_id(&mut self, order_id: usize) -> FiNum {
if let Some(&index)=self.order_finder.get(&order_id) {
self.capture0(index);
let rval=self.tree[index].acc;
self.tree[index].acc=FiNum::zero();
rval
} else { FiNum::zero() }
}
fn sub_weight(&mut self, index: usize, mut weight: FiNum) -> FiNum {
fn sub_weight(&mut self, index: usize, mut weight: FiNum) {
self.get_royalty(index);
let delta_count;
let wh=self.weight_here(index);
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();
loop {
@@ -300,8 +302,6 @@ impl RoyaltyTree {
if index==ff { break; }
index=wt_parent(index);
}
self.add_royalty(redist);
dist
}
fn set_location(&mut self, id: usize, index: usize) {
self.tree[index].order_id=id;
@@ -639,6 +639,20 @@ impl Market {
//println!("Transact at ask rate. Qty={} elt.sell_remain={} elt.royalty_remain={} elt.commission_remain={}",qty,elt.sell_remain,elt.royalty_remain,elt.commission_remain);
let rq2=if !elt.sell_remain.is_zero() { elt.royalty_remain *qty/elt.sell_remain } else { FiNum::zero() };
let cq2=if !elt.sell_remain.is_zero() { elt.commission_remain*qty/elt.sell_remain } else { FiNum::zero() };
self.royalties.entry(sell_type).or_insert(RoyaltyTree::new());
self.royalties.entry(buy_type) .or_insert(RoyaltyTree::new());
let rtb=self.royalties.entry(buy_type) .or_insert(RoyaltyTree::new());
let rts=self.royalties.entry(sell_type).or_insert(RoyaltyTree::new());
if let Some(&cap_index)=rts.order_finder.get(&elt.order_id) {
let wh0=rts.weight_here(cap_index);
let acc0=rts.tree[cap_index].acc;
let cap=rts.capture_by_order_id(elt.owner);
rts.sub_weight(cap_index,pay_qty);
let wh1=rts.weight_here(cap_index);
let acc1=rts.tree[cap_index].acc;
//println!("Weight {} / {} Acc {} / {} Cap {}",wh0,wh1,acc0,acc1,cap);
self.traders[elt.owner].add_balance(sell_type,cap);
}
//elt.sell_remain-=qty;
royalty_acc+=rq0+rq1;
commission_acc+=cq0+cq1;
@@ -659,7 +673,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;
let dist=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);
//println!("Popping top order {} and reclaiming {}",top_order.order_id,dist);
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);
@@ -1265,13 +1280,13 @@ fn interactive(m: &mut Market, mut out: Option<File>) {
} else { println!("Could not find trader {}",username); }
Command::None
},
["seedrandom", seed] => {
["seedrandom"|"sr", seed] => {
let seed=seed.parse().unwrap();
m.seed_random(seed);
println!("Seeded RNG with {}",seed);
Command::None
}
["randomcommands", qty] => {
["randomcommands"|"rc", qty] => {
let qty:u32=qty.parse().unwrap();
let start = Instant::now();
for i in 0..qty {