blastmud/blastmud_game/src/models/task.rs

147 lines
3.9 KiB
Rust
Raw Normal View History

use crate::services::effect::{DelayedHealthEffect, DelayedMessageEffect};
2023-04-16 01:54:03 +10:00
use crate::static_content::room::Direction;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::VecDeque;
use super::effect::EffectType;
#[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,
2023-01-22 01:16:00 +11:00
},
NPCWander {
npc_code: String,
},
NPCAggro {
npc_code: String,
},
2023-01-23 22:52:01 +11:00
AttackTick,
ShareTick,
2023-01-23 22:52:01 +11:00
RecloneNPC {
npc_code: String,
2023-01-23 22:52:01 +11:00
},
RotCorpse {
corpse_code: String,
2023-01-23 22:52:01 +11:00
},
2023-02-25 23:49:46 +11:00
DelayedHealth {
item: String,
effect_series: VecDeque<DelayedHealthEffect>,
},
DelayedMessage {
item: String,
effect_series: VecDeque<DelayedMessageEffect>,
},
ExpireItem {
item_code: String,
},
ChargeRoom {
zone_item: String,
daily_price: u64,
2023-04-16 01:54:03 +10:00
},
SwingShut {
room_item: String,
direction: Direction,
},
DestroyUser {
username: String,
},
ChargeWages {
npc: String,
},
TickUrges,
ResetSpawns,
2023-09-20 23:56:28 +10:00
ResetHanoi,
HospitalERSeePatient {
item: String,
},
DispelEffect {
target: String,
effect_type: EffectType,
},
}
impl TaskDetails {
pub fn name(self: &Self) -> &'static str {
use TaskDetails::*;
match self {
RunQueuedCommand => "RunQueuedCommand",
NPCSay { .. } => "NPCSay",
NPCWander { .. } => "NPCWander",
NPCAggro { .. } => "NPCAggro",
2023-01-23 22:52:01 +11:00
AttackTick => "AttackTick",
ShareTick => "ShareTick",
2023-01-23 22:52:01 +11:00
RecloneNPC { .. } => "RecloneNPC",
RotCorpse { .. } => "RotCorpse",
2023-02-25 23:49:46 +11:00
DelayedHealth { .. } => "DelayedHealth",
DelayedMessage { .. } => "DelayedMessage",
DispelEffect { .. } => "DispelEffect",
ExpireItem { .. } => "ExpireItem",
ChargeRoom { .. } => "ChargeRoom",
2023-04-16 01:54:03 +10:00
SwingShut { .. } => "SwingShut",
DestroyUser { .. } => "DestroyUser",
ChargeWages { .. } => "ChargeWages",
TickUrges => "TickUrges",
ResetSpawns => "ResetSpawns",
2023-09-20 23:56:28 +10:00
ResetHanoi => "ResetHanoi",
HospitalERSeePatient { .. } => "HospitalERSeePatient",
// 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>,
}
2023-01-02 13:25:05 +11:00
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),
2023-01-02 13:25:05 +11:00
}
}
}
#[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),
}