From 6149dfd297354c90103005ebbc0292fba59f4c6b Mon Sep 17 00:00:00 2001 From: Artus Date: Sun, 5 Jan 2020 15:37:40 +0100 Subject: [PATCH] will need to clean up db API --- lootalot_db/src/models/item.rs | 100 ++++++++++++++++++--------------- src/api.rs | 27 ++++++++- 2 files changed, 82 insertions(+), 45 deletions(-) diff --git a/lootalot_db/src/models/item.rs b/lootalot_db/src/models/item.rs index 30dd6e2..0e53614 100644 --- a/lootalot_db/src/models/item.rs +++ b/lootalot_db/src/models/item.rs @@ -44,6 +44,57 @@ impl Item { } } +const OF_GROUP = 0; +const OF_SHOP = -1; +const SOLD = -2; + +type WithOwner = Eq; +type OwnedLoot = Filter; + +/// 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> { + 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.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> { + // 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 { shop::table.find(&id).first::(self.0) } @@ -99,50 +155,6 @@ impl<'q> Shop<'q> { } } -type WithOwner = Eq; -type OwnedLoot = Filter; - -/// 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> { - 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.find(id) - } -} - /// Manager for a *single* player loot pub struct LootManager<'q>(pub &'q DbConnection, pub i32); diff --git a/src/api.rs b/src/api.rs index 584582e..4fd3a0a 100644 --- a/src/api.rs +++ b/src/api.rs @@ -98,6 +98,31 @@ pub enum ApiActions { //SetClaimsTimeout(pub i32), } +pub enum ApiEndpoints { + InventoryList, + InventoryCheck(Vec), + 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) => {