Browse Source

lobby joining

pbs
jake 5 years ago
parent
commit
8c4ea9b0d1
  1. 116
      src/ship/ship.rs

116
src/ship/ship.rs

@ -61,6 +61,7 @@ pub enum SendShipPacket {
FullCharacter(FullCharacter), FullCharacter(FullCharacter),
CharDataRequest(CharDataRequest), CharDataRequest(CharDataRequest),
JoinLobby(JoinLobby), JoinLobby(JoinLobby),
AddToLobby(AddToLobby),
Message(Message), Message(Message),
DirectMessage(DirectMessage), DirectMessage(DirectMessage),
} }
@ -74,6 +75,7 @@ impl SendServerPacket for SendShipPacket {
SendShipPacket::FullCharacter(pkt) => pkt.as_bytes(), SendShipPacket::FullCharacter(pkt) => pkt.as_bytes(),
SendShipPacket::CharDataRequest(pkt) => pkt.as_bytes(), SendShipPacket::CharDataRequest(pkt) => pkt.as_bytes(),
SendShipPacket::JoinLobby(pkt) => pkt.as_bytes(), SendShipPacket::JoinLobby(pkt) => pkt.as_bytes(),
SendShipPacket::AddToLobby(pkt) => pkt.as_bytes(),
SendShipPacket::Message(pkt) => pkt.as_bytes(), SendShipPacket::Message(pkt) => pkt.as_bytes(),
SendShipPacket::DirectMessage(pkt) => pkt.as_bytes(), SendShipPacket::DirectMessage(pkt) => pkt.as_bytes(),
} }
@ -87,6 +89,7 @@ struct ClientState {
character: Character, character: Character,
session: Session, session: Session,
block: u32, block: u32,
room_client_id: u32,
} }
impl ClientState { impl ClientState {
@ -96,7 +99,8 @@ impl ClientState {
settings: settings, settings: settings,
character: character, character: character,
session: session, session: session,
block: 0,
block: 1,
room_client_id: 0,
} }
} }
} }
@ -151,7 +155,7 @@ impl<EG: EntityGateway> ShipServerState<EG> {
let (level, stats) = self.level_table.get_stats_from_exp(character::Class::from(client.character.character.ch_class), client.character.character.exp); 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 items = self.entity_gateway.get_items_by_character(&client.character);
let (mut inventory, inv_len) = items
let (inventory, inv_len) = items
.iter() .iter()
.take(30) .take(30)
.fold(([character::InventoryItem::default(); 30], 0), |(mut inv, len), item| { .fold(([character::InventoryItem::default(); 30], 0), |(mut inv, len), item| {
@ -172,7 +176,7 @@ impl<EG: EntityGateway> ShipServerState<EG> {
.character(&CharacterBuilder::new() .character(&CharacterBuilder::new()
.character(&client.character.character) .character(&client.character.character)
.stats(&stats) .stats(&stats)
.level(level)
.level(level - 1)
.build()) .build())
.inventory(&inventory, inv_len) .inventory(&inventory, inv_len)
.key_config(&client.settings.settings.key_config) .key_config(&client.settings.settings.key_config)
@ -188,25 +192,85 @@ impl<EG: EntityGateway> ShipServerState<EG> {
]) ])
} }
fn send_player_to_lobby(&mut self, id: ClientId, pkt: &CharData) -> Result<Vec<SendShipPacket>, ShipError> {
let mut c = character::Character::default();
let joinlobby = JoinLobby {
client: 0,
leader: 0,
fn send_player_to_lobby(&mut self, id: ClientId, _pkt: &CharData) -> Result<Vec<(ClientId, SendShipPacket)>, 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: 0x100,
guildcard: room_client.index as u32,//client.user.id,
_unknown1: [0; 5],
client_id: room_client.index 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 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, one: 1,
lobby: 0,
lobby: lobby.id() as u8,
block: 1, block: 1,
event: 0, event: 0,
padding: 0, padding: 0,
playerinfo: vec![
PlayerInfo {
playerinfo: playerinfo.collect(),
};
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 { header: PlayerHeader {
tag: 0,
guildcard: 12345,
tag: 0x100,
guildcard: client.user.id,
_unknown1: [0; 5], _unknown1: [0; 5],
client_id: 0,
name: utf8_to_utf16_array!("HELLO", 16),
_unknown2: 0,
client_id: client_id as u32,
name: c.name,
_unknown2: 2,
}, },
inventory: character::Inventory { inventory: character::Inventory {
item_count: 0, item_count: 0,
@ -215,15 +279,19 @@ impl<EG: EntityGateway> ShipServerState<EG> {
language: 0, language: 0,
items: [character::InventoryItem::default(); 30], items: [character::InventoryItem::default(); 30],
}, },
character: c
}
]
character: c,
},
}; };
self.client_location.add_to_lobby(id, 0);
Ok(vec![
SendShipPacket::JoinLobby(joinlobby)
])
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<dyn Iterator<Item = (ClientId, SendShipPacket)>> { fn message(&mut self, id: ClientId, msg: &Message) -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)>> {
@ -278,7 +346,7 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
} }
}, },
RecvShipPacket::CharData(chardata) => { 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) => { RecvShipPacket::Message(msg) => {
self.message(id, msg) self.message(id, msg)

Loading…
Cancel
Save