blastmud/blastmud_game/src/services/idlepark.rs
2024-03-11 20:42:52 +11:00

67 lines
2.7 KiB
Rust

use async_trait::async_trait;
use chrono::Utc;
use crate::{
models::{
item::ItemFlag,
task::{Task, TaskDetails, TaskMeta, TaskRecurrence},
},
regular_tasks::{TaskHandler, TaskRunContext},
static_content::StaticTask,
DResult,
};
use std::time;
use super::comms::broadcast_to_room;
pub struct IdleparkTaskHandler;
#[async_trait]
impl TaskHandler for IdleparkTaskHandler {
async fn do_task(&self, ctx: &mut TaskRunContext) -> DResult<Option<time::Duration>> {
for user in ctx.trans.find_users_to_idlepark().await? {
if let Some(player_item) = ctx
.trans
.find_item_by_type_code("player", &user.username.to_lowercase())
.await?
{
if let Some((loc_type, loc_code)) = player_item.location.split_once("/") {
if let Some(loc) = ctx.trans.find_item_by_type_code(loc_type, loc_code).await? {
if !loc.flags.contains(&ItemFlag::PrivatePlace) {
let mut player_item = (*player_item).clone();
broadcast_to_room(&ctx.trans, &player_item.location, None,
&format!(
"A sweeping robot whirs as it passes by, sweeping up the slumbering {} and carrying {} off to the homeless shelter.\n",
&player_item.display_for_sentence(1, false),
&player_item.pronouns.object)).await?;
player_item.location = "room/melbs_homelessshelter".to_owned();
ctx.trans.save_item_model(&player_item).await?;
}
}
}
}
ctx.trans.mark_idleparked(&user.username).await?;
}
Ok(Some(time::Duration::from_secs(120)))
}
}
pub static IDLEPARK_HANDLER: &'static (dyn TaskHandler + Sync + Send) = &IdleparkTaskHandler;
pub fn idlepark_tasks() -> Box<dyn Iterator<Item = StaticTask>> {
Box::new(
vec![StaticTask {
task_code: "IdlePark".to_owned(),
initial_task: Box::new(|| Task {
meta: TaskMeta {
task_code: "IdlePark".to_owned(),
is_static: true,
recurrence: Some(TaskRecurrence::FixedDuration { seconds: 120 }),
next_scheduled: Utc::now() + chrono::TimeDelta::try_seconds(120).unwrap(),
..Default::default()
},
details: TaskDetails::IdlePark,
}),
}]
.into_iter(),
)
}