From 24807615f72c690208fba4f2cac431752e24fc78 Mon Sep 17 00:00:00 2001 From: Artus Date: Sat, 19 Oct 2019 16:12:27 +0200 Subject: [PATCH] adds docs, removes some useless pub statements --- lootalot_db/src/lib.rs | 7 +++---- lootalot_db/src/models/claim.rs | 8 +++++++- lootalot_db/src/models/item.rs | 11 +++++++++-- lootalot_db/src/models/player.rs | 22 ++++++++++++++-------- src/api.rs | 15 ++++++++++++++- 5 files changed, 47 insertions(+), 16 deletions(-) diff --git a/lootalot_db/src/lib.rs b/lootalot_db/src/lib.rs index 513ec3a..cafa91b 100644 --- a/lootalot_db/src/lib.rs +++ b/lootalot_db/src/lib.rs @@ -54,8 +54,7 @@ pub fn sell_item_transaction( conn.transaction(|| { let deleted = LootManager(conn, id) .remove(loot_id)?; - let mut sell_value = - deleted.base_price as f64 / 2.0; + let mut sell_value = deleted.sell_value() as f64; if let Some(modifier) = price_mod { sell_value *= modifier; } @@ -81,8 +80,8 @@ pub fn buy_item_from_inventory( let item = Inventory(conn).find(item_id)?; let new_item = LootManager(conn, id).add_from(&item)?; let sell_price = match price_mod { - Some(modifier) => item.base_price as f64 * modifier, - None => item.base_price as f64, + Some(modifier) => item.value() as f64 * modifier, + None => item.value() as f64, }; AsPlayer(conn, id) .update_wealth(-sell_price) diff --git a/lootalot_db/src/models/claim.rs b/lootalot_db/src/models/claim.rs index 107a1c7..f9a4eed 100644 --- a/lootalot_db/src/models/claim.rs +++ b/lootalot_db/src/models/claim.rs @@ -19,6 +19,7 @@ pub struct Claim { } impl Claim { + /// Resolves this claim (player wins the item) and deletes it pub fn resolve_claim(&self, conn: &DbConnection) -> QueryResult<()> { let loot: Loot = Loot::find(self.loot_id).first(conn)?; loot.set_owner(self.player_id, conn)?; @@ -35,6 +36,7 @@ impl Claim { pub struct Claims<'q>(pub &'q DbConnection); impl<'q> Claims<'q> { + /// Get all claims from database pub fn all(&self) -> QueryResult> { claims::table.load(self.0) } @@ -47,7 +49,11 @@ impl<'q> Claims<'q> { .first(self.0) } - /// Adds a claim in database and returns it + /// Adds a claim in database and returns it. + /// + /// Will validate that the claimed item exists and is + /// actually owned by the group. + /// Duplicates are also ignored. pub fn add(self, player_id: i32, loot_id: i32) -> QueryResult { // We need to validate that the claimed item exists // AND is actually owned by group (id 0) diff --git a/lootalot_db/src/models/item.rs b/lootalot_db/src/models/item.rs index 3ac9fd4..95d1ef0 100644 --- a/lootalot_db/src/models/item.rs +++ b/lootalot_db/src/models/item.rs @@ -13,14 +13,16 @@ type OwnedBy = Select; pub struct Item { pub id: i32, pub name: String, - pub base_price: i32, + base_price: i32, } impl Item { + /// Returns this item value pub fn value(&self) -> i32 { self.base_price } + /// Returns this item sell value pub fn sell_value(&self) -> i32 { self.base_price / 2 } @@ -33,10 +35,12 @@ impl Item { pub struct Inventory<'q>(pub &'q DbConnection); impl<'q> Inventory<'q> { + /// Get all items from Inventory pub fn all(&self) -> QueryResult> { items::table.load::(self.0) } + /// Find an item in Inventory pub fn find(&self, item_id: i32) -> QueryResult { items::table.find(item_id).first::(self.0) } @@ -86,7 +90,7 @@ impl Loot { } } -/// Manager for a player's loot +/// Manager for a *single* player loot pub struct LootManager<'q>(pub &'q DbConnection, pub i32); impl<'q> LootManager<'q> { @@ -96,6 +100,9 @@ impl<'q> LootManager<'q> { } /// Finds an item by id + /// + /// Returns a NotFound error if an item is found by it + /// does not belong to this player pub fn find(&self, loot_id: i32) -> QueryResult { Ok(Loot::find(loot_id).first(self.0).and_then(|loot: Loot| { if loot.owner != self.1 { diff --git a/lootalot_db/src/models/player.rs b/lootalot_db/src/models/player.rs index 38f06ad..d7bbfab 100644 --- a/lootalot_db/src/models/player.rs +++ b/lootalot_db/src/models/player.rs @@ -25,25 +25,34 @@ pub struct Player { pub struct Players<'q>(pub &'q DbConnection); impl<'q> Players<'q> { + /// Get all players from database pub fn all(&self) -> QueryResult> { players::table.load(self.0) } + /// Find a player by id pub fn find(&self, id: i32) -> QueryResult { players::table.find(id).first(self.0) } + /// Add a new player with name and starting wealth pub fn add(&self, name: &str, wealth: f64) -> QueryResult { diesel::insert_into(players::table) .values(&NewPlayer::create(name, wealth)) .execute(self.0)?; players::table.order(players::dsl::id.desc()).first(self.0) } + + /// Notify all players of an event + pub fn notifiy_all>(&self, text: S) -> QueryResult<()> { + Err(diesel::result::Error::NotFound) + } } pub struct AsPlayer<'q>(pub &'q DbConnection, pub i32); impl<'q> AsPlayer<'q> { + /// Updates this player's wealth, returning the difference pub fn update_wealth(&self, value_in_gp: f64) -> QueryResult { use crate::schema::players::dsl::*; let current_wealth = players @@ -55,10 +64,10 @@ impl<'q> AsPlayer<'q> { .filter(id.eq(self.1)) .set(&updated_wealth) .execute(self.0)?; - // Difference in coins that is sent back Ok(updated_wealth - current_wealth) } + /// Updates this player's debt pub fn update_debt(&self, value_in_gp: i32) -> QueryResult<()> { diesel::update(players::table.find(self.1)) .set(players::dsl::debt.eq(players::dsl::debt + value_in_gp)) @@ -130,9 +139,8 @@ impl Wealth { } } -use std::ops::Sub; -impl Sub for Wealth { +impl std::ops::Sub for Wealth { type Output = Self; /// What needs to be added to 'other' so that /// the result equals 'self' @@ -146,9 +154,7 @@ impl Sub for Wealth { } } -use std::ops::Add; - -impl Add for Wealth { +impl std::ops::Add for Wealth { type Output = Self; fn add(self, other: Self) -> Self { @@ -164,7 +170,7 @@ impl Add for Wealth { /// Representation of a new player record #[derive(Insertable)] #[table_name = "players"] -pub(crate) struct NewPlayer<'a> { +struct NewPlayer<'a> { name: &'a str, cp: i32, sp: i32, @@ -173,7 +179,7 @@ pub(crate) struct NewPlayer<'a> { } impl<'a> NewPlayer<'a> { - pub(crate) fn create(name: &'a str, wealth_in_gp: f64) -> Self { + fn create(name: &'a str, wealth_in_gp: f64) -> Self { let (cp, sp, gp, pp) = Wealth::from_gp(wealth_in_gp).as_tuple(); Self { name, diff --git a/src/api.rs b/src/api.rs index 981c394..992787f 100644 --- a/src/api.rs +++ b/src/api.rs @@ -148,6 +148,7 @@ pub fn execute( response.push_update(Update::Wealth(total_amount)); } ApiActions::SellItems(id, params) => { + // TODO: Different procedure for group and other players let mut all_results: Vec = Vec::with_capacity(params.len()); let mut sold_items: u16 = 0; for (loot_id, price_mod) in params.into_iter() { @@ -178,7 +179,19 @@ pub fn execute( response.notify(format!("Bof! Finalement non.")); } // Group actions - ApiActions::AddLoot(items) => {} + ApiActions::AddLoot(items) => { + let mut added_items = 0; + for item in items.into_iter() { + if let Ok(added) = db::LootManager(conn, 0).add_from(&item) { + response.push_update(Update::ItemAdded(added)); + added_items += 1; + } else { + response.push_error(format!("Error adding {:?}", item)); + } + } + response.notify(format!("{} objets lootés !", added_items)); + // TODO: notify players throught persistent notifications + } } Ok(response) }