adds selling procedure for 'group'
This commit is contained in:
33
src/api.rs
33
src/api.rs
@@ -101,8 +101,7 @@ pub enum ApiActions {
|
||||
FetchLoot(i32),
|
||||
UpdateWealth(i32, f64),
|
||||
BuyItems(i32, Vec<(i32, Option<f64>)>),
|
||||
SellItems(i32, Vec<(i32, Option<f64>)>),
|
||||
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<db::Wealth> = 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<db::Wealth> = Vec::with_capacity(params.len());
|
||||
let mut all_results: Vec<db::Wealth> = 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(
|
||||
|
||||
@@ -109,10 +109,7 @@ fn configure_app(config: &mut web::ServiceConfig) {
|
||||
))
|
||||
.route(web::delete().to_async(
|
||||
move |pool, (player, data): (PlayerId, web::Json<api::SellParams>)| {
|
||||
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()))
|
||||
},
|
||||
)),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user