From 08ce45da5829c5c4deffffc89ecd4dd34fdc4dd0 Mon Sep 17 00:00:00 2001 From: serxoz Date: Tue, 13 Sep 2022 18:50:01 +0200 Subject: [PATCH] buscar e coller --- Cargo.lock | 45 -------------- Cargo.toml | 2 - .../up.sql | 2 +- .../2022-09-12-174911_create_objects/up.sql | 5 +- .../2022-09-12-175351_populate_object/up.sql | 6 +- src/main.rs | 6 +- src/models.rs | 5 +- src/objects.rs | 58 ++++++++++++++++++- src/player.rs | 1 + src/rlib.rs | 3 + src/schema.rs | 5 +- 11 files changed, 78 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 130b281..356c380 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,12 +65,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "itoa" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" - [[package]] name = "mysqlclient-sys" version = "0.2.5" @@ -141,45 +135,6 @@ version = "0.1.0" dependencies = [ "diesel", "dotenv", - "serde", - "serde_json", -] - -[[package]] -name = "ryu" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" - -[[package]] -name = "serde" -version = "1.0.144" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.144" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.85" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" -dependencies = [ - "itoa", - "ryu", - "serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index d00d457..a780026 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -serde = { version = "1.0.144", features = ["derive"]} -serde_json = "1.0.85" diesel = { version = "2.0.0", features = ["mysql"] } dotenv = "0.15.0" diff --git a/migrations/2022-09-12-114839_populate_locations/up.sql b/migrations/2022-09-12-114839_populate_locations/up.sql index 9c75f13..684842b 100644 --- a/migrations/2022-09-12-114839_populate_locations/up.sql +++ b/migrations/2022-09-12-114839_populate_locations/up.sql @@ -1,6 +1,6 @@ INSERT INTO location (tag, nombre, descripcion, salida_norte, salida_sur, salida_este, salida_oeste, salida_arriba, salida_abajo) VALUES -('0', 'Pasillo', 'Estás en un pasillo muy oscuro.\nEscuchas un sonido de alarma en la lejanía.\nApenas puedes ver una luz pulsante al norte.\nEn el suelo encuntras una palanca.\n\n---\nSalidas: norte', "1", NULL, NULL, NULL, NULL, NULL), +('0', 'Pasillo', 'Estás en un pasillo muy oscuro.\nEscuchas un sonido de alarma en la lejanía.\nApenas puedes ver una luz pulsante al norte.\n\n---\nSalidas: norte', "1", NULL, NULL, NULL, NULL, NULL), ('1', 'Puente', 'La única iluminación de esta sala es una pequeña luz pulsante en un panel. Parece el puente de mando. Un humo denso llena la sala irritando tus pulmones. Entre el humo ves una puerta al este tras la cual parece que suena una alarma.\n\n---\nSalidas: este, sur', NULL, "0", "2", NULL, NULL, NULL), ('2', 'Vainas', 'De aquí surje el fuerte sonido de alarma que taladra tus tímpanos. Alrededor de la sala ves las vainas criogénicas en las que has estado durmiendo durante el viaje. Todas están vacías!\n\n---\nSalidas: oeste', NULL, NULL, NULL, "1", NULL, NULL); diff --git a/migrations/2022-09-12-174911_create_objects/up.sql b/migrations/2022-09-12-174911_create_objects/up.sql index 4178bf6..3936198 100644 --- a/migrations/2022-09-12-174911_create_objects/up.sql +++ b/migrations/2022-09-12-174911_create_objects/up.sql @@ -1,7 +1,8 @@ CREATE TABLE object ( tag varchar(100) NOT NULL, - nombre varchar(100), + nombre varchar(100) NOT NULL, descripcion varchar(2048) NOT NULL, - location_tag varchar(100) NOT NULL, + location varchar(100) NOT NULL, + peso int NOT NULL, CONSTRAINT PK_Object PRIMARY KEY (tag) ) diff --git a/migrations/2022-09-12-175351_populate_object/up.sql b/migrations/2022-09-12-175351_populate_object/up.sql index 15cc743..9ff476c 100644 --- a/migrations/2022-09-12-175351_populate_object/up.sql +++ b/migrations/2022-09-12-175351_populate_object/up.sql @@ -1,5 +1,5 @@ INSERT INTO object -(tag, nombre, descripcion, location_tag) +(tag, nombre, descripcion, location, peso) VALUES -('o0', 'palanca', 'Trozo de metal, servirá como palanca', '0'), -('o1', 'calmantes', 'Calmantes, sirven para el dolor', '2'); +('o0', 'palanca', 'Trozo de metal, servirá como palanca', '0', 0), +('o1', 'calmantes', 'Calmantes, sirven para el dolor', '2', 0); diff --git a/src/main.rs b/src/main.rs index 87be942..88f2aef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,11 @@ fn main() { let mut command = rlib::Command::new(); let mut output: String; - let mut player = Player{name: "manolo".to_string(), location: "0".to_string()}; + let mut player = Player{ + tag: "player1".to_string() , + name: "manolo".to_string(), + location: "0".to_string() + }; // main loop while command.verb != "salir" { diff --git a/src/models.rs b/src/models.rs index 9bf1b02..91b5dbf 100644 --- a/src/models.rs +++ b/src/models.rs @@ -16,7 +16,8 @@ pub struct RLocation { #[derive(Queryable)] pub struct RObject { pub tag: String, - pub nombre: Option, + pub nombre: String, pub descripcion: String, - pub location_tag: String, + pub location: String, + pub peso: i32, } diff --git a/src/objects.rs b/src/objects.rs index b3265c9..3c8fb61 100644 --- a/src/objects.rs +++ b/src/objects.rs @@ -1,6 +1,7 @@ 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 @@ -17,7 +18,7 @@ pub fn get_objects(pegatina: String) -> Result, diesel::result::Err use crate::schema::object::dsl::*; let conn = &mut establish_connection(); object - .filter(location_tag.eq(pegatina)) + .filter(location.eq(pegatina)) .get_results(conn) } @@ -26,7 +27,60 @@ pub fn get_object_here(pegatina: String, nome: String) -> Result Result { + 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 +} diff --git a/src/player.rs b/src/player.rs index 1e0f6d9..bf6c9a1 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,4 +1,5 @@ pub struct Player { + pub tag: String, pub name: String, pub location: String, } diff --git a/src/rlib.rs b/src/rlib.rs index e4ae6e1..9dcb0bf 100644 --- a/src/rlib.rs +++ b/src/rlib.rs @@ -1,6 +1,7 @@ use std::io::{self, Write}; use crate::rlocation::{execute_go, execute_look}; use crate::player::Player; +use crate::objects::{execute_search, execute_pick}; pub struct Command { pub verb: String, @@ -55,6 +56,8 @@ pub fn update_state(player: &mut Player, command: &Command) -> String { "e" => output = execute_go(player, "e"), "s" => output = execute_go(player, "s"), "o" => output = execute_go(player, "o"), + "buscar" => output = execute_search(player), + "coger" => output = execute_pick(player, command.noun.as_str()), _ => output = format!("No se como hacer eso."), } diff --git a/src/schema.rs b/src/schema.rs index 59b7e58..dbfb587 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -17,9 +17,10 @@ diesel::table! { diesel::table! { object (tag) { tag -> Varchar, - nombre -> Nullable, + nombre -> Varchar, descripcion -> Varchar, - location_tag -> Varchar, + location -> Varchar, + peso -> Integer, } }