begins work on updates
This commit is contained in:
@@ -26,6 +26,7 @@ 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 .as_player(player_id) -> AsPlayer<'q>
|
||||
/// v .loot() -> List of items owned (Vec<Item>)
|
||||
/// x .claim(item_id) -> Success status (bool)
|
||||
@@ -35,8 +36,9 @@ pub type ActionResult = QueryResult<bool>;
|
||||
/// x .update_wealth(gold_pieces) -> Success status (bool, new_wealth)
|
||||
/// x .as_admin()
|
||||
/// x .add_loot([inventory_item_ids]) -> Success status
|
||||
/// x .resolve_claims()
|
||||
/// x .sell_loot([players], [excluded_item_ids]) -> Success status (bool, player_share)
|
||||
/// x .resolve_claims()
|
||||
/// x .add_player(player_data)
|
||||
///
|
||||
pub struct DbApi<'q>(&'q DbConnection);
|
||||
|
||||
@@ -60,6 +62,15 @@ impl<'q> DbApi<'q> {
|
||||
.load::<models::Player>(self.0)?
|
||||
)
|
||||
}
|
||||
/// Fetch the inventory of items
|
||||
///
|
||||
/// Consumes the DbApi instance
|
||||
pub fn fetch_inventory(self) -> QueryResult<Vec<models::Item>> {
|
||||
Ok(
|
||||
schema::items::table
|
||||
.load::<models::Item>(self.0)?
|
||||
)
|
||||
}
|
||||
/// Wrapper for acting as a specific player
|
||||
///
|
||||
/// The DbApi is moved inside a new AsPlayer object.
|
||||
@@ -89,6 +100,15 @@ impl<'q> AsPlayer<'q> {
|
||||
.load(self.conn)?
|
||||
)
|
||||
}
|
||||
pub fn update_wealth(self, value: f32) -> ActionResult {
|
||||
Ok(
|
||||
false
|
||||
// TODO:
|
||||
// models::player::WealthUpdate::from_gp(self.id, value)
|
||||
// .save_changes(&conn)?;
|
||||
)
|
||||
|
||||
}
|
||||
/// Put a claim on a specific item
|
||||
pub fn claim(self, item: i32) -> ActionResult {
|
||||
Ok(false)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::schema::players;
|
||||
use crate::DbConnection;
|
||||
|
||||
/// Representation of a player in database
|
||||
#[derive(Debug, Queryable, Serialize)]
|
||||
@@ -14,28 +15,26 @@ pub struct Player {
|
||||
|
||||
|
||||
/// Wealth represented as a single fractionnal amount of gold pieces
|
||||
struct WealthInGold(f32);
|
||||
/// Data used to update wealth
|
||||
struct WealthUpdate(WealthInGold);
|
||||
|
||||
impl WealthInGold {
|
||||
/// Unpack individual pieces counts from gold value
|
||||
fn unpack(self) -> (i32, i32, i32, i32) {
|
||||
// TODO: 0,01 pp = 1 gp = 10 sp = 100 cp
|
||||
(0,0,0,0)
|
||||
#[derive(Identifiable, AsChangeset)]
|
||||
#[table_name="players"]
|
||||
struct WealthUpdate {
|
||||
id: i32,
|
||||
cp: i32,
|
||||
sp: i32,
|
||||
gp: i32,
|
||||
pp: i32
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
impl WealthUpdate{
|
||||
/// Apply the update to the specified player
|
||||
fn commit(self, player_id: i32) {
|
||||
// Extract (cp, sp, gp, pp) from floating gold piece value
|
||||
// Update record in db
|
||||
/// Unpack individual pieces counts from gold value
|
||||
fn from_gp(id: i32, gp: f32) -> Self {
|
||||
// TODO: 0,01 pp = 1 gp = 10 sp = 100 cp
|
||||
let (cp, sp, gp, pp) = (0,0,0,0);
|
||||
Self { id, cp, sp, gp, pp }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Representation of a new player record
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "players"]
|
||||
@@ -47,23 +46,6 @@ pub struct NewPlayer<'a> {
|
||||
pp: i32,
|
||||
}
|
||||
|
||||
impl<'a> NewPlayer<'a> {
|
||||
fn new(name: &'a str, wealth: Option<WealthInGold>) -> Self {
|
||||
let (cp, sp, gp, pp) = wealth.map(|w| w.unpack()).unwrap_or((0, 0, 0, 0));
|
||||
NewPlayer {
|
||||
name,
|
||||
cp,
|
||||
sp,
|
||||
gp,
|
||||
pp,
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(self) -> Result<(), ()> {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user