MudEngine Part 8: Inventory & Chat
Update Data Models
We need to add items to several types. These changes touch the shared types (src/game/persistence.rs, re-exported through messages.rs) and the ServerMessage definitions.
The structure stays exactly as Part 7 left it — persistence.rs holds the shared persisted/runtime/wire types plus the thin file wrappers; messages.rs re-exports them and defines the wire messages. We add items to RoomInfo and PlayerInfo, and add one new piece of live server state: a ground map tracking which items currently lie in each room.
Because RoomInfo is the single room type — Game.toml, runtime, and wire in one — adding items there automatically gives the static seed in Game.toml, the runtime room, and the field the client renders. There are no separate TomlRoom/TomlPlayer mirrors to keep in sync, so this is the only place items is defined per type.
| Type | File | What changes |
|---|---|---|
PlayerInfo | persistence.rs (re-exported) | New items: Vec<String> field (inventory) |
RoomInfo | persistence.rs (re-exported) | New items: Vec<String> field — the static seed in Game.toml and the live ground the client renders |
ClientMessage | messages.rs | New Say { message }, Take { item }, Drop { item } variants |
ServerMessage | messages.rs | New Chat { .. }, ItemTaken { .. }, ItemDropped { .. } variants |
TomlPlayers | persistence.rs | New room_items: Vec<TomlRoomItems> section ([[room_items]] in Players.toml) |
GameState | persistence.rs | New ground: HashMap<usize, Vec<String>> (live item positions) |
Note what is not in the list: there are no TomlRoom, TomlPlayer, or TomlExit types to update, and no separate runtime RoomData. RoomInfo/PlayerInfo are the single room/player type, so a field added there shows up in the TOML file, the runtime state, and the wire message at once. The only genuine new data shape is the live ground map — see below.
/// The single room type: Game.toml seed, runtime room, and wire payload. /// `items` is the static item layout from Game.toml; the live positions live in /// `GameState.ground` and override it when the State message is built. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct RoomInfo { pub id: usize, pub name: String, pub description: String, pub exits: Vec<Exit>, pub items: Vec<String>, } /// The single player type: Players.toml, runtime map, and wire message. /// `items` is the player's carried inventory. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct PlayerInfo { pub id: String, pub name: String, pub room: usize, pub items: Vec<String>, }
#[derive(Serialize, Deserialize, Debug, Clone)] pub enum ClientMessage { Move { direction: String }, Say { message: String }, Take { item: String }, Drop { item: String }, }
#[derive(Serialize, Deserialize, Debug, Clone)] pub 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, }, }
/// One live ground override: a room and the items currently lying there. /// This is the file shape for `[[room_items]]` in Players.toml — the only /// genuine conversion left, because TOML stores arrays of tables while the /// runtime keeps a `HashMap<usize, Vec<String>>`. #[derive(Serialize, Deserialize)] struct TomlRoomItems { room: usize, items: Vec<String>, } #[derive(Serialize, Deserialize)] struct TomlPlayers { #[serde(default)] players: Vec<PlayerInfo>, #[serde(default)] room_items: Vec<TomlRoomItems>, }
In earlier parts there were separate `TomlRoom`, `TomlExit`, and `TomlPlayer` mirror types that had to be edited **and** hand-mapped into runtime types. Part 7 removed them: `RoomInfo`/`PlayerInfo` (with `Exit`) are the single room/player type, reused for Game.toml, runtime, and wire. That is why adding `items` here means exactly **one** edit per concept — `RoomInfo` and `PlayerInfo`. `Game.toml` rooms automatically gain the `items` seed, `GameState.players` holds inventories, and the wire `State`/`PlayerMoved` messages carry them, all from the same field. The only genuinely new data shape is the live `ground` map. Its file form (`[[room_items]]`) is a `TomlRoomItems` wrapper — kept because TOML serializes an array of tables, not a `HashMap`. Everything else is reused as-is.
#[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>>, }
In Part 7 RoomInfo's rooms were purely static: they came from Game.toml and never changed. Items break that — they move constantly as players take and drop them. Instead of mutating the static rooms, the live positions live in their own GameState.ground map (room_id → items currently lying there).
The ground map is the items half of the autosave contract from Part 7:
- Seeding — at startup it's filled from each room's
itemsinGame.toml(the static, committed layout) - Ownership — from then on it's live in-memory state, flushed to
Players.tomlby the autosave task - Never recombined — the static world in
Game.tomlis never written back to
RoomInfo.items holds the static seed from Game.toml. The client's RoomInfo.items is filled from ground (the live positions) when the State message is built.
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.