diff --git a/lootalot_db/src/models/item.rs b/lootalot_db/src/models/item.rs index 62d2180..f34ca52 100644 --- a/lootalot_db/src/models/item.rs +++ b/lootalot_db/src/models/item.rs @@ -1,6 +1,15 @@ use diesel::prelude::*; use diesel::dsl::{Eq, Filter, Select}; use crate::schema::{looted, items}; +use crate::DbConnection; + +type 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 /// @@ -14,22 +23,7 @@ pub struct Item { 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) @@ -37,6 +31,9 @@ impl Item { } } +type WithOwner = Eq; +type OwnedLoot = Filter; + /// Represents an item that has been looted #[derive(Debug, Queryable, Serialize)] struct Loot { @@ -46,8 +43,23 @@ struct Loot { owner: i32, } -type WithOwner = Eq; -type OwnedLoot = Filter; +/// An item being looted or bought. +/// +/// The owner is set to 0 in case of looting, +/// to the id of buying player otherwise. +#[derive(Insertable)] +#[table_name = "looted"] +struct NewLoot<'a> { + name: &'a str, + 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 @@ -55,10 +67,30 @@ impl Loot { 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) + } }