This commit is contained in:
2024-07-29 16:45:47 -04:00
parent 695269c30a
commit d526a95489

View File

@@ -5,7 +5,7 @@
// new node. // new node.
// //
// Case to think about: // Case to think about:
// Selling 140000 USD to buy 2 BTC. Weight is ===140k UShyucfdD // Selling 140000 USD to buy 2 BTC. Weight is ===140k USD
// Selling 50000 GBP to buy 1 BTC. Weight is === 50k GBP // Selling 50000 GBP to buy 1 BTC. Weight is === 50k GBP
// //
@@ -134,6 +134,11 @@ impl RoyaltyTree {
fn new() -> Self { fn new() -> Self {
RoyaltyTree { tree:Vec::new(), next_entry:0, spare_change:FiNum::zero() } RoyaltyTree { tree:Vec::new(), next_entry:0, spare_change:FiNum::zero() }
} }
fn acc_total(&self) -> FiNum {
let mut rval=FiNum::zero();
for r in &self.tree { rval+= r.acc; }
rval
}
fn weight_here_below(&self, index:usize) -> FiNum { fn weight_here_below(&self, index:usize) -> FiNum {
self.tree[index].weight self.tree[index].weight
} }
@@ -349,13 +354,18 @@ impl Market {
sorted.sort_by(|a,b| { let a=a.borrow(); let b=b.borrow(); (a.sell_qty/a.buy_qty).cmp(&(b.sell_qty/b.buy_qty)) }); sorted.sort_by(|a,b| { let a=a.borrow(); let b=b.borrow(); (a.sell_qty/a.buy_qty).cmp(&(b.sell_qty/b.buy_qty)) });
for off in sorted.iter() { for off in sorted.iter() {
let off=off.borrow(); let off=off.borrow();
println!(" {} @ {} ({})", println!(" {} @ {} ({}), Acc: {}",
off.sell_remain, // off.buy_qty*off.sell_remain/off.sell_qty, off.sell_remain, // off.buy_qty*off.sell_remain/off.sell_qty,
off.buy_qty/off.sell_qty, off.buy_qty/off.sell_qty,
self.traders[off.owner as usize].name); self.traders[off.owner as usize].name,
self.royalties.get(&ap.1).unwrap().tree[off.rt_loc].acc);
} }
pq.dump(); pq.dump();
} }
for index in 0..self.assets.len() {
let tot=self.royalties.get(&index).unwrap().acc_total();
println!("Accumulated Royalties for {}: {}",self.number_to_asset(index).name,tot);
}
} }
fn replay_file(&mut self, fname:&str) { fn replay_file(&mut self, fname:&str) {
println!("Replaying {}",fname); println!("Replaying {}",fname);
@@ -364,11 +374,14 @@ impl Market {
for line in reader.lines() { for line in reader.lines() {
if let Ok(line) = line { if let Ok(line) = line {
let cmd=Command::deserialize(line); let cmd=Command::deserialize(line);
if let Command::NOP(comment)=cmd { println!("{}",comment); }
else {
let res=self.execute(&cmd); let res=self.execute(&cmd);
println!("{}",res.describe()); println!("{}",res.describe());
} }
} }
} }
}
fn make_order(&mut self, owner:usize, sell_type:usize, buy_type:usize, sell_qty_initial:FiNum, buy_qty_initial:FiNum) -> Result { fn make_order(&mut self, owner:usize, sell_type:usize, buy_type:usize, sell_qty_initial:FiNum, buy_qty_initial:FiNum) -> Result {
let mut log:Vec<String>=Vec::new(); let mut log:Vec<String>=Vec::new();
let initial_balance=self.traders[owner].get_balance(sell_type); let initial_balance=self.traders[owner].get_balance(sell_type);
@@ -668,8 +681,9 @@ impl Command {
Self::Order { user_id, sell_type, sell_qty, buy_type, buy_qty } Self::Order { user_id, sell_type, sell_qty, buy_type, buy_qty }
=> format!("OR {} {} {} {} {}",user_id,sell_type,sell_qty.serialize(),buy_type,buy_qty.serialize()), => format!("OR {} {} {} {} {}",user_id,sell_type,sell_qty.serialize(),buy_type,buy_qty.serialize()),
Self::Retract { order_id } => format!("RE {}",order_id), Self::Retract { order_id } => format!("RE {}",order_id),
Self::NOP(str) => format!("NOP: {}",clean(str)), Self::Error(str) => format!("NOP Error: {}",str),
_ => format!("NOP"), Self::NOP(str) => format!("NOP {}",clean(str)),
_ => format!("NOP (This should never happen)"),
} }
} }
fn deserialize(line: String) -> Self { fn deserialize(line: String) -> Self {
@@ -767,7 +781,7 @@ fn copy(source: &str, destination: &str) {
} }
fn tokens_to_command(m: &Market, logged_in: usize, tokens: Vec<&str>) -> Command { fn tokens_to_command(m: &Market, logged_in: usize, tokens: Vec<&str>,line: &str) -> Command {
let cmd:Command=match &tokens[..] { let cmd:Command=match &tokens[..] {
["addtrader", username] => Command::AddTrader { user: username.to_string() }, ["addtrader", username] => Command::AddTrader { user: username.to_string() },
["addasset", assetname] => Command::AddAsset { asset: assetname.to_string() }, ["addasset", assetname] => Command::AddAsset { asset: assetname.to_string() },
@@ -821,7 +835,7 @@ fn tokens_to_command(m: &Market, logged_in: usize, tokens: Vec<&str>) -> Command
else if qty1.is_zero() { Command::Error("Qty1 is must be > 0".to_string()) } else if qty1.is_zero() { Command::Error("Qty1 is must be > 0".to_string()) }
else { Command::Order { user_id: logged_in, sell_type: *cur0.unwrap(), sell_qty: qty0, buy_type: *cur1.unwrap(), buy_qty: qty1 } } else { Command::Order { user_id: logged_in, sell_type: *cur0.unwrap(), sell_qty: qty0, buy_type: *cur1.unwrap(), buy_qty: qty1 } }
} }
_ => { Command::Error("Unknown Command".to_string()) }, _ => { Command::Error(line.to_string()) },
}; };
cmd cmd
} }
@@ -855,7 +869,7 @@ fn interactive(m: &mut Market, mut out: Option<File>) {
}, },
["quit"] => { return }, ["quit"] => { return },
_ => { _ => {
let cmd=tokens_to_command(m,trader,tokens); let cmd=tokens_to_command(m,trader,tokens,&input);
if let Some(ref mut f)=out { if let Some(ref mut f)=out {
let ser=cmd.serialize(); let ser=cmd.serialize();
if let Err(e)=writeln!(f,"{}",ser) { eprintln!("An error occurred while writing {}",e); } if let Err(e)=writeln!(f,"{}",ser) { eprintln!("An error occurred while writing {}",e); }