summaryrefslogtreecommitdiff
path: root/src/world.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-04-01 21:25:18 +0200
committertroido <troido@protonmail.com>2020-04-01 21:25:18 +0200
commit9d61814eeb7873a1c21d0f521dcb3649f3b4baf8 (patch)
tree1b43e51aff93837a01616a028ada46b90c9f077a /src/world.rs
parent633cb7d5b2048324a13bedb43468a7f04a81a519 (diff)
room unloading works now
Diffstat (limited to 'src/world.rs')
-rw-r--r--src/world.rs23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/world.rs b/src/world.rs
index ecdc540..50a3a4b 100644
--- a/src/world.rs
+++ b/src/world.rs
@@ -24,6 +24,7 @@ pub struct World<'a, 'b> {
default_room: RoomId,
players: HashMap<PlayerId, RoomId>,
rooms: HashMap<RoomId, Room<'a, 'b>>,
+ room_age: HashMap<RoomId, u32>,
encyclopedia: Encyclopedia,
time: Timestamp
}
@@ -38,11 +39,13 @@ impl <'a, 'b>World<'a, 'b> {
default_room,
encyclopedia: encyclopedia.clone(),
players: HashMap::new(),
- rooms: hashmap!(purgatory::purgatory_id() => purgatory::create_purgatory(encyclopedia))
+ rooms: hashmap!(purgatory::purgatory_id() => purgatory::create_purgatory(encyclopedia)),
+ room_age: HashMap::new()
}
}
fn get_room_mut(&mut self, id: &RoomId) -> Result<&mut Room<'a, 'b>> {
+ self.room_age.insert(id.clone(), 0);
let result = self.get_room_mut_(id);
if let Err(err) = &result {
println!("Failed to load room {:?}: {:?}", id, err);
@@ -169,6 +172,24 @@ impl <'a, 'b>World<'a, 'b> {
}
}
+ pub fn unload_rooms(&mut self){
+ let mut to_remove = Vec::new();
+ for roomid in self.rooms.keys() {
+ if self.rooms[roomid].has_players() {
+ self.room_age.insert(roomid.clone(), 0);
+ } else {
+ let age = *self.room_age.get(&roomid).unwrap_or(&0) + 1;
+ self.room_age.insert(roomid.clone(), age);
+ if age > 2 {
+ to_remove.push(roomid.clone());
+ }
+ }
+ }
+ for roomid in to_remove {
+ self.rooms.remove(&roomid);
+ }
+ }
+
pub fn view(&self) -> HashMap<PlayerId, WorldMessage> {
let mut views = HashMap::new();
for room in self.rooms.values() {