← All Workshops

MudEngine Part 9: The Twin Guardians

Step 8 / 12

Update the Game Component — Quest UI

The client needs four changes in src/components/game.rs (the component Part 8 left you with):

  1. Quest display types — QuestDisplay and QuestGuardianDisplay, stored in a signal
  2. New signals — quest_display: Option<QuestDisplay> and vault_open: bool
  3. Receive loop — the State message now carries quest: Option<QuestInfo>; also handle QuestEvent and QuestCompleted
  4. RSX — a quest panel with live guardian chips, King Aldric's dialog, and the descend/ascend buttons

Quest display types

Add these alongside ChatMessage, before the Game component:

mud-engine/src/components/game.rs
#[derive(Clone, Debug)]
struct QuestDisplay {
    name: String,
    active: bool,
    completed: bool,
    guardians: Vec<QuestGuardianDisplay>,
}

#[derive(Clone, Debug)]
struct QuestGuardianDisplay {
    label: String,
    room_id: usize,
}
mud-engine/src/components/game.rs
#[component]
fn Game(name: String) -> Element {
    let mut players = use_signal::<HashMap<String, PlayerInfo>>(|| HashMap::new());
    let mut my_id = use_signal(|| String::new());
    let mut rooms = use_signal::<HashMap<usize, RoomInfo>>(|| HashMap::new());
    let mut chat_messages = use_signal::<Vec<ChatMessage>>(|| Vec::new());
    let mut command_input = use_signal(|| String::new());
    let mut quest_display = use_signal::<Option<QuestDisplay>>(|| None);
    let mut vault_open = use_signal(|| false);

    let mut socket = use_websocket(move || {
        mud_ws(name.clone(), WebSocketOptions::new())
    });
mud-engine/src/components/game.rs
                        ServerMessage::State {
                            players: p,
                            your_id,
                            rooms: r,
                            quest,
                        } => {
                            let mut map = players.write();
                            map.clear();
                            for pl in p {
                                map.insert(pl.id.clone(), pl);
                            }
                            my_id.set(your_id);

                            let mut room_map = rooms.write();
                            room_map.clear();
                            for room in r {
                                room_map.insert(room.id, room);
                            }

                            // Render the quest panel straight from the server data
                            if let Some(q) = quest {
                                quest_display.set(Some(QuestDisplay {
                                    name: q.name,
                                    active: q.active,
                                    completed: q.completed,
                                    guardians: q
                                        .guardians
                                        .into_iter()
                                        .map(|g| QuestGuardianDisplay {
                                            label: g.label,
                                            room_id: g.room_id,
                                        })
                                        .collect(),
                                }));
                                vault_open.set(q.active);
                            }
                        }
mud-engine/src/components/game.rs
                        ServerMessage::QuestEvent { active, message } => {
                            vault_open.set(active);
                            if let Some(ref mut qd) = *quest_display.write() {
                                qd.active = active;
                            }

                            let mut msgs = chat_messages.write();
                            msgs.push(ChatMessage {
                                from: "⚡ Quest".into(),
                                message,
                                room: 0,
                                is_mine: false,
                            });
                        }
                        ServerMessage::QuestCompleted { message } => {
                            if let Some(ref mut qd) = *quest_display.write() {
                                qd.completed = true;
                                qd.active = false;
                            }
                            vault_open.set(false);

                            let mut msgs = chat_messages.write();
                            msgs.push(ChatMessage {
                                from: "👑 King Aldric".into(),
                                message,
                                room: 0,
                                is_mine: false,
                            });
                        }
mud-engine/src/components/game.rs
            // ── Quest panel (between grid and bottom-area) ──
            if let Some(ref quest) = *quest_display.read() {
                if quest.completed {
                    div { class: "quest-panel completed",
                        div { class: "quest-title", "👑 Quest Complete: {quest.name}" }
                        div { class: "quest-complete-msg",
                            "King Aldric has the Star Fragment! The realm is saved!"
                        }
                    }
                } else {
                    div { class: "quest-panel",
                        div { class: "quest-title",
                            if quest.active { "🔓 " } else { "🔒 " }
                            "{quest.name}"
                        }
                        div { class: "quest-phase-hint",
                            if quest.active {
                                "Portal open! A hero must descend, retrieve the Star Fragment, and bring it to the King."
                            } else {
                                "Place two heroes on the pedestals to open the portal."
                            }
                        }
                        div { class: "quest-guardians",
                            for g in &quest.guardians {
                                {
                                    // Occupancy is derived live from the players
                                    // signal, so the chips stay accurate as heroes move.
                                    let g_active = players.read()
                                        .values()
                                        .any(|p| p.room == g.room_id);
                                    rsx! {
                                        div {
                                            key: "{g.label}",
                                            class: if g_active {
                                                "quest-guardian active"
                                            } else {
                                                "quest-guardian"
                                            },
                                            if g_active { "✅ " } else { "⏳ " }
                                            "{g.label}"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // ── King Aldric NPC dialog (shown in Town Square) ──
            if let Some(ref info) = my_info {
                if info.room == 4 {
                    div { class: "npc-dialog",
                        div { class: "npc-name", "👑 King Aldric" }
                        if quest_display.read().as_ref().map(|q| q.completed).unwrap_or(false) {
                            div { class: "npc-text",
                                ""The Star Fragment glows with ancient power! You have my eternal gratitude, brave adventurers!""
                            }
                        } else if info.items.contains(&"star fragment".into()) {
                            div { class: "npc-text highlight",
                                ""You have it! The Star Fragment! Quick, hand it to me so I can restore the wards!""
                            }
                        } else if *vault_open.read() {
                            div { class: "npc-text",
                                ""The portal is open! Someone must venture below and retrieve the Star Fragment!""
                            }
                        } else {
                            div { class: "npc-text",
                                ""The Ancient Vault lies sealed. I need two brave souls to stand on the Sun and Moon Pedestals to open the way.""
                            }
                        }
                    }
                }
            }

            // ── Quest action buttons ──
            if *vault_open.read() {
                if let Some(ref info) = my_info {
                    if info.room == 4 {
                        div { class: "quest-actions",
                            button {
                                class: "quest-btn",
                                onclick: move |_| {
                                    send(ClientMessage::Move { direction: "down".into() });
                                },
                                "⬇ Descend into the Ancient Vault"
                            }
                        }
                    }
                }
            }

            if let Some(ref info) = my_info {
                if info.room == 9 {
                    div { class: "quest-actions",
                        button {
                            class: "quest-btn",
                            onclick: move |_| {
                                send(ClientMessage::Move { direction: "up".into() });
                            },
                            "⬆ Ascend to Town Square"
                        }
                    }
                }
            }
💡 Quest data comes from the server

The quest UI is data-driven: the State message carries quest: Option<QuestInfo>, so the client renders the panel from real server data — no hardcoded guardian room IDs, no guessing about completion.

A few things to notice:

  • Live guardian chips — occupancy is derived from the players signal on every render (.any(|p| p.room == g.room_id)), so a PlayerMoved refresh updates the chips immediately without extra server messages.
  • vault_open — toggles the descend button. QuestEvent keeps it in sync while connected; State sets it correctly for players who join mid-quest.
  • Persisted completion — completed travels in State too. The server restores it from Players.toml (via LiveState.quest_completed), so after a restart reconnecting players still see the quest panel in its completed state.
Step 8 / 12