blastmud/blastmud_game/src/models/task.rs
2023-04-16 01:54:03 +10:00

115 lines
3.0 KiB
Rust

use serde::{Serialize, Deserialize};
use serde_json::Value;
use chrono::{DateTime, Utc};
use crate::services::effect::DelayedHealthEffect;
use std::collections::VecDeque;
use crate::static_content::room::Direction;
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum TaskRecurrence {
FixedDuration { seconds: u32 }
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[serde(tag="task_type", content="task_details")]
pub enum TaskDetails {
RunQueuedCommand,
NPCSay {
npc_code: String,
say_code: String
},
NPCWander {
npc_code: String,
},
NPCAggro {
npc_code: String,
},
AttackTick,
RecloneNPC {
npc_code: String
},
RotCorpse {
corpse_code: String
},
DelayedHealth {
item: String,
effect_series: VecDeque<DelayedHealthEffect>
},
ExpireItem {
item_code: String
},
ChargeRoom {
zone_item: String,
daily_price: u64
},
SwingShut {
room_item: String,
direction: Direction
}
}
impl TaskDetails {
pub fn name(self: &Self) -> &'static str {
use TaskDetails::*;
match self {
RunQueuedCommand => "RunQueuedCommand",
NPCSay { .. } => "NPCSay",
NPCWander { .. } => "NPCWander",
NPCAggro { .. } => "NPCAggro",
AttackTick => "AttackTick",
RecloneNPC { .. } => "RecloneNPC",
RotCorpse { .. } => "RotCorpse",
DelayedHealth { .. } => "DelayedHealth",
ExpireItem { .. } => "ExpireItem",
ChargeRoom { .. } => "ChargeRoom",
SwingShut { .. } => "SwingShut",
// Don't forget to add to TASK_HANDLER_REGISTRY in regular_tasks.rs too.
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct TaskMeta {
pub task_code: String,
pub is_static: bool,
pub recurrence: Option<TaskRecurrence>,
pub consecutive_failure_count: u32,
pub next_scheduled: DateTime<Utc>,
}
impl Default for TaskMeta {
fn default() -> Self {
Self {
task_code: "unspecified".to_string(),
is_static: false,
recurrence: None,
consecutive_failure_count: 0,
next_scheduled: Utc::now() + chrono::Duration::seconds(3600)
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Task {
#[serde(flatten)]
pub meta: TaskMeta,
#[serde(flatten)]
pub details: TaskDetails,
// Be careful of backwards compatibility if you add anything new
// (consider Option).
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct TaskOther {
#[serde(flatten)]
pub meta: TaskMeta,
pub task_type: String,
pub task_details: Value
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
#[serde(untagged)]
pub enum TaskParse {
Known(Task),
Unknown(TaskOther)
}