npcs, listado nas salas dos npc e mirar npc

This commit is contained in:
serxoz 2022-09-15 16:17:38 +02:00
parent 248d67ea3d
commit 01fac69f0b
10 changed files with 91 additions and 15 deletions

View File

@ -1 +1 @@
-- This file should undo anything in `up.sql`
TRUNCATE TABLE object;

View File

@ -0,0 +1 @@
DROP TABLE npc

View File

@ -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)
)

View File

@ -0,0 +1 @@
TRUNCATE TABLE npc;

View File

@ -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);

View File

@ -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;

View File

@ -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<String>,
@ -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,
}

22
src/npc.rs Normal file
View File

@ -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<Rnpc, diesel::result::Error> {
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<Vec<Rnpc>, diesel::result::Error> {
use crate::schema::npc::dsl::*;
let conn = &mut establish_connection();
npc
.filter(location.eq(pegatina))
.get_results(conn)
}

View File

@ -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?");

View File

@ -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,
);