Browse Source

lobby joining

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

120
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<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 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<EG: EntityGateway> ShipServerState<EG> {
.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<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,
one: 1,
lobby: 0,
block: 1,
event: 0,
padding: 0,
playerinfo: vec![
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: 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<EG: EntityGateway> ShipServerState<EG> {
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<dyn Iterator<Item = (ClientId, SendShipPacket)>> {
@ -278,7 +346,7 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
}
},
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)

Loading…
Cancel
Save