diff --git a/src/objects.rs b/src/objects.rs index 3c8fb61..f15c13c 100644 --- a/src/objects.rs +++ b/src/objects.rs @@ -35,7 +35,7 @@ pub fn get_object_here(pegatina: String, nome: String) -> Result Result { use crate::schema::object::dsl::*; - println!("{} {}", pegatina, new_location); + // println!("{} {}", pegatina, new_location); let conn = &mut establish_connection(); diesel::update(object.filter(tag.eq(pegatina))) .set(location.eq(new_location)) @@ -48,7 +48,7 @@ pub fn execute_search(player: &mut Player) -> String { let current = &player.location; let objetos = get_objects(current.to_string()); - if objetos.is_ok() { + if objetos.is_ok() && objetos.as_ref().unwrap().len() > 0 { output = String::from("En esta sala encuentras los siguientes objetos:\n"); for objeto in objetos.unwrap() { let mut nombre = objeto.nombre; @@ -56,7 +56,7 @@ pub fn execute_search(player: &mut Player) -> String { output.push_str(&nombre); } } else { - output = String::from("No encuentras nada en esta sala.") + output = String::from("No encuentras ningún objeto en esta sala.") } output diff --git a/src/player.rs b/src/player.rs index e26e161..9dea293 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,4 +1,4 @@ -use crate::objects::get_objects; +use crate::objects::{get_objects, get_object_here, update_object_location}; pub struct Player { pub tag: String, @@ -11,7 +11,7 @@ pub fn execute_inventory(player: &mut Player) -> String { let current = &player.tag; let objetos = get_objects(current.to_string()); - if objetos.is_ok() { + if objetos.is_ok() && objetos.as_ref().unwrap().len() > 0 { output = String::from("Portas los siguientes objetos:\n"); for objeto in objetos.unwrap() { let mut nombre = objeto.nombre; @@ -24,3 +24,25 @@ pub fn execute_inventory(player: &mut Player) -> String { output } + +pub fn execute_drop(player: &mut Player, nombre: &str) -> String { + let output: String; + let location = &player.tag; + let obxeto = get_object_here(location.to_string(), nombre.to_string()); + + if obxeto.is_ok() { + let res = update_object_location( + obxeto.as_ref().unwrap().tag.clone(), + player.location.to_string()); //tirao aquí + + if res.is_ok() && res.unwrap() > 0 { + output = format!("Tiras {}.", obxeto.unwrap().nombre); + } else { + output = format!("Error tirando {}.", obxeto.unwrap().nombre); + } + } else { + output = String::from("No encuentro lo que quieres tirar."); + } + + output +} diff --git a/src/rlib.rs b/src/rlib.rs index a949860..d867f55 100644 --- a/src/rlib.rs +++ b/src/rlib.rs @@ -1,6 +1,6 @@ use std::io::{self, Write}; use crate::rlocation::{execute_go, execute_look}; -use crate::player::{Player, execute_inventory}; +use crate::player::{Player, execute_inventory, execute_drop}; use crate::objects::{execute_search, execute_pick}; pub struct Command { @@ -58,6 +58,7 @@ pub fn update_state(player: &mut Player, command: &Command) -> String { "o" => output = execute_go(player, "o"), "buscar" => output = execute_search(player), "coger" => output = execute_pick(player, command.noun.as_str()), + "tirar" => output = execute_drop(player, command.noun.as_str()), "inventario" => output = execute_inventory(player), _ => output = format!("No se como hacer eso."), }