fixes bugs in group sell
This commit is contained in:
@@ -204,35 +204,42 @@ pub fn resolve_claims(conn: &DbConnection) -> QueryResult<()> {
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Wealth update with the amount of money actually shared
|
||||
/// A Wealth update with the amount of money shared with players
|
||||
pub fn split_and_share(
|
||||
conn: &DbConnection,
|
||||
amount: i32,
|
||||
players: &Vec<i32>,
|
||||
players: Vec<i32>,
|
||||
) -> UpdateResult {
|
||||
let players = match players.is_empty() {
|
||||
true => Players(conn)
|
||||
.all_except_group()?
|
||||
.iter()
|
||||
.map(|p| p.id)
|
||||
.collect(),
|
||||
false => players
|
||||
};
|
||||
let share = (
|
||||
amount / (players.len() + 1) as i32
|
||||
// +1 share for the group
|
||||
) as f64;
|
||||
conn.transaction(|| {
|
||||
// What we actually give, negative value
|
||||
let mut diff = 0.0;
|
||||
for p in players {
|
||||
let player = Players(conn).find(*p)?;
|
||||
let mut shared_total = 0.0;
|
||||
for id in players {
|
||||
let player = Players(conn).find(id)?;
|
||||
// Take debt into account
|
||||
match share - player.debt as f64 {
|
||||
rest if rest > 0.0 => {
|
||||
AsPlayer(conn, *p).update_debt(-player.debt)?;
|
||||
AsPlayer(conn, *p).update_wealth(rest)?;
|
||||
AsPlayer(conn, id).update_debt(-player.debt)?;
|
||||
AsPlayer(conn, id).update_wealth(rest)?;
|
||||
AsPlayer(conn, 0).update_wealth(-rest)?;
|
||||
diff -= rest;
|
||||
shared_total += rest;
|
||||
}
|
||||
_ => {
|
||||
AsPlayer(conn, *p).update_debt(-share as i32)?;
|
||||
AsPlayer(conn, id).update_debt(-share as i32)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Update::Wealth(Wealth::from_gp(diff)))
|
||||
Ok(Update::Wealth(Wealth::from_gp(shared_total)))
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,12 @@ impl<'q> Players<'q> {
|
||||
players::table.load(self.0)
|
||||
}
|
||||
|
||||
/// Get all non-group players
|
||||
pub fn all_except_group(&self) -> QueryResult<Vec<Player>> {
|
||||
use diesel::dsl::not;
|
||||
players::table.filter(not(players::id.eq(0))).load(self.0)
|
||||
}
|
||||
|
||||
/// Find a player by id
|
||||
pub fn find(&self, id: i32) -> QueryResult<Player> {
|
||||
players::table.find(id).first(self.0)
|
||||
|
||||
@@ -69,12 +69,7 @@ impl std::ops::Sub for Wealth {
|
||||
/// What needs to be added to 'other' so that
|
||||
/// the result equals 'self'
|
||||
fn sub(self, other: Self) -> Self {
|
||||
Wealth {
|
||||
cp: self.cp - other.cp,
|
||||
sp: self.sp - other.sp,
|
||||
gp: self.gp - other.gp,
|
||||
pp: self.pp - other.pp,
|
||||
}
|
||||
Wealth::from_gp(self.to_gp() - other.to_gp())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,12 +77,7 @@ impl std::ops::Add for Wealth {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self {
|
||||
Wealth {
|
||||
cp: self.cp + other.cp,
|
||||
sp: self.sp + other.sp,
|
||||
gp: self.gp + other.gp,
|
||||
pp: self.pp + other.pp
|
||||
}
|
||||
Wealth::from_gp(self.to_gp() + other.to_gp())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user