impls add player admin action
This commit is contained in:
@@ -25,19 +25,20 @@ pub type ActionResult = QueryResult<bool>;
|
||||
/// ::new() -> DbApi<'q> (Db finds a connection by itself, usefull for cli)
|
||||
/// ::with_conn(conn) -> DbApi<'q> (uses a user-defined connection)
|
||||
/// v .fetch_players()
|
||||
/// x .fetch_inventory()
|
||||
/// v .fetch_inventory()
|
||||
/// v .fetch_claims()
|
||||
/// v .as_player(player_id) -> AsPlayer<'q>
|
||||
/// v .loot() -> List of items owned (Vec<Item>)
|
||||
/// 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)
|
||||
/// v .update_wealth(gold_pieces) -> Success status (bool, new_wealth)
|
||||
/// x .as_admin()
|
||||
/// x .add_loot([inventory_item_ids]) -> Success status
|
||||
/// v .claim(loot_id) -> Success status (bool)
|
||||
/// v .unclaim(loot_id) -> Success status (bool)
|
||||
/// x .sell(loot_id) -> Success status (bool, earned)
|
||||
/// x .buy(item_desc) -> Success status (bool, cost)
|
||||
/// v .update_wealth(value_in_gold) -> Success status (bool, new_wealth)
|
||||
/// v .as_admin()
|
||||
/// x .add_loot(identifier, [items_desc]) -> Success status
|
||||
/// x .sell_loot([players], [excluded_item_ids]) -> Success status (bool, player_share)
|
||||
/// x .resolve_claims()
|
||||
/// x .add_player(player_data)
|
||||
/// v .add_player(player_data)
|
||||
///
|
||||
pub struct DbApi<'q>(&'q DbConnection);
|
||||
|
||||
@@ -89,6 +90,11 @@ impl<'q> DbApi<'q> {
|
||||
pub fn as_player(self, id: i32) -> AsPlayer<'q> {
|
||||
AsPlayer { id, conn: self.0 }
|
||||
}
|
||||
|
||||
/// Wrapper for acting as the admin
|
||||
pub fn as_admin(self) -> AsAdmin<'q> {
|
||||
AsAdmin(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper for interactions of players with the database
|
||||
@@ -106,6 +112,9 @@ impl<'q> AsPlayer<'q> {
|
||||
.load(self.conn)?
|
||||
)
|
||||
}
|
||||
/// Adds the value in gold to the player's wealth.
|
||||
///
|
||||
/// Value can be negative to substract wealth.
|
||||
pub fn update_wealth(self, value: f32) -> ActionResult {
|
||||
use schema::players::dsl::*;
|
||||
let current_wealth = players.find(self.id)
|
||||
@@ -130,6 +139,7 @@ impl<'q> AsPlayer<'q> {
|
||||
.execute(self.conn)
|
||||
.map(|r| match r { 1 => true, _ => false })
|
||||
}
|
||||
/// Withdraw claim
|
||||
pub fn unclaim(self, item: i32) -> ActionResult {
|
||||
use schema::claims::dsl::*;
|
||||
diesel::delete(
|
||||
@@ -142,6 +152,18 @@ impl<'q> AsPlayer<'q> {
|
||||
}
|
||||
|
||||
|
||||
pub struct AsAdmin<'q>(&'q DbConnection);
|
||||
|
||||
impl<'q> AsAdmin<'q> {
|
||||
|
||||
pub fn add_player(self, name: String, start_wealth: f32) -> ActionResult {
|
||||
diesel::insert_into(schema::players::table)
|
||||
.values(&models::NewPlayer::create(&name, start_wealth))
|
||||
.execute(self.0)
|
||||
.map(|r| match r { 1 => true, _ => false })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_pool() -> Pool {
|
||||
let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL");
|
||||
dbg!( &connspec );
|
||||
|
||||
Reference in New Issue
Block a user