reorganización, funciones del jugador

This commit is contained in:
serxoz 2022-09-13 19:11:46 +02:00
parent 2a9373b393
commit a10a109887
3 changed files with 47 additions and 47 deletions

View File

@ -1,7 +1,6 @@
use crate::models::*; use crate::models::*;
use diesel::prelude::*; use diesel::prelude::*;
use crate::database::establish_connection; use crate::database::establish_connection;
use crate::player::Player;
//get_object trae da base de datos un obxeto pola sua pegatina //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)) .set(location.eq(new_location))
.execute(conn) .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
}

View File

@ -6,6 +6,27 @@ pub struct Player {
pub location: String, 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 { pub fn execute_inventory(player: &mut Player) -> String {
let mut output: String; let mut output: String;
let current = &player.tag; let current = &player.tag;
@ -25,6 +46,30 @@ pub fn execute_inventory(player: &mut Player) -> String {
output 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 { pub fn execute_drop(player: &mut Player, nombre: &str) -> String {
let output: String; let output: String;
let location = &player.tag; let location = &player.tag;

View File

@ -1,7 +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, execute_drop}; use crate::player::{Player, execute_search, execute_inventory, execute_pick, execute_drop};
use crate::objects::{execute_search, execute_pick};
pub struct Command { pub struct Command {
pub verb: String, pub verb: String,
@ -57,9 +56,9 @@ pub fn update_state(player: &mut Player, command: &Command) -> String {
"s" => output = execute_go(player, "s"), "s" => output = execute_go(player, "s"),
"o" => output = execute_go(player, "o"), "o" => output = execute_go(player, "o"),
"buscar" => output = execute_search(player), "buscar" => output = execute_search(player),
"inventario" => output = execute_inventory(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()), "tirar" => output = execute_drop(player, command.noun.as_str()),
"inventario" => output = execute_inventory(player),
_ => output = format!("No se como hacer eso."), _ => output = format!("No se como hacer eso."),
} }