This commit is contained in:
serxoz 2022-09-13 19:06:39 +02:00
parent 8dc914e5e6
commit 2a9373b393
3 changed files with 29 additions and 6 deletions

View File

@ -35,7 +35,7 @@ pub fn get_object_here(pegatina: String, nome: String) -> Result<RObject, diesel
// actualiza a localización de un obxeto, devolve o número de rows afectados // actualiza a localización de un obxeto, devolve o número de rows afectados
pub fn update_object_location(pegatina: String, new_location: String) -> Result<usize, diesel::result::Error> { pub fn update_object_location(pegatina: String, new_location: String) -> Result<usize, diesel::result::Error> {
use crate::schema::object::dsl::*; use crate::schema::object::dsl::*;
println!("{} {}", pegatina, new_location); // println!("{} {}", pegatina, new_location);
let conn = &mut establish_connection(); let conn = &mut establish_connection();
diesel::update(object.filter(tag.eq(pegatina))) diesel::update(object.filter(tag.eq(pegatina)))
.set(location.eq(new_location)) .set(location.eq(new_location))
@ -48,7 +48,7 @@ pub fn execute_search(player: &mut Player) -> String {
let current = &player.location; let current = &player.location;
let objetos = get_objects(current.to_string()); 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"); output = String::from("En esta sala encuentras los siguientes objetos:\n");
for objeto in objetos.unwrap() { for objeto in objetos.unwrap() {
let mut nombre = objeto.nombre; let mut nombre = objeto.nombre;
@ -56,7 +56,7 @@ pub fn execute_search(player: &mut Player) -> String {
output.push_str(&nombre); output.push_str(&nombre);
} }
} else { } else {
output = String::from("No encuentras nada en esta sala.") output = String::from("No encuentras ningún objeto en esta sala.")
} }
output output

View File

@ -1,4 +1,4 @@
use crate::objects::get_objects; use crate::objects::{get_objects, get_object_here, update_object_location};
pub struct Player { pub struct Player {
pub tag: String, pub tag: String,
@ -11,7 +11,7 @@ pub fn execute_inventory(player: &mut Player) -> String {
let current = &player.tag; let current = &player.tag;
let objetos = get_objects(current.to_string()); 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"); output = String::from("Portas los siguientes objetos:\n");
for objeto in objetos.unwrap() { for objeto in objetos.unwrap() {
let mut nombre = objeto.nombre; let mut nombre = objeto.nombre;
@ -24,3 +24,25 @@ pub fn execute_inventory(player: &mut Player) -> String {
output 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
}

View File

@ -1,6 +1,6 @@
use std::io::{self, Write}; use std::io::{self, Write};
use crate::rlocation::{execute_go, execute_look}; 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}; use crate::objects::{execute_search, execute_pick};
pub struct Command { pub struct Command {
@ -58,6 +58,7 @@ pub fn update_state(player: &mut Player, command: &Command) -> String {
"o" => output = execute_go(player, "o"), "o" => output = execute_go(player, "o"),
"buscar" => output = execute_search(player), "buscar" => output = execute_search(player),
"coger" => output = execute_pick(player, command.noun.as_str()), "coger" => output = execute_pick(player, command.noun.as_str()),
"tirar" => output = execute_drop(player, command.noun.as_str()),
"inventario" => output = execute_inventory(player), "inventario" => output = execute_inventory(player),
_ => output = format!("No se como hacer eso."), _ => output = format!("No se como hacer eso."),
} }