moves all api logic inside PlayerView, found weird bug in unpack_gold_value (floating error)

This commit is contained in:
2019-10-18 16:21:00 +02:00
parent 3fce1dc7d0
commit 61c9a4e6d4
7 changed files with 78 additions and 48 deletions

View File

@@ -50,7 +50,11 @@ impl<'q> AsPlayer<'q> {
.find(self.1)
.select((cp, sp, gp, pp))
.first::<Wealth>(self.0)?;
let updated_wealth = Wealth::from_gp(current_wealth.to_gp() + value_in_gp);
dbg!(&current_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)),
];