2022-09-12 20:47:35 +02:00
|
|
|
use crate::models::*;
|
|
|
|
use diesel::prelude::*;
|
|
|
|
use crate::database::establish_connection;
|
2022-09-13 18:50:01 +02:00
|
|
|
use crate::player::Player;
|
2022-09-08 19:13:08 +02:00
|
|
|
|
|
|
|
|
2022-09-12 20:47:35 +02:00
|
|
|
//get_object trae da base de datos un obxeto pola sua pegatina
|
|
|
|
pub fn get_object(pegatina: String) -> Result<RObject, diesel::result::Error> {
|
|
|
|
use crate::schema::object::dsl::*;
|
|
|
|
let conn = &mut establish_connection();
|
|
|
|
object
|
|
|
|
.filter(tag.eq(pegatina))
|
|
|
|
.first(conn)
|
|
|
|
}
|
2022-09-08 19:13:08 +02:00
|
|
|
|
2022-09-12 20:47:35 +02:00
|
|
|
// get_objects trae da base de datos todos os obxetos de unha localización
|
|
|
|
pub fn get_objects(pegatina: String) -> Result<Vec<RObject>, diesel::result::Error> {
|
|
|
|
use crate::schema::object::dsl::*;
|
|
|
|
let conn = &mut establish_connection();
|
|
|
|
object
|
2022-09-13 18:50:01 +02:00
|
|
|
.filter(location.eq(pegatina))
|
2022-09-12 20:47:35 +02:00
|
|
|
.get_results(conn)
|
|
|
|
}
|
2022-09-08 19:13:08 +02:00
|
|
|
|
2022-09-12 20:47:35 +02:00
|
|
|
//get_object_here trae da base de datos o obxeto polo nome e a pegatina da sala actual
|
|
|
|
pub fn get_object_here(pegatina: String, nome: String) -> Result<RObject, diesel::result::Error> {
|
|
|
|
use crate::schema::object::dsl::*;
|
|
|
|
let conn = &mut establish_connection();
|
2022-09-08 19:13:08 +02:00
|
|
|
object
|
2022-09-13 18:50:01 +02:00
|
|
|
.filter(location.eq(pegatina))
|
2022-09-12 20:47:35 +02:00
|
|
|
.filter(nombre.eq(nome))
|
|
|
|
.first(conn)
|
2022-09-08 19:13:08 +02:00
|
|
|
}
|
2022-09-13 18:50:01 +02:00
|
|
|
|
|
|
|
// 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> {
|
|
|
|
use crate::schema::object::dsl::*;
|
|
|
|
println!("{} {}", pegatina, new_location);
|
|
|
|
let conn = &mut establish_connection();
|
|
|
|
diesel::update(object.filter(tag.eq(pegatina)))
|
|
|
|
.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() {
|
|
|
|
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 nada 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
|
|
|
|
}
|