← All Workshops

MudEngine Part 8: Inventory & Chat

Step 4 / 10

Update Data Models

We need to add items to several types. These changes touch both the TOML types (for serialization) and runtime types (for game logic), plus the RoomInfo sent to clients.

Changes at a glance

Update the types in src/main.rs. Start with the shared message types at the top:

🔍 Type changes
TypeWhat changes
PlayerInfoNew items: Vec<String> field
TomlRoomNew items: Vec<String> field
TomlPlayerNew items: Vec<String> field
RoomInfoNew items: Vec<String> field
RoomDataNew items: Vec<String> field
ClientMessageNew Say { message }, Take { item }, Drop { item } variants
ServerMessageNew Chat { .. }, ItemTaken { .. }, ItemDropped { .. } variants
mud-engine/src/main.rs
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
struct PlayerInfo {
    id: String,
    name: String,
    room: usize,
    items: Vec<String>,
}
mud-engine/src/main.rs
#[derive(Serialize, Deserialize, Debug, Clone)]
enum ClientMessage {
    Move { direction: String },
    Say { message: String },
    Take { item: String },
    Drop { item: String },
}
mud-engine/src/main.rs
#[derive(Serialize, Deserialize, Debug, Clone)]
enum ServerMessage {
    State {
        players: Vec<PlayerInfo>,
        your_id: String,
        rooms: Vec<RoomInfo>,
    },
    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,
    },
}
mud-engine/src/main.rs
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
struct RoomInfo {
    id: usize,
    name: String,
    description: String,
    items: Vec<String>,
}
mud-engine/src/main.rs
#[derive(Serialize, Deserialize)]
struct TomlRoom {
    id: usize,
    name: String,
    description: String,
    exits: Vec<TomlExit>,
    items: Vec<String>,
}

#[derive(Serialize, Deserialize)]
struct TomlPlayer {
    id: String,
    name: String,
    room: usize,
    items: Vec<String>,
}
mud-engine/src/main.rs
#[cfg(feature = "server")]
struct RoomData {
    id: usize,
    name: String,
    description: String,
    exits: Vec<(String, usize)>,
    items: Vec<String>,
}
💡 Why Chat includes `room`

The Chat message includes a room field so the client can decide whether to display it. The server broadcasts chat to all players (through the existing broadcast channel), and each client filters: only show chats where room == my_room.

This is the simplest approach — no room-based broadcast channels, no per-room subscriber lists. For a 9-room demo MUD with tens of players, broadcasting everything is negligible overhead. For a production MUD with thousands of players you'd want room-scoped channels, but that's a future refactor.

Step 4 / 10