cargo fmt

This commit is contained in:
serxoz 2022-09-26 11:59:53 +02:00
parent 952acda755
commit cb8b9790f0
9 changed files with 104 additions and 109 deletions

View File

@ -6,8 +6,6 @@ use std::env;
pub fn establish_connection() -> PgConnection {
dotenv().ok();
let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
PgConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url))
}

View File

@ -1,19 +1,19 @@
use std::{env, io};
use tokio::io::AsyncWriteExt;
use tokio::net::{TcpListener, TcpStream};
use std::{env, io};
pub mod database;
pub mod models;
pub mod npc;
pub mod objects;
pub mod player;
pub mod rlib;
pub mod rlocation;
pub mod player;
pub mod objects;
pub mod schema;
pub mod models;
pub mod database;
pub mod npc;
// use crate::player::Player;
use crate::rlib::set_player;
use crate::player::update_player;
use crate::rlib::set_player;
#[tokio::main]
async fn main() -> io::Result<()> {
@ -50,10 +50,13 @@ async fn procesar_socket(mut socket: TcpStream) -> io::Result<()> {
let mut output: String;
let mut player = set_player(&mut socket).await;
let intro = format!("Bienvenido {}. Vamos a empezar a jugar... \n\n\
let intro = format!(
"Bienvenido {}. Vamos a empezar a jugar... \n\n\
Despiertas en la oscuridad con un fuerte dolor de cabeza. \n\
Un fuerte sonido de alarma machaca tus oídos. No ayuda nada a tu dolor de cabeza. \n\
Qué vas a hacer?", player.nombre);
Qué vas a hacer?",
player.nombre
);
socket.write_all(intro.as_bytes()).await?;
// main loop

View File

@ -1,5 +1,5 @@
use diesel::prelude::*;
use crate::schema::*;
use diesel::prelude::*;
#[derive(Queryable)]
#[diesel(table_name = location)]

View File

@ -1,13 +1,12 @@
use crate::database::establish_connection;
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))
npc.filter(location.eq(pegatina))
.filter(nombre.eq(nome))
.first(conn)
}
@ -16,7 +15,5 @@ pub fn get_npc_here(pegatina: String, nome: String) -> Result<Rnpc, diesel::resu
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)
npc.filter(location.eq(pegatina)).get_results(conn)
}

View File

@ -1,6 +1,6 @@
use crate::database::establish_connection;
use crate::models::RObject;
use diesel::prelude::*;
use crate::database::establish_connection;
//get_object trae da base de datos un obxeto pola sua pegatina
pub fn get_object(pegatina: String) -> Result<RObject, diesel::result::Error> {
@ -33,7 +33,10 @@ pub fn get_object_here(pegatina: String, nome: String) -> Result<RObject, diesel
}
// 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> {
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();
@ -43,7 +46,10 @@ pub fn update_object_location(pegatina: String, new_location: String) -> Result<
}
// actualiza as condiciós de un obxeto, devolve o número de rows afectados
pub fn update_object_conditions(pegatina: String, cond: Vec<Option<String>>) -> Result<usize, diesel::result::Error> {
pub fn update_object_conditions(
pegatina: String,
cond: Vec<Option<String>>,
) -> Result<usize, diesel::result::Error> {
use crate::schema::object::dsl::*;
let conn = &mut establish_connection();
diesel::update(object.filter(tag.eq(pegatina)))
@ -71,4 +77,3 @@ pub fn get_salidas_here(pegatina: String, nome: String) -> Result<RObject, diese
.filter(nombre.eq(nome))
.first(conn)
}

View File

@ -1,17 +1,17 @@
use crate::objects::{get_objects, get_object_here, update_object_location, update_object_conditions};
use crate::models::Player;
use crate::database::establish_connection;
use diesel::prelude::*;
use crate::models::Player;
use crate::npc::get_npc_here;
use crate::objects::{
get_object_here, get_objects, update_object_conditions, update_object_location,
};
use diesel::prelude::*;
use rand::Rng;
// get_player le o xogador polo nick
pub fn get_player(nome: String) -> Result<Player, diesel::result::Error> {
use crate::schema::player::dsl::*;
let conn = &mut establish_connection();
player
.filter(nombre.eq(nome))
.first(conn)
player.filter(nombre.eq(nome)).first(conn)
}
// create_player garda o xogador
@ -82,9 +82,8 @@ pub fn execute_pick(player: &mut Player, nombre: &str) -> String {
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());
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);
@ -107,7 +106,8 @@ pub fn execute_drop(player: &mut Player, nombre: &str) -> String {
if obxeto.is_ok() {
let res = update_object_location(
obxeto.as_ref().unwrap().tag.clone(),
player.location.to_string()); //tirao aquí
player.location.to_string(),
); //tirao aquí
if res.is_ok() && res.unwrap() > 0 {
output = format!("Tiras {}.", obxeto.unwrap().nombre);
@ -136,7 +136,10 @@ pub fn execute_kill(player: &mut Player, nombre: &str) -> String {
if tohit >= malo.ca {
let damage = rand::thread_rng().gen_range(1..6);
malo.hp = malo.hp - damage;
let tmp = format!("Golpeas a {} por {} de daño, le quedan {} pg.", malo.nombre, damage, malo.hp);
let tmp = format!(
"Golpeas a {} por {} de daño, le quedan {} pg.",
malo.nombre, damage, malo.hp
);
output = format!("{}\n{}", output, tmp);
} else {
let tmp = format!("No consigues golpear a {}.", malo.nombre);
@ -148,7 +151,10 @@ pub fn execute_kill(player: &mut Player, nombre: &str) -> String {
if tohit >= player.ca {
let damage = rand::thread_rng().gen_range(1..6);
player.hp = player.hp - damage;
let tmp = format!("{} te golpea por {} de daño, te quedan {} pg.", malo.nombre, damage, player.hp);
let tmp = format!(
"{} te golpea por {} de daño, te quedan {} pg.",
malo.nombre, damage, player.hp
);
output = format!("{}\n{}", output, tmp);
} else {
let tmp = format!("{} no consigue herirte.", malo.nombre);
@ -166,7 +172,6 @@ pub fn execute_kill(player: &mut Player, nombre: &str) -> String {
resolucion = String::from("No se que ha pasado. Alguien debería estar muerto.");
}
output = format!("{}\n{}", output, resolucion);
} else {
output = String::from("No encuentro lo que quieres matar.");
}

View File

@ -1,8 +1,8 @@
use tokio::net::TcpStream;
use tokio::io::{AsyncReadExt,AsyncWriteExt};
use crate::rlocation::{execute_go, execute_look};
use crate::player::*;
use crate::models::Player;
use crate::player::*;
use crate::rlocation::{execute_go, execute_look};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use uuid::Uuid;
pub struct Command {
@ -29,14 +29,14 @@ impl Command {
pub async fn set_player(socket: &mut TcpStream) -> Player {
// prompt
socket.write_all(b"\n").await.unwrap();
socket.write_all("Cuál es tu nombre?\n".as_bytes()).await.unwrap();
socket
.write_all("Cuál es tu nombre?\n".as_bytes())
.await
.unwrap();
socket.write_all(b"> ").await.unwrap();
let mut buf = vec![0; 1024];
let n = socket
.read(&mut buf)
.await
.unwrap();
let n = socket.read(&mut buf).await.unwrap();
let input_str = String::from_utf8(buf[0..n].to_vec()).unwrap();
@ -75,10 +75,7 @@ pub async fn get_input(socket: &mut TcpStream) -> Command {
socket.write_all(b"> ").await.unwrap();
let mut buf = vec![0; 1024];
let n = socket
.read(&mut buf)
.await
.unwrap();
let n = socket.read(&mut buf).await.unwrap();
let input_str = String::from_utf8(buf[0..n].to_vec()).unwrap();

View File

@ -1,10 +1,9 @@
use crate::objects::{get_salidas, get_object_here, get_salidas_here};
use crate::npc::{get_npc_here, get_npcs};
use crate::objects::{get_object_here, get_salidas, get_salidas_here};
use crate::database::establish_connection;
use crate::models::*;
use diesel::prelude::*;
use crate::database::establish_connection;
pub fn execute_look(player: &mut Player, nombre: &str) -> String {
let mut output: String;
@ -40,7 +39,6 @@ pub fn execute_look(player: &mut Player, nombre: &str) -> String {
output.push_str(&nombre);
}
}
} else {
output = String::from("Navengado el ether!");
}
@ -62,7 +60,7 @@ pub fn execute_look(player: &mut Player, nombre: &str) -> String {
output = String::from("Qué quieres mirar?");
}
return output
return output;
}
pub fn execute_go(player: &mut Player, coord: &str) -> String {
@ -106,7 +104,6 @@ pub fn execute_go(player: &mut Player, coord: &str) -> String {
} else {
output = String::from("No existe esa salida.");
}
} else {
output = String::from("No entiendo a donde quieres ir. Debes elegir una coordenada correspondiente a las salidas de esta sala.");
}
@ -118,7 +115,5 @@ pub fn execute_go(player: &mut Player, coord: &str) -> String {
pub fn get_location(pegatina: String) -> Result<RLocation, diesel::result::Error> {
use crate::schema::location::dsl::*;
let conn = &mut establish_connection();
location
.filter(tag.eq(pegatina))
.first(conn)
location.filter(tag.eq(pegatina)).first(conn)
}

View File

@ -45,9 +45,4 @@ diesel::table! {
}
}
diesel::allow_tables_to_appear_in_same_query!(
location,
npc,
object,
player,
);
diesel::allow_tables_to_appear_in_same_query!(location, npc, object, player,);