diff --git a/src/api.rs b/src/api.rs index 2bb2985..26f3a0c 100644 --- a/src/api.rs +++ b/src/api.rs @@ -101,8 +101,7 @@ pub enum ApiActions { FetchLoot(i32), UpdateWealth(i32, f64), BuyItems(i32, Vec<(i32, Option)>), - SellItems(i32, Vec<(i32, Option)>), - SellGroupItems(i32, SellParams), + SellItems(i32, SellParams), ClaimItem(i32, i32), UnclaimItem(i32, i32), // Group actions @@ -147,6 +146,7 @@ pub fn execute( response.notify(format!("Mis à jour ({}po)!", amount)); } ApiActions::BuyItems(id, params) => { + // TODO: check that player has enough money ! let mut cumulated_diff: Vec = Vec::with_capacity(params.len()); let mut added_items: u16 = 0; for (item_id, price_mod) in params.into_iter() { @@ -164,31 +164,34 @@ pub fn execute( response.notify(format!("{} objets achetés pour {}po", added_items, total_amount.to_gp())); response.push_update(Update::Wealth(total_amount)); } + // 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 all_results: Vec = Vec::with_capacity(params.len()); + let mut all_results: Vec = Vec::with_capacity(params.items.len()); let mut sold_items: u16 = 0; - for (loot_id, price_mod) in params.into_iter() { - if let Ok((deleted, diff)) = db::sell_item_transaction(conn, id, loot_id, price_mod) { + for (loot_id, price_mod) in params.items.iter() { + if let Ok((deleted, diff)) = db::sell_item_transaction(conn, id, *loot_id, *price_mod) { all_results.push(diff); response.push_update(Update::ItemRemoved(deleted)); sold_items += 1; } else { - response.push_error(format!("Error selling {}", loot_id)); + response.push_error(format!("Erreur lors de la vente (loot_id : {})", loot_id)); } } let total_amount = all_results .into_iter() .fold(db::Wealth::from_gp(0.0), |acc, i| acc + i); - response.notify(format!("{} objet(s) vendu(s) pour {} po", sold_items, total_amount.to_gp())); - response.push_update(Update::Wealth(total_amount)); - } - ApiActions::SellGroupItems(id, params) => { - conn.transaction(|| { - for i in params.items.into_iter() { - + match id { + 0 => { + let share = db::split_and_share(conn, total_amount.to_gp() as i32, params.players.expect("Should not be None"))?; + response.notify(format!("Les objets ont été vendus, chaque joueur a reçu {} po", share.to_gp())); + response.push_update(Update::Wealth(share)); + }, + _ => { + response.notify(format!("{} objet(s) vendu(s) pour {} po", sold_items, total_amount.to_gp())); + response.push_update(Update::Wealth(total_amount)); } - Err(diesel::result::Error::RollbackTransaction) - }); + } } ApiActions::ClaimItem(id, item) => { response.push_update(Update::ClaimAdded( diff --git a/src/server.rs b/src/server.rs index ca26388..6f9e1e4 100644 --- a/src/server.rs +++ b/src/server.rs @@ -109,10 +109,7 @@ fn configure_app(config: &mut web::ServiceConfig) { )) .route(web::delete().to_async( move |pool, (player, data): (PlayerId, web::Json)| { - match *player { - 0 => db_call(pool, Q::SellItems(*player, data.into_inner().items)), - _ => db_call(pool, Q::SellGroupItems(*player, data.into_inner())), - } + db_call(pool, Q::SellItems(*player, data.into_inner())) }, )), ),