MudEngine Part 8: Inventory & Chat
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 | What changes |
|---|---|
PlayerInfo | New items: Vec<String> field |
TomlRoom | New items: Vec<String> field |
TomlPlayer | New items: Vec<String> field |
RoomInfo | New items: Vec<String> field |
RoomData | New items: Vec<String> field |
ClientMessage | New Say { message }, Take { item }, Drop { item } variants |
ServerMessage | New Chat { .. }, ItemTaken { .. }, ItemDropped { .. } variants |
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] struct PlayerInfo { id: String, name: String, room: usize, items: Vec<String>, }
#[derive(Serialize, Deserialize, Debug, Clone)] enum ClientMessage { Move { direction: String }, Say { message: String }, Take { item: String }, Drop { item: String }, }
#[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, }, }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] struct RoomInfo { id: usize, name: String, description: String, items: Vec<String>, }
#[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>, }
#[cfg(feature = "server")] struct RoomData { id: usize, name: String, description: String, exits: Vec<(String, usize)>, items: Vec<String>, }
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.