From 9acedd4119a66c49762173c32e7a12dfcca48e80 Mon Sep 17 00:00:00 2001 From: Artus Date: Mon, 1 Jul 2019 16:05:42 +0200 Subject: [PATCH] begins work on updates --- lootalot_db/src/lib.rs | 22 ++++++++++++++- lootalot_db/src/models/player.rs | 48 ++++++++++---------------------- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/lootalot_db/src/lib.rs b/lootalot_db/src/lib.rs index 07db7c3..c53675a 100644 --- a/lootalot_db/src/lib.rs +++ b/lootalot_db/src/lib.rs @@ -26,6 +26,7 @@ pub type ActionResult = QueryResult; /// ::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) /// x .claim(item_id) -> Success status (bool) @@ -35,8 +36,9 @@ pub type ActionResult = QueryResult; /// 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::(self.0)? ) } + /// Fetch the inventory of items + /// + /// Consumes the DbApi instance + pub fn fetch_inventory(self) -> QueryResult> { + Ok( + schema::items::table + .load::(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) diff --git a/lootalot_db/src/models/player.rs b/lootalot_db/src/models/player.rs index 580456d..b298647 100644 --- a/lootalot_db/src/models/player.rs +++ b/lootalot_db/src/models/player.rs @@ -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); +#[derive(Identifiable, AsChangeset)] +#[table_name="players"] +struct WealthUpdate { + id: i32, + cp: i32, + sp: i32, + gp: i32, + pp: i32 +} -impl WealthInGold { +impl WealthUpdate{ /// 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 - (0,0,0,0) - } - -} - - -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 + 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) -> 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::*;