From a10a109887c58175a10da72c859221247341cf2e Mon Sep 17 00:00:00 2001 From: serxoz Date: Tue, 13 Sep 2022 19:11:46 +0200 Subject: [PATCH] =?UTF-8?q?reorganizaci=C3=B3n,=20funciones=20del=20jugado?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/objects.rs | 44 -------------------------------------------- src/player.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/rlib.rs | 5 ++--- 3 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/objects.rs b/src/objects.rs index f15c13c..59f4dd0 100644 --- a/src/objects.rs +++ b/src/objects.rs @@ -1,7 +1,6 @@ use crate::models::*; use diesel::prelude::*; use crate::database::establish_connection; -use crate::player::Player; //get_object trae da base de datos un obxeto pola sua pegatina @@ -41,46 +40,3 @@ pub fn update_object_location(pegatina: String, new_location: String) -> Result< .set(location.eq(new_location)) .execute(conn) } - -// execute_search mostra os obxetos na ubicación do xogador -pub fn execute_search(player: &mut Player) -> String { - let mut output: String; - let current = &player.location; - let objetos = get_objects(current.to_string()); - - 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; - nombre.push_str("\n"); - output.push_str(&nombre); - } - } else { - output = String::from("No encuentras ningún objeto en esta sala.") - } - - output -} - -// execute_pick recolle un obxeto -pub fn execute_pick(player: &mut Player, nombre: &str) -> String { - let output: String; - let location = &player.location; - 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.tag.to_string()); - - if res.is_ok() && res.unwrap() > 0 { - output = format!("{} ahora está en tu inventario.", obxeto.unwrap().nombre); - } else { - output = format!("Error cogiendo {}.", obxeto.unwrap().nombre); - } - } else { - output = String::from("No encuentro lo que quieres coger."); - } - - output -} diff --git a/src/player.rs b/src/player.rs index 9dea293..2a32faa 100644 --- a/src/player.rs +++ b/src/player.rs @@ -6,6 +6,27 @@ pub struct Player { pub location: String, } +// execute_search mostra os obxetos na ubicación do xogador +pub fn execute_search(player: &mut Player) -> String { + let mut output: String; + let current = &player.location; + let objetos = get_objects(current.to_string()); + + 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; + nombre.push_str("\n"); + output.push_str(&nombre); + } + } else { + output = String::from("No encuentras ningún objeto en esta sala.") + } + + output +} + +// execute_inventory abre o inventario pub fn execute_inventory(player: &mut Player) -> String { let mut output: String; let current = &player.tag; @@ -25,6 +46,30 @@ pub fn execute_inventory(player: &mut Player) -> String { output } +// execute_pick recolle un obxeto +pub fn execute_pick(player: &mut Player, nombre: &str) -> String { + let output: String; + let location = &player.location; + 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.tag.to_string()); + + if res.is_ok() && res.unwrap() > 0 { + output = format!("{} ahora está en tu inventario.", obxeto.unwrap().nombre); + } else { + output = format!("Error cogiendo {}.", obxeto.unwrap().nombre); + } + } else { + output = String::from("No encuentro lo que quieres coger."); + } + + output +} + +// execute_drop tira un obxeto pub fn execute_drop(player: &mut Player, nombre: &str) -> String { let output: String; let location = &player.tag; diff --git a/src/rlib.rs b/src/rlib.rs index d867f55..06be803 100644 --- a/src/rlib.rs +++ b/src/rlib.rs @@ -1,7 +1,6 @@ use std::io::{self, Write}; use crate::rlocation::{execute_go, execute_look}; -use crate::player::{Player, execute_inventory, execute_drop}; -use crate::objects::{execute_search, execute_pick}; +use crate::player::{Player, execute_search, execute_inventory, execute_pick, execute_drop}; pub struct Command { pub verb: String, @@ -57,9 +56,9 @@ pub fn update_state(player: &mut Player, command: &Command) -> String { "s" => output = execute_go(player, "s"), "o" => output = execute_go(player, "o"), "buscar" => output = execute_search(player), + "inventario" => output = execute_inventory(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."), }