fixes bugs in group sell
This commit is contained in:
79
src/api.rs
79
src/api.rs
@@ -122,7 +122,7 @@ pub fn execute(
|
||||
None
|
||||
}
|
||||
ApiActions::FetchPlayers => {
|
||||
response.set_value(Value::PlayerList(db::Players(conn).all()?));
|
||||
response.set_value(Value::PlayerList(db::Players(conn).all_except_group()?));
|
||||
None
|
||||
}
|
||||
ApiActions::FetchInventory => {
|
||||
@@ -167,9 +167,7 @@ pub fn execute(
|
||||
if has_enough_gold {
|
||||
let mut gains: Vec<db::Wealth> = Vec::with_capacity(params.items.len());
|
||||
for (item_id, price_mod) in params.items.into_iter() {
|
||||
if let Ok((item, diff)) =
|
||||
db::buy_item_from_shop(conn, id, item_id, price_mod)
|
||||
{
|
||||
if let Ok((item, diff)) = db::buy_item_from_shop(conn, id, item_id, price_mod) {
|
||||
response.push_update(item);
|
||||
gains.push(diff);
|
||||
} else {
|
||||
@@ -195,46 +193,47 @@ pub fn execute(
|
||||
// Behavior differs if player is group or regular.
|
||||
// Group sells item like players then split the total amount among players.
|
||||
ApiActions::SellItems(id, params) => {
|
||||
let mut gains: Vec<db::Wealth> = Vec::with_capacity(params.items.len());
|
||||
for (loot_id, price_mod) in params.items.iter() {
|
||||
if let Ok((deleted, diff)) =
|
||||
db::sell_item_transaction(conn, id, *loot_id, *price_mod)
|
||||
{
|
||||
response.push_update(deleted);
|
||||
gains.push(diff);
|
||||
} else {
|
||||
response.push_error(format!("Erreur lors de la vente (loot_id : {})", loot_id));
|
||||
}
|
||||
}
|
||||
let sold_items = gains.len();
|
||||
let total_amount = gains
|
||||
.into_iter()
|
||||
.fold(db::Wealth::from_gp(0.0), |acc, i| acc + i);
|
||||
match id {
|
||||
0 => {
|
||||
let players = params
|
||||
.players
|
||||
.unwrap_or(db::Players(conn).all()?.into_iter().map(|p| p.id).collect());
|
||||
if let Update::Wealth(shared) =
|
||||
db::split_and_share(conn, total_amount.to_gp() as i32, &players)?
|
||||
conn.transaction(|| -> Result<Option<(i32, &str)>, diesel::result::Error> {
|
||||
let mut gains: Vec<db::Wealth> = Vec::with_capacity(params.items.len());
|
||||
for (loot_id, price_mod) in params.items.iter() {
|
||||
if let Ok((deleted, diff)) =
|
||||
db::sell_item_transaction(conn, id, *loot_id, *price_mod)
|
||||
{
|
||||
response.push_update(deleted);
|
||||
gains.push(diff);
|
||||
} else {
|
||||
response
|
||||
.push_error(format!("Erreur lors de la vente (loot_id : {})", loot_id));
|
||||
}
|
||||
}
|
||||
let sold_items = gains.len();
|
||||
let total_amount = gains
|
||||
.into_iter()
|
||||
.fold(db::Wealth::from_gp(0.0), |acc, i| acc + i);
|
||||
match id {
|
||||
0 => {
|
||||
let players = params.players.unwrap_or_default();
|
||||
if let Update::Wealth(shared) =
|
||||
db::split_and_share(conn, total_amount.to_gp() as i32, players)?
|
||||
{
|
||||
response.notify(format!(
|
||||
"Les objets ont été vendus, les joueurs ont reçu (au total) {} po",
|
||||
shared.to_gp()
|
||||
));
|
||||
response.push_update(Update::Wealth(total_amount - shared));
|
||||
};
|
||||
}
|
||||
_ => {
|
||||
response.notify(format!(
|
||||
"Les objets ont été vendus, les joueurs ont reçu (au total) {} po",
|
||||
shared.to_gp()
|
||||
"{} objet(s) vendu(s) pour {} po",
|
||||
sold_items,
|
||||
total_amount.to_gp()
|
||||
));
|
||||
response.push_update(Update::Wealth(db::Wealth::from_gp(total_amount.to_gp() - shared.to_gp())));
|
||||
};
|
||||
response.push_update(Update::Wealth(total_amount));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
response.notify(format!(
|
||||
"{} objet(s) vendu(s) pour {} po",
|
||||
sold_items,
|
||||
total_amount.to_gp()
|
||||
));
|
||||
response.push_update(Update::Wealth(total_amount));
|
||||
}
|
||||
}
|
||||
Some((id, "Vente d'objets"))
|
||||
Ok(Some((id, "Vente d'objets")))
|
||||
})?
|
||||
}
|
||||
ApiActions::ClaimItem(id, item) => {
|
||||
response.push_update(db::Claims(conn).add(id, item)?);
|
||||
|
||||
Reference in New Issue
Block a user