unifies Buy and Sell parameters

This commit is contained in:
2019-11-06 14:31:05 +01:00
parent ee0b7b2b7a
commit 1afbcff12a
2 changed files with 18 additions and 20 deletions

View File

@@ -3,7 +3,7 @@ use lootalot_db::{self as db, DbConnection, Update, Value};
pub type ItemListWithMods = Vec<(i32, Option<f64>)>; pub type ItemListWithMods = Vec<(i32, Option<f64>)>;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct SellParams { pub struct BuySellParams {
pub items: ItemListWithMods, pub items: ItemListWithMods,
players: Option<Vec<i32>>, players: Option<Vec<i32>>,
global_mod: Option<f64>, global_mod: Option<f64>,
@@ -64,8 +64,8 @@ pub enum ApiActions {
FetchNotifications(i32), FetchNotifications(i32),
FetchLoot(i32), FetchLoot(i32),
UpdateWealth(i32, f64), UpdateWealth(i32, f64),
BuyItems(i32, Vec<(i32, Option<f64>)>), BuyItems(i32, BuySellParams),
SellItems(i32, SellParams), SellItems(i32, BuySellParams),
ClaimItem(i32, i32), ClaimItem(i32, i32),
UnclaimItem(i32, i32), UnclaimItem(i32, i32),
UndoLastAction(i32), UndoLastAction(i32),
@@ -119,19 +119,18 @@ pub fn execute(
} }
ApiActions::BuyItems(id, params) => { ApiActions::BuyItems(id, params) => {
// TODO: check that player has enough money ! // TODO: check that player has enough money !
let mut cumulated_diff: Vec<db::Wealth> = Vec::with_capacity(params.len()); let mut gains: Vec<db::Wealth> = Vec::with_capacity(params.items.len());
let mut added_items: u16 = 0; for (item_id, price_mod) in params.items.into_iter() {
for (item_id, price_mod) in params.into_iter() {
if let Ok((item, diff)) = db::buy_item_from_inventory(conn, id, item_id, price_mod) if let Ok((item, diff)) = db::buy_item_from_inventory(conn, id, item_id, price_mod)
{ {
cumulated_diff.push(diff); response.push_update(item);
response.push_update(Update::ItemAdded(item)); gains.push(diff);
added_items += 1;
} else { } else {
response.push_error(format!("Error adding {}", item_id)); response.push_error(format!("Error adding {}", item_id));
} }
} }
let total_amount = cumulated_diff let added_items = gains.len();
let total_amount = gains
.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!( response.notify(format!(
@@ -145,20 +144,19 @@ pub fn execute(
// Behavior differs if player is group or regular. // Behavior differs if player is group or regular.
// Group sells item like players then split the total amount among players. // 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.items.len()); let mut gains: Vec<db::Wealth> = Vec::with_capacity(params.items.len());
let mut sold_items: u16 = 0;
for (loot_id, price_mod) in params.items.iter() { for (loot_id, price_mod) in params.items.iter() {
if let Ok((deleted, diff)) = if let Ok((deleted, diff)) =
db::sell_item_transaction(conn, id, *loot_id, *price_mod) db::sell_item_transaction(conn, id, *loot_id, *price_mod)
{ {
all_results.push(diff); response.push_update(deleted);
response.push_update(Update::ItemRemoved(deleted)); gains.push(diff);
sold_items += 1;
} else { } else {
response.push_error(format!("Erreur lors de la vente (loot_id : {})", loot_id)); response.push_error(format!("Erreur lors de la vente (loot_id : {})", loot_id));
} }
} }
let total_amount = all_results let sold_items = gains.len();
let total_amount = gains
.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);
match id { match id {
@@ -214,7 +212,7 @@ pub fn execute(
let mut added_items = 0; let mut added_items = 0;
for item in items.into_iter() { for item in items.into_iter() {
if let Ok(added) = db::LootManager(conn, 0).add_from(&item) { if let Ok(added) = db::LootManager(conn, 0).add_from(&item) {
response.push_update(Update::ItemAdded(added)); response.push_update(added);
added_items += 1; added_items += 1;
} else { } else {
response.push_error(format!("Error adding {:?}", item)); response.push_error(format!("Error adding {:?}", item));

View File

@@ -1,7 +1,7 @@
use actix_cors::Cors; use actix_cors::Cors;
use actix_files as fs; use actix_files as fs;
use actix_web::{web, middleware, App, Error, HttpResponse, HttpServer, Either}; use actix_web::{web, middleware, App, Error, HttpResponse, HttpServer, Either};
use futures::{Future, IntoFuture}; use futures::{Future};
use std::env; use std::env;
use lootalot_db as db; use lootalot_db as db;
@@ -98,7 +98,7 @@ fn configure_app(config: &mut web::ServiceConfig) {
db_call(pool, Q::FetchLoot(*player)) db_call(pool, Q::FetchLoot(*player))
})) }))
.route(web::put().to_async( .route(web::put().to_async(
move |pool, (player, data): (PlayerId, web::Json<api::ItemListWithMods>)| { move |pool, (player, data): (PlayerId, web::Json<api::BuySellParams>)| {
db_call(pool, Q::BuyItems(*player, data.into_inner())) db_call(pool, Q::BuyItems(*player, data.into_inner()))
}, },
)) ))
@@ -108,7 +108,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::BuySellParams>)| {
db_call(pool, Q::SellItems(*player, data.into_inner())) db_call(pool, Q::SellItems(*player, data.into_inner()))
}, },
)), )),