← All Workshops

MudEngine Part 9: The Twin Guardians

Step 4 / 12

Quest Data Models

Quest support needs types in two files — the same split Part 8 left you with:

  • src/game/persistence.rs — the quest config (TomlQuest) mirroring the [quest] section in Game.toml, plus the server-only runtime state (QuestStatus)
  • src/game/messages.rs — the client-facing wire types (QuestInfo) and the new ServerMessage variants

GameState gains an Option<QuestStatus> field, and the persistence types learn the one live quest bit: completed.

Start with the config types in src/game/persistence.rs:

mud-engine/src/game/persistence.rs
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TomlQuestGuardian {
    pub name: String,
    pub room_id: usize,
    pub label: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TomlQuestReward {
    pub source_room_id: usize,
    pub target_room_id: usize,
    pub direction: String,
    pub target_name: String,
    pub target_description: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TomlQuest {
    pub name: String,
    pub retrieval_item: String,
    pub completion_room: usize,
    pub guardians: Vec<TomlQuestGuardian>,
    pub reward: TomlQuestReward,
}
mud-engine/src/game/persistence.rs
/// Runtime quest state — tracked live on the server.
/// Only the `completed` flag is persisted (to Players.toml via the autosave task).
#[cfg(feature = "server")]
pub struct QuestStatus {
    pub config: TomlQuest,
    /// Whether the portal gate is open right now — session-only, never persisted.
    pub previously_active: bool,
    /// Whether the Star Fragment reached the King. Persisted.
    pub completed: bool,
}
mud-engine/src/game/persistence.rs
#[cfg(feature = "server")]
pub struct GameState {
    pub rooms: Vec<RoomInfo>,
    pub players: HashMap<String, PlayerInfo>,
    /// Live item positions: which items currently lie on the floor of each room.
    /// Seeded from Game.toml, then flushes to Players.toml via the autosave task.
    pub ground: HashMap<usize, Vec<String>>,
    /// The active quest, if Game.toml has a `[quest]` section.
    pub quest: Option<QuestStatus>,
}
mud-engine/src/game/messages.rs
/// Quest info sent to the client in the State message — the client renders
/// its quest UI from this data, nothing is hardcoded.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct QuestInfo {
    pub name: String,
    pub active: bool,
    pub completed: bool,
    pub guardians: Vec<QuestGuardianInfo>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct QuestGuardianInfo {
    pub name: String,
    pub room_id: usize,
    pub label: String,
    pub active: bool,
}
mud-engine/src/game/messages.rs
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ServerMessage {
    State {
        players: Vec<PlayerInfo>,
        your_id: String,
        rooms: Vec<RoomInfo>,
        /// Quest data so the client can render quest status immediately
        /// — None when Game.toml has no `[quest]` section.
        quest: Option<QuestInfo>,
    },
    PlayerJoined(PlayerInfo),
    PlayerMoved(PlayerInfo),
    PlayerLeft { id: String },
    Chat {
        from: String,
        message: String,
        room: usize,
    },
    ItemTaken {
        player_id: String,
        player_name: String,
        item: String,
    },
    ItemDropped {
        player_id: String,
        player_name: String,
        item: String,
        room: usize,
    },
    QuestEvent {
        active: bool,
        message: String,
    },
    QuestCompleted {
        message: String,
    },
}
💡 Config ↔ runtime ↔ wire

The quest exists in three shapes, matching the pattern you already know from Part 7 and 8:

  • TomlQuest (persistence.rs) — raw mirror of the [quest] section in Game.toml. Read once at startup, never written back.
  • QuestStatus (persistence.rs, server-only) — the config plus live runtime bits: previously_active (is the portal open right now?) and completed (did the fragment reach the King?).
  • QuestInfo (messages.rs) — what the server puts into State so the client can render the quest panel without hardcoding room IDs.

Note what's missing from QuestInfo: the reward table. That's the server-side portal plumbing (which room opens where) — the client never needs it. Only name, the active/completed flags, and the two guardian pedestals cross the wire.

Step 4 / 12