changes
This commit is contained in:
34
src/main.rs
34
src/main.rs
@@ -5,7 +5,7 @@
|
||||
// new node.
|
||||
//
|
||||
// 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
|
||||
//
|
||||
|
||||
@@ -134,6 +134,11 @@ impl RoyaltyTree {
|
||||
fn new() -> Self {
|
||||
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 {
|
||||
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)) });
|
||||
for off in sorted.iter() {
|
||||
let off=off.borrow();
|
||||
println!(" {} @ {} ({})",
|
||||
println!(" {} @ {} ({}), Acc: {}",
|
||||
off.sell_remain, // off.buy_qty*off.sell_remain/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();
|
||||
}
|
||||
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) {
|
||||
println!("Replaying {}",fname);
|
||||
@@ -364,8 +374,11 @@ impl Market {
|
||||
for line in reader.lines() {
|
||||
if let Ok(line) = line {
|
||||
let cmd=Command::deserialize(line);
|
||||
let res=self.execute(&cmd);
|
||||
println!("{}",res.describe());
|
||||
if let Command::NOP(comment)=cmd { println!("{}",comment); }
|
||||
else {
|
||||
let res=self.execute(&cmd);
|
||||
println!("{}",res.describe());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -668,8 +681,9 @@ impl Command {
|
||||
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()),
|
||||
Self::Retract { order_id } => format!("RE {}",order_id),
|
||||
Self::NOP(str) => format!("NOP: {}",clean(str)),
|
||||
_ => format!("NOP"),
|
||||
Self::Error(str) => format!("NOP Error: {}",str),
|
||||
Self::NOP(str) => format!("NOP {}",clean(str)),
|
||||
_ => format!("NOP (This should never happen)"),
|
||||
}
|
||||
}
|
||||
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[..] {
|
||||
["addtrader", username] => Command::AddTrader { user: username.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 { 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
|
||||
}
|
||||
@@ -855,7 +869,7 @@ fn interactive(m: &mut Market, mut out: Option<File>) {
|
||||
},
|
||||
["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 {
|
||||
let ser=cmd.serialize();
|
||||
if let Err(e)=writeln!(f,"{}",ser) { eprintln!("An error occurred while writing {}",e); }
|
||||
|
||||
Reference in New Issue
Block a user