diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.rs | 10 | ||||
| -rw-r--r-- | src/main.rs | 8 | ||||
| -rw-r--r-- | src/timestamp.rs | 7 | ||||
| -rw-r--r-- | src/world.rs | 15 |
4 files changed, 27 insertions, 13 deletions
diff --git a/src/config.rs b/src/config.rs index 0adeb9e..ea1cab1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -20,7 +20,17 @@ pub struct Config { pub user_dir: Option<PathBuf>, + #[structopt(long, env="USER", help="The name(s) of the server admin(s)")] pub admins: String, + #[structopt(long, default_value="100", help="The time (in milliseconds) between two steps")] + pub step_duration: u64, + + #[structopt(long, default_value="300", help="The time (in steps) between two saves")] + pub save_interval: i64, + + #[structopt(long, default_value="300", help="The time (in steps) between the last player leaving a room and the room getting unloaded. Unloading is only done when the rooms are saved, so it could actually take up to save_interval more steps")] + pub unload_age: i64, + } diff --git a/src/main.rs b/src/main.rs index 9020e3c..58faf21 100644 --- a/src/main.rs +++ b/src/main.rs @@ -134,7 +134,6 @@ fn main(){ println!("asciifarm started"); - let mut count = 0; while running.load(Ordering::SeqCst) { let actions = gameserver.update(); for action in actions { @@ -161,9 +160,9 @@ fn main(){ } } world.update(); - if count % 50 == 0 { + if world.time.0 % config.save_interval == 0 { world.save(); - world.unload_rooms(); + world.unload_rooms(config.unload_age); } let messages = world.view(); for (player, mut message) in messages { @@ -177,8 +176,7 @@ fn main(){ } } - count += 1; - sleep(Duration::from_millis(100)); + sleep(Duration::from_millis(config.step_duration)); } println!("saving world"); world.save(); diff --git a/src/timestamp.rs b/src/timestamp.rs index 645fb0d..86dfa2a 100644 --- a/src/timestamp.rs +++ b/src/timestamp.rs @@ -17,3 +17,10 @@ impl Sub<i64> for Timestamp { Self(self.0 - other) } } + +impl Sub<Self> for Timestamp { + type Output = i64; + fn sub(self, other: Self) -> i64 { + self.0 - other.0 + } +} diff --git a/src/world.rs b/src/world.rs index b42a1db..6e65f84 100644 --- a/src/world.rs +++ b/src/world.rs @@ -27,9 +27,9 @@ pub struct World<'a, 'b> { default_room: RoomId, players: HashMap<PlayerId, RoomId>, rooms: HashMap<RoomId, Room<'a, 'b>>, - room_age: HashMap<RoomId, u32>, + room_age: HashMap<RoomId, Timestamp>, encyclopedia: Encyclopedia, - time: Timestamp, + pub time: Timestamp, default_dispatcher: Dispatcher<'a, 'b> } @@ -65,7 +65,7 @@ impl <'a, 'b>World<'a, 'b> { } fn get_room_mut(&mut self, id: &RoomId) -> Result<&mut Room<'a, 'b>> { - self.room_age.insert(id.clone(), 0); + self.room_age.insert(id.clone(), self.time); let result = self.get_room_mut_(id); if let Err(err) = &result { println!("Failed to load room {:?}: {:?}", id, err); @@ -208,15 +208,14 @@ impl <'a, 'b>World<'a, 'b> { } } - pub fn unload_rooms(&mut self){ + pub fn unload_rooms(&mut self, min_age: i64){ 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); + self.room_age.insert(roomid.clone(), self.time); } else { - let age = *self.room_age.get(&roomid).unwrap_or(&0) + 1; - self.room_age.insert(roomid.clone(), age); - if age > 2 { + let age = self.time - *self.room_age.get(&roomid).unwrap_or(&Timestamp(0)); + if age >= min_age { to_remove.push(roomid.clone()); } } |
