preps historic events management
This commit is contained in:
142
src/api.rs
142
src/api.rs
@@ -1,5 +1,4 @@
|
||||
use diesel::prelude::*;
|
||||
use lootalot_db::{self as db, DbConnection, QueryResult};
|
||||
use lootalot_db::{self as db, DbConnection, Update, Value};
|
||||
|
||||
pub type ItemListWithMods = Vec<(i32, Option<f64>)>;
|
||||
|
||||
@@ -10,42 +9,6 @@ pub struct SellParams {
|
||||
global_mod: Option<f64>,
|
||||
}
|
||||
|
||||
/// Every possible update which can happen during a query
|
||||
#[derive(Serialize, Debug)]
|
||||
pub enum Update {
|
||||
Wealth(db::Wealth),
|
||||
ItemAdded(db::Item),
|
||||
ItemRemoved(db::Item),
|
||||
ClaimAdded(db::Claim),
|
||||
ClaimRemoved(db::Claim),
|
||||
}
|
||||
|
||||
/// Every value which can be queried
|
||||
#[derive(Debug)]
|
||||
pub enum Value {
|
||||
Player(db::Player),
|
||||
Item(db::Item),
|
||||
Claim(db::Claim),
|
||||
ItemList(Vec<db::Item>),
|
||||
ClaimList(Vec<db::Claim>),
|
||||
PlayerList(Vec<db::Player>),
|
||||
Notifications(Vec<String>),
|
||||
}
|
||||
|
||||
impl serde::Serialize for Value {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
match self {
|
||||
Value::Player(v) => v.serialize(serializer),
|
||||
Value::Item(v) => v.serialize(serializer),
|
||||
Value::Claim(v) => v.serialize(serializer),
|
||||
Value::ItemList(v) => v.serialize(serializer),
|
||||
Value::ClaimList(v) => v.serialize(serializer),
|
||||
Value::PlayerList(v) => v.serialize(serializer),
|
||||
Value::Notifications(v) => v.serialize(serializer),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A generic response for all queries
|
||||
#[derive(Serialize, Debug, Default)]
|
||||
pub struct ApiResponse {
|
||||
@@ -60,7 +23,7 @@ pub struct ApiResponse {
|
||||
}
|
||||
|
||||
impl ApiResponse {
|
||||
fn push_update(&mut self, update: Update) {
|
||||
fn push_update(&mut self, update: db::Update) {
|
||||
if let Some(v) = self.updates.as_mut() {
|
||||
v.push(update);
|
||||
} else {
|
||||
@@ -76,7 +39,7 @@ impl ApiResponse {
|
||||
}
|
||||
}
|
||||
|
||||
fn set_value(&mut self, value: Value) {
|
||||
fn set_value(&mut self, value: db::Value) {
|
||||
self.value = Some(value);
|
||||
}
|
||||
|
||||
@@ -122,25 +85,47 @@ pub fn execute(
|
||||
let mut response = ApiResponse::default();
|
||||
// TODO: Return an Option<String> that describes what happened.
|
||||
// If there is some value, store the actions in db so that it can be reversed.
|
||||
let _action_text: () = match query {
|
||||
ApiActions::FetchPlayers => response.set_value(Value::PlayerList(db::Players(conn).all()?)),
|
||||
ApiActions::FetchInventory => response.set_value(Value::ItemList(db::Inventory(conn).all()?)),
|
||||
ApiActions::FetchClaims => response.set_value(Value::ClaimList(db::fetch_claims(conn)?)),
|
||||
ApiActions::FetchPlayer(id) => response.set_value(Value::Player(db::Players(conn).find(id)?)),
|
||||
ApiActions::FetchNotifications(id) => response.set_value(Value::Notifications(db::AsPlayer(conn, id).notifications()?)),
|
||||
ApiActions::FetchLoot(id) => response.set_value(Value::ItemList(db::LootManager(conn, id).all()?)),
|
||||
let action_text: Option<&str> = match query {
|
||||
ApiActions::FetchPlayers => {
|
||||
response.set_value(Value::PlayerList(db::Players(conn).all()?));
|
||||
None
|
||||
}
|
||||
ApiActions::FetchInventory => {
|
||||
response.set_value(Value::ItemList(db::Inventory(conn).all()?));
|
||||
None
|
||||
}
|
||||
ApiActions::FetchClaims => {
|
||||
response.set_value(Value::ClaimList(db::fetch_claims(conn)?));
|
||||
None
|
||||
}
|
||||
ApiActions::FetchPlayer(id) => {
|
||||
response.set_value(Value::Player(db::Players(conn).find(id)?));
|
||||
None
|
||||
}
|
||||
ApiActions::FetchNotifications(id) => {
|
||||
response.set_value(Value::Notifications(
|
||||
db::AsPlayer(conn, id).notifications()?,
|
||||
));
|
||||
None
|
||||
}
|
||||
ApiActions::FetchLoot(id) => {
|
||||
response.set_value(Value::ItemList(db::LootManager(conn, id).all()?));
|
||||
None
|
||||
}
|
||||
ApiActions::UpdateWealth(id, amount) => {
|
||||
response.push_update(Update::Wealth(
|
||||
db::AsPlayer(conn, id).update_wealth(amount)?,
|
||||
));
|
||||
response.notify(format!("Mis à jour ({}po)!", amount));
|
||||
Some("Argent mis à jour")
|
||||
}
|
||||
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() {
|
||||
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(Update::ItemAdded(item));
|
||||
added_items += 1;
|
||||
@@ -149,10 +134,15 @@ pub fn execute(
|
||||
}
|
||||
}
|
||||
let total_amount = cumulated_diff
|
||||
.into_iter()
|
||||
.fold(db::Wealth::from_gp(0.0), |acc, i| acc + i);
|
||||
response.notify(format!("{} objets achetés pour {}po", added_items, total_amount.to_gp()));
|
||||
.into_iter()
|
||||
.fold(db::Wealth::from_gp(0.0), |acc, i| acc + i);
|
||||
response.notify(format!(
|
||||
"{} objets achetés pour {}po",
|
||||
added_items,
|
||||
total_amount.to_gp()
|
||||
));
|
||||
response.push_update(Update::Wealth(total_amount));
|
||||
Some("Achat d'objets")
|
||||
}
|
||||
// Behavior differs if player is group or regular.
|
||||
// Group sells item like players then split the total amount among players.
|
||||
@@ -160,7 +150,9 @@ pub fn execute(
|
||||
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.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);
|
||||
response.push_update(Update::ItemRemoved(deleted));
|
||||
sold_items += 1;
|
||||
@@ -169,39 +161,49 @@ pub fn execute(
|
||||
}
|
||||
}
|
||||
let total_amount = all_results
|
||||
.into_iter()
|
||||
.fold(db::Wealth::from_gp(0.0), |acc, i| acc + i);
|
||||
.into_iter()
|
||||
.fold(db::Wealth::from_gp(0.0), |acc, i| acc + i);
|
||||
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()));
|
||||
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.notify(format!(
|
||||
"{} objet(s) vendu(s) pour {} po",
|
||||
sold_items,
|
||||
total_amount.to_gp()
|
||||
));
|
||||
response.push_update(Update::Wealth(total_amount));
|
||||
}
|
||||
}
|
||||
Some("Vente d'objets")
|
||||
}
|
||||
ApiActions::ClaimItem(id, item) => {
|
||||
response.push_update(Update::ClaimAdded(
|
||||
db::Claims(conn).add(id, item)?,
|
||||
));
|
||||
response.push_update(Update::ClaimAdded(db::Claims(conn).add(id, item)?));
|
||||
response.notify(format!("Pour moi !"));
|
||||
None
|
||||
}
|
||||
ApiActions::UnclaimItem(id, item) => {
|
||||
response.push_update(Update::ClaimRemoved(
|
||||
db::Claims(conn).remove(id, item)?,
|
||||
));
|
||||
response.push_update(Update::ClaimRemoved(db::Claims(conn).remove(id, item)?));
|
||||
response.notify(format!("Bof! Finalement non."));
|
||||
None
|
||||
}
|
||||
// Group actions
|
||||
ApiActions::AddLoot(items) => {
|
||||
let mut added_items = 0;
|
||||
for item in items.into_iter() {
|
||||
if let Ok(added) = db::LootManager(conn, 0).add_from(&item) {
|
||||
response.push_update(Update::ItemAdded(added));
|
||||
added_items += 1;
|
||||
response.push_update(Update::ItemAdded(added));
|
||||
added_items += 1;
|
||||
} else {
|
||||
response.push_error(format!("Error adding {:?}", item));
|
||||
}
|
||||
@@ -213,17 +215,17 @@ pub fn execute(
|
||||
{
|
||||
response.push_error(format!("Erreur durant la notification : {:?}", e));
|
||||
};
|
||||
Some("Nouveau loot")
|
||||
}
|
||||
};
|
||||
|
||||
// match _action_text -> Save updates in DB
|
||||
dbg!(&action_text);
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
/// Reverts the last action stored for player
|
||||
fn revert_last_action(
|
||||
conn: &DbConnection,
|
||||
id: i32,
|
||||
) -> Result<ApiResponse, diesel::result::Error> {
|
||||
fn revert_last_action(conn: &DbConnection, id: i32) -> Result<ApiResponse, diesel::result::Error> {
|
||||
let mut response = ApiResponse::default();
|
||||
// 1. Load the last action
|
||||
// 2. Iterate trought updates and reverse them (in a single transaction)
|
||||
|
||||
Reference in New Issue
Block a user