From 828f0d83dcb258a8e5efd55a7775592c6e5f77bc Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 28 Jun 2018 04:00:48 -0500 Subject: -moved over it if let from bad matches --- src/client/dashboard.rs | 11 ++++------- src/client/engines.rs | 19 ++++++++----------- src/client/mining.rs | 19 ++++++++----------- src/client/navigation.rs | 29 +++++++++++++---------------- src/client/refinery.rs | 19 ++++++++----------- src/modules/construction.rs | 13 +++++-------- src/modules/mining.rs | 11 ++++------- src/modules/navigation.rs | 29 +++++++++-------------------- src/modules/refinery.rs | 11 ++++------- src/server/construction.rs | 11 +++++------ src/server/engines.rs | 19 ++++++++----------- src/server/mining.rs | 31 +++++++++++-------------------- src/server/navigation.rs | 18 +++++++++--------- src/server/refinery.rs | 10 ++++------ 14 files changed, 100 insertions(+), 150 deletions(-) diff --git a/src/client/dashboard.rs b/src/client/dashboard.rs index cbc55ba..5815ee2 100644 --- a/src/client/dashboard.rs +++ b/src/client/dashboard.rs @@ -23,14 +23,11 @@ pub fn client_dashboard(mut buff_r : BufReader) { termion::cursor::Goto(1,1), ship).unwrap(); - match stdin.next() { - Some(c) => { - let c = c.unwrap() as char; - if c == 'q' { - break; - } + if let Some(c) = stdin.next() { + let c = c.unwrap() as char; + if c == 'q' { + break; } - None => (), } stdout.flush().unwrap(); } diff --git a/src/client/engines.rs b/src/client/engines.rs index 6c6b384..9b2d314 100644 --- a/src/client/engines.rs +++ b/src/client/engines.rs @@ -30,18 +30,15 @@ pub fn client_engines(mut stream : TcpStream, mut buff_r : BufReader) write!(stdout, "{}t : accelerate torwards target", termion::cursor::Goto(1, 7)).unwrap(); } - match stdin.next() { - Some(c) => { - let c = c.unwrap(); - let mut send = String::new(); - send.push(c as char); - if send.as_bytes() == b"q" { - break; - } - send.push_str("\n"); - stream.write(send.as_bytes()).unwrap(); + if let Some(c) = stdin.next() { + let c = c.unwrap(); + let mut send = String::new(); + send.push(c as char); + if send.as_bytes() == b"q" { + break; } - None => () + send.push_str("\n"); + stream.write(send.as_bytes()).unwrap(); } stdout.flush().unwrap(); diff --git a/src/client/mining.rs b/src/client/mining.rs index 80cd611..221f7ce 100644 --- a/src/client/mining.rs +++ b/src/client/mining.rs @@ -38,18 +38,15 @@ pub fn client_mining(mut stream : TcpStream, mut buff_r : BufReader) false => write!(stdout, "{}Ship has no astroid targeted.", clear).unwrap(), } - match stdin.next() { - Some(c) => { - let c = c.unwrap(); - let mut send = String::new(); - send.push(c as char); - if send.as_bytes() == b"q" { - break; - } - send.push_str("\n"); - stream.write(send.as_bytes()).unwrap(); + if let Some(c) = stdin.next() { + let c = c.unwrap(); + let mut send = String::new(); + send.push(c as char); + if send.as_bytes() == b"q" { + break; } - None => () + send.push_str("\n"); + stream.write(send.as_bytes()).unwrap(); } stdout.flush().unwrap(); diff --git a/src/client/navigation.rs b/src/client/navigation.rs index 9bc13f9..34e1b7a 100644 --- a/src/client/navigation.rs +++ b/src/client/navigation.rs @@ -44,23 +44,20 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf ).unwrap(); } - match stdin.next() { - Some(c) => { - let c = c.unwrap() as char; - if c == 'q' { - break; + if let Some(c) = stdin.next() { + let c = c.unwrap() as char; + if c == 'q' { + break; + } + else { + let i = c.to_digit(10).unwrap() as usize; + if i < within_range.len() { + let mut send = String::new(); + send.push_str(within_range.iter().nth(i).unwrap().0); + send.push_str("\n"); + stream.write(send.as_bytes()).unwrap(); } - else { - let i = c.to_digit(10).unwrap() as usize; - if i < within_range.len() { - let mut send = String::new(); - send.push_str(within_range.iter().nth(i).unwrap().0); - send.push_str("\n"); - stream.write(send.as_bytes()).unwrap(); - } - } - }, - None => (), + } } } stdout.flush().unwrap(); diff --git a/src/client/refinery.rs b/src/client/refinery.rs index 5f126bf..013172d 100644 --- a/src/client/refinery.rs +++ b/src/client/refinery.rs @@ -32,18 +32,15 @@ pub fn client_refinery(mut stream : TcpStream, mut buff_r : BufReader false => write!(stdout, "{}You have no refinable minerals.", clear).unwrap(), } - match stdin.next() { - Some(c) => { - let c = c.unwrap(); - let mut send = String::new(); - send.push(c as char); - if send.as_bytes() == b"q" { - break; - } - send.push_str("\n"); - stream.write(send.as_bytes()).unwrap(); + if let Some(c) = stdin.next() { + let c = c.unwrap(); + let mut send = String::new(); + send.push(c as char); + if send.as_bytes() == b"q" { + break; } - None => () + send.push_str("\n"); + stream.write(send.as_bytes()).unwrap(); } stdout.flush().unwrap(); diff --git a/src/modules/construction.rs b/src/modules/construction.rs index 22086a5..46ddbb1 100644 --- a/src/modules/construction.rs +++ b/src/modules/construction.rs @@ -27,14 +27,11 @@ impl Construction { } pub fn process(&mut self) { - match self.start.clone() { - Some(timer) => { - if timer.elapsed().unwrap().as_secs() > self.time { - self.start = Some(SystemTime::now()); - self.status = ConstructionStatus::Constructed; - } + if let Some(timer) = self.start.clone() { + if timer.elapsed().unwrap().as_secs() > self.time { + self.start = Some(SystemTime::now()); + self.status = ConstructionStatus::Constructed; } - _ => (), } } @@ -56,6 +53,6 @@ impl Construction { } pub fn take(&mut self) { - self.status = ConstructionStatus::None; + self.off() } } diff --git a/src/modules/mining.rs b/src/modules/mining.rs index 7a6461a..3c091b9 100644 --- a/src/modules/mining.rs +++ b/src/modules/mining.rs @@ -26,14 +26,11 @@ impl Mining { } pub fn process(&mut self) { - match self.start.clone() { - Some(timer) => { - if timer.elapsed().unwrap().as_secs() > self.time { - self.status = MiningStatus::Mined; - self.start = Some(SystemTime::now()); - } + if let Some(timer) = self.start.clone() { + if timer.elapsed().unwrap().as_secs() > self.time { + self.status = MiningStatus::Mined; + self.start = Some(SystemTime::now()); } - _ => (), } } diff --git a/src/modules/navigation.rs b/src/modules/navigation.rs index c4b3224..84f315c 100644 --- a/src/modules/navigation.rs +++ b/src/modules/navigation.rs @@ -32,19 +32,11 @@ impl Navigation { } pub fn process(&mut self) { - match self.start.clone() { - Some(timer) => { - if timer.elapsed().unwrap().as_secs() > self.time { - self.status = NavigationStatus::Targeted; - self.start = None; - } + if let Some(timer) = self.start.clone() { + if timer.elapsed().unwrap().as_secs() > self.time { + self.status = NavigationStatus::Targeted; + self.start = None; } - _ => (), - } - - match self.target_name { - None => self.status = NavigationStatus::None, - _ => (), } } @@ -55,15 +47,12 @@ impl Navigation { } pub fn verify_target(&mut self, ship_position : (f64, f64, f64), masses : &HashMap) { - match self.target_name.clone() { - Some(name) => { - let target = masses.get(&name).unwrap(); - if distance(target.position, ship_position) > self.range { - self.target_name = None; - self.status = NavigationStatus::None; - } + if let Some(name) = self.target_name.clone() { + let target = masses.get(&name).unwrap(); + if distance(target.position, ship_position) > self.range { + self.target_name = None; + self.status = NavigationStatus::None; } - _ => (), } } } diff --git a/src/modules/refinery.rs b/src/modules/refinery.rs index cdb5de0..5100f3d 100644 --- a/src/modules/refinery.rs +++ b/src/modules/refinery.rs @@ -24,14 +24,11 @@ impl Refinery { } pub fn process(&mut self) { - match self.start.clone() { - Some(timer) => { - if timer.elapsed().unwrap().as_secs() > self.time { - self.status = RefineryStatus::Refined; - self.start = Some(SystemTime::now()); - } + if let Some(timer) = self.start.clone() { + if timer.elapsed().unwrap().as_secs() > self.time { + self.status = RefineryStatus::Refined; + self.start = Some(SystemTime::now()); } - _ => (), } } diff --git a/src/server/construction.rs b/src/server/construction.rs index 0eb2179..94a6b0f 100644 --- a/src/server/construction.rs +++ b/src/server/construction.rs @@ -32,6 +32,7 @@ impl ServerConnection { } if construction_data.status == ConstructionStatus::Constructed { + println!("inserted"); construction.take(); masses.insert("Station".to_string(), Mass::new_station(ModuleType::Refinery, ship_clone.position, ship_clone.velocity)); } @@ -42,14 +43,13 @@ impl ServerConnection { fn txrx_construction(&mut self, construction_data : &ConstructionData) -> bool { let send = serde_json::to_string(construction_data).unwrap() + "\n"; - match self.stream.write(send.as_bytes()) { - Err(_error) => self.open = false, - _ => (), + if let Err(_err) = self.stream.write(send.as_bytes()) { + self.open = false; } let mut recv = String::new(); - match self.buff_r.read_line(&mut recv) { - Ok(result) => match recv.as_bytes() { + if let Ok(result) = self.buff_r.read_line(&mut recv) { + match recv.as_bytes() { b"c\n" => { if construction_data.has_refined { return true @@ -61,7 +61,6 @@ impl ServerConnection { } }, } - _ => (), } false diff --git a/src/server/engines.rs b/src/server/engines.rs index 2c96920..c0d4ad5 100644 --- a/src/server/engines.rs +++ b/src/server/engines.rs @@ -20,24 +20,21 @@ impl ServerConnection { let targeted = navigation.status == NavigationStatus::Targeted; let send = serde_json::to_string(&targeted).unwrap() + "\n"; - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => self.open = false, + if let Err(_err) = self.stream.write(send.as_bytes()) { + self.open = false; } let target = match navigation.target_name { Some(name) => masses.get(&name), None => None, }; + let mut recv = String::new(); - match self.buff_r.read_line(&mut recv) { - Ok(result) => { - engines.give_client_data(&ship_clone, target, recv); - if result == 0 { - self.open = false; - } - }, - Err(_error) => (), + if let Ok(result) = self.buff_r.read_line(&mut recv) { + engines.give_client_data(&ship_clone, target, recv); + if result == 0 { + self.open = false; + } } } diff --git a/src/server/mining.rs b/src/server/mining.rs index 115c034..bfe9223 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -42,26 +42,19 @@ impl ServerConnection { else { if mining.status == MiningStatus::Mined { mining.take(); - match navigation.target_name.clone() { - Some(name) => { - let target = masses.get_mut(&name).unwrap(); - item = target.take("Mineral"); - } - _ => (), + if let Some(name) = navigation.target_name.clone() { + let target = masses.get_mut(&name).unwrap(); + item = target.take("Mineral"); } } } } - match item { - Some(item) => match ship.give(item.clone()) { - false => { - let mass = Mass::new_item(item.clone(), ship.position, ship.velocity); - masses.insert(item.name.clone(), mass); - } - true => (), + if let Some(item) = item { + if !ship.give(item.clone()) { + let mass = Mass::new_item(item.clone(), ship.position, ship.velocity); + masses.insert(item.name.clone(), mass); } - None => (), } masses.insert(self.name.clone(), ship); @@ -69,14 +62,13 @@ impl ServerConnection { fn txrx_mining(&mut self, mining_data : &MiningData) -> bool { let send = serde_json::to_string(mining_data).unwrap() + "\n"; - match self.stream.write(send.as_bytes()) { - Err(_error) => self.open = false, - _ => (), + if let Err(_err) = self.stream.write(send.as_bytes()) { + self.open = false; } let mut recv = String::new(); - match self.buff_r.read_line(&mut recv) { - Ok(result) => match recv.as_bytes() { + if let Ok(result) = self.buff_r.read_line(&mut recv) { + match recv.as_bytes() { b"F\n" => { if mining_data.is_within_range { return true; @@ -88,7 +80,6 @@ impl ServerConnection { } }, } - _ => (), } false } diff --git a/src/server/navigation.rs b/src/server/navigation.rs index 461a2b8..3eaaa8d 100644 --- a/src/server/navigation.rs +++ b/src/server/navigation.rs @@ -24,18 +24,18 @@ impl ServerConnection { if self.open { let send = serde_json::to_string(&within_range).unwrap() + "\n"; - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => self.open = false, + if let Err(_err) = self.stream.write(send.as_bytes()) { + self.open = false; }; let mut recv = String::new(); - match self.buff_r.read_line(&mut recv) { - Ok(_result) => (), - Err(_error) => (), - } - if !recv.is_empty() { - navigation.give_target(recv.replace("\n", "")); + if let Ok(result) = self.buff_r.read_line(&mut recv) { + if result == 0 { + self.open = false; + } + if !recv.is_empty() { + navigation.give_target(recv.replace("\n", "")); + } } } } diff --git a/src/server/refinery.rs b/src/server/refinery.rs index 1f7628f..eb82bb2 100644 --- a/src/server/refinery.rs +++ b/src/server/refinery.rs @@ -55,14 +55,13 @@ impl ServerConnection { fn txrx_refinery(&mut self, refinery_data : &RefineryData) -> bool { let send = serde_json::to_string(refinery_data).unwrap() + "\n"; - match self.stream.write(send.as_bytes()) { - Err(_error) => self.open = false, - _ => (), + if let Err(_err) = self.stream.write(send.as_bytes()) { + self.open = false; } let mut recv = String::new(); - match self.buff_r.read_line(&mut recv) { - Ok(result) => match recv.as_bytes() { + if let Ok(result) = self.buff_r.read_line(&mut recv) { + match recv.as_bytes() { b"R\n" => { if refinery_data.has_minerals { return true @@ -74,7 +73,6 @@ impl ServerConnection { } }, } - _ => (), } false -- cgit v1.2.3