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),
|
FetchLoot(i32),
|
||||||
UpdateWealth(i32, f64),
|
UpdateWealth(i32, f64),
|
||||||
BuyItems(i32, Vec<(i32, Option<f64>)>),
|
BuyItems(i32, Vec<(i32, Option<f64>)>),
|
||||||
SellItems(i32, Vec<(i32, Option<f64>)>),
|
SellItems(i32, SellParams),
|
||||||
SellGroupItems(i32, SellParams),
|
|
||||||
ClaimItem(i32, i32),
|
ClaimItem(i32, i32),
|
||||||
UnclaimItem(i32, i32),
|
UnclaimItem(i32, i32),
|
||||||
// Group actions
|
// Group actions
|
||||||
@@ -147,6 +146,7 @@ pub fn execute(
|
|||||||
response.notify(format!("Mis à jour ({}po)!", amount));
|
response.notify(format!("Mis à jour ({}po)!", amount));
|
||||||
}
|
}
|
||||||
ApiActions::BuyItems(id, params) => {
|
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 cumulated_diff: Vec<db::Wealth> = Vec::with_capacity(params.len());
|
||||||
let mut added_items: u16 = 0;
|
let mut added_items: u16 = 0;
|
||||||
for (item_id, price_mod) in params.into_iter() {
|
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.notify(format!("{} objets achetés pour {}po", added_items, total_amount.to_gp()));
|
||||||
response.push_update(Update::Wealth(total_amount));
|
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) => {
|
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;
|
let mut sold_items: u16 = 0;
|
||||||
for (loot_id, price_mod) in params.into_iter() {
|
for (loot_id, price_mod) in params.items.iter() {
|
||||||
if let Ok((deleted, diff)) = db::sell_item_transaction(conn, id, loot_id, price_mod) {
|
if let Ok((deleted, diff)) = db::sell_item_transaction(conn, id, *loot_id, *price_mod) {
|
||||||
all_results.push(diff);
|
all_results.push(diff);
|
||||||
response.push_update(Update::ItemRemoved(deleted));
|
response.push_update(Update::ItemRemoved(deleted));
|
||||||
sold_items += 1;
|
sold_items += 1;
|
||||||
} else {
|
} 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
|
let total_amount = all_results
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.fold(db::Wealth::from_gp(0.0), |acc, i| acc + i);
|
.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()));
|
match id {
|
||||||
response.push_update(Update::Wealth(total_amount));
|
0 => {
|
||||||
}
|
let share = db::split_and_share(conn, total_amount.to_gp() as i32, params.players.expect("Should not be None"))?;
|
||||||
ApiActions::SellGroupItems(id, params) => {
|
response.notify(format!("Les objets ont été vendus, chaque joueur a reçu {} po", share.to_gp()));
|
||||||
conn.transaction(|| {
|
response.push_update(Update::Wealth(share));
|
||||||
for i in params.items.into_iter() {
|
},
|
||||||
|
_ => {
|
||||||
|
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) => {
|
ApiActions::ClaimItem(id, item) => {
|
||||||
response.push_update(Update::ClaimAdded(
|
response.push_update(Update::ClaimAdded(
|
||||||
|
|||||||
@@ -109,10 +109,7 @@ fn configure_app(config: &mut web::ServiceConfig) {
|
|||||||
))
|
))
|
||||||
.route(web::delete().to_async(
|
.route(web::delete().to_async(
|
||||||
move |pool, (player, data): (PlayerId, web::Json<api::SellParams>)| {
|
move |pool, (player, data): (PlayerId, web::Json<api::SellParams>)| {
|
||||||
match *player {
|
db_call(pool, Q::SellItems(*player, data.into_inner()))
|
||||||
0 => db_call(pool, Q::SellItems(*player, data.into_inner().items)),
|
|
||||||
_ => db_call(pool, Q::SellGroupItems(*player, data.into_inner())),
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user