impls claims on looted items
This commit is contained in:
@@ -28,11 +28,11 @@ pub type ActionResult = QueryResult<bool>;
|
||||
/// x .fetch_inventory()
|
||||
/// v .as_player(player_id) -> AsPlayer<'q>
|
||||
/// v .loot() -> List of items owned (Vec<Item>)
|
||||
/// x .claim(item_id) -> Success status (bool)
|
||||
/// x .unclaim(item_id) -> Success status (bool)
|
||||
/// v .claim(item_id) -> Success status (bool)
|
||||
/// v .unclaim(item_id) -> Success status (bool)
|
||||
/// x .sell(item_id) -> Success status (bool, earned)
|
||||
/// x .buy(inventory_item_id) -> Success status (bool, cost)
|
||||
/// x .update_wealth(gold_pieces) -> Success status (bool, new_wealth)
|
||||
/// v .update_wealth(gold_pieces) -> Success status (bool, new_wealth)
|
||||
/// x .as_admin()
|
||||
/// x .add_loot([inventory_item_ids]) -> Success status
|
||||
/// x .sell_loot([players], [excluded_item_ids]) -> Success status (bool, player_share)
|
||||
@@ -70,6 +70,13 @@ impl<'q> DbApi<'q> {
|
||||
.load::<models::Item>(self.0)?
|
||||
)
|
||||
}
|
||||
|
||||
pub fn fetch_claims(self) -> QueryResult<Vec<models::Claim>> {
|
||||
Ok(
|
||||
schema::claims::table
|
||||
.load::<models::Claim>(self.0)?
|
||||
)
|
||||
}
|
||||
/// Wrapper for acting as a specific player
|
||||
///
|
||||
/// The DbApi is moved inside a new AsPlayer object.
|
||||
@@ -117,7 +124,20 @@ impl<'q> AsPlayer<'q> {
|
||||
}
|
||||
/// Put a claim on a specific item
|
||||
pub fn claim(self, item: i32) -> ActionResult {
|
||||
Ok(false)
|
||||
let request = models::NewClaim { player_id: self.id, loot_id: item };
|
||||
diesel::insert_into(schema::claims::table)
|
||||
.values(&request)
|
||||
.execute(self.conn)
|
||||
.map(|r| match r { 1 => true, _ => false })
|
||||
}
|
||||
pub fn unclaim(self, item: i32) -> ActionResult {
|
||||
use schema::claims::dsl::*;
|
||||
diesel::delete(
|
||||
claims
|
||||
.filter(loot_id.eq(item))
|
||||
.filter(player_id.eq(self.id)))
|
||||
.execute(self.conn)
|
||||
.map(|r| match r { 1 => true, _ => false })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
17
lootalot_db/src/models/claim.rs
Normal file
17
lootalot_db/src/models/claim.rs
Normal 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,
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -62,6 +62,10 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
||||
db_call(pool, move |api| api.fetch_players())
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/claims",
|
||||
web::get().to_async(move |pool: AppPool| db_call(pool, move |api| api.fetch_claims())),
|
||||
)
|
||||
.route(
|
||||
"/update-wealth/{player_id}/{amount}",
|
||||
web::get().to_async(move |pool: AppPool, data: web::Path<(i32, f32)>| {
|
||||
|
||||
Reference in New Issue
Block a user