impls undoable events
This commit is contained in:
41
src/api.rs
41
src/api.rs
@@ -68,6 +68,7 @@ pub enum ApiActions {
|
||||
SellItems(i32, SellParams),
|
||||
ClaimItem(i32, i32),
|
||||
UnclaimItem(i32, i32),
|
||||
UndoLastAction(i32),
|
||||
// Group level
|
||||
AddLoot(Vec<db::Item>),
|
||||
// Admin level
|
||||
@@ -84,7 +85,7 @@ pub fn execute(
|
||||
let mut response = ApiResponse::default();
|
||||
// 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: Option<&str> = match query {
|
||||
let action_text: Option<(i32, &str)> = match query {
|
||||
ApiActions::FetchPlayers => {
|
||||
response.set_value(Value::PlayerList(db::Players(conn).all()?));
|
||||
None
|
||||
@@ -116,7 +117,7 @@ pub fn execute(
|
||||
db::AsPlayer(conn, id).update_wealth(amount)?,
|
||||
));
|
||||
response.notify(format!("Mis à jour ({}po)!", amount));
|
||||
Some("Argent mis à jour")
|
||||
Some((id, "Argent mis à jour"))
|
||||
}
|
||||
ApiActions::BuyItems(id, params) => {
|
||||
// TODO: check that player has enough money !
|
||||
@@ -141,7 +142,7 @@ pub fn execute(
|
||||
total_amount.to_gp()
|
||||
));
|
||||
response.push_update(Update::Wealth(total_amount));
|
||||
Some("Achat d'objets")
|
||||
Some((id, "Achat d'objets"))
|
||||
}
|
||||
// Behavior differs if player is group or regular.
|
||||
// Group sells item like players then split the total amount among players.
|
||||
@@ -184,7 +185,7 @@ pub fn execute(
|
||||
response.push_update(Update::Wealth(total_amount));
|
||||
}
|
||||
}
|
||||
Some("Vente d'objets")
|
||||
Some((id, "Vente d'objets"))
|
||||
}
|
||||
ApiActions::ClaimItem(id, item) => {
|
||||
response.push_update(Update::ClaimAdded(db::Claims(conn).add(id, item)?));
|
||||
@@ -196,6 +197,13 @@ pub fn execute(
|
||||
response.notify("Bof! Finalement non.".to_string());
|
||||
None
|
||||
}
|
||||
ApiActions::UndoLastAction(id) => {
|
||||
match db::undo_last_action(conn, id) {
|
||||
Ok(ev) => response.notify(format!("'{}' annulé(e)", ev.name())),
|
||||
Err(e) => response.push_error(format!("Erreur : {:?}", e)),
|
||||
};
|
||||
None
|
||||
}
|
||||
// Group actions
|
||||
ApiActions::AddLoot(items) => {
|
||||
let mut added_items = 0;
|
||||
@@ -214,20 +222,23 @@ pub fn execute(
|
||||
{
|
||||
response.push_error(format!("Erreur durant la notification : {:?}", e));
|
||||
};
|
||||
Some("Nouveau loot")
|
||||
Some((0, "Nouveau loot"))
|
||||
}
|
||||
};
|
||||
|
||||
// match _action_text -> Save updates in DB
|
||||
// Store the event if it can be undone.
|
||||
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> {
|
||||
let mut response = ApiResponse::default();
|
||||
// 1. Load the last action
|
||||
// 2. Iterate trought updates and reverse them (in a single transaction)
|
||||
// 3. Send nice message back ("'action desc text' annulé !")
|
||||
if let Some((id, text)) = action_text {
|
||||
db::models::history::insert_event(
|
||||
conn,
|
||||
id,
|
||||
text,
|
||||
response
|
||||
.updates
|
||||
.as_ref()
|
||||
.expect("there should be updates in here !"),
|
||||
)?;
|
||||
}
|
||||
// match _action_text -> Save updates in DB
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user