begins work on updates

This commit is contained in:
2019-07-01 16:05:42 +02:00
parent 5f598aff24
commit 9acedd4119
2 changed files with 36 additions and 34 deletions

View File

@@ -26,6 +26,7 @@ pub type ActionResult = QueryResult<bool>;
/// ::new() -> DbApi<'q> (Db finds a connection by itself, usefull for cli) /// ::new() -> DbApi<'q> (Db finds a connection by itself, usefull for cli)
/// ::with_conn(conn) -> DbApi<'q> (uses a user-defined connection) /// ::with_conn(conn) -> DbApi<'q> (uses a user-defined connection)
/// v .fetch_players() /// v .fetch_players()
/// x .fetch_inventory()
/// v .as_player(player_id) -> AsPlayer<'q> /// v .as_player(player_id) -> AsPlayer<'q>
/// v .loot() -> List of items owned (Vec<Item>) /// v .loot() -> List of items owned (Vec<Item>)
/// x .claim(item_id) -> Success status (bool) /// 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 .update_wealth(gold_pieces) -> Success status (bool, new_wealth)
/// x .as_admin() /// x .as_admin()
/// x .add_loot([inventory_item_ids]) -> Success status /// x .add_loot([inventory_item_ids]) -> Success status
/// x .resolve_claims()
/// x .sell_loot([players], [excluded_item_ids]) -> Success status (bool, player_share) /// 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); pub struct DbApi<'q>(&'q DbConnection);
@@ -60,6 +62,15 @@ impl<'q> DbApi<'q> {
.load::<models::Player>(self.0)? .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 /// Wrapper for acting as a specific player
/// ///
/// The DbApi is moved inside a new AsPlayer object. /// The DbApi is moved inside a new AsPlayer object.
@@ -89,6 +100,15 @@ impl<'q> AsPlayer<'q> {
.load(self.conn)? .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 /// Put a claim on a specific item
pub fn claim(self, item: i32) -> ActionResult { pub fn claim(self, item: i32) -> ActionResult {
Ok(false) Ok(false)

View File

@@ -1,4 +1,5 @@
use crate::schema::players; use crate::schema::players;
use crate::DbConnection;
/// Representation of a player in database /// Representation of a player in database
#[derive(Debug, Queryable, Serialize)] #[derive(Debug, Queryable, Serialize)]
@@ -14,28 +15,26 @@ pub struct Player {
/// Wealth represented as a single fractionnal amount of gold pieces /// Wealth represented as a single fractionnal amount of gold pieces
struct WealthInGold(f32); #[derive(Identifiable, AsChangeset)]
/// Data used to update wealth #[table_name="players"]
struct WealthUpdate(WealthInGold); struct WealthUpdate {
id: i32,
cp: i32,
sp: i32,
gp: i32,
pp: i32
}
impl WealthInGold { impl WealthUpdate{
/// Unpack individual pieces counts from gold value /// Unpack individual pieces counts from gold value
fn unpack(self) -> (i32, i32, i32, i32) { fn from_gp(id: i32, gp: f32) -> Self {
// TODO: 0,01 pp = 1 gp = 10 sp = 100 cp // TODO: 0,01 pp = 1 gp = 10 sp = 100 cp
(0,0,0,0) let (cp, sp, gp, pp) = (0,0,0,0);
} Self { id, cp, sp, gp, pp }
}
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
} }
} }
/// Representation of a new player record /// Representation of a new player record
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "players"] #[table_name = "players"]
@@ -47,23 +46,6 @@ pub struct NewPlayer<'a> {
pp: i32, 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;