From c2f8993d07f50585e1cf68adbd8ee54dc4099683 Mon Sep 17 00:00:00 2001 From: ntr Date: Tue, 11 Sep 2018 23:04:17 +1000 Subject: [PATCH] diesel --- .cargo/config | 3 ++ .env | 1 + Cargo.toml | 2 ++ WORKLOG.md | 7 ++-- diesel.toml | 5 +++ migrations/.gitkeep | 0 .../down.sql | 6 ++++ .../up.sql | 36 +++++++++++++++++++ .../2018-09-11-113648_create_lobbies/down.sql | 1 + .../2018-09-11-113648_create_lobbies/up.sql | 6 ++++ src/combat.rs | 18 ++++++---- src/db.rs | 13 +++++++ src/lib.rs | 6 ++++ src/main.rs | 6 ++++ src/models.rs | 9 +++++ src/schema.rs | 8 +++++ 16 files changed, 119 insertions(+), 8 deletions(-) create mode 100755 .cargo/config create mode 100755 .env mode change 100644 => 100755 WORKLOG.md create mode 100644 diesel.toml create mode 100644 migrations/.gitkeep create mode 100644 migrations/00000000000000_diesel_initial_setup/down.sql create mode 100644 migrations/00000000000000_diesel_initial_setup/up.sql create mode 100755 migrations/2018-09-11-113648_create_lobbies/down.sql create mode 100755 migrations/2018-09-11-113648_create_lobbies/up.sql create mode 100755 src/db.rs create mode 100755 src/lib.rs create mode 100755 src/models.rs create mode 100755 src/schema.rs diff --git a/.cargo/config b/.cargo/config new file mode 100755 index 00000000..3c3311fc --- /dev/null +++ b/.cargo/config @@ -0,0 +1,3 @@ +[target.x86_64-pc-windows-msvc.gnu] +rustc-link-search = ["C:\\Program Files\\PostgreSQL\\pg96\\lib"] + diff --git a/.env b/.env new file mode 100755 index 00000000..f42f5c28 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DATABASE_URL=postgres://cryps:craftbeer@localhost/cryps diff --git a/Cargo.toml b/Cargo.toml index edbb45ab..98351e3e 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,5 @@ serde_derive = "1" serde_cbor = "0.9" ws = "*" env_logger = "*" +diesel = { version = "1.0.0", features = ["postgres", "uuid"] } +dotenv = "0.9.0" diff --git a/WORKLOG.md b/WORKLOG.md old mode 100644 new mode 100755 index 557cfdf9..65aaa642 --- a/WORKLOG.md +++ b/WORKLOG.md @@ -1,10 +1,13 @@ * Battling - * Cryp Serialisation - * RPC * Logins * Cryp Ownership * Matchmaking + * Lobbies + * Create + * Join + * Resolve * Stats + * Missions * Cryp Generation * diff --git a/diesel.toml b/diesel.toml new file mode 100644 index 00000000..92267c82 --- /dev/null +++ b/diesel.toml @@ -0,0 +1,5 @@ +# For documentation on how to configure this file, +# see diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" diff --git a/migrations/.gitkeep b/migrations/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/migrations/00000000000000_diesel_initial_setup/down.sql b/migrations/00000000000000_diesel_initial_setup/down.sql new file mode 100644 index 00000000..a9f52609 --- /dev/null +++ b/migrations/00000000000000_diesel_initial_setup/down.sql @@ -0,0 +1,6 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + +DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); +DROP FUNCTION IF EXISTS diesel_set_updated_at(); diff --git a/migrations/00000000000000_diesel_initial_setup/up.sql b/migrations/00000000000000_diesel_initial_setup/up.sql new file mode 100644 index 00000000..d68895b1 --- /dev/null +++ b/migrations/00000000000000_diesel_initial_setup/up.sql @@ -0,0 +1,36 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + + + + +-- Sets up a trigger for the given table to automatically set a column called +-- `updated_at` whenever the row is modified (unless `updated_at` was included +-- in the modified columns) +-- +-- # Example +-- +-- ```sql +-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); +-- +-- SELECT diesel_manage_updated_at('users'); +-- ``` +CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ +BEGIN + EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s + FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); +END; +$$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ +BEGIN + IF ( + NEW IS DISTINCT FROM OLD AND + NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at + ) THEN + NEW.updated_at := current_timestamp; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; diff --git a/migrations/2018-09-11-113648_create_lobbies/down.sql b/migrations/2018-09-11-113648_create_lobbies/down.sql new file mode 100755 index 00000000..62b267e6 --- /dev/null +++ b/migrations/2018-09-11-113648_create_lobbies/down.sql @@ -0,0 +1 @@ +DROP TABLE lobbies; \ No newline at end of file diff --git a/migrations/2018-09-11-113648_create_lobbies/up.sql b/migrations/2018-09-11-113648_create_lobbies/up.sql new file mode 100755 index 00000000..c0e4cd3e --- /dev/null +++ b/migrations/2018-09-11-113648_create_lobbies/up.sql @@ -0,0 +1,6 @@ +CREATE TABLE lobbies ( + id uuid PRIMARY KEY, + a uuid NOT NULL, + b uuid, + data bytea +); diff --git a/src/combat.rs b/src/combat.rs index f0c9a1a1..d10ffbc9 100755 --- a/src/combat.rs +++ b/src/combat.rs @@ -91,16 +91,22 @@ pub fn test_battle() { #[cfg(test)] mod tests { - use *; + use super::*; #[test] fn pve_test() { - // let player = Cryp::new() - // .named("ca phe sua da".to_string()) - // .level(2) - // .create(); + let player = Cryp::new() + .named("ca phe sua da".to_string()) + .level(2) + .create(); - // levelling(player); + levelling(player); + return; + } + + #[test] + fn battle_test() { + test_battle(); return; } diff --git a/src/db.rs b/src/db.rs new file mode 100755 index 00000000..e68f8bab --- /dev/null +++ b/src/db.rs @@ -0,0 +1,13 @@ +use diesel::prelude::*; +use diesel::pg::PgConnection; +use dotenv::dotenv; +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)) +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100755 index 00000000..fb094dc4 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,6 @@ +extern crate uuid; +#[macro_use] +extern crate diesel; + +pub mod schema; +pub mod models; diff --git a/src/main.rs b/src/main.rs index 8e1b515b..4db11083 100755 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,10 @@ extern crate uuid; extern crate ws; extern crate env_logger; +#[macro_use] +extern crate diesel; +extern crate dotenv; + extern crate serde; extern crate serde_cbor; #[macro_use] @@ -14,6 +18,8 @@ mod net; mod combat; mod skill; mod rpc; +mod schema; +mod models; use net::{start}; diff --git a/src/models.rs b/src/models.rs new file mode 100755 index 00000000..f60baece --- /dev/null +++ b/src/models.rs @@ -0,0 +1,9 @@ +use diesel::sql_types::{Uuid,Bytea}; + +#[derive(Queryable)] +pub struct Lobby { + pub id: Uuid, + pub a: Uuid, + pub b: Uuid, + pub data: Bytea, +} \ No newline at end of file diff --git a/src/schema.rs b/src/schema.rs new file mode 100755 index 00000000..49615c42 --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,8 @@ +table! { + lobbies (id) { + id -> Uuid, + a -> Uuid, + b -> Nullable, + data -> Nullable, + } +}