use crcbl_core::{PlayerId, TickId};
use crate::World;
#[derive(Clone, Copy, Debug)]
pub struct ClientInputs<'a> {
frames: &'a [(TickId, Vec<u8>)],
dropped: u32,
player: Option<PlayerId>,
}
impl<'a> ClientInputs<'a> {
#[must_use]
pub const fn new(frames: &'a [(TickId, Vec<u8>)], dropped: u32) -> Self {
Self {
frames,
dropped,
player: None,
}
}
#[must_use]
pub const fn with_player(self, player: Option<PlayerId>) -> Self {
Self { player, ..self }
}
#[must_use]
pub const fn empty() -> Self {
Self {
frames: &[],
dropped: 0,
player: None,
}
}
pub fn iter(self) -> impl Iterator<Item = (TickId, &'a [u8])> {
self.frames
.iter()
.map(|(tick, data)| (*tick, data.as_slice()))
}
#[must_use]
pub const fn len(&self) -> usize {
self.frames.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.frames.is_empty()
}
#[must_use]
pub const fn dropped(&self) -> u32 {
self.dropped
}
#[must_use]
pub const fn player(&self) -> Option<PlayerId> {
self.player
}
}
pub trait GameModule: Send {
fn name(&self) -> &str;
fn register(&self, world: &mut World);
fn tick(&mut self, _world: &mut World, _inputs: ClientInputs<'_>) {}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::System;
struct CounterModule {
name: String,
tick_count: u32,
}
impl CounterModule {
fn new(name: &str) -> Self {
Self {
name: name.to_owned(),
tick_count: 0,
}
}
}
impl GameModule for CounterModule {
fn name(&self) -> &str {
&self.name
}
fn register(&self, world: &mut World) {
let mut system = System::<f32>::new("counter");
let e = world.spawn();
system.attach(e, 0.0);
world.register_system(Box::new(system));
}
fn tick(&mut self, world: &mut World, _inputs: ClientInputs<'_>) {
self.tick_count += 1;
let _ = world.entity_count();
}
}
#[test]
fn a_game_module_reports_the_name_it_was_built_with() {
let module = CounterModule::new("test_mod");
assert_eq!(module.name(), "test_mod");
}
#[test]
fn a_modules_register_call_spawns_into_the_world_it_is_handed() {
let mut world = World::new();
let module = CounterModule::new("test_mod");
module.register(&mut world);
assert_eq!(world.entity_count(), 1);
}
#[test]
fn module_tick_increments_counter() {
let mut module = CounterModule::new("test_mod");
let mut world = World::new();
module.register(&mut world);
assert_eq!(module.tick_count, 0);
module.tick(&mut world, ClientInputs::empty());
assert_eq!(module.tick_count, 1);
module.tick(&mut world, ClientInputs::empty());
assert_eq!(module.tick_count, 2);
}
struct NoopModule;
impl GameModule for NoopModule {
fn name(&self) -> &str {
"noop"
}
fn register(&self, _world: &mut World) {}
}
#[test]
fn the_default_tick_leaves_the_world_it_is_handed_unchanged() {
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
fn hash(world: &World) -> u64 {
let mut hasher = DefaultHasher::new();
world.hash_state(&mut hasher);
hasher.finish()
}
let mut world = World::new();
CounterModule::new("counter").register(&mut world);
let entity = world.spawn();
world.despawn(entity);
let (before, entities, dead) = (hash(&world), world.entity_count(), world.dead_queue_len());
let mut module = NoopModule;
module.tick(&mut world, ClientInputs::empty());
assert_eq!(hash(&world), before, "the systems' state moved");
assert_eq!(world.entity_count(), entities);
assert_eq!(
world.dead_queue_len(),
dead,
"the pending despawn was swept"
);
}
#[test]
fn the_view_yields_every_frame_in_the_order_queued_with_its_own_tick() {
let frames = vec![
(TickId::from_raw(7), vec![1, 2]),
(TickId::from_raw(9), vec![3]),
];
let inputs = ClientInputs::new(&frames, 0);
assert_eq!(inputs.len(), 2);
assert!(!inputs.is_empty());
assert_eq!(
inputs.iter().collect::<Vec<_>>(),
vec![
(TickId::from_raw(7), &[1, 2][..]),
(TickId::from_raw(9), &[3][..]),
],
);
}
#[test]
fn an_empty_view_is_empty_and_a_capped_one_says_how_many_it_refused() {
let empty = ClientInputs::empty();
assert!(empty.is_empty());
assert_eq!(empty.len(), 0);
assert_eq!(empty.iter().count(), 0);
assert_eq!(empty.dropped(), 0);
let frames = vec![(TickId::ZERO, vec![0])];
let capped = ClientInputs::new(&frames, 5);
assert_eq!(capped.len(), 1);
assert_eq!(capped.dropped(), 5);
}
#[test]
fn a_view_names_the_player_it_was_handed_with_and_keeps_its_frames() {
let frames = vec![(TickId::from_raw(3), vec![9])];
let anonymous = ClientInputs::new(&frames, 2);
assert_eq!(anonymous.player(), None);
assert_eq!(ClientInputs::empty().player(), None);
let player = PlayerId::from_seed(7);
let named = anonymous.with_player(Some(player));
assert_eq!(named.player(), Some(player));
assert_eq!(named.dropped(), 2);
assert_eq!(
named.iter().collect::<Vec<_>>(),
[(TickId::from_raw(3), &[9][..])]
);
assert_eq!(named.with_player(None).player(), None);
}
#[test]
fn module_tick_after_schedule() {
let mut world = World::new();
let mut module = CounterModule::new("counter");
module.register(&mut world);
world.tick();
module.tick(&mut world, ClientInputs::empty());
assert_eq!(module.tick_count, 1);
world.tick();
module.tick(&mut world, ClientInputs::empty());
assert_eq!(module.tick_count, 2);
}
}