feat: Added CLI rooms (dis-)connect functionality

This commit is contained in:
Alexey 2025-12-04 12:54:22 +03:00
commit 5d6aa0422d
2 changed files with 53 additions and 5 deletions

View file

@ -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);
},
MapCommands::Disconnect(_) => {
todo!();
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(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}");
}
}
}
}
}

View file

@ -1 +1 @@
room = []