use bevy_app::prelude::*;
use bevy_connect::prelude::*;
use bevy_ecs::prelude::*;
use crate::{networking::assets::AssetMessage, SyncStats};
pub(crate) struct MetricsPlugin;
impl Plugin for MetricsPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<SyncStats>();
app.add_systems(Update, update_stats);
}
}
fn update_stats(
mut stats: ResMut<SyncStats>,
syncs: Option<Res<Channel<crate::proto::Message>>>,
assets: Option<Res<Channel<AssetMessage>>>,
) {
if let Some(syncs) = syncs {
let r = syncs.total_read();
let s = syncs.total_sent();
if stats.total_sync_net_read_prev < r {
stats.total_sync_net_read += r - stats.total_sync_net_read_prev;
}
if stats.total_sync_net_sent_prev < s {
stats.total_sync_net_sent += s - stats.total_sync_net_sent_prev;
}
stats.total_sync_net_read_prev = r;
stats.total_sync_net_sent_prev = s;
}
if let Some(assets) = assets {
let r = assets.total_read();
let s = assets.total_sent();
if stats.total_asset_net_read_prev < r {
stats.total_asset_net_read += r - stats.total_sync_net_read_prev;
}
if stats.total_asset_net_sent_prev < s {
stats.total_asset_net_sent += s - stats.total_sync_net_sent_prev;
}
stats.total_asset_net_read_prev = r;
stats.total_asset_net_sent_prev = s;
}
}