will need to clean up db API

This commit is contained in:
2020-01-05 15:37:40 +01:00
parent a47646bd5f
commit 6149dfd297
2 changed files with 82 additions and 45 deletions

View File

@@ -44,6 +44,57 @@ impl Item {
}
}
const OF_GROUP = 0;
const OF_SHOP = -1;
const SOLD = -2;
type WithOwner = Eq<looted::owner_id, i32>;
type OwnedLoot = Filter<looted::table, WithOwner>;
/// An owned item
///
/// The owner is a Player, the Group, the Merchant
/// OR the SOLD state.
#[derive(Identifiable, Debug, Queryable)]
#[table_name = "looted"]
pub(super) struct Loot {
id: i32,
name: String,
base_price: i32,
owner: i32,
}
impl Loot {
/// A filter on Loot that is owned by given player
pub(super) fn owned_by(id: i32) -> OwnedLoot {
looted::table.filter(looted::owner_id.eq(id))
}
fn exists(id: i32) -> Exists<Find<looted::table, i32>> {
exists(looted::table.find(id))
}
pub(super) fn set_owner(&self, owner: i32, conn: &DbConnection) -> QueryResult<()> {
diesel::update(looted::table.find(self.id))
.set(looted::dsl::owner_id.eq(owner))
.execute(conn)?;
Ok(())
}
pub(super) fn into_item(self) -> Item {
Item {
id: self.id,
name: self.name,
base_price: self.base_price,
}
}
pub(super) fn find(id: i32) -> Find<looted::table, i32> {
looted::table.find(id)
}
}
pub struct Inventory<'q>(pub &'q DbConnection);
impl<'q> Inventory<'q> {
@@ -58,13 +109,18 @@ impl<'q> Inventory<'q> {
}
}
/// The shop resource
pub struct Shop<'q>(pub &'q DbConnection);
impl<'q> Shop<'q> {
// Rename to list
pub fn all(&self) -> QueryResult<Vec<Item>> {
// Loot::owned_by(OF_SHOP).load(self.0)
shop::table.load(self.0)
}
// pub fn buy(&self, items: Vec<(i32, f64)>, buyer: i32) -> UpdateResult {}
pub fn get(&self, id: i32) -> QueryResult<Item> {
shop::table.find(&id).first::<Item>(self.0)
}
@@ -99,50 +155,6 @@ impl<'q> Shop<'q> {
}
}
type WithOwner = Eq<looted::owner_id, i32>;
type OwnedLoot = Filter<looted::table, WithOwner>;
/// Represents an item that has been looted,
/// hence has an owner.
#[derive(Identifiable, Debug, Queryable)]
#[table_name = "looted"]
pub(super) struct Loot {
id: i32,
name: String,
base_price: i32,
owner: i32,
}
impl Loot {
/// A filter on Loot that is owned by given player
pub(super) fn owned_by(id: i32) -> OwnedLoot {
looted::table.filter(looted::owner_id.eq(id))
}
fn exists(id: i32) -> Exists<Find<looted::table, i32>> {
exists(looted::table.find(id))
}
pub(super) fn set_owner(&self, owner: i32, conn: &DbConnection) -> QueryResult<()> {
diesel::update(looted::table.find(self.id))
.set(looted::dsl::owner_id.eq(owner))
.execute(conn)?;
Ok(())
}
pub(super) fn into_item(self) -> Item {
Item {
id: self.id,
name: self.name,
base_price: self.base_price,
}
}
pub(super) fn find(id: i32) -> Find<looted::table, i32> {
looted::table.find(id)
}
}
/// Manager for a *single* player loot
pub struct LootManager<'q>(pub &'q DbConnection, pub i32);

View File

@@ -98,6 +98,31 @@ pub enum ApiActions {
//SetClaimsTimeout(pub i32),
}
pub enum ApiEndpoints {
InventoryList,
InventoryCheck(Vec<String>),
InventoryAdd(String, i32), // db::Inventory(conn)::add(new_item)
ShopList, // db::Shop(conn)::list()
BuyItems(i32, BuySellParams), // db::Shop(conn)::buy(params)
RefreshShop(ItemList), // db::Shop(conn)::replace_list(items)
// db::Players::get returns AsPlayer<'q>
PlayerList, //db::Players(conn)::list()
PlayerAdd(NewPlayer), //db::Players(conn)::add(player)
PlayerFetch(i32), // db::Players(conn)::get(id)
PlayerClaims(i32), // db::Players(conn)::get(id).claims()
PlayerNotifications(i32), // db::Players(conn)::get(id).notifications()
PlayerLoot(i32),// db::Players(conn)::get(id).loot()
PlayerUpdateWealth(i32, f64), // db::Players(conn)::get(id).update_wealth(f64)
SellItems(i32, BuySellParams), // db::Players(conn)::get(id).sell(params)
ClaimItems(i32, IdList), // db::Players(conn)::get(id).claim(items)
UndoLastAction(i32), // db::Players(conn)::get(id).undo_last()
AddLoot(NewGroupLoot), // db::Group(conn)::add_loot(loot)
ResolveClaims, // db::Group(conn)::resolve_claims()
}
pub fn execute(
conn: &DbConnection,
query: ApiActions,
@@ -143,7 +168,7 @@ pub fn execute(
None
}
ApiActions::FetchPlayer(id) => {
response.set_value(Value::Player(db::Players(conn).find(id)?));
response.set_value(Value::Players(conn)(db::Players(conn).find(id)?));
None
}
ApiActions::FetchPlayerClaims(id) => {