From 5379ad236ff1e6d88bca4651b4d0c9d6f91929cd Mon Sep 17 00:00:00 2001 From: Artus Date: Sat, 22 Jun 2019 21:55:21 +0200 Subject: [PATCH] uses tips from diesel.rs guide for application integration --- lootalot_db/db.sqlite3 | Bin 28672 -> 28672 bytes lootalot_db/src/lib.rs | 47 +++++------------------- lootalot_db/src/models/item.rs | 60 +++++++++++++++++++++++++++++-- lootalot_db/src/models/player.rs | 2 -- src/api.rs | 4 +-- 5 files changed, 69 insertions(+), 44 deletions(-) diff --git a/lootalot_db/db.sqlite3 b/lootalot_db/db.sqlite3 index 41341aa13774383cb49ea2ec9ccaac0c1f78fb19..60b70f1d634d2b2a8ea33fca85a4fdefd00d06ac 100644 GIT binary patch delta 60 zcmZp8z}WDBae_1>&qNt#MxKocOYGSg`JXfJKi@29aF<_FoRO75mx<$WXTjl>sR}vy QdFiF83fe{-CW`_T07K9ct^fc4 delta 29 lcmZp8z}WDBae_1>_e2?IM(&LXOYAqZ7%&NJW()Ww4*->; pub type QueryResult = Result; pub type ActionResult = QueryResult; -pub use models::Item; -pub use models::Player; - impl Player { /// Query the list of all players pub fn fetch_list(conn: &SqliteConnection) -> QueryResult> { - use schema::players::dsl::*; - Ok( players - .load::(conn)? - ) - } - - /// Query the list of items belonging to player - pub fn fetch_loot(player_id: i32, conn: &SqliteConnection) -> QueryResult> { - use schema::looted::dsl::*; - Ok( looted - .filter(owner_id.eq(player_id)) - .select((id, name, base_price)) - .load::(conn)? - ) + Ok( schema::players::table.load::(conn)? ) } pub fn action_claim_object(player_id: i32, item_id: i32, conn: &DbConnection) -> ActionResult { @@ -48,8 +37,8 @@ impl Player { pub fn create_pool() -> Pool { - dbg!(dotenv::dotenv().ok()); let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL"); + dbg!( &connspec ); let manager = ConnectionManager::::new(connspec); r2d2::Pool::builder() .build(manager) @@ -58,23 +47,5 @@ pub fn create_pool() -> Pool { #[cfg(test)] mod tests { - use super::*; - fn with_db(f: F) -> () - where - F: Fn(&SqliteConnection) -> (), - { - let conn = establish_connection().unwrap(); - conn.test_transaction::<_, diesel::result::Error, _>(|| { - f(&conn); - Ok(()) - }); - } - - #[test] - fn database_connection() { - use crate::establish_connection; - let conn = establish_connection(); - assert_eq!(conn.is_ok(), true); - } } diff --git a/lootalot_db/src/models/item.rs b/lootalot_db/src/models/item.rs index 6c52198..62d2180 100644 --- a/lootalot_db/src/models/item.rs +++ b/lootalot_db/src/models/item.rs @@ -1,8 +1,64 @@ -use crate::*; +use diesel::prelude::*; +use diesel::dsl::{Eq, Filter, Select}; +use crate::schema::{looted, items}; +/// Represents a unique item in inventory +/// +/// It is also used as a public representation of Loot, since owner +/// information is implicit. +/// Or maybe this is a little too confusing ?? #[derive(Debug, Queryable, Serialize)] pub struct Item { id: i32, name: String, - base_value: i32, + base_price: i32, +} + +type ItemColumns = ( looted::id, looted::name, looted::base_price, ); +type OwnedBy = Select; +const ITEM_COLUMNS: ItemColumns = ( looted::id, looted::name, looted::base_price, ); + +impl Item { + /// Insert this item inside the Looted table. + /// + /// This adds a copy of the item into the group chest, + /// to be claimed by players or sold. + /// Returns the id of the looted item (differs from the original id) + pub fn to_looted(self) -> Result{ + // Copy data inside a new Loot + // Set the owner id to 0 (group) + Err(()) + } + + /// Public proxy for Loot::owned_by that selects only Item fields + pub fn owned_by(player: i32) -> OwnedBy { + Loot::owned_by(player) + .select(ITEM_COLUMNS) + } +} + +/// Represents an item that has been looted +#[derive(Debug, Queryable, Serialize)] +struct Loot { + id: i32, + name: String, + base_value: i32, + owner: i32, +} + +type WithOwner = Eq; +type OwnedLoot = Filter; + +impl Loot { + /// A filter on Loot that is owned by given player + fn owned_by(id: i32) -> OwnedLoot { + looted::table + .filter(looted::owner_id.eq(id)) + } + /// Delete the item, returning the gained wealth + fn sell(_modifier: i8) -> Result { + // Calculate sell value : base_value / 2 * modifier + // Delete recording of loot + Err(()) + } } diff --git a/lootalot_db/src/models/player.rs b/lootalot_db/src/models/player.rs index 5d759dc..b772cef 100644 --- a/lootalot_db/src/models/player.rs +++ b/lootalot_db/src/models/player.rs @@ -1,6 +1,4 @@ use crate::schema::players; -use crate::serde_derive::Serialize; -use crate::*; /// Representation of a player in database #[derive(Debug, Queryable, Serialize)] diff --git a/src/api.rs b/src/api.rs index ae8e65d..705727b 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,7 +1,7 @@ use actix_web::{web, web::Json, Error, HttpResponse, Result}; use futures::Future; -use lootalot_db::{Pool, DbConnection, QueryResult, ActionResult}; +use lootalot_db::{RunQueryDsl, Pool, DbConnection, QueryResult, ActionResult}; use lootalot_db::{Item, Player}; /// A wrapper providing an API over the database @@ -49,7 +49,7 @@ pub struct AsPlayer<'q> { impl<'q> AsPlayer<'q> { /// Fetch the content of a player's chest pub fn loot(self) -> QueryResult> { - Player::fetch_loot(self.id, self.conn) + dbg!( Ok( Item::owned_by(self.id).load(self.conn)? ) ) } /// Put a claim on a specific item pub fn claim(self, item: i32) -> ActionResult {