fixes some misbehaviour
This commit is contained in:
49
src/api.rs
49
src/api.rs
@@ -1,11 +1,14 @@
|
||||
use diesel::connection::Connection;
|
||||
use lootalot_db::{self as db, DbConnection, Update, Value};
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub type IdList = Vec<i32>;
|
||||
pub type ItemListWithMods = Vec<(i32, Option<f64>)>;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct BuySellParams {
|
||||
pub items: ItemListWithMods,
|
||||
players: Option<Vec<i32>>,
|
||||
players: Option<IdList>,
|
||||
global_mod: Option<f64>,
|
||||
}
|
||||
|
||||
@@ -66,6 +69,7 @@ pub enum ApiActions {
|
||||
UpdateWealth(i32, f64),
|
||||
BuyItems(i32, BuySellParams),
|
||||
SellItems(i32, BuySellParams),
|
||||
ClaimItems(i32, IdList),
|
||||
ClaimItem(i32, i32),
|
||||
UnclaimItem(i32, i32),
|
||||
UndoLastAction(i32),
|
||||
@@ -161,18 +165,23 @@ pub fn execute(
|
||||
.fold(db::Wealth::from_gp(0.0), |acc, i| acc + i);
|
||||
match id {
|
||||
0 => {
|
||||
let players = params.players.expect("The player list should be passed in !");
|
||||
let share = db::split_and_share(
|
||||
conn,
|
||||
total_amount.to_gp() as i32,
|
||||
&players,
|
||||
)?;
|
||||
let players = params
|
||||
.players
|
||||
.unwrap_or(db::Players(conn).all()?.into_iter().map(|p| p.id).collect());
|
||||
let shared = db::split_and_share(conn, total_amount.to_gp() as i32, &players)?;
|
||||
let shared_amount = {
|
||||
if let Update::Wealth(amount) = shared {
|
||||
amount.to_gp()
|
||||
} else {
|
||||
panic!("cannot happen")
|
||||
}
|
||||
};
|
||||
response.notify(format!(
|
||||
"Les objets ont été vendus, chaque joueur a reçu {} po",
|
||||
share.to_gp()
|
||||
"Les objets ont été vendus, les joueurs ont reçu (au total) {} po",
|
||||
shared_amount
|
||||
));
|
||||
//response.push_update(Update::GroupShare(players, share));
|
||||
response.push_update(Update::Wealth(share));
|
||||
response.push_update(shared);
|
||||
}
|
||||
_ => {
|
||||
response.notify(format!(
|
||||
@@ -186,15 +195,31 @@ pub fn execute(
|
||||
Some((id, "Vente d'objets"))
|
||||
}
|
||||
ApiActions::ClaimItem(id, item) => {
|
||||
response.push_update(Update::ClaimAdded(db::Claims(conn).add(id, item)?));
|
||||
response.push_update(db::Claims(conn).add(id, item)?);
|
||||
response.notify("Pour moi !".to_string());
|
||||
None
|
||||
}
|
||||
ApiActions::UnclaimItem(id, item) => {
|
||||
response.push_update(Update::ClaimRemoved(db::Claims(conn).remove(id, item)?));
|
||||
response.push_update(db::Claims(conn).remove(id, item)?);
|
||||
response.notify("Bof! Finalement non.".to_string());
|
||||
None
|
||||
}
|
||||
ApiActions::ClaimItems(id, items) => {
|
||||
conn.transaction(|| -> Result<Option<(i32, &str)>, diesel::result::Error> {
|
||||
let current_claims: HashSet<i32> =
|
||||
db::Claims(conn).all()?.iter().filter(|c| c.player_id == id).map(|c| c.loot_id).collect();
|
||||
let new_claims: HashSet<i32> = items.into_iter().collect();
|
||||
// Claims to delete
|
||||
for item in current_claims.difference(&new_claims) {
|
||||
response.push_update(db::Claims(conn).remove(id, *item)?);
|
||||
}
|
||||
// Claims to add
|
||||
for item in new_claims.difference(¤t_claims) {
|
||||
response.push_update(db::Claims(conn).add(id, *item)?);
|
||||
}
|
||||
Ok(Some((id, "Requête(s) mises(s) à jour")))
|
||||
})?
|
||||
}
|
||||
ApiActions::UndoLastAction(id) => {
|
||||
if let Ok(event) = db::models::history::get_last_of_player(conn, id) {
|
||||
let name = String::from(event.name());
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::api;
|
||||
type AppPool = web::Data<db::Pool>;
|
||||
type PlayerId = web::Path<i32>;
|
||||
type ItemId = web::Json<i32>;
|
||||
type IdList = web::Json<api::IdList>;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct NewGroupLoot {
|
||||
@@ -66,6 +67,12 @@ fn configure_api(config: &mut web::ServiceConfig) {
|
||||
.service(
|
||||
web::resource("/claims")
|
||||
//.route(web::get().to_async(endpoints::player_claims))
|
||||
.route(web::post().to_async(
|
||||
|pool, (player, data): (PlayerId, IdList)|
|
||||
{
|
||||
db_call(pool, Q::ClaimItems(*player, data.clone()))
|
||||
}
|
||||
))
|
||||
.route(web::put().to_async(
|
||||
|pool, (player, data): (PlayerId, ItemId)| {
|
||||
db_call(pool, Q::ClaimItem(*player, *data))
|
||||
@@ -142,7 +149,7 @@ pub fn serve() -> std::io::Result<()> {
|
||||
.configure(configure_api)
|
||||
.wrap(
|
||||
Cors::new()
|
||||
.allowed_origin("http://localhost:8080")
|
||||
.allowed_origin("http://localhost:8088")
|
||||
.allowed_methods(vec!["GET", "POST", "PUT", "DELETE", "OPTIONS"])
|
||||
.max_age(3600),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user