Browse Source

direct message packet

pbs
jake 5 years ago
parent
commit
7808bf831c
  1. 26
      src/ship/ship.rs

26
src/ship/ship.rs

@ -37,6 +37,7 @@ pub enum RecvShipPacket {
MenuSelect(MenuSelect), MenuSelect(MenuSelect),
CharData(CharData), CharData(CharData),
Message(Message), Message(Message),
DirectMessage(DirectMessage),
} }
impl RecvServerPacket for RecvShipPacket { impl RecvServerPacket for RecvShipPacket {
@ -46,6 +47,7 @@ impl RecvServerPacket for RecvShipPacket {
0x10 => Ok(RecvShipPacket::MenuSelect(MenuSelect::from_bytes(data)?)), 0x10 => Ok(RecvShipPacket::MenuSelect(MenuSelect::from_bytes(data)?)),
0x61 => Ok(RecvShipPacket::CharData(CharData::from_bytes(data)?)), 0x61 => Ok(RecvShipPacket::CharData(CharData::from_bytes(data)?)),
0x60 => Ok(RecvShipPacket::Message(Message::from_bytes(data)?)), 0x60 => Ok(RecvShipPacket::Message(Message::from_bytes(data)?)),
0x62 => Ok(RecvShipPacket::DirectMessage(DirectMessage::from_bytes(data)?)),
_ => Err(PacketParseError::WrongPacketForServerType(u16::from_le_bytes([data[2], data[3]]), data.to_vec())) _ => Err(PacketParseError::WrongPacketForServerType(u16::from_le_bytes([data[2], data[3]]), data.to_vec()))
} }
} }
@ -60,6 +62,7 @@ pub enum SendShipPacket {
CharDataRequest(CharDataRequest), CharDataRequest(CharDataRequest),
JoinLobby(JoinLobby), JoinLobby(JoinLobby),
Message(Message), Message(Message),
DirectMessage(DirectMessage),
} }
impl SendServerPacket for SendShipPacket { impl SendServerPacket for SendShipPacket {
@ -72,6 +75,7 @@ impl SendServerPacket for SendShipPacket {
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::Message(pkt) => pkt.as_bytes(), SendShipPacket::Message(pkt) => pkt.as_bytes(),
SendShipPacket::DirectMessage(pkt) => pkt.as_bytes(),
} }
} }
} }
@ -224,9 +228,20 @@ impl<EG: EntityGateway> ShipServerState<EG> {
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)>> {
let cmsg = msg.clone(); let cmsg = msg.clone();
Box::new(self.client_location.get_client_neighbors(id).into_iter().map(move |client| {
(client, SendShipPacket::Message(cmsg.clone()))
}))
Box::new(self.client_location.get_area_by_user(id).clients().iter()
.filter(|client| client.client_id != id)
.map(move |client| {
(client.client_id, SendShipPacket::Message(cmsg.clone()))
}).collect::<Vec<_>>().into_iter())
}
fn direct_message(&mut self, id: ClientId, msg: &DirectMessage) -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)>> {
let cmsg = msg.clone();
Box::new(self.client_location.get_area_by_user(id).clients().iter()
.filter(|client| client.index == cmsg.flag as usize)
.map(|client| {
(client.client_id, SendShipPacket::DirectMessage(cmsg.clone()))
}).collect::<Vec<_>>().into_iter())
} }
} }
@ -267,7 +282,10 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
}, },
RecvShipPacket::Message(msg) => { RecvShipPacket::Message(msg) => {
self.message(id, msg) self.message(id, msg)
}
},
RecvShipPacket::DirectMessage(msg) => {
self.direct_message(id, msg)
},
}) })
} }
} }
Loading…
Cancel
Save