use std::collections::HashMap;
use pogo_masterfile_types::{
MasterfileEntry,
combat_league::{CombatLeagueEntry, CombatLeagueTemplateId},
};
pub struct CombatLeagueAccessor<'a> {
pub(crate) entries: &'a [MasterfileEntry],
pub(crate) index: &'a HashMap<CombatLeagueTemplateId, usize>,
pub(crate) order: &'a [usize],
}
impl<'a> CombatLeagueAccessor<'a> {
pub fn get<I>(&self, id: I) -> Option<&'a CombatLeagueEntry>
where
I: TryInto<CombatLeagueTemplateId>,
{
let typed = id.try_into().ok()?;
let idx = *self.index.get(&typed)?;
match &self.entries[idx] {
MasterfileEntry::CombatLeague(e) => Some(e),
_ => None,
}
}
pub fn has<I>(&self, id: I) -> bool
where
I: TryInto<CombatLeagueTemplateId>,
{
self.get(id).is_some()
}
pub fn iter(&self) -> impl Iterator<Item = &'a CombatLeagueEntry> + '_ {
self.order
.iter()
.filter_map(|&idx| match &self.entries[idx] {
MasterfileEntry::CombatLeague(e) => Some(e),
_ => None,
})
}
pub fn template_ids(&self) -> impl Iterator<Item = CombatLeagueTemplateId> + '_ {
self.index.keys().copied()
}
pub fn len(&self) -> usize {
self.order.len()
}
pub fn is_empty(&self) -> bool {
self.order.is_empty()
}
}