gardando o estado do xogador na base de datos

This commit is contained in:
serxoz 2022-09-15 13:30:08 +02:00
parent c2471743d0
commit 248d67ea3d
11 changed files with 205 additions and 16 deletions

88
Cargo.lock generated
View File

@ -14,6 +14,12 @@ version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "diesel" name = "diesel"
version = "2.0.0" version = "2.0.0"
@ -55,6 +61,17 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "getrandom"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]] [[package]]
name = "idna" name = "idna"
version = "0.3.0" version = "0.3.0"
@ -65,6 +82,12 @@ dependencies = [
"unicode-normalization", "unicode-normalization",
] ]
[[package]]
name = "libc"
version = "0.2.132"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5"
[[package]] [[package]]
name = "mysqlclient-sys" name = "mysqlclient-sys"
version = "0.2.5" version = "0.2.5"
@ -87,6 +110,12 @@ version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
[[package]]
name = "ppv-lite86"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]] [[package]]
name = "proc-macro-error" name = "proc-macro-error"
version = "1.0.4" version = "1.0.4"
@ -129,12 +158,43 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
]
[[package]] [[package]]
name = "reentrada" name = "reentrada"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"diesel", "diesel",
"dotenv", "dotenv",
"uuid",
] ]
[[package]] [[package]]
@ -195,6 +255,28 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "uuid"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd6469f4314d5f1ffec476e05f17cc9a78bc7a27a6a857842170bdf8d6f98d2f"
dependencies = [
"getrandom",
"rand",
"uuid-macro-internal",
]
[[package]]
name = "uuid-macro-internal"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "548f7181a5990efa50237abb7ebca410828b57a8955993334679f8b50b35c97d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.15" version = "0.2.15"
@ -206,3 +288,9 @@ name = "version_check"
version = "0.9.4" version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

View File

@ -8,3 +8,11 @@ edition = "2021"
[dependencies] [dependencies]
diesel = { version = "2.0.0", features = ["mysql"] } diesel = { version = "2.0.0", features = ["mysql"] }
dotenv = "0.15.0" dotenv = "0.15.0"
[dependencies.uuid]
version = "1.1.2"
features = [
"v4", # Lets you generate random UUIDs
"fast-rng", # Use a faster (but still sufficiently random) RNG
"macro-diagnostics", # Enable better diagnostics for compile-time UUIDs
]

8
diesel.toml Normal file
View File

@ -0,0 +1,8 @@
# For documentation on how to configure this file,
# see https://diesel.rs/guides/configuring-diesel-cli
[print_schema]
file = "src/schema.rs"
[migrations_directory]
dir = "migrations"

View File

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

View File

@ -0,0 +1,10 @@
CREATE TABLE player (
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

@ -8,6 +8,7 @@ pub mod database;
// use crate::player::Player; // use crate::player::Player;
use crate::rlib::set_player; use crate::rlib::set_player;
use crate::player::update_player;
fn main() { fn main() {
// intro // intro
@ -30,7 +31,7 @@ fn main() {
let mut output: String; let mut output: String;
let mut player = set_player(); let mut player = set_player();
println!("Bienvenido {}. Vamos a empezar a jugar...", player.name); println!("Bienvenido {}. Vamos a empezar a jugar...", player.nombre);
println!(""); println!("");
println!("Despiertas en la oscuridad con un fuerte dolor de cabeza."); println!("Despiertas en la oscuridad con un fuerte dolor de cabeza.");
println!("Un fuerte sonido de alarma machaca tus oídos. No ayuda nada a tu dolor de cabeza."); println!("Un fuerte sonido de alarma machaca tus oídos. No ayuda nada a tu dolor de cabeza.");
@ -41,6 +42,7 @@ fn main() {
command = rlib::get_input(); command = rlib::get_input();
output = rlib::update_state(&mut player, &command); output = rlib::update_state(&mut player, &command);
rlib::update_screen(output); rlib::update_screen(output);
update_player(&player).expect("Error actualizando jugador!!");
} }
// salir // salir

View File

@ -1,6 +1,8 @@
use diesel::prelude::*; use diesel::prelude::*;
use crate::schema::*;
#[derive(Queryable)] #[derive(Queryable)]
#[diesel(table_name = location)]
pub struct RLocation { pub struct RLocation {
pub tag: String, pub tag: String,
pub nombre: Option<String>, pub nombre: Option<String>,
@ -13,7 +15,8 @@ pub struct RLocation {
pub salida_abajo: Option<String>, pub salida_abajo: Option<String>,
} }
#[derive(Queryable)] #[derive(Queryable, Insertable)]
#[diesel(table_name = object)]
pub struct RObject { pub struct RObject {
pub tag: String, pub tag: String,
pub nombre: String, pub nombre: String,
@ -22,8 +25,22 @@ pub struct RObject {
pub peso: i32, pub peso: i32,
} }
#[derive(Queryable)] // #[derive(Queryable, Insertable)]
pub struct Rnpc { // #[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))]
pub struct Player {
pub tag: String, pub tag: String,
pub nombre: String, pub nombre: String,
pub descripcion: String, pub descripcion: String,

View File

@ -1,9 +1,35 @@
use crate::objects::{get_objects, get_object_here, update_object_location}; use crate::objects::{get_objects, get_object_here, update_object_location};
use crate::models::Player;
use crate::database::establish_connection;
use diesel::prelude::*;
pub struct Player { // get_player le o xogador polo nick
pub tag: String, pub fn get_player(nome: String) -> Result<Player, diesel::result::Error> {
pub name: String, use crate::schema::player::dsl::*;
pub location: String, let conn = &mut establish_connection();
player
.filter(nombre.eq(nome))
.first(conn)
}
// create_player garda o xogador
pub fn create_player(newplayer: &Player) -> Result<usize, diesel::result::Error> {
use crate::schema::player;
let conn = &mut establish_connection();
diesel::insert_into(player::table)
.values(newplayer)
.execute(conn)
}
// update_player actualiza o xogador
pub fn update_player(newplayer: &Player) -> Result<usize, diesel::result::Error> {
use crate::schema::player;
let conn = &mut establish_connection();
diesel::update(player::table)
.set(newplayer) // co trait AsChangeSet podese actualizar a struct enteira
.execute(conn)
} }
// execute_search mostra os obxetos na ubicación do xogador // execute_search mostra os obxetos na ubicación do xogador

View File

@ -1,6 +1,8 @@
use std::io::{self, Write}; use std::io::{self, Write};
use crate::rlocation::{execute_go, execute_look}; use crate::rlocation::{execute_go, execute_look};
use crate::player::{Player, execute_search, execute_inventory, execute_pick, execute_drop}; use crate::player::*;
use crate::models::Player;
use uuid::Uuid;
pub struct Command { pub struct Command {
pub verb: String, pub verb: String,
@ -40,12 +42,27 @@ pub fn set_player() -> Player {
// parse // parse
let nick = input_str.trim(); let nick = input_str.trim();
// instance player //comprobar si existe
let player = Player{ // si existe instanciase o que xa está na base de datos
tag: "player1".to_string() , // si non existe instanciase o novo e gárdase na base de datos
name: nick.to_string(), let player: Player;
location: "0".to_string()
}; let check_player = get_player(nick.to_string());
if check_player.is_ok() {
player = check_player.unwrap();
} else {
player = Player{
tag: Uuid::new_v4().to_string(),
nombre: nick.to_string(),
descripcion: "humano, equipo básico".to_string(),
location: "0".to_string(),
ca: 10,
ba: 0,
hp: 8,
};
create_player(&player).expect("Error creando jugador!!");
}
// return // return
player player

View File

@ -1,4 +1,3 @@
use crate::player::Player;
use crate::objects::get_object_here; use crate::objects::get_object_here;
use crate::models::*; use crate::models::*;

View File

@ -24,7 +24,20 @@ diesel::table! {
} }
} }
diesel::table! {
player (tag) {
tag -> Varchar,
nombre -> Varchar,
descripcion -> Varchar,
location -> Varchar,
ca -> Integer,
ba -> Integer,
hp -> Integer,
}
}
diesel::allow_tables_to_appear_in_same_query!( diesel::allow_tables_to_appear_in_same_query!(
location, location,
object, object,
player,
); );