diff --git a/src/common/leveltable.rs b/src/common/leveltable.rs index 9d9f4c0..d60d1fe 100644 --- a/src/common/leveltable.rs +++ b/src/common/leveltable.rs @@ -30,9 +30,8 @@ pub struct CharacterLevelTable { table: HashMap, } - -impl CharacterLevelTable { - pub fn new() -> CharacterLevelTable { +impl Default for CharacterLevelTable { + fn default() -> CharacterLevelTable { let file = File::open("data/char_stats.json").unwrap(); let json: Value = serde_json::from_reader(file).unwrap(); let mut table = HashMap::new(); @@ -55,7 +54,7 @@ impl CharacterLevelTable { }; let mut statlist = [CharacterLevelEntry::default(); 200]; - for (i, stat) in it.1.as_array().unwrap().into_iter().enumerate() { + for (i, stat) in it.1.as_array().unwrap().iter().enumerate() { statlist[i] = CharacterLevelEntry { hp: stat["hp"].as_i64().unwrap() as u32, atp: stat["atp"].as_i64().unwrap() as u32, @@ -75,7 +74,9 @@ impl CharacterLevelTable { table: table, } } +} +impl CharacterLevelTable { pub fn get_level_from_exp(&self, ch_class: CharacterClass, exp: u32) -> u32 { if let Some(statlist) = self.table.get(&ch_class) { statlist @@ -120,14 +121,14 @@ mod test { use super::*; #[test] fn test_stat_levels() { - let table = CharacterLevelTable::new(); + let table = CharacterLevelTable::default(); assert!(table.get_stats_from_exp(CharacterClass::FOmarl, 0) == (1, CharacterStats { hp: 20, atp: 13, mst: 53, evp: 35, dfp: 10, ata: 15, lck: 10 })); assert!(table.get_stats_from_exp(CharacterClass::FOmarl, 1 << 17) == (36, CharacterStats { hp: 125, atp: 114, mst: 219, evp: 182, dfp: 42, ata: 213, lck: 10 })); } #[test] fn test_levels() { - let table = CharacterLevelTable::new(); + let table = CharacterLevelTable::default(); assert!(table.get_level_from_exp(CharacterClass::FOmarl, 0) == 1); assert!(table.get_level_from_exp(CharacterClass::FOmarl, 3000) == 8); assert!(table.get_level_from_exp(CharacterClass::FOmarl, 3200) == 9); diff --git a/src/common/mainloop/client.rs b/src/common/mainloop/client.rs index c13acbf..1bfc1ce 100644 --- a/src/common/mainloop/client.rs +++ b/src/common/mainloop/client.rs @@ -65,7 +65,7 @@ impl PacketReceiver { return Err(NetworkError::ClientDisconnected); } - self.recv_buffer.extend_from_slice(&mut data[..len]); + self.recv_buffer.extend_from_slice(&data[..len]); let mut dec_buf = { let mut cipher = self.cipher.lock().await; diff --git a/src/common/mainloop/interserver.rs b/src/common/mainloop/interserver.rs index 473abb4..cc6cce2 100644 --- a/src/common/mainloop/interserver.rs +++ b/src/common/mainloop/interserver.rs @@ -163,7 +163,7 @@ where warn!("interserver send failed: {:?}", err); break; } - if let Err(err) = socket.write_all(&payload.as_bytes()).await { + if let Err(err) = socket.write_all(payload.as_bytes()).await { warn!("intserserver send failed: {:?}", err); break; } diff --git a/src/common/mainloop/mod.rs b/src/common/mainloop/mod.rs index b0e207b..2a5b3ff 100644 --- a/src/common/mainloop/mod.rs +++ b/src/common/mainloop/mod.rs @@ -25,7 +25,7 @@ pub fn patch_mainloop(patch_state: PatchServerState, patch_port: u16) -> Pin(login_state: LoginServerState, login_port: u16) -> Pin>> { let login_state = Arc::new(Mutex::new(login_state)); - let client_mainloop = client_accept_mainloop(login_state.clone(), login_port); + let client_mainloop = client_accept_mainloop(login_state, login_port); Box::pin(client_mainloop) } diff --git a/src/login/character.rs b/src/login/character.rs index 9e05d26..6867189 100644 --- a/src/login/character.rs +++ b/src/login/character.rs @@ -306,7 +306,7 @@ impl CharacterServerState { param_data: param_data, clients: HashMap::new(), ships: BTreeMap::new(), - level_table: CharacterLevelTable::new(), + level_table: CharacterLevelTable::default(), auth_token: auth_token, authenticated_ships: BTreeSet::new(), ship_sender: BTreeMap::new(), diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 6206440..8091647 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -367,7 +367,7 @@ impl ShipServerStateBuilder { ShipServerState { entity_gateway: self.entity_gateway.unwrap(), clients: HashMap::new(), - level_table: CharacterLevelTable::new(), + level_table: CharacterLevelTable::default(), name: self.name.unwrap_or_else(|| "NAMENOTSET".into()), item_manager: items::ItemManager::default(), quests: quests::load_quests("data/quests.toml".into()).unwrap(), diff --git a/tests/test_exp_gain.rs b/tests/test_exp_gain.rs index 6d3e8b3..2223c8e 100644 --- a/tests/test_exp_gain.rs +++ b/tests/test_exp_gain.rs @@ -83,7 +83,7 @@ async fn test_character_levels_up() { assert!(matches!(levelup_pkt[1].1, SendShipPacket::Message(Message {msg: GameMessage::PlayerLevelUp(PlayerLevelUp {lvl: 2, ..})}))); - let leveltable = CharacterLevelTable::new(); + let leveltable = CharacterLevelTable::default(); let c1 = ship.clients.get(&ClientId(1)).unwrap(); assert!(leveltable.get_level_from_exp(c1.character.char_class, c1.character.exp) == 2); }