← All Workshops

MudEngine Part 7: Save & Load

Step 14 / 17

Update the WorldGrid component

WorldGrid is the component that draws the 3×3 grid. Part 6 fed it room names and exits from the hardcoded ROOM_NAMES / EXITS statics. Now it receives the server's RoomInfo map as a new rooms prop and derives everything from that:

  • Room names — looked up by id from rooms
  • Clickable cells — the server sends each room's exits (loaded from Game.toml), so WorldGrid still knows which neighbouring cells are reachable and keeps rendering them as .clickable
  • Movement — unchanged, cells still call onmove with the direction

RoomCell, DirectionPad, Button, and Separator need no changes at all. Replace the contents of src/components/world_grid.rs:

mud-engine/src/components/world_grid.rs
use std::collections::HashMap;

use crate::components::room_cell::RoomCell;
use crate::game::messages::{PlayerInfo, RoomInfo};
use dioxus::prelude::*;

#[component]
pub fn WorldGrid(
    players: ReadSignal<HashMap<String, PlayerInfo>>,
    my_id: ReadSignal<String>,
    rooms: ReadSignal<HashMap<usize, RoomInfo>>,
    onmove: Callback<String>,
) -> Element {
    let my_room = players.read().get(&*my_id.read()).map(|p| p.room);

    // Rooms reachable from the current room, mapped to the direction name.
    // Exits come from the server-loaded room data (Game.toml) via the State message.
    let reachable: HashMap<usize, String> = match my_room {
        Some(room) => rooms
            .read()
            .get(&room)
            .map(|r| {
                r.exits
                    .iter()
                    .map(|e| (e.destination, e.direction.clone()))
                    .collect()
            })
            .unwrap_or_default(),
        None => HashMap::new(),
    };

    let cells: Vec<Element> = (0..9)
        .map(|idx| {
            let name = rooms
                .read()
                .get(&idx)
                .map(|r| r.name.clone())
                .unwrap_or_default();
            let occupants: Vec<String> = players
                .read()
                .values()
                .filter(|p| p.room == idx)
                .filter(|p| *my_id.read() != p.id)
                .map(|p| p.name.clone())
                .collect();
            let clickable = reachable.contains_key(&idx);
            let dir = reachable.get(&idx).cloned().unwrap_or_default();
            rsx! {
                RoomCell {
                    key: "{idx}",
                    name,
                    active: my_room == Some(idx),
                    clickable,
                    players: occupants,
                    onclick: move |_| if !dir.is_empty() { onmove.call(dir.clone()) },
                }
            }
        })
        .collect();

    rsx! {
        div { class: "world-grid", {cells.into_iter()} }
    }
}
💡 Removed statics

The old ROOM_NAMES, ROOM_DESCS, and EXITS statics in src/game/room_data.rs are now completely unused — every component reads room data from the server instead.

Open src/game/room_data.rs and delete the file, and remove its pub mod room_data; declaration from src/game.rs. Any statics that stay behind generate compiler warnings, so clean up after yourself:

// src/game.rs — module index after this step
pub mod messages;
pub mod random_name;
pub mod persistence;
pub mod server;
Step 14 / 17