use bevy_connect::{ClientId, prelude::Channel};
use bevy_ecs::world::World;
use tracing::{debug, info, warn};
use crate::{full_sync::build_full_sync, proto::Message};
pub(crate) fn send_initial_sync(client_id: ClientId, world: &mut World) {
info!("Sending initial sync to client id {}", client_id);
let mut initial_sync = match build_full_sync(world) {
Ok(initial_sync) => initial_sync,
Err(err) => {
warn!(
"Failed initial sync to client id {} because {}",
client_id, err
);
return;
}
};
let mut server = world.resource_mut::<Channel<Message>>();
debug!("Initial sync size: {}", initial_sync.len());
for msg in initial_sync.drain(..) {
server.send_to(client_id, msg);
}
server.send_to(client_id, Message::FinishedInitialSync);
}