From 61c9a4e6d44bea32d05e62709548ba2cd14c9894 Mon Sep 17 00:00:00 2001 From: Artus Date: Fri, 18 Oct 2019 16:21:00 +0200 Subject: [PATCH] moves all api logic inside PlayerView, found weird bug in unpack_gold_value (floating error) --- lootalot_db/src/models/player.rs | 15 ++++++++-- lootalot_front/src/App.vue | 8 ++++-- lootalot_front/src/AppStorage.js | 26 ----------------- lootalot_front/src/components/Chest.vue | 11 +++++++ lootalot_front/src/components/PlayerView.js | 32 +++++++++++++++++---- lootalot_front/src/components/Request.vue | 32 ++++++++++++++------- src/api.rs | 2 +- 7 files changed, 78 insertions(+), 48 deletions(-) diff --git a/lootalot_db/src/models/player.rs b/lootalot_db/src/models/player.rs index a3c0ad8..bb7fbe5 100644 --- a/lootalot_db/src/models/player.rs +++ b/lootalot_db/src/models/player.rs @@ -50,7 +50,11 @@ impl<'q> AsPlayer<'q> { .find(self.1) .select((cp, sp, gp, pp)) .first::(self.0)?; - let updated_wealth = Wealth::from_gp(current_wealth.to_gp() + value_in_gp); + dbg!(¤t_wealth); + dbg!(current_wealth.to_gp()); + dbg!(value_in_gp); + let updated_wealth = Wealth::from_gp(dbg!((current_wealth.to_gp() * 100.0 + value_in_gp * 100.0) / 100.0)); + dbg!(&updated_wealth); // Difference in coins that is sent back let difference = Wealth { cp: updated_wealth.cp - current_wealth.cp, @@ -58,6 +62,7 @@ impl<'q> AsPlayer<'q> { gp: updated_wealth.gp - current_wealth.gp, pp: updated_wealth.pp - current_wealth.pp, }; + diesel::update(players) .filter(id.eq(self.1)) .set(&updated_wealth) @@ -122,8 +127,8 @@ impl Wealth { /// # Examples /// ``` /// # use lootalot_db::models::Wealth; - /// let wealth = Wealth{ pp: 4, gp: 3, sp: 2, cp: 1}; - /// assert_eq!(wealth.to_gp(), 403.21); + /// let wealth = Wealth{ pp: 4, gp: 5, sp: 8, cp: 4}; + /// assert_eq!(wealth.to_gp(), 405.84); /// ``` pub fn to_gp(&self) -> f32 { let i = self.pp * 100 + self.gp; @@ -182,12 +187,16 @@ mod tests { fn test_unpack_gold_values() { use super::unpack_gold_value; let test_values = [ + (0.01, (1, 0, 0, 0)), + (0.1, (0, 1, 0, 0)), (1.0, (0, 0, 1, 0)), (1.23, (3, 2, 1, 0)), (1.03, (3, 0, 1, 0)), (100.23, (3, 2, 0, 1)), (-100.23, (-3, -2, -0, -1)), (10189.23, (3, 2, 89, 101)), + (141805.9, (0, 9, 5, 1418)), + (123141805.9, (0, 9, 5, 1231418)), (-8090.20, (0, -2, -90, -80)), ]; diff --git a/lootalot_front/src/App.vue b/lootalot_front/src/App.vue index 06aa34e..396b84b 100644 --- a/lootalot_front/src/App.vue +++ b/lootalot_front/src/App.vue @@ -1,7 +1,7 @@ - { + for (var idx in r.value) { + var claim = r.value[idx]; + if (!(claim.player_id in this.claims)) { + this.$set(this.claims, claim.player_id, []); + } + this.claims[claim.player_id].push(claim.loot_id); + } + }); + }, methods: { parseUpdate (update) { if (update.Wealth) { @@ -21,16 +34,24 @@ export default { this.player.gp += w.gp; this.player.pp += w.pp; } - if (update.ItemAdded) { + else if (update.ItemAdded) { var i = update.ItemAdded; this.loot.push(i); } - if (update.ItemRemoved) { + else if (update.ItemRemoved) { var i = update.ItemRemoved; this.loot.splice(this.loot.indexOf(i), 1); } - if (update.ClaimAdded || update.ClaimRemoved) { - console.error("cannot handle claim !", update); + else if (update.ClaimAdded) { + var c = update.ClaimAdded; + this.claims[c.player_id].push(c.loot_id); + } + else if (update.ClaimRemoved) { + var c = update.ClaimRemoved; + this.claims[c.player_id].splice( + this.claims[c.player_id].indexOf(c.loot_id), + 1 + ); } }, call (resource, method, payload) { @@ -79,7 +100,8 @@ export default { withdrawClaim: this.withdrawClaim, buyItems: this.buyItems, sellItems: this.sellItems, - } + }, + claims: this.claims, }) } } diff --git a/lootalot_front/src/components/Request.vue b/lootalot_front/src/components/Request.vue index f0ab45e..b1fe6c8 100644 --- a/lootalot_front/src/components/Request.vue +++ b/lootalot_front/src/components/Request.vue @@ -14,7 +14,7 @@ -