Browse Source

lint src/ship/*

pull/42/head
jake 3 years ago
parent
commit
53ec82d9eb
  1. 6
      src/bin/main.rs
  2. 2
      src/bin/ship.rs
  3. 22
      src/ship/character.rs
  4. 30
      src/ship/location.rs
  5. 1
      src/ship/mod.rs
  6. 2
      src/ship/packet/builder/mod.rs
  7. 2
      src/ship/packet/handler/lobby.rs
  8. 14
      src/ship/room.rs
  9. 64
      src/ship/ship.rs

6
src/bin/main.rs

@ -383,7 +383,7 @@ fn main() {
let thread_entity_gateway = entity_gateway.clone();
info!("[ship] starting server");
let ship_state = Box::new(ShipServerStateBuilder::new()
let ship_state = Box::new(ShipServerStateBuilder::default()
.name("US/Sona-Nyl".into())
.ip(Ipv4Addr::new(127,0,0,1))
.port(elseware::ship::ship::SHIP_PORT)
@ -392,7 +392,7 @@ fn main() {
let ship_loop = ship_mainloop(*ship_state, elseware::ship::ship::SHIP_PORT, std::net::Ipv4Addr::new(127, 0, 0, 1), elseware::login::login::COMMUNICATION_PORT);
let thread_entity_gateway = entity_gateway.clone();
let ship_state = Box::new(ShipServerStateBuilder::new()
let ship_state = Box::new(ShipServerStateBuilder::default()
.name("EU/Dylath-Leen".into())
.ip(Ipv4Addr::new(127,0,0,1))
.port(elseware::ship::ship::SHIP_PORT+2000)
@ -401,7 +401,7 @@ fn main() {
let ship_loop2 = ship_mainloop(*ship_state, elseware::ship::ship::SHIP_PORT+2000, std::net::Ipv4Addr::new(127, 0, 0, 1), elseware::login::login::COMMUNICATION_PORT);
let thread_entity_gateway = entity_gateway.clone();
let ship_state = Box::new(ShipServerStateBuilder::new()
let ship_state = Box::new(ShipServerStateBuilder::default()
.name("JP/Thalarion".into())
.ip(Ipv4Addr::new(127,0,0,1))
.port(elseware::ship::ship::SHIP_PORT+3000)

2
src/bin/ship.rs

@ -37,7 +37,7 @@ fn main() {
let shipgate_token = std::env::var("SHIPGATE_TOKEN").unwrap();
let ship_name = std::env::var("SHIP_NAME").unwrap().parse().unwrap();
let ip = std::env::var("SELF_IP").unwrap().parse().unwrap();
let ship_state = ShipServerStateBuilder::new()
let ship_state = ShipServerStateBuilder::default()
.name(ship_name)
.ip(ip)
.port(elseware::ship::ship::SHIP_PORT)

22
src/ship/character.rs

@ -9,16 +9,18 @@ pub struct CharacterBytesBuilder<'a> {
level: Option<u32>,
}
impl<'a> CharacterBytesBuilder<'a> {
pub fn new() -> CharacterBytesBuilder<'a> {
impl<'a> Default for CharacterBytesBuilder<'a> {
fn default() -> CharacterBytesBuilder<'a> {
CharacterBytesBuilder {
character: None,
stats: None,
level: None,
}
}
}
impl<'a> CharacterBytesBuilder<'a> {
pub fn character(self, character: &'a CharacterEntity) -> CharacterBytesBuilder<'a> {
CharacterBytesBuilder {
character: Some(character),
@ -89,9 +91,8 @@ pub struct FullCharacterBytesBuilder<'a> {
option_flags: Option<u32>,
}
impl<'a> FullCharacterBytesBuilder<'a> {
pub fn new() -> FullCharacterBytesBuilder<'a> {
impl<'a> Default for FullCharacterBytesBuilder<'a> {
fn default() -> FullCharacterBytesBuilder<'a> {
FullCharacterBytesBuilder {
character: None,
stats: None,
@ -105,7 +106,10 @@ impl<'a> FullCharacterBytesBuilder<'a> {
option_flags: None,
}
}
}
impl<'a> FullCharacterBytesBuilder<'a> {
pub fn character(self, character: &'a CharacterEntity) -> FullCharacterBytesBuilder<'a> {
FullCharacterBytesBuilder {
character: Some(character),
@ -196,9 +200,9 @@ impl<'a> FullCharacterBytesBuilder<'a> {
inventory_items[11].material_count = character.materials.luck as u8;
character::FullCharacter {
character: CharacterBytesBuilder::new()
.character(&character)
.stats(&stats)
character: CharacterBytesBuilder::default()
.character(character)
.stats(stats)
.level(level - 1)
.build(),
inventory: character::Inventory {

30
src/ship/location.rs

@ -155,8 +155,7 @@ impl ClientLocation {
let l = self.lobbies.get_mut(lobby.0).ok_or(JoinLobbyError::LobbyDoesNotExist)?;
let (index, empty_slot) = l.0.iter_mut()
.enumerate()
.filter(|(_, k)| k.is_none())
.nth(0)
.find(|(_, k)| k.is_none())
.ok_or(JoinLobbyError::LobbyFull)?;
*empty_slot = Some(AreaClient {
client: id,
@ -173,10 +172,9 @@ impl ClientLocation {
let new_lobby = LobbyId((lobby.0 + lobby_index) % 15);
(new_lobby, self.add_client_to_lobby(id, new_lobby))
})
.filter(|(_, lobby_option)| {
.find(|(_, lobby_option)| {
lobby_option.is_ok()
})
.nth(0)
.ok_or(JoinLobbyError::LobbyFull)?;
Ok(l.0)
@ -185,8 +183,7 @@ impl ClientLocation {
pub fn create_new_room(&mut self, id: ClientId) -> Result<RoomId, CreateRoomError> {
let (index, empty_slot) = self.rooms.iter_mut()
.enumerate()
.filter(|(_, r)| r.is_none())
.nth(0)
.find(|(_, r)| r.is_none())
.ok_or(CreateRoomError::NoOpenSlots)?;
*empty_slot = Some(Room([None; 4]));
self.add_client_to_room(id, RoomId(index)).map_err(|_err| CreateRoomError::JoinError)?;
@ -201,8 +198,7 @@ impl ClientLocation {
.ok_or(JoinRoomError::RoomDoesNotExist)?;
let (index, empty_slot) = r.0.iter_mut()
.enumerate()
.filter(|(_, k)| k.is_none())
.nth(0)
.find(|(_, k)| k.is_none())
.ok_or(JoinRoomError::RoomFull)?;
*empty_slot = Some(AreaClient {
client: id,
@ -252,7 +248,7 @@ impl ClientLocation {
let mut r = self.rooms[room.0]
.as_ref()
.ok_or(GetLeaderError::InvalidArea)?
.0.iter().flat_map(|k| k)
.0.iter().flatten()
.collect::<Vec<_>>();
r.sort_by_key(|k| k.time_join);
let c = r.get(0).ok_or(GetLeaderError::NoClientInArea)?;
@ -261,7 +257,7 @@ impl ClientLocation {
pub fn get_lobby_leader(&self, lobby: LobbyId) -> Result<AreaClient, GetLeaderError> {
let mut l = self.lobbies[lobby.0]
.0.iter().flat_map(|k| k)
.0.iter().flatten()
.collect::<Vec<_>>();
l.sort_by_key(|k| k.time_join);
let c = l.get(0).ok_or(GetLeaderError::NoClientInArea)?;
@ -319,15 +315,13 @@ impl ClientLocation {
RoomLobby::Room(room) => {
self.get_clients_in_room(*room).map_err(|_| GetClientsError::InvalidArea)?
.into_iter()
.filter(|c| c.client == id)
.nth(0)
.find(|c| c.client == id)
.ok_or(GetClientsError::InvalidClient)
},
RoomLobby::Lobby(lobby) => {
self.get_clients_in_lobby(*lobby).map_err(|_| GetClientsError::InvalidArea)?
.into_iter()
.filter(|c| c.client == id)
.nth(0)
.find(|c| c.client == id)
.ok_or(GetClientsError::InvalidClient)
}
}
@ -361,9 +355,11 @@ impl ClientLocation {
let area = self.client_location.get_mut(&id).ok_or(ClientRemovalError::ClientNotInArea)?;
let client_list = match area {
RoomLobby::Room(room) => {
self.rooms[room.0].as_mut().map_or(None, |r| {
Some(r.0.iter_mut())
})
self.rooms[room.0]
.as_mut()
.map(|r| {
r.0.iter_mut()
})
},
RoomLobby::Lobby(lobby) => {
Some(self.lobbies[lobby.0].0.iter_mut())

1
src/ship/mod.rs

@ -1,3 +1,4 @@
#[allow(clippy::module_inception)]
pub mod ship;
pub mod location;
pub mod character;

2
src/ship/packet/builder/mod.rs

@ -26,7 +26,7 @@ pub fn player_header(tag: u32, client: &ClientState, area_client: &AreaClient) -
pub fn player_info(tag: u32, client: &ClientState, area_client: &AreaClient, item_manager: &ItemManager, level_table: &CharacterLevelTable) -> PlayerInfo {
let (level, stats) = level_table.get_stats_from_exp(client.character.char_class, client.character.exp);
let inventory = item_manager.get_character_inventory(&client.character).unwrap();
let character = CharacterBytesBuilder::new()
let character = CharacterBytesBuilder::default()
.character(&client.character)
.stats(&stats)
.level(level - 1)

2
src/ship/packet/handler/lobby.rs

@ -24,7 +24,7 @@ pub fn block_selected(id: ClientId,
let inventory = item_manager.get_character_inventory(&client.character).unwrap();
let bank = item_manager.get_character_bank(&client.character).unwrap();
let fc = FullCharacterBytesBuilder::new()
let fc = FullCharacterBytesBuilder::default()
.character(&client.character)
.stats(&stats)
.level(level)

14
src/ship/room.rs

@ -40,9 +40,9 @@ impl TryFrom<u8> for Episode {
}
}
impl Into<u8> for Episode {
fn into(self) -> u8 {
match self {
impl From<Episode> for u8 {
fn from(other: Episode) -> u8 {
match other {
Episode::One => 1,
Episode::Two => 2,
Episode::Four => 3,
@ -83,9 +83,9 @@ impl TryFrom<u8> for Difficulty {
}
}
impl Into<u8> for Difficulty {
fn into(self) -> u8 {
match self {
impl From<Difficulty> for u8 {
fn from(other: Difficulty) -> u8 {
match other {
Difficulty::Normal => 0,
Difficulty::Hard => 1,
Difficulty::VeryHard => 2,
@ -232,7 +232,7 @@ impl RoomState {
};
Ok(RoomState {
monster_stats: Box::new(load_monster_stats_table(&room_mode).map_err(|_| RoomCreationError::CouldNotLoadMonsterStats(room_mode.clone()))?),
monster_stats: Box::new(load_monster_stats_table(&room_mode).map_err(|_| RoomCreationError::CouldNotLoadMonsterStats(room_mode))?),
mode: room_mode,
random_seed: rand::thread_rng().gen(),
name: String::from_utf16_lossy(&create_room.name).trim_matches(char::from(0)).into(),

64
src/ship/ship.rs

@ -262,10 +262,10 @@ pub struct ClientState {
impl ClientState {
pub fn new(user: UserAccountEntity, settings: UserSettingsEntity, character: CharacterEntity, session: Session) -> ClientState {
ClientState {
user: user,
settings: settings,
character: character,
session: session,
user,
settings,
character,
session,
block: 0,
item_drop_location: None,
done_loading_quest: false,
@ -287,8 +287,8 @@ pub struct ItemShops {
pub armor_shop: ArmorShop<rand_chacha::ChaCha20Rng>,
}
impl ItemShops {
pub fn new() -> ItemShops {
impl Default for ItemShops {
fn default() -> ItemShops {
let difficulty = [room::Difficulty::Normal, room::Difficulty::Hard, room::Difficulty::VeryHard, room::Difficulty::Ultimate];
let section_id = [SectionID::Viridia, SectionID::Greenill, SectionID::Skyly, SectionID::Bluefull, SectionID::Purplenum,
SectionID::Pinkal, SectionID::Redria, SectionID::Oran, SectionID::Yellowboze, SectionID::Whitill];
@ -301,7 +301,7 @@ impl ItemShops {
}
ItemShops {
weapon_shop: weapon_shop,
weapon_shop,
tool_shop: ToolShop::default(),
armor_shop: ArmorShop::default(),
}
@ -318,8 +318,8 @@ pub struct ShipServerStateBuilder<EG: EntityGateway> {
num_blocks: usize,
}
impl<EG: EntityGateway> ShipServerStateBuilder<EG> {
pub fn new() -> ShipServerStateBuilder<EG> {
impl<EG: EntityGateway> Default for ShipServerStateBuilder<EG> {
fn default() -> ShipServerStateBuilder<EG> {
ShipServerStateBuilder {
entity_gateway: None,
name: None,
@ -329,7 +329,9 @@ impl<EG: EntityGateway> ShipServerStateBuilder<EG> {
num_blocks: 2,
}
}
}
impl<EG: EntityGateway> ShipServerStateBuilder<EG> {
pub fn gateway(mut self, entity_gateway: EG) -> ShipServerStateBuilder<EG> {
self.entity_gateway = Some(entity_gateway);
self
@ -366,16 +368,16 @@ impl<EG: EntityGateway> ShipServerStateBuilder<EG> {
entity_gateway: self.entity_gateway.unwrap(),
clients: HashMap::new(),
level_table: CharacterLevelTable::new(),
name: self.name.unwrap_or("NAMENOTSET".into()),
name: self.name.unwrap_or_else(|| "NAMENOTSET".into()),
item_manager: items::ItemManager::default(),
quests: quests::load_quests("data/quests.toml".into()).unwrap(),
ip: self.ip.unwrap_or(Ipv4Addr::new(127,0,0,1)),
ip: self.ip.unwrap_or_else(|| Ipv4Addr::new(127,0,0,1)),
port: self.port.unwrap_or(SHIP_PORT),
shops: Box::new(ItemShops::new()),
shops: Box::new(ItemShops::default()),
blocks: Blocks(blocks),
auth_token: self.auth_token.unwrap_or(AuthToken("".into())),
auth_token: self.auth_token.unwrap_or_else(|| AuthToken("".into())),
ship_list: Vec::new(),
shipgate_sender: None,
}
@ -428,7 +430,7 @@ pub struct ShipServerState<EG: EntityGateway> {
impl<EG: EntityGateway> ShipServerState<EG> {
pub fn builder() -> ShipServerStateBuilder<EG> {
ShipServerStateBuilder::new()
ShipServerStateBuilder::default()
}
pub fn set_sender(&mut self, sender: Box<dyn Fn(ShipMessage) + Send + Sync>) {
@ -443,7 +445,7 @@ impl<EG: EntityGateway> ShipServerState<EG> {
},
GameMessage::PlayerDropItem(player_drop_item) => {
let block = self.blocks.with_client(id, &self.clients)?;
handler::message::player_drop_item(id, player_drop_item, &mut self.entity_gateway, &mut block.client_location, &mut self.clients, &mut block.rooms, &mut self.item_manager).await?
handler::message::player_drop_item(id, player_drop_item, &mut self.entity_gateway, &block.client_location, &mut self.clients, &mut block.rooms, &mut self.item_manager).await?
},
GameMessage::DropCoordinates(drop_coordinates) => {
let block = self.blocks.with_client(id, &self.clients)?;
@ -451,37 +453,37 @@ impl<EG: EntityGateway> ShipServerState<EG> {
},
GameMessage::PlayerNoLongerHasItem(no_longer_has_item) => {
let block = self.blocks.with_client(id, &self.clients)?;
handler::message::no_longer_has_item(id, no_longer_has_item, &mut self.entity_gateway, &mut block.client_location, &mut self.clients, &mut self.item_manager).await?
handler::message::no_longer_has_item(id, no_longer_has_item, &mut self.entity_gateway, &block.client_location, &mut self.clients, &mut self.item_manager).await?
},
GameMessage::PlayerChangedMap(_) | GameMessage::PlayerChangedMap2(_) | GameMessage::TellOtherPlayerMyLocation(_) |
GameMessage::PlayerWarpingToFloor(_) | GameMessage::PlayerTeleported(_) | GameMessage::PlayerStopped(_) |
GameMessage::PlayerLoadedIn(_) | GameMessage::PlayerWalking(_) | GameMessage::PlayerRunning(_) |
GameMessage::PlayerWarped(_) | GameMessage::PlayerChangedFloor(_) | GameMessage::InitializeSpeechNpc(_) => {
let block = self.blocks.with_client(id, &self.clients)?;
handler::message::update_player_position(id, &msg, &mut self.clients, &mut block.client_location, &block.rooms)?
handler::message::update_player_position(id, msg, &mut self.clients, &block.client_location, &block.rooms)?
},
GameMessage::ChargeAttack(charge_attack) => {
handler::message::charge_attack(id, charge_attack, &mut self.clients, &mut self.entity_gateway).await?
},
GameMessage::PlayerUseItem(player_use_item) => {
let block = self.blocks.with_client(id, &self.clients)?;
handler::message::use_item(id, player_use_item, &mut self.entity_gateway, &mut block.client_location, &mut self.clients, &mut self.item_manager).await?
handler::message::use_item(id, player_use_item, &mut self.entity_gateway, &block.client_location, &mut self.clients, &mut self.item_manager).await?
},
GameMessage::PlayerUsedMedicalCenter(player_used_medical_center) => {
handler::message::player_used_medical_center(id, &player_used_medical_center, &mut self.entity_gateway, &mut self.clients).await?
handler::message::player_used_medical_center(id, player_used_medical_center, &mut self.entity_gateway, &mut self.clients).await?
},
GameMessage::PlayerFeedMag(player_feed_mag) => {
let block = self.blocks.with_client(id, &self.clients)?;
handler::message::player_feed_mag(id, &player_feed_mag, &mut self.entity_gateway, &mut block.client_location, &mut self.clients, &mut self.item_manager).await?
handler::message::player_feed_mag(id, player_feed_mag, &mut self.entity_gateway, &block.client_location, &self.clients, &mut self.item_manager).await?
},
GameMessage::PlayerEquipItem(player_equip_item) => {
handler::message::player_equips_item(id, &player_equip_item, &mut self.entity_gateway, &mut self.clients, &mut self.item_manager).await?
handler::message::player_equips_item(id, player_equip_item, &mut self.entity_gateway, &self.clients, &mut self.item_manager).await?
},
GameMessage::PlayerUnequipItem(player_unequip_item) => {
handler::message::player_unequips_item(id, &player_unequip_item, &mut self.entity_gateway, &mut self.clients, &mut self.item_manager).await?
handler::message::player_unequips_item(id, player_unequip_item, &mut self.entity_gateway, &self.clients, &mut self.item_manager).await?
},
GameMessage::SortItems(sort_items) => {
handler::message::player_sorts_items(id, sort_items, &mut self.entity_gateway, &mut self.clients, &mut self.item_manager).await?
handler::message::player_sorts_items(id, sort_items, &mut self.entity_gateway, &self.clients, &mut self.item_manager).await?
},
_ => {
let cmsg = msg.clone();
@ -502,13 +504,13 @@ impl<EG: EntityGateway> ShipServerState<EG> {
handler::direct_message::guildcard_send(id, guildcard_send, target, &block.client_location, &self.clients)
},
GameMessage::RequestItem(request_item) => {
handler::direct_message::request_item(id, request_item, &mut self.entity_gateway, &mut block.client_location, &mut self.clients, &mut block.rooms, &mut self.item_manager).await?
handler::direct_message::request_item(id, request_item, &mut self.entity_gateway, &block.client_location, &mut self.clients, &mut block.rooms, &mut self.item_manager).await?
},
GameMessage::PickupItem(pickup_item) => {
handler::direct_message::pickup_item(id, pickup_item, &mut self.entity_gateway, &mut block.client_location, &mut self.clients, &mut self.item_manager).await?
handler::direct_message::pickup_item(id, pickup_item, &mut self.entity_gateway, &block.client_location, &mut self.clients, &mut self.item_manager).await?
},
GameMessage::BoxDropRequest(box_drop_request) => {
handler::direct_message::request_box_item(id, box_drop_request, &mut self.entity_gateway, &mut block.client_location, &mut self.clients, &mut block.rooms, &mut self.item_manager).await?
handler::direct_message::request_box_item(id, box_drop_request, &mut self.entity_gateway, &block.client_location, &mut self.clients, &mut block.rooms, &mut self.item_manager).await?
},
GameMessage::BankRequest(_bank_request) => {
handler::direct_message::send_bank_list(id, &self.clients, &mut self.item_manager).await?
@ -626,7 +628,7 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
},
RecvShipPacket::PlayerChat(msg) => {
let block = self.blocks.with_client(id, &self.clients)?;
Box::new(handler::communication::player_chat(id, msg, &block.client_location, &self.clients)?.into_iter())
Box::new(handler::communication::player_chat(id, msg, &block.client_location, &self.clients)?)
},
RecvShipPacket::CreateRoom(create_room) => {
let block = self.blocks.with_client(id, &self.clients)?;
@ -706,7 +708,7 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
let pkt = match block.client_location.get_area(id)? {
RoomLobby::Room(room) => {
if neighbors.len() == 0 {
if neighbors.is_empty() {
block.rooms[room.0] = None;
}
let leader = block.client_location.get_room_leader(room)?;
@ -719,7 +721,7 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
};
if let Some(shipgate_sender) = self.shipgate_sender.as_ref() {
shipgate_sender(ShipMessage::RemoveUser(client.user.id.clone()));
shipgate_sender(ShipMessage::RemoveUser(client.user.id));
}
block.client_location.remove_client_from_area(id);
@ -748,7 +750,7 @@ impl<EG: EntityGateway> InterserverActor for ShipServerState<EG> {
(id, ShipMessage::Authenticate(self.auth_token.clone())),
(id, ShipMessage::NewShip(Ship {
name: self.name.clone(),
ip: self.ip.clone(),
ip: self.ip,
port: self.port,
block_count: 2,
})),
@ -768,7 +770,7 @@ impl<EG: EntityGateway> InterserverActor for ShipServerState<EG> {
LoginMessage::RequestUsers => {
Ok(self.clients.iter()
.map(|(_, client)| {
(id, ShipMessage::AddUser(client.user.id.clone()))
(id, ShipMessage::AddUser(client.user.id))
})
.collect())
}

Loading…
Cancel
Save