From 248d67ea3de5d839b063267c9b6856b56063270f Mon Sep 17 00:00:00 2001 From: serxoz Date: Thu, 15 Sep 2022 13:30:08 +0200 Subject: [PATCH] gardando o estado do xogador na base de datos --- Cargo.lock | 88 +++++++++++++++++++ Cargo.toml | 8 ++ diesel.toml | 8 ++ .../2022-09-14-140919_create_player/down.sql | 1 + .../2022-09-14-140919_create_player/up.sql | 10 +++ src/main.rs | 4 +- src/models.rs | 23 ++++- src/player.rs | 34 ++++++- src/rlib.rs | 31 +++++-- src/rlocation.rs | 1 - src/schema.rs | 13 +++ 11 files changed, 205 insertions(+), 16 deletions(-) create mode 100644 diesel.toml create mode 100644 migrations/2022-09-14-140919_create_player/down.sql create mode 100644 migrations/2022-09-14-140919_create_player/up.sql diff --git a/Cargo.lock b/Cargo.lock index 356c380..10b622b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,6 +14,12 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "diesel" version = "2.0.0" @@ -55,6 +61,17 @@ dependencies = [ "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]] name = "idna" version = "0.3.0" @@ -65,6 +82,12 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "libc" +version = "0.2.132" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" + [[package]] name = "mysqlclient-sys" version = "0.2.5" @@ -87,6 +110,12 @@ version = "0.3.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" +[[package]] +name = "ppv-lite86" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -129,12 +158,43 @@ dependencies = [ "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]] name = "reentrada" version = "0.1.0" dependencies = [ "diesel", "dotenv", + "uuid", ] [[package]] @@ -195,6 +255,28 @@ dependencies = [ "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]] name = "vcpkg" version = "0.2.15" @@ -206,3 +288,9 @@ name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/Cargo.toml b/Cargo.toml index a780026..ec331fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,11 @@ edition = "2021" [dependencies] diesel = { version = "2.0.0", features = ["mysql"] } 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 +] diff --git a/diesel.toml b/diesel.toml new file mode 100644 index 0000000..35a12ff --- /dev/null +++ b/diesel.toml @@ -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" diff --git a/migrations/2022-09-14-140919_create_player/down.sql b/migrations/2022-09-14-140919_create_player/down.sql new file mode 100644 index 0000000..d4bd032 --- /dev/null +++ b/migrations/2022-09-14-140919_create_player/down.sql @@ -0,0 +1 @@ +DROP TABLE player diff --git a/migrations/2022-09-14-140919_create_player/up.sql b/migrations/2022-09-14-140919_create_player/up.sql new file mode 100644 index 0000000..1ebd1bc --- /dev/null +++ b/migrations/2022-09-14-140919_create_player/up.sql @@ -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) +) diff --git a/src/main.rs b/src/main.rs index 34f2c73..1dc3883 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ pub mod database; // use crate::player::Player; use crate::rlib::set_player; +use crate::player::update_player; fn main() { // intro @@ -30,7 +31,7 @@ fn main() { let mut output: String; 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!("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."); @@ -41,6 +42,7 @@ fn main() { command = rlib::get_input(); output = rlib::update_state(&mut player, &command); rlib::update_screen(output); + update_player(&player).expect("Error actualizando jugador!!"); } // salir diff --git a/src/models.rs b/src/models.rs index 2f79d21..82903d9 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,6 +1,8 @@ use diesel::prelude::*; +use crate::schema::*; #[derive(Queryable)] +#[diesel(table_name = location)] pub struct RLocation { pub tag: String, pub nombre: Option, @@ -13,7 +15,8 @@ pub struct RLocation { pub salida_abajo: Option, } -#[derive(Queryable)] +#[derive(Queryable, Insertable)] +#[diesel(table_name = object)] pub struct RObject { pub tag: String, pub nombre: String, @@ -22,8 +25,22 @@ pub struct RObject { pub peso: i32, } -#[derive(Queryable)] -pub struct Rnpc { +// #[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))] +pub struct Player { pub tag: String, pub nombre: String, pub descripcion: String, diff --git a/src/player.rs b/src/player.rs index 2a32faa..167969b 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,9 +1,35 @@ 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 { - pub tag: String, - pub name: String, - pub location: String, +// get_player le o xogador polo nick +pub fn get_player(nome: String) -> Result { + use crate::schema::player::dsl::*; + let conn = &mut establish_connection(); + player + .filter(nombre.eq(nome)) + .first(conn) +} + +// create_player garda o xogador +pub fn create_player(newplayer: &Player) -> Result { + 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 { + 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 diff --git a/src/rlib.rs b/src/rlib.rs index f62203e..7cfb44e 100644 --- a/src/rlib.rs +++ b/src/rlib.rs @@ -1,6 +1,8 @@ use std::io::{self, Write}; 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 verb: String, @@ -40,12 +42,27 @@ pub fn set_player() -> Player { // parse let nick = input_str.trim(); - // instance player - let player = Player{ - tag: "player1".to_string() , - name: nick.to_string(), - location: "0".to_string() - }; + //comprobar si existe + // si existe instanciase o que xa está na base de datos + // si non existe instanciase o novo e gárdase na base de datos + let player: Player; + + 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 player diff --git a/src/rlocation.rs b/src/rlocation.rs index 7af4bb2..1e6c51c 100644 --- a/src/rlocation.rs +++ b/src/rlocation.rs @@ -1,4 +1,3 @@ -use crate::player::Player; use crate::objects::get_object_here; use crate::models::*; diff --git a/src/schema.rs b/src/schema.rs index dbfb587..8a89cf3 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -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!( location, object, + player, );