From 74ee4a831b7154abe60b73ee97b01f67bbb14601 Mon Sep 17 00:00:00 2001 From: Artus Date: Sun, 20 Oct 2019 22:03:06 +0200 Subject: [PATCH] works on selling group items --- lootalot_db/src/lib.rs | 15 +++++++++++++++ src/api.rs | 20 +++++++++++++++++++- src/server.rs | 12 +++++++----- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/lootalot_db/src/lib.rs b/lootalot_db/src/lib.rs index cafa91b..4626605 100644 --- a/lootalot_db/src/lib.rs +++ b/lootalot_db/src/lib.rs @@ -118,6 +118,21 @@ pub fn resolve_claims(conn: &DbConnection) -> QueryResult<()> { Ok(()) } +/// Split up and share group money among selected players +pub fn split_and_share(conn: &DbConnection, amount: i32, players: Vec) -> QueryResult { + let share = ( + amount / (players.len() + 1) as i32 // +1 share for the group + ) as f64; + conn.transaction(|| { + for p in players.into_iter() { + AsPlayer(conn, p).update_wealth(share)?; + AsPlayer(conn, 0).update_wealth(-share)?; + } + Ok(Wealth::from_gp(share)) + }) +} + + #[cfg(none)] mod tests_old { diff --git a/src/api.rs b/src/api.rs index 3a1bf93..2bb2985 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,5 +1,15 @@ +use diesel::prelude::*; use lootalot_db::{self as db, DbConnection, QueryResult}; +pub type ItemListWithMods = Vec<(i32, Option)>; + +#[derive(Serialize, Deserialize, Debug)] +pub struct SellParams { + pub items: ItemListWithMods, + players: Option>, + global_mod: Option, +} + /// Every possible update which can happen during a query #[derive(Serialize, Debug)] pub enum Update { @@ -92,6 +102,7 @@ pub enum ApiActions { UpdateWealth(i32, f64), BuyItems(i32, Vec<(i32, Option)>), SellItems(i32, Vec<(i32, Option)>), + SellGroupItems(i32, SellParams), ClaimItem(i32, i32), UnclaimItem(i32, i32), // Group actions @@ -154,7 +165,6 @@ pub fn execute( response.push_update(Update::Wealth(total_amount)); } ApiActions::SellItems(id, params) => { - // TODO: Different procedure for group and other players let mut all_results: Vec = Vec::with_capacity(params.len()); let mut sold_items: u16 = 0; for (loot_id, price_mod) in params.into_iter() { @@ -172,6 +182,14 @@ pub fn execute( 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() { + + } + Err(diesel::result::Error::RollbackTransaction) + }); + } ApiActions::ClaimItem(id, item) => { response.push_update(Update::ClaimAdded( db::Claims(conn).add(id, item)?, diff --git a/src/server.rs b/src/server.rs index c2c8637..ca26388 100644 --- a/src/server.rs +++ b/src/server.rs @@ -10,7 +10,6 @@ use crate::api; type AppPool = web::Data; type PlayerId = web::Path; type ItemId = web::Json; -type ItemListWithMods = web::Json)>>; #[derive(Serialize, Deserialize, Debug)] struct NewGroupLoot { @@ -99,18 +98,21 @@ fn configure_app(config: &mut web::ServiceConfig) { db_call(pool, Q::FetchLoot(*player)) })) .route(web::put().to_async( - move |pool, (player, data): (PlayerId, ItemListWithMods)| { + move |pool, (player, data): (PlayerId, web::Json)| { db_call(pool, Q::BuyItems(*player, data.into_inner())) }, )) .route(web::post().to( move |pool, (player, data): (PlayerId, web::Json)| { - forbidden_to_players(*player, (pool, Q::AddLoot(data.items.clone()))) + forbidden_to_players(*player, (pool, Q::AddLoot(data.into_inner().items))) }, )) .route(web::delete().to_async( - move |pool, (player, data): (PlayerId, ItemListWithMods)| { - db_call(pool, Q::SellItems(*player, data.into_inner())) + 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())), + } }, )), ),