MudEngine Part 8: Inventory & Chat
Persist items: seed, load, and snapshot
Now that the data models carry items, the persistence layer needs three additions — all inside persistence.rs, keeping the Part 7 shape:
seed_ground— parseGame.tomland build theroom_id → itemsmap from each room's staticitems(the initial world layout)load_players— now also reads the live[[room_items]]section fromPlayers.tomland returns aLiveState(players + ground overrides)snapshot— serializes players (with inventories) and the live ground intoPlayers.toml. Note what it does not touch:Game.tomlstays read-only, exactly as in Part 7.
Also update the RoomInfo construction in the WebSocket endpoint to fill items from ground.
#[cfg(feature = "server")] /// Read Game.toml and build the initial item layout: room_id → items lying there. pub fn seed_ground(path: &str) -> HashMap<usize, Vec<String>> { let content = std::fs::read_to_string(path).unwrap_or_else(|e| panic!("Failed to read {}: {}", path, e)); let toml_world: TomlWorld = toml::from_str(&content).unwrap_or_else(|e| panic!("Failed to parse {}: {}", path, e)); toml_world .rooms .into_iter() .map(|r| (r.id, r.items)) .collect() } #[cfg(feature = "server")] /// What load_players returns: the live players and the live ground overrides. pub struct LiveState { pub players: HashMap<String, PlayerInfo>, /// rooms whose ground has diverged from the Game.toml seed pub ground: HashMap<usize, Vec<String>>, } #[cfg(feature = "server")] pub fn load_players(path: &str) -> LiveState { let Ok(content) = std::fs::read_to_string(path) else { return LiveState { players: HashMap::new(), ground: HashMap::new(), }; }; let toml_players: TomlPlayers = toml::from_str(&content).unwrap_or_else(|e| panic!("Failed to parse {}: {}", path, e)); let players = toml_players .players .into_iter() .map(|p| (p.id.clone(), p)) .collect(); let ground = toml_players .room_items .into_iter() .map(|ri| (ri.room, ri.items)) .collect(); LiveState { players, ground } }
#[cfg(feature = "server")] /// Serialize the live state into the TOML that goes into Players.toml. /// Fast (a few strings), so it is safe to run while holding the lock. /// The disk write itself happens separately, in `write_players`. pub fn snapshot(state: &GameState) -> String { let toml_players = TomlPlayers { players: state.players.values().cloned().collect(), room_items: state .ground .iter() .map(|(room, items)| TomlRoomItems { room: *room, items: items.clone(), }) .collect(), }; toml::to_string_pretty(&toml_players).unwrap() }
Part 7's write loop is unchanged: the autosave task wakes every AUTOSAVE_INTERVAL, calls persistence::snapshot(&state) under the lock, then persistence::write_players(&snapshot) outside it. The WebSocket handlers below are pure in-memory mutators — they never touch disk.
write_players and can_move need no changes at all. The only things they didn't know about — inventories and ground items — are fully captured by snapshot. Inventories serialize with zero effort because PlayerInfo already is the file type; only the ground map needs the little TomlRoomItems wrapper (HashMap → array of tables).
Because every mutation now waits for the next autosave tick to reach Players.toml, lower AUTOSAVE_INTERVAL to Duration::from_secs(5) while working through this part (see Part 7's sync step) so you can watch the file update live.
You'll notice we use .clone() on item strings in several places. Items are small strings — Vec<String> is cheap to clone. In a production MUD with thousands of items you might use Arc<String> or interned IDs, but for a workshop demo the simplicity of cloning strings is the right trade-off.