adds docs, removes some useless pub statements
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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<Vec<Claim>> {
|
||||
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<Claim> {
|
||||
// We need to validate that the claimed item exists
|
||||
// AND is actually owned by group (id 0)
|
||||
|
||||
@@ -13,14 +13,16 @@ type OwnedBy = Select<OwnedLoot, ItemColumns>;
|
||||
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<Vec<Item>> {
|
||||
items::table.load::<Item>(self.0)
|
||||
}
|
||||
|
||||
/// Find an item in Inventory
|
||||
pub fn find(&self, item_id: i32) -> QueryResult<Item> {
|
||||
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);
|
||||
|
||||
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<Item> {
|
||||
Ok(Loot::find(loot_id).first(self.0).and_then(|loot: Loot| {
|
||||
if loot.owner != self.1 {
|
||||
|
||||
@@ -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<Vec<Player>> {
|
||||
players::table.load(self.0)
|
||||
}
|
||||
|
||||
/// Find a player by id
|
||||
pub fn find(&self, id: i32) -> QueryResult<Player> {
|
||||
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> {
|
||||
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<S: Into<String>>(&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<Wealth> {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user