mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
2a7378b4ae
His reason to reqeust that is, that there might not be a perfect disctinction in the future. Now we need to send ServerGeneral over streams and do additional checking at various places to verify that not the wrong variant is send.
74 lines
2.2 KiB
Rust
74 lines
2.2 KiB
Rust
use super::SysTimer;
|
|
use crate::client::Client;
|
|
use common::{
|
|
comp::group::{Invite, PendingInvites},
|
|
msg::{InviteAnswer, ServerGeneral},
|
|
span,
|
|
sync::Uid,
|
|
};
|
|
use specs::{Entities, Join, ReadStorage, System, Write, WriteStorage};
|
|
|
|
/// This system removes timed out group invites
|
|
pub struct Sys;
|
|
impl<'a> System<'a> for Sys {
|
|
#[allow(clippy::type_complexity)] // TODO: Pending review in #587
|
|
type SystemData = (
|
|
Entities<'a>,
|
|
WriteStorage<'a, Invite>,
|
|
WriteStorage<'a, PendingInvites>,
|
|
WriteStorage<'a, Client>,
|
|
ReadStorage<'a, Uid>,
|
|
Write<'a, SysTimer<Self>>,
|
|
);
|
|
|
|
fn run(
|
|
&mut self,
|
|
(entities, mut invites, mut pending_invites, mut clients, uids, mut timer): Self::SystemData,
|
|
) {
|
|
span!(_guard, "run", "invite_timeout::Sys::run");
|
|
timer.start();
|
|
|
|
let now = std::time::Instant::now();
|
|
|
|
let timed_out_invites = (&entities, &invites)
|
|
.join()
|
|
.filter_map(|(invitee, Invite(inviter))| {
|
|
// Retrieve timeout invite from pending invites
|
|
let pending = &mut pending_invites.get_mut(*inviter)?.0;
|
|
let index = pending.iter().position(|p| p.0 == invitee)?;
|
|
|
|
// Stop if not timed out
|
|
if pending[index].1 > now {
|
|
return None;
|
|
}
|
|
|
|
// Remove pending entry
|
|
pending.swap_remove(index);
|
|
|
|
// If no pending invites remain remove the component
|
|
if pending.is_empty() {
|
|
pending_invites.remove(*inviter);
|
|
}
|
|
|
|
// Inform inviter of timeout
|
|
if let (Some(client), Some(target)) =
|
|
(clients.get_mut(*inviter), uids.get(invitee).copied())
|
|
{
|
|
client.send_msg(ServerGeneral::InviteComplete {
|
|
target,
|
|
answer: InviteAnswer::TimedOut,
|
|
})
|
|
}
|
|
|
|
Some(invitee)
|
|
})
|
|
.collect::<Vec<_>>();
|
|
|
|
for entity in timed_out_invites {
|
|
invites.remove(entity);
|
|
}
|
|
|
|
timer.end();
|
|
}
|
|
}
|