diff --git a/src/bin/cli.rs b/src/bin/cli.rs index 19bbf4e..ff2bac7 100644 --- a/src/bin/cli.rs +++ b/src/bin/cli.rs @@ -620,11 +620,59 @@ fn main() -> Result<(), Error> { } } }, - MapCommands::Connect(_) => { - todo!(); + MapCommands::Connect(args) => { + // We iterate twice to make references first->second and second->first + for (first, second) in [(args.first, args.second),(args.second, args.first)] { + let Some(room) = map.room.iter_mut().find(|r| r.id == first) else { + panic!("Error: Room #{} not found", first); + }; + + match room.children.iter().find(|id| **id == second) { + Some(_) => { + println!("Room #{} already has reference to #{}", first, second); + }, + None => { + room.children.push(second); + } + } + } + + match map.save(map_path.parent().unwrap_or(Path::new("")).to_owned()) { + Ok(_) => { + println!("Connected rooms #{} <-> #{}.", args.first, args.second); + println!("Successfully saved map."); + }, + Err(error) => { + eprintln!("Error while saving map: {error}"); + } + } }, - MapCommands::Disconnect(_) => { - todo!(); + MapCommands::Disconnect(args) => { + // We iterate twice to make references first->second and second->first + for (first, second) in [(args.first, args.second),(args.second, args.first)] { + let Some(room) = map.room.iter_mut().find(|r| r.id == first) else { + panic!("Error: Room #{} not found", first); + }; + + match room.children.iter().position(|id| *id == second) { + Some(id) => { + room.children.remove(id as usize); + }, + None => { + println!("Room #{} has no reference to #{}", first, second); + } + } + } + + match map.save(map_path.parent().unwrap_or(Path::new("")).to_owned()) { + Ok(_) => { + println!("Disconnected rooms #{} #{}.", args.first, args.second); + println!("Successfully saved map."); + }, + Err(error) => { + eprintln!("Error while saving map: {error}"); + } + } } } } diff --git a/tests/io/map.toml b/tests/io/map.toml index 8b13789..38dffa5 100644 --- a/tests/io/map.toml +++ b/tests/io/map.toml @@ -1 +1 @@ - +room = []