adds docs, removes some useless pub statements

This commit is contained in:
2019-10-19 16:12:27 +02:00
parent a552d7beee
commit 24807615f7
5 changed files with 47 additions and 16 deletions

View File

@@ -54,8 +54,7 @@ pub fn sell_item_transaction(
conn.transaction(|| { conn.transaction(|| {
let deleted = LootManager(conn, id) let deleted = LootManager(conn, id)
.remove(loot_id)?; .remove(loot_id)?;
let mut sell_value = let mut sell_value = deleted.sell_value() as f64;
deleted.base_price as f64 / 2.0;
if let Some(modifier) = price_mod { if let Some(modifier) = price_mod {
sell_value *= modifier; sell_value *= modifier;
} }
@@ -81,8 +80,8 @@ pub fn buy_item_from_inventory(
let item = Inventory(conn).find(item_id)?; let item = Inventory(conn).find(item_id)?;
let new_item = LootManager(conn, id).add_from(&item)?; let new_item = LootManager(conn, id).add_from(&item)?;
let sell_price = match price_mod { let sell_price = match price_mod {
Some(modifier) => item.base_price as f64 * modifier, Some(modifier) => item.value() as f64 * modifier,
None => item.base_price as f64, None => item.value() as f64,
}; };
AsPlayer(conn, id) AsPlayer(conn, id)
.update_wealth(-sell_price) .update_wealth(-sell_price)

View File

@@ -19,6 +19,7 @@ pub struct Claim {
} }
impl Claim { impl Claim {
/// Resolves this claim (player wins the item) and deletes it
pub fn resolve_claim(&self, conn: &DbConnection) -> QueryResult<()> { pub fn resolve_claim(&self, conn: &DbConnection) -> QueryResult<()> {
let loot: Loot = Loot::find(self.loot_id).first(conn)?; let loot: Loot = Loot::find(self.loot_id).first(conn)?;
loot.set_owner(self.player_id, conn)?; loot.set_owner(self.player_id, conn)?;
@@ -35,6 +36,7 @@ impl Claim {
pub struct Claims<'q>(pub &'q DbConnection); pub struct Claims<'q>(pub &'q DbConnection);
impl<'q> Claims<'q> { impl<'q> Claims<'q> {
/// Get all claims from database
pub fn all(&self) -> QueryResult<Vec<Claim>> { pub fn all(&self) -> QueryResult<Vec<Claim>> {
claims::table.load(self.0) claims::table.load(self.0)
} }
@@ -47,7 +49,11 @@ impl<'q> Claims<'q> {
.first(self.0) .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<Claim> { pub fn add(self, player_id: i32, loot_id: i32) -> QueryResult<Claim> {
// We need to validate that the claimed item exists // We need to validate that the claimed item exists
// AND is actually owned by group (id 0) // AND is actually owned by group (id 0)

View File

@@ -13,14 +13,16 @@ type OwnedBy = Select<OwnedLoot, ItemColumns>;
pub struct Item { pub struct Item {
pub id: i32, pub id: i32,
pub name: String, pub name: String,
pub base_price: i32, base_price: i32,
} }
impl Item { impl Item {
/// Returns this item value
pub fn value(&self) -> i32 { pub fn value(&self) -> i32 {
self.base_price self.base_price
} }
/// Returns this item sell value
pub fn sell_value(&self) -> i32 { pub fn sell_value(&self) -> i32 {
self.base_price / 2 self.base_price / 2
} }
@@ -33,10 +35,12 @@ impl Item {
pub struct Inventory<'q>(pub &'q DbConnection); pub struct Inventory<'q>(pub &'q DbConnection);
impl<'q> Inventory<'q> { impl<'q> Inventory<'q> {
/// Get all items from Inventory
pub fn all(&self) -> QueryResult<Vec<Item>> { pub fn all(&self) -> QueryResult<Vec<Item>> {
items::table.load::<Item>(self.0) items::table.load::<Item>(self.0)
} }
/// Find an item in Inventory
pub fn find(&self, item_id: i32) -> QueryResult<Item> { pub fn find(&self, item_id: i32) -> QueryResult<Item> {
items::table.find(item_id).first::<Item>(self.0) items::table.find(item_id).first::<Item>(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); pub struct LootManager<'q>(pub &'q DbConnection, pub i32);
impl<'q> LootManager<'q> { impl<'q> LootManager<'q> {
@@ -96,6 +100,9 @@ impl<'q> LootManager<'q> {
} }
/// Finds an item by id /// 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<Item> { pub fn find(&self, loot_id: i32) -> QueryResult<Item> {
Ok(Loot::find(loot_id).first(self.0).and_then(|loot: Loot| { Ok(Loot::find(loot_id).first(self.0).and_then(|loot: Loot| {
if loot.owner != self.1 { if loot.owner != self.1 {

View File

@@ -25,25 +25,34 @@ pub struct Player {
pub struct Players<'q>(pub &'q DbConnection); pub struct Players<'q>(pub &'q DbConnection);
impl<'q> Players<'q> { impl<'q> Players<'q> {
/// Get all players from database
pub fn all(&self) -> QueryResult<Vec<Player>> { pub fn all(&self) -> QueryResult<Vec<Player>> {
players::table.load(self.0) players::table.load(self.0)
} }
/// Find a player by id
pub fn find(&self, id: i32) -> QueryResult<Player> { pub fn find(&self, id: i32) -> QueryResult<Player> {
players::table.find(id).first(self.0) 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<Player> { pub fn add(&self, name: &str, wealth: f64) -> QueryResult<Player> {
diesel::insert_into(players::table) diesel::insert_into(players::table)
.values(&NewPlayer::create(name, wealth)) .values(&NewPlayer::create(name, wealth))
.execute(self.0)?; .execute(self.0)?;
players::table.order(players::dsl::id.desc()).first(self.0) players::table.order(players::dsl::id.desc()).first(self.0)
} }
/// Notify all players of an event
pub fn notifiy_all<S: Into<String>>(&self, text: S) -> QueryResult<()> {
Err(diesel::result::Error::NotFound)
}
} }
pub struct AsPlayer<'q>(pub &'q DbConnection, pub i32); pub struct AsPlayer<'q>(pub &'q DbConnection, pub i32);
impl<'q> AsPlayer<'q> { impl<'q> AsPlayer<'q> {
/// Updates this player's wealth, returning the difference
pub fn update_wealth(&self, value_in_gp: f64) -> QueryResult<Wealth> { pub fn update_wealth(&self, value_in_gp: f64) -> QueryResult<Wealth> {
use crate::schema::players::dsl::*; use crate::schema::players::dsl::*;
let current_wealth = players let current_wealth = players
@@ -55,10 +64,10 @@ impl<'q> AsPlayer<'q> {
.filter(id.eq(self.1)) .filter(id.eq(self.1))
.set(&updated_wealth) .set(&updated_wealth)
.execute(self.0)?; .execute(self.0)?;
// Difference in coins that is sent back
Ok(updated_wealth - current_wealth) Ok(updated_wealth - current_wealth)
} }
/// Updates this player's debt
pub fn update_debt(&self, value_in_gp: i32) -> QueryResult<()> { pub fn update_debt(&self, value_in_gp: i32) -> QueryResult<()> {
diesel::update(players::table.find(self.1)) diesel::update(players::table.find(self.1))
.set(players::dsl::debt.eq(players::dsl::debt + value_in_gp)) .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; type Output = Self;
/// What needs to be added to 'other' so that /// What needs to be added to 'other' so that
/// the result equals 'self' /// the result equals 'self'
@@ -146,9 +154,7 @@ impl Sub for Wealth {
} }
} }
use std::ops::Add; impl std::ops::Add for Wealth {
impl Add for Wealth {
type Output = Self; type Output = Self;
fn add(self, other: Self) -> Self { fn add(self, other: Self) -> Self {
@@ -164,7 +170,7 @@ impl Add for Wealth {
/// Representation of a new player record /// Representation of a new player record
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "players"] #[table_name = "players"]
pub(crate) struct NewPlayer<'a> { struct NewPlayer<'a> {
name: &'a str, name: &'a str,
cp: i32, cp: i32,
sp: i32, sp: i32,
@@ -173,7 +179,7 @@ pub(crate) struct NewPlayer<'a> {
} }
impl<'a> 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(); let (cp, sp, gp, pp) = Wealth::from_gp(wealth_in_gp).as_tuple();
Self { Self {
name, name,

View File

@@ -148,6 +148,7 @@ pub fn execute(
response.push_update(Update::Wealth(total_amount)); response.push_update(Update::Wealth(total_amount));
} }
ApiActions::SellItems(id, params) => { ApiActions::SellItems(id, params) => {
// TODO: Different procedure for group and other players
let mut all_results: Vec<db::Wealth> = Vec::with_capacity(params.len()); let mut all_results: Vec<db::Wealth> = Vec::with_capacity(params.len());
let mut sold_items: u16 = 0; let mut sold_items: u16 = 0;
for (loot_id, price_mod) in params.into_iter() { for (loot_id, price_mod) in params.into_iter() {
@@ -178,7 +179,19 @@ pub fn execute(
response.notify(format!("Bof! Finalement non.")); response.notify(format!("Bof! Finalement non."));
} }
// Group actions // 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) Ok(response)
} }