@@ -21,13 +21,13 @@ use xxhash_rust::xxh3;
2121mod commands;
2222mod file;
2323mod miner;
24+ mod utils;
2425mod wallet;
2526
2627use crate :: miner:: * ;
2728use crate :: wallet:: * ;
2829
2930const VERSION : & str = env ! ( "CARGO_PKG_VERSION" ) ;
30- const CLR : & str = "\x1b [2J\x1b [;H" ;
3131const PORT : u16 = 7654 ;
3232const SAVE : & str = ".idlecoin" ;
3333const AUTOSAVE : usize = 300 ;
@@ -50,6 +50,15 @@ Source: https://github.com/genonullfree/idlecoin
5050Please enter your username: " ;
5151const 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 ) ]
5463pub 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.\n Timestamp : {}\n Message : {}" , 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.{} \n Message : {}{}{}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- "\n Logged in as Wallet: 0x{:016x} Miner: 0x{:08x}\n " ,
144- miner. wallet_id, miner. miner_id
154+ "\n Logged 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-
460455fn format_msg ( input : & mut String , actions : & mut Vec < String > ) {
461456 if actions. is_empty ( ) {
462457 return ;
0 commit comments