veloren/server/src/sys/loot.rs

53 lines
1.7 KiB
Rust
Raw Normal View History

2022-06-04 17:16:12 +00:00
use common::{
comp::{group::GroupManager, loot_owner::LootOwnerKind, LootOwner},
uid::IdMaps,
2022-06-04 17:16:12 +00:00
};
use common_ecs::{Job, Origin, Phase, System};
use specs::{Entities, Entity, Join, Read, WriteStorage};
use tracing::debug;
// This system manages loot that exists in the world
#[derive(Default)]
pub struct Sys;
impl<'a> System<'a> for Sys {
type SystemData = (
Entities<'a>,
WriteStorage<'a, LootOwner>,
Read<'a, IdMaps>,
2022-06-04 17:16:12 +00:00
Read<'a, GroupManager>,
);
const NAME: &'static str = "loot";
const ORIGIN: Origin = Origin::Server;
const PHASE: Phase = Phase::Create;
2022-06-04 17:16:12 +00:00
fn run(
_job: &mut Job<Self>,
(entities, mut loot_owners, id_maps, group_manager): Self::SystemData,
2022-06-04 17:16:12 +00:00
) {
// Find and remove expired loot ownership. Loot ownership is expired when either
2022-06-04 17:16:12 +00:00
// the expiry time has passed, or the owner no longer exists
let expired = (&entities, &loot_owners)
.join()
.filter(|(_, loot_owner)| {
loot_owner.expired()
2022-06-04 17:16:12 +00:00
|| match loot_owner.owner() {
LootOwnerKind::Player(uid) => id_maps
.uid_entity(uid)
2022-06-04 17:16:12 +00:00
.map_or(true, |entity| !entities.is_alive(entity)),
LootOwnerKind::Group(group) => group_manager.group_info(group).is_none(),
}
})
.map(|(entity, _)| entity)
.collect::<Vec<Entity>>();
if !&expired.is_empty() {
debug!("Removing {} expired loot ownerships", expired.iter().len());
}
for entity in expired {
loot_owners.remove(entity);
}
}
}