impls claims on looted items

This commit is contained in:
2019-07-02 15:33:36 +02:00
parent 36c8f24fdc
commit 5a792edb20
5 changed files with 59 additions and 50 deletions

View File

@@ -28,11 +28,11 @@ pub type ActionResult = QueryResult<bool>;
/// x .fetch_inventory()
/// v .as_player(player_id) -> AsPlayer<'q>
/// v .loot() -> List of items owned (Vec<Item>)
/// 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::<models::Item>(self.0)?
)
}
pub fn fetch_claims(self) -> QueryResult<Vec<models::Claim>> {
Ok(
schema::claims::table
.load::<models::Claim>(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 })
}
}