Skip to content

Commit 3f05a84

Browse files
authored
Merge pull request #56 from genonullfree/fix/miner-display
Fix/miner display
2 parents d29fe8d + 28490d0 commit 3f05a84

File tree

6 files changed

+454
-327
lines changed

6 files changed

+454
-327
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "idlecoin"
3-
version = "0.3.4"
3+
version = "0.3.5"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/commands.rs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}

src/file.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use crate::*;
2+
3+
pub fn load_stats(wallets: &Arc<Mutex<Vec<Wallet>>>) -> Result<(), Error> {
4+
let mut j = String::new();
5+
6+
// Attempt to open and read the saved stats file
7+
let mut file = match File::open(&SAVE) {
8+
Ok(f) => f,
9+
Err(_) => {
10+
println!("No stats file found.");
11+
return Ok(());
12+
}
13+
};
14+
15+
file.read_to_string(&mut j)?;
16+
17+
// Exit if file is empty
18+
if j.is_empty() {
19+
return Err(Error::new(ErrorKind::InvalidData, "No data to load"));
20+
}
21+
22+
// Attempt to deserialize the json file data
23+
println!("Loading stats...");
24+
if let Ok(mut wallet) = serde_json::from_str(&j) {
25+
// Update the wallets struct
26+
let mut gens = wallets.lock().unwrap();
27+
gens.append(&mut wallet);
28+
drop(gens);
29+
println!("Successfully loaded stats file {}", SAVE);
30+
} else {
31+
return Err(Error::new(
32+
ErrorKind::InvalidData,
33+
format!("Failed to load {}", SAVE),
34+
));
35+
}
36+
37+
Ok(())
38+
}
39+
40+
pub fn save_stats(wallets: Arc<Mutex<Vec<Wallet>>>) {
41+
// Serialize the stats data to json
42+
println!("Saving stats...");
43+
let gens = wallets.lock().unwrap();
44+
let j = serde_json::to_string_pretty(&gens.deref()).unwrap();
45+
drop(gens);
46+
47+
// Open the stats file for writing
48+
let mut file = match File::create(&SAVE) {
49+
Ok(f) => f,
50+
Err(_) => {
51+
println!("Error opening {} for writing!", SAVE);
52+
return;
53+
}
54+
};
55+
56+
// Write out the json stats data
57+
let len = file.write(j.as_bytes()).unwrap();
58+
if j.len() != len {
59+
println!("Error writing save data to {}", SAVE);
60+
return;
61+
}
62+
63+
println!("Successfully saved data to {}", SAVE);
64+
}

0 commit comments

Comments
 (0)