diff --git a/migrations/2022-09-12-175351_populate_object/down.sql b/migrations/2022-09-12-175351_populate_object/down.sql index 291a97c..d95c04c 100644 --- a/migrations/2022-09-12-175351_populate_object/down.sql +++ b/migrations/2022-09-12-175351_populate_object/down.sql @@ -1 +1 @@ --- This file should undo anything in `up.sql` \ No newline at end of file +TRUNCATE TABLE object; diff --git a/migrations/2022-09-15-133454_create_npc/down.sql b/migrations/2022-09-15-133454_create_npc/down.sql new file mode 100644 index 0000000..5e1cc87 --- /dev/null +++ b/migrations/2022-09-15-133454_create_npc/down.sql @@ -0,0 +1 @@ +DROP TABLE npc diff --git a/migrations/2022-09-15-133454_create_npc/up.sql b/migrations/2022-09-15-133454_create_npc/up.sql new file mode 100644 index 0000000..0186a27 --- /dev/null +++ b/migrations/2022-09-15-133454_create_npc/up.sql @@ -0,0 +1,10 @@ +CREATE TABLE npc ( + tag varchar(100) NOT NULL, + nombre varchar(100) NOT NULL, + descripcion varchar(2048) NOT NULL, + location varchar(100) NOT NULL, + ca int NOT NULL, + ba int NOT NULL, + hp int NOT NULL, + CONSTRAINT PK_Player PRIMARY KEY (tag) +) diff --git a/migrations/2022-09-15-134151_populate_npc/down.sql b/migrations/2022-09-15-134151_populate_npc/down.sql new file mode 100644 index 0000000..b75d20e --- /dev/null +++ b/migrations/2022-09-15-134151_populate_npc/down.sql @@ -0,0 +1 @@ +TRUNCATE TABLE npc; diff --git a/migrations/2022-09-15-134151_populate_npc/up.sql b/migrations/2022-09-15-134151_populate_npc/up.sql new file mode 100644 index 0000000..33b7437 --- /dev/null +++ b/migrations/2022-09-15-134151_populate_npc/up.sql @@ -0,0 +1,5 @@ +INSERT INTO npc +(tag, nombre, descripcion, location, ca, ba, hp) +VALUES +('npc0', 'babosa', 'Babosa nuclear, suelta baba radiactiva.', '1', 10, 1, 8), +('npc1', 'tentaculo', 'Tentáculo bailarín.', '2', 12, 1, 4); diff --git a/src/main.rs b/src/main.rs index 1dc3883..df20d31 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ pub mod objects; pub mod schema; pub mod models; pub mod database; +pub mod npc; // use crate::player::Player; use crate::rlib::set_player; diff --git a/src/models.rs b/src/models.rs index 82903d9..6bce165 100644 --- a/src/models.rs +++ b/src/models.rs @@ -3,6 +3,7 @@ use crate::schema::*; #[derive(Queryable)] #[diesel(table_name = location)] +#[diesel(primary_key(tag))] pub struct RLocation { pub tag: String, pub nombre: Option, @@ -17,6 +18,7 @@ pub struct RLocation { #[derive(Queryable, Insertable)] #[diesel(table_name = object)] +#[diesel(primary_key(tag))] pub struct RObject { pub tag: String, pub nombre: String, @@ -25,18 +27,6 @@ pub struct RObject { pub peso: i32, } -// #[derive(Queryable, Insertable)] -// #[diesel(table_name = npc)] -// pub struct Rnpc { -// pub tag: String, -// pub nombre: String, -// pub descripcion: String, -// pub location: String, -// pub ca: i32, -// pub ba: i32, -// pub hp: i32, -// } - #[derive(Queryable, Insertable, AsChangeset)] #[diesel(table_name = player)] #[diesel(primary_key(tag))] @@ -49,3 +39,16 @@ pub struct Player { pub ba: i32, pub hp: i32, } + +#[derive(Queryable, Insertable)] +#[diesel(table_name = npc)] +#[diesel(primary_key(tag))] +pub struct Rnpc { + pub tag: String, + pub nombre: String, + pub descripcion: String, + pub location: String, + pub ca: i32, + pub ba: i32, + pub hp: i32, +} diff --git a/src/npc.rs b/src/npc.rs new file mode 100644 index 0000000..d53e06a --- /dev/null +++ b/src/npc.rs @@ -0,0 +1,22 @@ +use crate::models::*; +use diesel::prelude::*; +use crate::database::establish_connection; + +//get_npc_here trae da base de datos o npc polo nome e a pegatina da sala actual +pub fn get_npc_here(pegatina: String, nome: String) -> Result { + use crate::schema::npc::dsl::*; + let conn = &mut establish_connection(); + npc + .filter(location.eq(pegatina)) + .filter(nombre.eq(nome)) + .first(conn) +} + +// get_npcs trae da base de datos todos os npcs de unha localización +pub fn get_npcs(pegatina: String) -> Result, diesel::result::Error> { + use crate::schema::npc::dsl::*; + let conn = &mut establish_connection(); + npc + .filter(location.eq(pegatina)) + .get_results(conn) +} diff --git a/src/rlocation.rs b/src/rlocation.rs index 1e6c51c..2a1e506 100644 --- a/src/rlocation.rs +++ b/src/rlocation.rs @@ -1,4 +1,5 @@ use crate::objects::get_object_here; +use crate::npc::{get_npc_here, get_npcs}; use crate::models::*; use diesel::prelude::*; @@ -6,13 +7,26 @@ use crate::database::establish_connection; pub fn execute_look(player: &mut Player, nombre: &str) -> String { - let output: String; + let mut output: String; let player_location = &player.location; let location = get_location(player_location.to_string()); if nombre == "sala" { if player_location.as_str() != "" { output = String::from(location.unwrap().descripcion); + + // agregar á salida os npcs que esteñan na sala + let npcs = get_npcs(player_location.to_string()); + + if npcs.is_ok() && npcs.as_ref().unwrap().len() > 0 { + output.push_str("\n---\nTambién ves:\n"); + for objeto in npcs.unwrap() { + let mut nombre = "- ".to_string(); + nombre.push_str(&objeto.nombre); + nombre.push_str("\n"); + output.push_str(&nombre); + } + } } else { output = String::from("Navengado el ether!"); } @@ -22,7 +36,13 @@ pub fn execute_look(player: &mut Player, nombre: &str) -> String { if objeto.is_ok() { output = String::from(objeto.unwrap().descripcion); } else { - output = String::from("No encuentro lo que quieres mirar."); + // non é un obxeto ou non existe, proba con npc + let npc = get_npc_here(player_location.to_string(), nombre.to_string()); + if npc.is_ok() { + output = String::from(npc.unwrap().descripcion); + } else { + output = String::from("No encuentro lo que quieres mirar."); + } } } else { output = String::from("Qué quieres mirar?"); diff --git a/src/schema.rs b/src/schema.rs index 8a89cf3..6e3ddfe 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -14,6 +14,18 @@ diesel::table! { } } +diesel::table! { + npc (tag) { + tag -> Varchar, + nombre -> Varchar, + descripcion -> Varchar, + location -> Varchar, + ca -> Integer, + ba -> Integer, + hp -> Integer, + } +} + diesel::table! { object (tag) { tag -> Varchar, @@ -38,6 +50,7 @@ diesel::table! { diesel::allow_tables_to_appear_in_same_query!( location, + npc, object, player, );