Skip to content

Commit 85d12b2

Browse files
author
VictoremWinbringer
committed
clear code
1 parent 33ca997 commit 85d12b2

File tree

10 files changed

+49
-58
lines changed

10 files changed

+49
-58
lines changed

examples/ping_pong_client.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ fn main() {
1111
if timer.elapsed() > period {
1212
timer = Instant::now();
1313
id += 1;
14-
let _ = client
15-
.send(format!("Ping {}", id).into_bytes());
14+
let _ = client.send(format!("Ping {}", id).into_bytes());
1615
}
1716
let _ = client
1817
.recv()

examples/ping_pong_server.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ impl victorem::Game for PingPongGame {
3232
}
3333

3434
fn main() {
35-
let mut server = victorem::GameServer::new(
36-
PingPongGame { id: 0 },
37-
"2222",
38-
).unwrap();
35+
let mut server = victorem::GameServer::new(PingPongGame { id: 0 }, "2222").unwrap();
3936
server.run();
4037
}

src/business_logic_layer/id.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ pub struct Generator {
3131
}
3232

3333
impl Generator {
34-
pub fn new(start: u32) -> Generator { Generator { id: start } }
34+
pub fn new(start: u32) -> Generator {
35+
Generator { id: start }
36+
}
3537
pub fn generate(&mut self) -> u32 {
3638
let result = self.id;
3739
self.id += 1;
@@ -44,11 +46,12 @@ pub struct Filter {
4446
}
4547

4648
impl Filter {
47-
pub fn new(start: u32) -> Filter { Filter { id: start } }
49+
pub fn new(start: u32) -> Filter {
50+
Filter { id: start }
51+
}
4852

49-
pub fn is_valid_last_recv_id(&mut self, data: &impl IWithId) -> Result<(), Exception> {
50-
if data.get() > self.id
51-
|| self.id - data.get() > MAX_ID_BREAK {
53+
pub fn filter(&mut self, data: &impl IWithId) -> Result<(), Exception> {
54+
if data.get() > self.id || self.id - data.get() > MAX_ID_BREAK {
5255
self.id = data.get();
5356
Ok(())
5457
} else {
@@ -63,7 +66,6 @@ pub struct Arranger<T: IWithId> {
6366
received: Vec<u32>,
6467
}
6568

66-
6769
const MAX_ID_BREAK: u32 = 64;
6870
const MAX_SAVED: usize = (MAX_ID_BREAK * 2) as usize;
6971
const MAX_RECEIVED: usize = MAX_SAVED;

src/business_logic_layer/key.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::entities::{CommandPacket, Exception, StatePacket};
2-
use std::time::{Duration, SystemTimeError, SystemTime, UNIX_EPOCH};
1+
use crate::entities::{CommandPacket, StatePacket};
2+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
33

44
pub trait IWithKey {
55
fn get(&self) -> Duration;
@@ -27,27 +27,28 @@ impl IWithKey for CommandPacket {
2727
}
2828

2929
pub fn new_key() -> Duration {
30-
match SystemTime::now()
31-
.duration_since(UNIX_EPOCH) {
30+
match SystemTime::now().duration_since(UNIX_EPOCH) {
3231
Ok(d) => d,
3332
Err(e) => e.duration(),
3433
}
3534
}
3635

3736
pub struct Generator {
38-
key: Duration
37+
key: Duration,
3938
}
4039

4140
impl Generator {
4241
pub fn new() -> Generator {
4342
Generator { key: new_key() }
4443
}
4544

46-
pub fn generate(&self) -> Duration { self.key }
45+
pub fn generate(&self) -> Duration {
46+
self.key
47+
}
4748
}
4849

4950
pub struct Filter {
50-
key: Duration
51+
key: Duration,
5152
}
5253

5354
impl Filter {

src/business_logic_layer/mod.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
use std::collections::HashMap;
2-
use std::thread;
3-
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH, SystemTimeError};
4-
51
mod version;
62

73
mod protocol;
@@ -12,13 +8,13 @@ pub mod timer;
128

139
mod key;
1410

15-
use crate::data_access_layer::Cache;
16-
use crate::entities::{CommandPacket, Exception, StatePacket};
17-
use self::id::{Filter, Generator, Arranger};
18-
use self::version::VersionChecker;
11+
use self::id::{Arranger, Filter, Generator};
12+
use self::key as k;
1913
use self::protocol::ProtocolChecker;
2014
use self::timer::SleepTimer;
21-
use self::key as k;
15+
use self::version::VersionChecker;
16+
use crate::data_access_layer::Cache;
17+
use crate::entities::{CommandPacket, Exception, StatePacket};
2218

2319
pub struct Client {
2420
protocol_version: VersionChecker,
@@ -48,8 +44,8 @@ impl Client {
4844

4945
fn create_command(&mut self, command: Vec<u8>) -> CommandPacket {
5046
CommandPacket {
51-
protocol_id: self.protocol_id.get(),
52-
protocol_version:self.protocol_version.get(),
47+
protocol_id: self.protocol_id.get(),
48+
protocol_version: self.protocol_version.get(),
5349
id: self.id.generate(),
5450
command,
5551
session_key: self.key_generator.generate(),
@@ -70,7 +66,7 @@ impl Client {
7066
self.key_filter = k::Filter::new(state.session_key);
7167
self.id_filter = Filter::new(0);
7268
}
73-
self.id_filter.is_valid_last_recv_id(&state)?;
69+
self.id_filter.filter(&state)?;
7470
let vec = self.cache.get_range(&state.lost_ids);
7571
Ok((state.state, vec))
7672
}
@@ -98,7 +94,6 @@ impl Server {
9894
}
9995
}
10096

101-
10297
pub fn send(&mut self, state: Vec<u8>) -> StatePacket {
10398
StatePacket {
10499
protocol_id: self.protocol_id.get(),

src/business_logic_layer/timer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::entities::{CommandPacket, Exception, StatePacket};
2-
use std::time::{Duration, Instant};
31
use std::thread;
2+
use std::time::{Duration, Instant};
43

54
pub struct SleepTimer {
65
time: Duration,

src/business_logic_layer/version.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::entities::{CommandPacket, Exception, StatePacket};
22

33
const PROTOCOL_VERSION: u8 = 1;
44

5-
trait IWithVersion {
5+
pub trait IWithVersion {
66
fn get(&self) -> u8;
77
fn set(&mut self, version: u8);
88
}

src/entities.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::error::Error;
33
use std::fmt::Display;
44
use std::fmt::Formatter;
55
use std::io;
6-
use std::time::{Duration, SystemTimeError};
6+
use std::time::Duration;
77

88
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
99
pub struct CommandPacket {

src/lib.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::data_access_layer::{TypedClientSocket, TypedServerSocket};
88
pub use crate::entities::Exception;
99
use std::collections::HashMap;
1010
use std::net::SocketAddr;
11-
use std::time::{Duration, SystemTime, UNIX_EPOCH};
11+
use std::time::Duration;
1212

1313
//TODO: Add Session ID for Client And Server!
1414

@@ -131,19 +131,16 @@ impl ServerSocket {
131131
}
132132

133133
pub fn add(&mut self, client: &SocketAddr) {
134-
use std::net::*;
135134
if !self.servers.contains_key(client) {
136-
self.servers.insert(
137-
client.clone(),
138-
bll::Server::new(),
139-
);
135+
self.servers.insert(client.clone(), bll::Server::new());
140136
}
141137
}
142138

143139
pub fn send_to_all(&mut self, state: Vec<u8>) -> Vec<(SocketAddr, Exception)> {
144140
let mut exceptions = Vec::new();
145141
for (a, s) in &mut self.servers {
146-
let _ = self.socket
142+
let _ = self
143+
.socket
147144
.write(a, &s.send(state.clone()))
148145
.map_err(|e| exceptions.push((*a, e)));
149146
}
@@ -189,8 +186,7 @@ impl<T: Game> GameServer<T> {
189186
let state = self.game.draw(self.draw_elapsed.elapsed());
190187
self.game.add_client().map(|a| self.socket.add(&a));
191188
self.game.remove_client().map(|a| self.socket.remove(&a));
192-
self.is_running = self.is_running
193-
& &self
189+
self.is_running &= self
194190
.socket
195191
.send_to_all(state)
196192
.into_iter()
@@ -203,21 +199,20 @@ impl<T: Game> GameServer<T> {
203199
}
204200

205201
fn update(&mut self) {
206-
let _ = self.socket
202+
let _ = self
203+
.socket
207204
.recv()
208205
.map(|(commands, from)| {
209206
if self.game.allow_connect(&from) {
210-
self.is_running = self.is_running
211-
& &self
212-
.game
213-
.handle_command(self.update.elapsed(), commands, from);
207+
self.is_running &=
208+
self.game
209+
.handle_command(self.update.elapsed(), commands, from);
214210
} else {
215211
self.socket.remove(&from);
216212
}
217213
})
218214
.map_err(|e| {
219-
self.is_running = self.is_running
220-
& &self
215+
self.is_running &= self
221216
.game
222217
.handle_server_event(ServerEvent::ExceptionOnRecv(e))
223218
});

tests/test.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ impl<'a> Game for GameMock<'a> {
7878
fn add_client(&mut self) -> Option<SocketAddr> {
7979
self.data.new_client.clone()
8080
}
81+
fn remove_client(&mut self) -> Option<SocketAddr> {
82+
self.data.disconnect_this_client.clone()
83+
}
8184
}
8285

8386
fn create_server(game: GameMock, port: String) -> Result<GameServer<GameMock>, Exception> {
@@ -114,7 +117,7 @@ fn server_should_stop_if_handle_command_returns_false() -> Result<(), Exception>
114117
let res = ClientSocket::new("1112", "127.0.0.1:3333")
115118
.map(|mut c| {
116119
for _i in 0..1000 {
117-
c.send(vec![1u8, 3u8]);
120+
let _ = c.send(vec![1u8, 3u8]);
118121
}
119122
1
120123
})
@@ -153,7 +156,7 @@ fn server_should_recv_commands_from_client() -> Result<(), Exception> {
153156
let res = ClientSocket::new("1111", "127.0.0.1:3335")
154157
.map(|mut c| {
155158
for _i in 0..1000 {
156-
c.send(vec![1u8, 3u8]);
159+
let _ = c.send(vec![1u8, 3u8]);
157160
}
158161
1
159162
})
@@ -237,9 +240,9 @@ struct Calculator<T> {
237240
pub result: Option<T>,
238241
}
239242

240-
impl<'a, 'b: 'a, T: 'b + Add<Output=T> + Mul<Output=T> + Borrow<T>> Calculator<T>
241-
where
242-
&'a T: Add<Output=T> + Mul<Output=T>,
243+
impl<'a, 'b: 'a, T: 'b + Add<Output = T> + Mul<Output = T> + Borrow<T>> Calculator<T>
244+
where
245+
&'a T: Add<Output = T> + Mul<Output = T>,
243246
{
244247
pub fn calculate_procedurally(&'b mut self) {
245248
let res: T = match self.op {
@@ -250,7 +253,7 @@ impl<'a, 'b: 'a, T: 'b + Add<Output=T> + Mul<Output=T> + Borrow<T>> Calculator<T
250253
}
251254
}
252255

253-
impl<T: Add<Output=T> + Mul<Output=T> + Clone> Calculator<T> {
256+
impl<T: Add<Output = T> + Mul<Output = T> + Clone> Calculator<T> {
254257
pub fn calculate_functionally(mut self) -> Self {
255258
self.result = Some(match self.op {
256259
Operation::Add => self.lhs.clone() + self.rhs.clone(),

0 commit comments

Comments
 (0)