mod cache;
mod entry;
mod player;
pub use cache::*;
pub use entry::*;
pub use player::*;
use std::ops;
#[derive(Debug, Clone)]
pub struct Tracker<T> {
entries: Vec<Entry<T>>,
}
impl<T> Tracker<T> {
pub const fn new() -> Self {
Self {
entries: Vec::new(),
}
}
pub fn add_player(&mut self, player: Player, data: T) {
self.entries.push(Entry::new(player, data))
}
pub fn add_player_default(&mut self, player: Player)
where
T: Default,
{
self.add_player(player, T::default())
}
pub fn remove_player(&mut self, id: usize) -> Option<Entry<T>> {
self.entries
.iter()
.position(|entry| entry.player.id == id)
.map(|index| self.entries.remove(index))
}
pub fn get_self(&self) -> Option<&Entry<T>> {
self.entries.iter().find(|entry| entry.player.is_self)
}
pub fn get_self_mut(&mut self) -> Option<&mut Entry<T>> {
self.entries.iter_mut().find(|entry| entry.player.is_self)
}
pub fn player(&self, id: usize) -> Option<&Entry<T>> {
self.entries.iter().find(|entry| entry.player.id == id)
}
pub fn player_mut(&mut self, id: usize) -> Option<&mut Entry<T>> {
self.entries.iter_mut().find(|entry| entry.player.id == id)
}
}
impl<T> Default for Tracker<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> ops::Deref for Tracker<T> {
type Target = [Entry<T>];
fn deref(&self) -> &Self::Target {
self.entries.as_slice()
}
}
impl<T> ops::DerefMut for Tracker<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.entries.as_mut_slice()
}
}