From 8c4ea9b0d14413ab803d26cf496ca63731ad1bae Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 15 Dec 2019 23:23:18 -0800 Subject: [PATCH] lobby joining --- src/ship/ship.rs | 120 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 94 insertions(+), 26 deletions(-) diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 5cac2b7..de36cde 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -61,6 +61,7 @@ pub enum SendShipPacket { FullCharacter(FullCharacter), CharDataRequest(CharDataRequest), JoinLobby(JoinLobby), + AddToLobby(AddToLobby), Message(Message), DirectMessage(DirectMessage), } @@ -74,6 +75,7 @@ impl SendServerPacket for SendShipPacket { SendShipPacket::FullCharacter(pkt) => pkt.as_bytes(), SendShipPacket::CharDataRequest(pkt) => pkt.as_bytes(), SendShipPacket::JoinLobby(pkt) => pkt.as_bytes(), + SendShipPacket::AddToLobby(pkt) => pkt.as_bytes(), SendShipPacket::Message(pkt) => pkt.as_bytes(), SendShipPacket::DirectMessage(pkt) => pkt.as_bytes(), } @@ -87,6 +89,7 @@ struct ClientState { character: Character, session: Session, block: u32, + room_client_id: u32, } impl ClientState { @@ -96,7 +99,8 @@ impl ClientState { settings: settings, character: character, session: session, - block: 0, + block: 1, + room_client_id: 0, } } } @@ -151,7 +155,7 @@ impl ShipServerState { let (level, stats) = self.level_table.get_stats_from_exp(character::Class::from(client.character.character.ch_class), client.character.character.exp); let items = self.entity_gateway.get_items_by_character(&client.character); - let (mut inventory, inv_len) = items + let (inventory, inv_len) = items .iter() .take(30) .fold(([character::InventoryItem::default(); 30], 0), |(mut inv, len), item| { @@ -172,7 +176,7 @@ impl ShipServerState { .character(&CharacterBuilder::new() .character(&client.character.character) .stats(&stats) - .level(level) + .level(level - 1) .build()) .inventory(&inventory, inv_len) .key_config(&client.settings.settings.key_config) @@ -188,25 +192,29 @@ impl ShipServerState { ]) } - fn send_player_to_lobby(&mut self, id: ClientId, pkt: &CharData) -> Result, ShipError> { - let mut c = character::Character::default(); - let joinlobby = JoinLobby { - client: 0, - leader: 0, - one: 1, - lobby: 0, - block: 1, - event: 0, - padding: 0, - playerinfo: vec![ + fn send_player_to_lobby(&mut self, id: ClientId, _pkt: &CharData) -> Result, ShipError> { + self.client_location.add_to_lobby(id, 0).unwrap(); + + let lobby = self.client_location.get_area_by_user(id); + let clients = lobby.clients(); + println!("clients in lobby: {:?}", clients); + let playerinfo = clients.iter() + .map(|room_client| { + let client = self.clients.get(&room_client.client_id).ok_or(ShipError::ClientNotFound(id)).unwrap(); + let (level, stats) = self.level_table.get_stats_from_exp(character::Class::from(client.character.character.ch_class), client.character.character.exp); + let c = CharacterBuilder::new() + .character(&client.character.character) + .stats(&stats) + .level(level - 1) + .build(); PlayerInfo { header: PlayerHeader { - tag: 0, - guildcard: 12345, + tag: 0x100, + guildcard: room_client.index as u32,//client.user.id, _unknown1: [0; 5], - client_id: 0, - name: utf8_to_utf16_array!("HELLO", 16), - _unknown2: 0, + client_id: room_client.index as u32, + name: c.name, + _unknown2: 2, }, inventory: character::Inventory { item_count: 0, @@ -215,15 +223,75 @@ impl ShipServerState { language: 0, items: [character::InventoryItem::default(); 30], }, - character: c + character: c, + } + }); + let client_id = clients.iter() + .fold(0, |acc, k| { + if k.client_id == id { + k.index } - ] + else { + acc + } + }); + + let join_lobby = JoinLobby { + client: client_id as u8, + leader: lobby.leader().index as u8, + one: 1, + lobby: lobby.id() as u8, + block: 1, + event: 0, + padding: 0, + playerinfo: playerinfo.collect(), }; - self.client_location.add_to_lobby(id, 0); - Ok(vec![ - SendShipPacket::JoinLobby(joinlobby) - ]) + let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id)).unwrap(); + let (level, stats) = self.level_table.get_stats_from_exp(character::Class::from(client.character.character.ch_class), client.character.character.exp); + let c = CharacterBuilder::new() + .character(&client.character.character) + .stats(&stats) + .level(level - 1) + .build(); + let addto = AddToLobby { + flag: 1, + client: client_id as u8, + leader: lobby.leader().index as u8, + one: 1, + lobby: lobby.id() as u8, + block: 1, + event: 0, + padding: 0, + playerinfo: PlayerInfo { + header: PlayerHeader { + tag: 0x100, + guildcard: client.user.id, + _unknown1: [0; 5], + client_id: client_id as u32, + name: c.name, + _unknown2: 2, + }, + inventory: character::Inventory { + item_count: 0, + hp_mats_used: 0, + tp_mats_used: 0, + language: 0, + items: [character::InventoryItem::default(); 30], + }, + character: c, + }, + }; + + let mut v = Vec::new(); + v.push((id, SendShipPacket::JoinLobby(join_lobby))); + for client in clients { + if client.client_id != id { + v.push((client.client_id, SendShipPacket::AddToLobby(addto.clone())) + )} + } + + Ok(v) } fn message(&mut self, id: ClientId, msg: &Message) -> Box> { @@ -278,7 +346,7 @@ impl ServerState for ShipServerState { } }, RecvShipPacket::CharData(chardata) => { - Box::new(self.send_player_to_lobby(id, chardata)?.into_iter().map(move |pkt| (id, pkt))) + Box::new(self.send_player_to_lobby(id, chardata)?.into_iter()) }, RecvShipPacket::Message(msg) => { self.message(id, msg)