Skip to content

Commit

Permalink
Hide extra functionality from ServerEventQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Jan 10, 2024
1 parent a502a7d commit 6c99614
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Refactor `ServerEventQueue` to have an explicit interface.
- Hide extra functionality from `ServerEventQueue`.
- Moved server event reset system to new set `ClientSet::ResetEvents` in `PreUpdate`.

## [0.19.0] - 2024-01-07
Expand Down
27 changes: 9 additions & 18 deletions src/network_event/server_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@ fn queue_system<T: Event>(
mut server_events: EventWriter<T>,
mut event_queue: ResMut<ServerEventQueue<T>>,
) {
while let Some(event) = event_queue.pop_next(*replicon_tick) {
while event_queue
.0
.front()
.filter(|(&tick, _)| tick <= *replicon_tick)
.is_some()
{
let (_, event) = event_queue.0.pop_front().unwrap();
server_events.send(event);
}
}
Expand Down Expand Up @@ -291,7 +297,7 @@ fn local_resending_system<T: Event>(
}

fn reset_system<T: Event>(mut event_queue: ResMut<ServerEventQueue<T>>) {
event_queue.clear();
event_queue.0.clear();
}

/// Sends serialized `message` to clients.
Expand Down Expand Up @@ -346,27 +352,12 @@ pub enum SendMode {
pub struct ServerEventQueue<T>(ListOrderedMultimap<RepliconTick, T>);

impl<T> ServerEventQueue<T> {
/// Clears the event queue.
pub fn clear(&mut self) {
self.0.clear();
}

/// Inserts a new event.
///
/// The event will be queued until [`Self::pop_next`] is called with a replicon tick >= the tick specified here,
/// or until [`Self::clear`] is called.
/// The event will be queued until [`RepliconTick`] will be bigger or equal to the tick specified here.
pub fn insert(&mut self, tick: RepliconTick, event: T) {
self.0.insert(tick, event);
}

/// Pops the next event that is at least as old as the specified replicon tick.
pub fn pop_next(&mut self, replicon_tick: RepliconTick) -> Option<T> {
let (tick, _) = self.0.front()?;
if *tick > replicon_tick {
return None;
}
self.0.pop_front().map(|(_, event)| event)
}
}

impl<T> Default for ServerEventQueue<T> {
Expand Down

0 comments on commit 6c99614

Please sign in to comment.