adds new 'shop' table

This commit is contained in:
2019-11-18 15:07:35 +01:00
parent cb8dfa9a2a
commit d1a85ed1d0
8 changed files with 173 additions and 54 deletions

View File

@@ -19,7 +19,7 @@ mod schema;
pub use models::{
claim::{Claim, Claims},
history::{Event, UpdateList},
item::{Inventory, Item, LootManager},
item::{Inventory, Shop, Item, LootManager},
player::{AsPlayer, Player, Players, Wealth},
};
@@ -140,8 +140,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.value() as f64 * modifier,
None => item.value() as f64,
Some(modifier) => item.value() * modifier,
None => item.value(),
};
if let Update::Wealth(diff) = AsPlayer(conn, id).update_wealth(-sell_price)? {
Ok((new_item, diff))
@@ -151,11 +151,31 @@ pub fn buy_item_from_inventory(
})
}
/// Fetch all existing claims
pub fn fetch_claims(conn: &DbConnection) -> QueryResult<Vec<models::Claim>> {
schema::claims::table.load::<models::Claim>(conn)
pub fn buy_item_from_shop(
conn: &DbConnection,
id: i32,
item_id: i32,
price_mod: Option<f64>,
) -> QueryResult<(Update, Wealth)> {
conn.transaction(|| {
let shop = Shop(conn);
// Find item in inventory
let item = shop.get(item_id)?;
let new_item = LootManager(conn, id).add_from(&item)?;
let _deleted = shop.remove(item_id)?;
let sell_price = match price_mod {
Some(modifier) => item.value() * modifier,
None => item.value(),
};
if let Update::Wealth(diff) = AsPlayer(conn, id).update_wealth(-sell_price)? {
Ok((new_item, diff))
} else {
Err(diesel::result::Error::RollbackTransaction)
}
})
}
/// Resolve all pending claims and dispatch claimed items.
///
/// When a player gets an item, it's debt is increased by this item sell value
@@ -171,7 +191,7 @@ pub fn resolve_claims(conn: &DbConnection) -> QueryResult<()> {
let winner = claims.get(0).expect("Claims should not be empty !");
let player_id = winner.player_id;
winner.resolve_claim(conn)?;
models::player::AsPlayer(conn, player_id).update_debt(item.sell_value())?;
models::player::AsPlayer(conn, player_id).update_debt(item.sell_value() as i32)?;
}
Ok(())
})

View File

@@ -41,6 +41,11 @@ impl<'q> Claims<'q> {
claims::table.load(self.0)
}
pub fn by_player(&self, id: i32) -> QueryResult<Vec<Claim>> {
claims::table.filter(claims::dsl::player_id.eq(id))
.load(self.0)
}
/// Finds a single claim by association of player and loot ids.
pub fn find(&self, player_id: i32, loot_id: i32) -> QueryResult<Claim> {
claims::table

View File

@@ -2,7 +2,7 @@ use diesel::dsl::{exists, Eq, Filter, Find, Select};
use diesel::expression::exists::Exists;
use diesel::prelude::*;
use crate::schema::{items, looted};
use crate::schema::{items, looted, shop};
use crate::{DbConnection, QueryResult, Update, UpdateResult, Claims };
type ItemColumns = (looted::id, looted::name, looted::base_price);
const ITEM_COLUMNS: ItemColumns = (looted::id, looted::name, looted::base_price);
@@ -18,13 +18,13 @@ pub struct Item {
impl Item {
/// Returns this item value
pub fn value(&self) -> i32 {
self.base_price
pub fn value(&self) -> f64 {
self.base_price as f64
}
/// Returns this item sell value
pub fn sell_value(&self) -> i32 {
self.base_price / 2
pub fn sell_value(&self) -> f64 {
self.base_price as f64 / 2.0
}
pub fn remove(self, conn: &DbConnection) -> UpdateResult {
@@ -58,6 +58,47 @@ impl<'q> Inventory<'q> {
}
}
pub struct Shop<'q>(pub &'q DbConnection);
impl<'q> Shop<'q> {
pub fn all(&self) -> QueryResult<Vec<Item>> {
shop::table.load(self.0)
}
pub fn get(&self, id: i32) -> QueryResult<Item> {
shop::table.find(&id).first::<Item>(self.0)
}
pub fn remove(&self, id: i32) -> QueryResult<()> {
diesel::delete(
shop::table.find(&id)
).execute(self.0)?;
Ok(())
}
pub fn replace_list(&self, items: Vec<Item>) -> QueryResult<()> {
self.0.transaction(
|| -> QueryResult<()>
{
// Remove all content
diesel::delete(shop::table).execute(self.0)?;
// Adds new list
for item in &items {
let new_item = NewItem {
name: &item.name,
base_price: item.base_price,
};
diesel::insert_into(shop::table)
.values(&new_item)
.execute(self.0)?;
}
Ok(())
}
)
}
}
type WithOwner = Eq<looted::owner_id, i32>;
type OwnedLoot = Filter<looted::table, WithOwner>;
@@ -169,3 +210,10 @@ struct NewLoot<'a> {
base_price: i32,
owner_id: i32,
}
#[derive(Insertable)]
#[table_name = "shop"]
struct NewItem<'a> {
name: &'a str,
base_price: i32,
}

View File

@@ -54,6 +54,14 @@ table! {
}
}
table! {
shop (id) {
id -> Integer,
name -> Text,
base_price -> Integer,
}
}
joinable!(claims -> looted (loot_id));
joinable!(claims -> players (player_id));
joinable!(history -> players (player_id));
@@ -67,4 +75,5 @@ allow_tables_to_appear_in_same_query!(
looted,
notifications,
players,
shop,
);