|
| 1 | +use crate::*; |
| 2 | + |
| 3 | +pub fn read_inputs( |
| 4 | + connections: &Arc<Mutex<Vec<Connection>>>, |
| 5 | + wallets: &Arc<Mutex<Vec<Wallet>>>, |
| 6 | + msg: &mut Vec<String>, |
| 7 | +) { |
| 8 | + let mut cons = connections.lock().unwrap(); |
| 9 | + |
| 10 | + for c in cons.iter_mut() { |
| 11 | + let mut buf = [0; 1024]; |
| 12 | + let len = match c.stream.read(&mut buf) { |
| 13 | + Ok(l) => l, |
| 14 | + Err(_) => continue, |
| 15 | + }; |
| 16 | + |
| 17 | + if len > 0 { |
| 18 | + println!( |
| 19 | + "> User: 0x{:016x} Miner 0x{:08x} sent: {:?} from: {:?}", |
| 20 | + c.miner.wallet_id, |
| 21 | + c.miner.miner_id, |
| 22 | + &buf[..len], |
| 23 | + c.stream |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + let mut new_boost = 0; |
| 28 | + let mut new_boost_cost = 0u128; |
| 29 | + let mut new_miners = 0; |
| 30 | + let mut new_miners_cost = 0u128; |
| 31 | + |
| 32 | + for i in buf[..len].iter() { |
| 33 | + if *i == b'b' { |
| 34 | + // Purchase boost |
| 35 | + let mut wals = wallets.lock().unwrap(); |
| 36 | + for w in wals.iter_mut() { |
| 37 | + if w.id == c.miner.wallet_id { |
| 38 | + let cost = buy_boost(c, w); |
| 39 | + if cost > 0 { |
| 40 | + new_boost += 128; |
| 41 | + new_boost_cost += cost as u128; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + drop(wals); |
| 46 | + } |
| 47 | + if *i == b'm' { |
| 48 | + // Purchase miner licenses |
| 49 | + let mut wals = wallets.lock().unwrap(); |
| 50 | + for w in wals.iter_mut() { |
| 51 | + if w.id == c.miner.wallet_id { |
| 52 | + let cost = buy_miner(c, w); |
| 53 | + if cost > 0 { |
| 54 | + new_miners += 1; |
| 55 | + new_miners_cost += cost as u128; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + drop(wals); |
| 60 | + } |
| 61 | + } |
| 62 | + let t: DateTime<Local> = Local::now(); |
| 63 | + if new_boost > 0 { |
| 64 | + msg.insert( |
| 65 | + 0, |
| 66 | + format!( |
| 67 | + " [{}] Miner 0x{:08x} bought {} boost seconds with {} idlecoin\n", |
| 68 | + t.format("%Y-%m-%d %H:%M:%S"), |
| 69 | + c.miner.miner_id, |
| 70 | + new_boost, |
| 71 | + new_boost_cost, |
| 72 | + ), |
| 73 | + ); |
| 74 | + } |
| 75 | + if new_miners > 0 { |
| 76 | + msg.insert( |
| 77 | + 0, |
| 78 | + format!( |
| 79 | + " [{}] Wallet 0x{:016x} bought new miner license(s) with {} idlecoin\n", |
| 80 | + t.format("%Y-%m-%d %H:%M:%S"), |
| 81 | + c.miner.wallet_id, |
| 82 | + new_miners_cost |
| 83 | + ), |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + drop(cons); |
| 88 | +} |
| 89 | + |
| 90 | +pub fn boost_cost(cps: u64) -> u64 { |
| 91 | + let v = u64::BITS - cps.leading_zeros() - 1; |
| 92 | + 1u64.checked_shl(v).unwrap_or(0) |
| 93 | +} |
| 94 | + |
| 95 | +fn buy_boost(connection: &mut Connection, wallet: &mut Wallet) -> u64 { |
| 96 | + if connection.miner.cps < 1024 { |
| 97 | + connection |
| 98 | + .updates |
| 99 | + .push("You need at least 1024 Cps to be able to purchase boost\n".to_string()); |
| 100 | + return 0; |
| 101 | + } |
| 102 | + let cost = boost_cost(connection.miner.cps); |
| 103 | + if wallet.idlecoin < cost && wallet.supercoin == 0 { |
| 104 | + connection |
| 105 | + .updates |
| 106 | + .push("You do not have the funds to purchase boost\n".to_string()); |
| 107 | + return 0; |
| 108 | + } |
| 109 | + let boost = 128u64; |
| 110 | + miner::sub_idlecoins(wallet, cost); |
| 111 | + connection.miner.boost = connection.miner.boost.saturating_add(boost); |
| 112 | + |
| 113 | + cost |
| 114 | +} |
| 115 | + |
| 116 | +fn buy_miner(connection: &mut Connection, mut wallet: &mut Wallet) -> u64 { |
| 117 | + if wallet.max_miners >= 10 { |
| 118 | + connection |
| 119 | + .updates |
| 120 | + .push("You cannot purchase any more miners\n".to_string()); |
| 121 | + return 0; |
| 122 | + } |
| 123 | + |
| 124 | + let cost = u64::MAX / (100000 >> (wallet.max_miners - 5)); |
| 125 | + if wallet.idlecoin > cost || wallet.supercoin > 0 { |
| 126 | + miner::sub_idlecoins(wallet, cost); |
| 127 | + wallet.max_miners += 1; |
| 128 | + } |
| 129 | + |
| 130 | + cost |
| 131 | +} |
0 commit comments