uses tips from diesel.rs guide for application integration

This commit is contained in:
2019-06-22 21:55:21 +02:00
parent 99404543b3
commit 5379ad236f
5 changed files with 69 additions and 44 deletions

View File

@@ -1,8 +1,64 @@
use crate::*;
use diesel::prelude::*;
use diesel::dsl::{Eq, Filter, Select};
use crate::schema::{looted, items};
/// Represents a unique item in inventory
///
/// It is also used as a public representation of Loot, since owner
/// information is implicit.
/// Or maybe this is a little too confusing ??
#[derive(Debug, Queryable, Serialize)]
pub struct Item {
id: i32,
name: String,
base_value: 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 {
/// 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
pub fn owned_by(player: i32) -> OwnedBy {
Loot::owned_by(player)
.select(ITEM_COLUMNS)
}
}
/// Represents an item that has been looted
#[derive(Debug, Queryable, Serialize)]
struct Loot {
id: i32,
name: String,
base_value: i32,
owner: i32,
}
type WithOwner = Eq<looted::owner_id, i32>;
type OwnedLoot = Filter<looted::table, WithOwner>;
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))
}
/// 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(())
}
}

View File

@@ -1,6 +1,4 @@
use crate::schema::players;
use crate::serde_derive::Serialize;
use crate::*;
/// Representation of a player in database
#[derive(Debug, Queryable, Serialize)]