Skip to content

Commit 37100f1

Browse files
authored
Merge pull request #72 from genonullfree/feature/colors
Feature/colors
2 parents a62bae5 + 9de235f commit 37100f1

File tree

7 files changed

+122
-75
lines changed

7 files changed

+122
-75
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.4.2"
3+
version = "0.4.3"
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: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,30 +112,30 @@ pub fn read_inputs(
112112
PurchaseType::Boost => msg.insert(
113113
0,
114114
format!(
115-
" [{}] Miner 0x{:08x} bought {} boost seconds with {} idlecoin\n",
116-
t.format("%Y-%m-%d %H:%M:%S"),
117-
c.miner.miner_id,
118-
p.bought,
119-
p.cost,
115+
" [{}] Miner {}0x{:08x}{} bought {} boost seconds with {}{}{} idlecoin\n",
116+
t.format("%Y-%m-%d %H:%M:%S"), BLUE,
117+
c.miner.miner_id, RST,
118+
p.bought, YELLOW,
119+
p.cost,RST,
120120
),
121121
),
122122
PurchaseType::Miner => msg.insert(
123123
0,
124124
format!(
125-
" [{}] Wallet 0x{:016x} bought {} new miner license(s) with {} idlecoin\n",
126-
t.format("%Y-%m-%d %H:%M:%S"),
127-
c.miner.wallet_id,
128-
p.bought,
129-
p.cost,
125+
" [{}] Wallet {}0x{:016x}{} bought {} new miner license(s) with {}{}{} idlecoin\n",
126+
t.format("%Y-%m-%d %H:%M:%S"), BLUE,
127+
c.miner.wallet_id, RST,
128+
p.bought,YELLOW,
129+
p.cost,RST,
130130
),
131131
),
132132
PurchaseType::Chrono => msg.insert(
133133
0,
134-
format!(" [{}] Miner 0x{:08x} travelled {} hours forward in time with {} chronocoins\n",
135-
t.format("%Y-%m-%d %H:%M:%S"),
136-
c.miner.miner_id,
137-
p.bought,
138-
p.cost,
134+
format!(" [{}] Miner {}0x{:08x}{} travelled {} hours forward in time with {}{}{} chronocoins\n",
135+
t.format("%Y-%m-%d %H:%M:%S"),BLUE,
136+
c.miner.miner_id,RST,
137+
p.bought,YELLOW,
138+
p.cost,RST,
139139
)),
140140
}
141141
}

src/main.rs

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ use xxhash_rust::xxh3;
2121
mod commands;
2222
mod file;
2323
mod miner;
24+
mod utils;
2425
mod wallet;
2526

2627
use crate::miner::*;
2728
use crate::wallet::*;
2829

2930
const VERSION: &str = env!("CARGO_PKG_VERSION");
30-
const CLR: &str = "\x1b[2J\x1b[;H";
3131
const PORT: u16 = 7654;
3232
const SAVE: &str = ".idlecoin";
3333
const AUTOSAVE: usize = 300;
@@ -50,6 +50,15 @@ Source: https://github.com/genonullfree/idlecoin
5050
Please enter your username: ";
5151
const TURDS: [u64; 1] = [0xe492ba332d614d88];
5252

53+
const CLR: &str = "\x1b[2J\x1b[;H";
54+
const RST: &str = "\x1b[0m";
55+
const RED: &str = "\x1b[1;31m";
56+
const GREEN: &str = "\x1b[1;32m";
57+
const YELLOW: &str = "\x1b[1;33m";
58+
const BLUE: &str = "\x1b[1;34m";
59+
const PURPLE: &str = "\x1b[1;35m";
60+
const CYAN: &str = "\x1b[1;36m";
61+
5362
#[derive(Debug)]
5463
pub struct Connection {
5564
miner: Miner, // Miner for connection
@@ -92,7 +101,7 @@ fn main() -> Result<(), Error> {
92101
let mut cons = conns_exit.lock().unwrap();
93102
for c in cons.iter_mut() {
94103
// Send out the shutdown message to all connected miners
95-
if c.stream.write_all(format!("{}{}v{}\n\n[!] The Idlecoin server is shutting down.\nTimestamp: {}\nMessage: {}", CLR, IDLECOIN, VERSION, t, input).as_bytes()).is_ok() {}
104+
if c.stream.write_all(format!("{}{}{}{}v{}\n\n{}[!] The Idlecoin server is shutting down.{}\nMessage: {}{}{}Timestamp: {}\n", CLR, YELLOW, IDLECOIN, RST, VERSION, RED, RST, RED, input,RST, t).as_bytes()).is_ok() {}
96105
}
97106

98107
// Save the current stats file
@@ -125,6 +134,8 @@ fn main() -> Result<(), Error> {
125134
Ok(_) => (),
126135
Err(e) => println!("Failed to send: {e}"),
127136
};
137+
} else {
138+
println!("Error in login: {} from {:?}", e, s);
128139
}
129140
continue;
130141
}
@@ -140,8 +151,8 @@ fn main() -> Result<(), Error> {
140151
};
141152

142153
let updates = vec![format!(
143-
"\nLogged in as Wallet: 0x{:016x} Miner: 0x{:08x}\n",
144-
miner.wallet_id, miner.miner_id
154+
"\nLogged in as Wallet: {}0x{:016x}{} Miner: {}0x{:08x}{}\n",
155+
PURPLE, miner.wallet_id, RST, BLUE, miner.miner_id, RST
145156
)
146157
.to_owned()];
147158
let conn = Connection {
@@ -202,15 +213,27 @@ fn login(
202213
connections: &Arc<Mutex<Vec<Connection>>>,
203214
) -> Result<Miner, Error> {
204215
// Request userid
205-
let msg = format!("{}Welcome to{}v{}\n\n{}", CLR, IDLECOIN, VERSION, BANNER);
206-
stream.write_all(msg.as_bytes())?;
216+
let msg = format!(
217+
"{}Welcome to{}{}{}v{}\n\n{}",
218+
CLR, YELLOW, IDLECOIN, RST, VERSION, BANNER
219+
);
220+
if stream.write_all(msg.as_bytes()).is_err() {
221+
return Err(Error::new(ErrorKind::ConnectionReset, "No write-back"));
222+
};
207223

208224
// Read userid
209225
let mut id_raw: [u8; 1024] = [0; 1024];
210226

211227
// Only read 0-1023 to have the end NULL so we can safely do the
212228
// \r\n => \n\0 conversion
213-
let len = stream.read(&mut id_raw[..1023])?;
229+
let len = match stream.read(&mut id_raw[..1023]) {
230+
Ok(l) => l,
231+
Err(e) => return Err(e),
232+
};
233+
if len == 0 {
234+
return Err(Error::new(ErrorKind::ConnectionReset, "Nothing read"));
235+
}
236+
214237
for i in 0..len {
215238
if id_raw[i] == b'\r' && id_raw[i + 1] == b'\n' {
216239
id_raw[i] = b'\n';
@@ -228,7 +251,10 @@ fn login(
228251
if *t == wallet_id {
229252
return Err(Error::new(
230253
ErrorKind::ConnectionRefused,
231-
format!("Wallet 0x{:016x}: Don't be a turd", wallet_id),
254+
format!(
255+
"Wallet {}0x{:016x}{}: Don't be a turd",
256+
PURPLE, wallet_id, RST
257+
),
232258
));
233259
}
234260
}
@@ -258,8 +284,8 @@ fn login(
258284
}
259285
if num >= max_miners {
260286
let msg = format!(
261-
"Connection refused: Too many miners connected for user 0x{:016x} (max: {})",
262-
wallet_id, max_miners,
287+
"{}Connection refused{}: Too many miners connected for user {}0x{:016x}{} (max: {})",
288+
RED, RST, PURPLE, wallet_id, RST, max_miners,
263289
);
264290
println!("{}", msg);
265291
drop(cons);
@@ -340,7 +366,7 @@ fn print_wallets(
340366
connections: &Arc<Mutex<Vec<Connection>>>,
341367
wallets: &Arc<Mutex<Vec<Wallet>>>,
342368
) -> String {
343-
let mut msg = format!("{}{}v{}\n\n", CLR, IDLECOIN, VERSION);
369+
let mut msg = format!("{}{}{}{}v{}\n\n", CLR, YELLOW, IDLECOIN, RST, VERSION);
344370
let mut gens = wallets.lock().unwrap().deref().clone();
345371
let mut cons = connections.lock().unwrap();
346372

@@ -380,16 +406,7 @@ fn print_wallets(
380406
}
381407

382408
// Build miner display
383-
miner_line.push(
384-
format!(
385-
"[M:0x{:0>8x} Cps:{} B:{} L:{:<2}] ",
386-
c.miner.miner_id,
387-
disp_units(c.miner.cps),
388-
disp_units(c.miner.boost),
389-
c.miner.level
390-
)
391-
.to_owned(),
392-
);
409+
miner_line.push(c.miner.print());
393410
total_cps += c.miner.cps as u128;
394411
num += 1;
395412
if num > 3 {
@@ -410,22 +427,21 @@ fn print_wallets(
410427
}
411428

412429
let wal = &format!(
413-
"[{:03}/{:03}] Wallet 0x{:016x} Miner Licenses: {} Chronocoin: {} Randocoin: {} Coins: {}:{} Total Cps: {}\n",
430+
"[{}{:03}/{:03}{}] {} Total Cps: {}{}{}\n",
431+
CYAN,
414432
gens.len() - i,
415433
gens.len(),
416-
g.id,
417-
g.max_miners,
418-
g.chronocoin,
419-
g.randocoin,
420-
g.supercoin,
421-
g.idlecoin,
434+
RST,
435+
g.print(),
436+
GREEN,
422437
total_cps,
438+
RST,
423439
)
424440
.to_owned();
425441

426442
if !min.is_empty() {
427443
msg += wal;
428-
msg += " [*] Miners:\n";
444+
msg += &format!(" [{}*{}] Miners:\n", BLUE, RST);
429445
msg += &min;
430446
msg += "\n";
431447
}
@@ -436,27 +452,6 @@ fn print_wallets(
436452
msg
437453
}
438454

439-
fn disp_units(num: u64) -> String {
440-
let unit = [' ', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
441-
let mut value = num as f64;
442-
443-
let mut count = 0;
444-
loop {
445-
if (value / 1000.0) > 1.0 {
446-
count += 1;
447-
value /= 1000.0;
448-
} else {
449-
break;
450-
}
451-
if count == unit.len() - 1 {
452-
break;
453-
}
454-
}
455-
456-
let n = if count > 0 { 1 } else { 0 };
457-
format!("{:.*}{:>1}", n, value, unit[count])
458-
}
459-
460455
fn format_msg(input: &mut String, actions: &mut Vec<String>) {
461456
if actions.is_empty() {
462457
return;

src/miner.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ pub fn action_miners(
3737
msg.insert(
3838
0,
3939
format!(
40-
" [{}] Wallet 0x{:016x} was taxed 10% by the IRS!\n",
40+
" [{}] Wallet {}0x{:016x}{} was taxed 10% by the IRS!\n",
4141
t.format("%Y-%m-%d %H:%M:%S"),
42-
c.miner.miner_id
42+
BLUE,
43+
c.miner.miner_id,
44+
RST,
4345
),
4446
);
4547
}
@@ -53,9 +55,11 @@ pub fn action_miners(
5355
msg.insert(
5456
0,
5557
format!(
56-
" [{}] Miner 0x{:08x} lost a level\n",
58+
" [{}] Miner {}0x{:08x}{} lost a level\n",
5759
t.format("%Y-%m-%d %H:%M:%S"),
58-
c.miner.miner_id
60+
BLUE,
61+
c.miner.miner_id,
62+
RST,
5963
),
6064
);
6165
}
@@ -67,9 +71,11 @@ pub fn action_miners(
6771
msg.insert(
6872
0,
6973
format!(
70-
" [{}] Miner 0x{:08x} leveled up\n",
74+
" [{}] Miner {}0x{:08x}{} leveled up\n",
7175
t.format("%Y-%m-%d %H:%M:%S"),
72-
c.miner.miner_id
76+
BLUE,
77+
c.miner.miner_id,
78+
RST,
7379
),
7480
);
7581
};
@@ -79,9 +85,11 @@ pub fn action_miners(
7985
msg.insert(
8086
0,
8187
format!(
82-
" [{}] Miner 0x{:08x} gained 10% CPS boost\n",
88+
" [{}] Miner {}0x{:08x}{} gained 10% CPS boost\n",
8389
t.format("%Y-%m-%d %H:%M:%S"),
84-
c.miner.miner_id
90+
BLUE,
91+
c.miner.miner_id,
92+
RST,
8593
),
8694
);
8795
}
@@ -135,6 +143,20 @@ impl Miner {
135143
self.inc = self.inc.saturating_sub(self.level);
136144
self.pow = self.pow.saturating_div(10);
137145
}
146+
147+
pub fn print(&self) -> String {
148+
format!(
149+
"[M:{}0x{:0>8x}{} Cps:{}{}{} B:{} L:{:<2}] ",
150+
BLUE,
151+
self.miner_id,
152+
RST,
153+
GREEN,
154+
utils::disp_units(self.cps),
155+
RST,
156+
utils::disp_units(self.boost),
157+
self.level,
158+
)
159+
}
138160
}
139161

140162
pub fn miner_session(mut miner: &mut Miner) {

src/utils.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
pub fn disp_units(num: u64) -> String {
2+
let unit = [' ', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
3+
let mut value = num as f64;
4+
5+
let mut count = 0;
6+
loop {
7+
if (value / 1000.0) > 1.0 {
8+
count += 1;
9+
value /= 1000.0;
10+
} else {
11+
break;
12+
}
13+
if count == unit.len() - 1 {
14+
break;
15+
}
16+
}
17+
18+
let n = if count > 0 { 1 } else { 0 };
19+
format!("{:.*}{:>1}", n, value, unit[count])
20+
}

src/wallet.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,14 @@ impl Wallet {
8686
};
8787
Ok(())
8888
}
89+
90+
pub fn print(&self) -> String {
91+
format!("Wallet {}0x{:016x}{} Miner Licenses: {}{}{} Chronocoin: {}{}{} Randocoin: {}{}{} Coins: {}{}:{}{}",
92+
PURPLE, self.id, RST,
93+
BLUE, self.max_miners, RST,
94+
YELLOW, self.chronocoin, RST,
95+
YELLOW, self.randocoin, RST,
96+
YELLOW, self.supercoin, self.idlecoin, RST,
97+
)
98+
}
8999
}

0 commit comments

Comments
 (0)