summaryrefslogtreecommitdiff
path: root/src
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
parent633cb7d5b2048324a13bedb43468a7f04a81a519 (diff)
room unloading works now
Diffstat (limited to 'src')
-rw-r--r--src/componentwrapper.rs1
-rw-r--r--src/main.rs1
-rw-r--r--src/room.rs4
-rw-r--r--src/world.rs23
4 files changed, 28 insertions, 1 deletions
diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs
index 589da15..42eccff 100644
--- a/src/componentwrapper.rs
+++ b/src/componentwrapper.rs
@@ -196,6 +196,7 @@ components!(
into,
delay,
target_time: if target_time == 0 { None } else { Some(Timestamp(target_time)) }
+ // please forgive me for using 0 as null
};
Equipment () {panic!("equipment from parameters not implemented")};
CreationTime (time: Int) {CreationTime{time: Timestamp(time)}};
diff --git a/src/main.rs b/src/main.rs
index 4732d77..68e0f50 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -125,6 +125,7 @@ fn main() -> Result<()>{
world.update();
if count % 50 == 0 {
world.save();
+ world.unload_rooms();
}
let messages = world.view();
for (player, mut message) in messages {
diff --git a/src/room.rs b/src/room.rs
index 9df131a..805f94e 100644
--- a/src/room.rs
+++ b/src/room.rs
@@ -228,6 +228,10 @@ impl <'a, 'b>Room<'a, 'b> {
}
}
+ pub fn has_players(&self) -> bool {
+ !self.world.read_component::<Player>().is_empty()
+ }
+
pub fn save_players(&self) -> HashMap<PlayerId, PlayerState> {
let mut states = HashMap::new();
let players = self.world.read_component::<Player>();
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() {