From 5a792edb20a764842893b7414acb75dcb143444b Mon Sep 17 00:00:00 2001 From: Artus Date: Tue, 2 Jul 2019 15:33:36 +0200 Subject: [PATCH] impls claims on looted items --- lootalot_db/src/lib.rs | 28 +++++++++++++--- lootalot_db/src/models/claim.rs | 17 ++++++++++ lootalot_db/src/models/item.rs | 58 +++++++-------------------------- lootalot_db/src/models/mod.rs | 2 ++ src/server.rs | 4 +++ 5 files changed, 59 insertions(+), 50 deletions(-) create mode 100644 lootalot_db/src/models/claim.rs diff --git a/lootalot_db/src/lib.rs b/lootalot_db/src/lib.rs index 1c0b481..a9488be 100644 --- a/lootalot_db/src/lib.rs +++ b/lootalot_db/src/lib.rs @@ -28,11 +28,11 @@ pub type ActionResult = QueryResult; /// x .fetch_inventory() /// v .as_player(player_id) -> AsPlayer<'q> /// v .loot() -> List of items owned (Vec) -/// x .claim(item_id) -> Success status (bool) -/// x .unclaim(item_id) -> Success status (bool) +/// v .claim(item_id) -> Success status (bool) +/// v .unclaim(item_id) -> Success status (bool) /// x .sell(item_id) -> Success status (bool, earned) /// x .buy(inventory_item_id) -> Success status (bool, cost) -/// x .update_wealth(gold_pieces) -> Success status (bool, new_wealth) +/// v .update_wealth(gold_pieces) -> Success status (bool, new_wealth) /// x .as_admin() /// x .add_loot([inventory_item_ids]) -> Success status /// x .sell_loot([players], [excluded_item_ids]) -> Success status (bool, player_share) @@ -70,6 +70,13 @@ impl<'q> DbApi<'q> { .load::(self.0)? ) } + + pub fn fetch_claims(self) -> QueryResult> { + Ok( + schema::claims::table + .load::(self.0)? + ) + } /// Wrapper for acting as a specific player /// /// The DbApi is moved inside a new AsPlayer object. @@ -117,7 +124,20 @@ impl<'q> AsPlayer<'q> { } /// Put a claim on a specific item pub fn claim(self, item: i32) -> ActionResult { - Ok(false) + let request = models::NewClaim { player_id: self.id, loot_id: item }; + diesel::insert_into(schema::claims::table) + .values(&request) + .execute(self.conn) + .map(|r| match r { 1 => true, _ => false }) + } + pub fn unclaim(self, item: i32) -> ActionResult { + use schema::claims::dsl::*; + diesel::delete( + claims + .filter(loot_id.eq(item)) + .filter(player_id.eq(self.id))) + .execute(self.conn) + .map(|r| match r { 1 => true, _ => false }) } } diff --git a/lootalot_db/src/models/claim.rs b/lootalot_db/src/models/claim.rs new file mode 100644 index 0000000..7cea712 --- /dev/null +++ b/lootalot_db/src/models/claim.rs @@ -0,0 +1,17 @@ +use diesel::prelude::*; +use crate::schema::claims; + +#[derive(Queryable, Serialize, Debug)] +pub struct Claim { + id: i32, + player_id: i32, + loot_id: i32, + resolve: i32, +} + +#[derive(Insertable, Debug)] +#[table_name="claims"] +pub struct NewClaim { + pub player_id: i32, + pub loot_id: i32, +} diff --git a/lootalot_db/src/models/item.rs b/lootalot_db/src/models/item.rs index f34ca52..5d08f15 100644 --- a/lootalot_db/src/models/item.rs +++ b/lootalot_db/src/models/item.rs @@ -3,13 +3,11 @@ use diesel::dsl::{Eq, Filter, Select}; use crate::schema::{looted, items}; use crate::DbConnection; -type ItemColumns = ( looted::id, looted::name, looted::base_price, ); +type ItemColumns = + ( looted::id, looted::name, looted::base_price, ); +const ITEM_COLUMNS: ItemColumns = + ( looted::id, looted::name, looted::base_price, ); type OwnedBy = Select; -const ITEM_COLUMNS: ItemColumns = ( looted::id, looted::name, looted::base_price, ); - -/// New type to handle the inventory (items table) -/// This will be used to reduce confusion with looted items. -struct InventoryItem(Item); /// Represents a unique item in inventory /// @@ -43,6 +41,14 @@ struct Loot { owner: i32, } +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)) + } +} + /// An item being looted or bought. /// /// The owner is set to 0 in case of looting, @@ -54,43 +60,3 @@ struct NewLoot<'a> { base_price: i32, owner_id: i32, } - -impl<'a> NewLoot<'a> { - fn insert(&self, conn: &DbConnection) -> QueryResult { - Ok(0) - } -} - -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)) - } - /// Loot an item, adding it to the group chest - fn loot(name: &str, base_price: i32) -> Result { - let loot = NewLoot{ name, base_price, owner_id: 0 }; - // Insert into table - // Retrieve id of created loot - let loot_id = 0; - Ok(loot_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(()) - } - fn buy(buyer_id: i32, item_desc: (&str, i32)) -> Result { - let loot = NewLoot{ - name: item_desc.0, - base_price: item_desc.1, - owner_id: buyer_id - }; - // Insert into table - // Retrieve id of created loot; - - // Withdraw value from player wealth. - Ok(item_desc.1) - } -} diff --git a/lootalot_db/src/models/mod.rs b/lootalot_db/src/models/mod.rs index d899110..0ad2ec6 100644 --- a/lootalot_db/src/models/mod.rs +++ b/lootalot_db/src/models/mod.rs @@ -1,6 +1,8 @@ mod item; mod player; +mod claim; pub use item::Item; pub use player::Player; pub use player::WealthUpdate; +pub use claim::{NewClaim, Claim}; diff --git a/src/server.rs b/src/server.rs index b10de90..a97a682 100644 --- a/src/server.rs +++ b/src/server.rs @@ -62,6 +62,10 @@ pub(crate) fn serve() -> std::io::Result<()> { db_call(pool, move |api| api.fetch_players()) }), ) + .route( + "/claims", + web::get().to_async(move |pool: AppPool| db_call(pool, move |api| api.fetch_claims())), + ) .route( "/update-wealth/{player_id}/{amount}", web::get().to_async(move |pool: AppPool, data: web::Path<(i32, f32)>| {