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

@@ -0,0 +1,17 @@
use diesel::prelude::*;
use crate::schema::claims;
#[derive(Queryable, Serialize, Debug)]
pub struct Claim {
id: i32,
player_id: i32,
loot_id: i32,
resolve: i32,
}
#[derive(Insertable, Debug)]
#[table_name="claims"]
pub struct NewClaim {
pub player_id: i32,
pub loot_id: i32,
}

View File

@@ -3,13 +3,11 @@ use diesel::dsl::{Eq, Filter, Select};
use crate::schema::{looted, items};
use crate::DbConnection;
type ItemColumns = ( looted::id, looted::name, looted::base_price, );
type ItemColumns =
( looted::id, looted::name, looted::base_price, );
const ITEM_COLUMNS: ItemColumns =
( looted::id, looted::name, looted::base_price, );
type OwnedBy = Select<OwnedLoot, ItemColumns>;
const ITEM_COLUMNS: ItemColumns = ( looted::id, looted::name, looted::base_price, );
/// New type to handle the inventory (items table)
/// This will be used to reduce confusion with looted items.
struct InventoryItem(Item);
/// Represents a unique item in inventory
///
@@ -43,6 +41,14 @@ struct Loot {
owner: i32,
}
impl Loot {
/// A filter on Loot that is owned by given player
fn owned_by(id: i32) -> OwnedLoot {
looted::table
.filter(looted::owner_id.eq(id))
}
}
/// An item being looted or bought.
///
/// The owner is set to 0 in case of looting,
@@ -54,43 +60,3 @@ struct NewLoot<'a> {
base_price: i32,
owner_id: i32,
}
impl<'a> NewLoot<'a> {
fn insert(&self, conn: &DbConnection) -> QueryResult<i32> {
Ok(0)
}
}
impl Loot {
/// A filter on Loot that is owned by given player
fn owned_by(id: i32) -> OwnedLoot {
looted::table
.filter(looted::owner_id.eq(id))
}
/// Loot an item, adding it to the group chest
fn loot(name: &str, base_price: i32) -> Result<i32, ()> {
let loot = NewLoot{ name, base_price, owner_id: 0 };
// Insert into table
// Retrieve id of created loot
let loot_id = 0;
Ok(loot_id)
}
/// Delete the item, returning the gained wealth
fn sell(_modifier: i8) -> Result<i32, ()> {
// Calculate sell value : base_value / 2 * modifier
// Delete recording of loot
Err(())
}
fn buy(buyer_id: i32, item_desc: (&str, i32)) -> Result<i32, ()> {
let loot = NewLoot{
name: item_desc.0,
base_price: item_desc.1,
owner_id: buyer_id
};
// Insert into table
// Retrieve id of created loot;
// Withdraw value from player wealth.
Ok(item_desc.1)
}
}

View File

@@ -1,6 +1,8 @@
mod item;
mod player;
mod claim;
pub use item::Item;
pub use player::Player;
pub use player::WealthUpdate;
pub use claim::{NewClaim, Claim};