keeps working on design, updates doc comments
This commit is contained in:
@@ -1,6 +1,15 @@
|
|||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel::dsl::{Eq, Filter, Select};
|
use diesel::dsl::{Eq, Filter, Select};
|
||||||
use crate::schema::{looted, items};
|
use crate::schema::{looted, items};
|
||||||
|
use crate::DbConnection;
|
||||||
|
|
||||||
|
type 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
|
/// Represents a unique item in inventory
|
||||||
///
|
///
|
||||||
@@ -14,22 +23,7 @@ pub struct Item {
|
|||||||
base_price: i32,
|
base_price: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
type ItemColumns = ( looted::id, looted::name, looted::base_price, );
|
|
||||||
type OwnedBy = Select<OwnedLoot, ItemColumns>;
|
|
||||||
const ITEM_COLUMNS: ItemColumns = ( looted::id, looted::name, looted::base_price, );
|
|
||||||
|
|
||||||
impl Item {
|
impl Item {
|
||||||
/// Insert this item inside the Looted table.
|
|
||||||
///
|
|
||||||
/// This adds a copy of the item into the group chest,
|
|
||||||
/// to be claimed by players or sold.
|
|
||||||
/// Returns the id of the looted item (differs from the original id)
|
|
||||||
pub fn to_looted(self) -> Result<i32, ()>{
|
|
||||||
// Copy data inside a new Loot
|
|
||||||
// Set the owner id to 0 (group)
|
|
||||||
Err(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Public proxy for Loot::owned_by that selects only Item fields
|
/// Public proxy for Loot::owned_by that selects only Item fields
|
||||||
pub fn owned_by(player: i32) -> OwnedBy {
|
pub fn owned_by(player: i32) -> OwnedBy {
|
||||||
Loot::owned_by(player)
|
Loot::owned_by(player)
|
||||||
@@ -37,6 +31,9 @@ impl Item {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type WithOwner = Eq<looted::owner_id, i32>;
|
||||||
|
type OwnedLoot = Filter<looted::table, WithOwner>;
|
||||||
|
|
||||||
/// Represents an item that has been looted
|
/// Represents an item that has been looted
|
||||||
#[derive(Debug, Queryable, Serialize)]
|
#[derive(Debug, Queryable, Serialize)]
|
||||||
struct Loot {
|
struct Loot {
|
||||||
@@ -46,8 +43,23 @@ struct Loot {
|
|||||||
owner: i32,
|
owner: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
type WithOwner = Eq<looted::owner_id, i32>;
|
/// An item being looted or bought.
|
||||||
type OwnedLoot = Filter<looted::table, WithOwner>;
|
///
|
||||||
|
/// The owner is set to 0 in case of looting,
|
||||||
|
/// to the id of buying player otherwise.
|
||||||
|
#[derive(Insertable)]
|
||||||
|
#[table_name = "looted"]
|
||||||
|
struct NewLoot<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
base_price: i32,
|
||||||
|
owner_id: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> NewLoot<'a> {
|
||||||
|
fn insert(&self, conn: &DbConnection) -> QueryResult<i32> {
|
||||||
|
Ok(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Loot {
|
impl Loot {
|
||||||
/// A filter on Loot that is owned by given player
|
/// A filter on Loot that is owned by given player
|
||||||
@@ -55,10 +67,30 @@ impl Loot {
|
|||||||
looted::table
|
looted::table
|
||||||
.filter(looted::owner_id.eq(id))
|
.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
|
/// Delete the item, returning the gained wealth
|
||||||
fn sell(_modifier: i8) -> Result<i32, ()> {
|
fn sell(_modifier: i8) -> Result<i32, ()> {
|
||||||
// Calculate sell value : base_value / 2 * modifier
|
// Calculate sell value : base_value / 2 * modifier
|
||||||
// Delete recording of loot
|
// Delete recording of loot
|
||||||
Err(())
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user