Compare commits

2 Commits

Author SHA1 Message Date
8d1344e0b6 adds events api 2019-10-28 15:29:17 +01:00
e9f535ac86 impls undoable events 2019-10-28 15:27:26 +01:00
7 changed files with 132 additions and 22 deletions

View File

@@ -20,6 +20,7 @@ pub use models::{
claim::{Claim, Claims}, claim::{Claim, Claims},
item::{Item, LootManager, Inventory}, item::{Item, LootManager, Inventory},
player::{Player, Wealth, Players, AsPlayer}, player::{Player, Wealth, Players, AsPlayer},
history::Event,
}; };
/// The connection used /// The connection used
@@ -53,11 +54,18 @@ pub enum Update {
impl Update { impl Update {
/// Change back what has been updated /// Change back what has been updated
fn undo(self) -> QueryResult<()> { fn undo(&self, conn: &DbConnection, id: i32) -> QueryResult<()> {
match self { match self {
Update::Wealth(diff) => {}, Update::Wealth(diff) => {
Update::ItemAdded(item) => {}, AsPlayer(conn, id).update_wealth(-diff.to_gp())?;
Update::ItemRemoved(item) => {}, },
Update::ItemAdded(item) => {
LootManager(conn, id).remove(item.id)?;
},
Update::ItemRemoved(item) => {
LootManager(conn, id).add_from(&item)?;
},
// Unused for now
Update::ClaimAdded(claim) => {}, Update::ClaimAdded(claim) => {},
Update::ClaimRemoved(claim) => {}, Update::ClaimRemoved(claim) => {},
}; };
@@ -191,6 +199,12 @@ pub fn split_and_share(conn: &DbConnection, amount: i32, players: Vec<i32>) -> Q
} }
/// Reverts the last action stored for player
pub fn undo_last_action(conn: &DbConnection, id: i32) -> QueryResult<Event> {
let last_event = models::history::get_last_of_player(conn, id)?;
last_event.undo(conn)
}
#[cfg(none)] #[cfg(none)]
mod tests_old { mod tests_old {

View File

@@ -9,6 +9,9 @@ use crate::{DbConnection, QueryResult, Update};
#[derive(Debug, FromSqlRow)] #[derive(Debug, FromSqlRow)]
pub struct UpdateList(Vec<Update>); pub struct UpdateList(Vec<Update>);
// TODO: decide if updates is really optionnal or not
// (like if storing an event without update is usefull ?)
/// An event in history /// An event in history
#[derive(Debug, Queryable)] #[derive(Debug, Queryable)]
pub struct Event { pub struct Event {
@@ -19,6 +22,26 @@ pub struct Event {
updates: Option<UpdateList>, updates: Option<UpdateList>,
} }
impl Event {
pub fn name(&self) -> &str {
&self.text
}
/// TODO: why a move here ??
/// Undo all updates in a single transaction
pub fn undo(self, conn: &DbConnection) -> QueryResult<Self> {
conn.transaction(move || {
if let Some(ref updates) = self.updates {
for update in updates.0.iter() {
update.undo(conn, self.player_id)?;
}
diesel::delete(history::table.find(self.id)).execute(conn)?;
}
Ok(self)
})
}
}
impl<DB: Backend> FromSql<Text, DB> for UpdateList impl<DB: Backend> FromSql<Text, DB> for UpdateList
where where
String: FromSql<Text, DB>, String: FromSql<Text, DB>,
@@ -38,7 +61,11 @@ struct NewEvent<'a> {
updates: Option<String>, updates: Option<String>,
} }
/// Insert a new event /// Insert a new event
///
/// # Warning
/// This actually swallow up conversion errors
pub fn insert_event(conn: &DbConnection, id: i32, text: &str, updates: &Vec<Update>) -> QueryResult<Event> { pub fn insert_event(conn: &DbConnection, id: i32, text: &str, updates: &Vec<Update>) -> QueryResult<Event> {
diesel::insert_into(history::table) diesel::insert_into(history::table)
.values(&NewEvent { .values(&NewEvent {

View File

@@ -22,7 +22,7 @@ fn unpack_gold_value(gold: f64) -> (i32, i32, i32, i32) {
/// ///
/// Values are held as individual pieces counts. /// Values are held as individual pieces counts.
/// Allows conversion from and to a floating amount of gold pieces. /// Allows conversion from and to a floating amount of gold pieces.
#[derive(Queryable, AsChangeset, Serialize, Deserialize, Debug)] #[derive(Queryable, AsChangeset, Serialize, Deserialize, PartialEq, Copy, Clone, Debug)]
#[table_name = "players"] #[table_name = "players"]
pub struct Wealth { pub struct Wealth {
pub cp: i32, pub cp: i32,
@@ -116,4 +116,52 @@ mod tests {
} }
} }
#[test]
fn test_negative_wealth() {
use super::Wealth;
assert_eq!(
Wealth{ cp: 3, sp: 2, gp: 1, pp: 0 } + Wealth{ cp: -8, pp: 0, sp: 0, gp: 0 },
Wealth::from_gp(1.23 - 0.08)
)
}
#[test]
fn test_negative_wealth_inverse() {
use super::Wealth;
assert_eq!(
(Wealth{ cp: 3, sp: 2, gp: 1, pp: 0 } + Wealth{ cp: -8, pp: 0, sp: 0, gp: 0 }).to_gp(),
1.23 - 0.08
)
}
#[test]
fn test_diff_adding() {
use super::Wealth;
/// Let say we add 0.08 gp
/// 1.23 + 0.08 gold is 1.31, diff is cp: -2, sp: +1
let old = Wealth::from_gp(1.23);
let new = Wealth::from_gp(1.31);
let diff = new - old;
assert_eq!(diff.as_tuple(), (-2, 1, 0, 0));
assert_eq!(diff.to_gp(), 0.08);
assert_eq!(new - diff, old);
}
#[test]
fn test_diff_subbing() {
use super::Wealth;
/// Let say we sub 0.08 gp
/// 1.31 - 0.08 gold is 1.23, diff is cp: +2, sp: -1
let old = Wealth::from_gp(1.31);
let new = Wealth::from_gp(1.23);
let diff = new - old;
assert_eq!(diff.as_tuple(), (2, -1, 0, 0));
assert_eq!(diff.to_gp(), -0.08);
assert_eq!(new - diff, old);
}
} }

View File

@@ -31,6 +31,7 @@
@update="actions.updateWealth" @update="actions.updateWealth"
></Wealth> ></Wealth>
<p v-show="notifications.length > 0">{{ notifications }}</p> <p v-show="notifications.length > 0">{{ notifications }}</p>
<button class="button" @click="actions.undoLastAction">Annuler action</button>
</header> </header>
<nav> <nav>
<div class="tabs is-centered is-boxed is-medium"> <div class="tabs is-centered is-boxed is-medium">

View File

@@ -75,7 +75,8 @@ export default {
putClaim (itemId) { this.call("claims", "PUT", itemId) }, putClaim (itemId) { this.call("claims", "PUT", itemId) },
withdrawClaim (itemId) { this.call("claims", "DELETE", itemId) }, withdrawClaim (itemId) { this.call("claims", "DELETE", itemId) },
buyItems(items) { this.call("loot", "PUT", items) }, buyItems(items) { this.call("loot", "PUT", items) },
sellItems (items) { this.call("loot", "DELETE", items) }, sellItems (items) { this.call("loot", "DELETE", { items, global_mod: null, players: null }) },
undoLastAction () { this.call("events/last", "DELETE", null) },
}, },
watch: { watch: {
id: { id: {
@@ -100,6 +101,7 @@ export default {
withdrawClaim: this.withdrawClaim, withdrawClaim: this.withdrawClaim,
buyItems: this.buyItems, buyItems: this.buyItems,
sellItems: this.sellItems, sellItems: this.sellItems,
undoLastAction: this.undoLastAction,
}, },
claims: this.claims, claims: this.claims,
}) })

View File

@@ -68,6 +68,7 @@ pub enum ApiActions {
SellItems(i32, SellParams), SellItems(i32, SellParams),
ClaimItem(i32, i32), ClaimItem(i32, i32),
UnclaimItem(i32, i32), UnclaimItem(i32, i32),
UndoLastAction(i32),
// Group level // Group level
AddLoot(Vec<db::Item>), AddLoot(Vec<db::Item>),
// Admin level // Admin level
@@ -84,7 +85,7 @@ pub fn execute(
let mut response = ApiResponse::default(); let mut response = ApiResponse::default();
// Return an Option<String> that describes what happened. // Return an Option<String> that describes what happened.
// If there is some value, store the actions in db so that it can be reversed. // 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 => { ApiActions::FetchPlayers => {
response.set_value(Value::PlayerList(db::Players(conn).all()?)); response.set_value(Value::PlayerList(db::Players(conn).all()?));
None None
@@ -116,7 +117,7 @@ pub fn execute(
db::AsPlayer(conn, id).update_wealth(amount)?, db::AsPlayer(conn, id).update_wealth(amount)?,
)); ));
response.notify(format!("Mis à jour ({}po)!", amount)); response.notify(format!("Mis à jour ({}po)!", amount));
Some("Argent mis à jour") Some((id, "Argent mis à jour"))
} }
ApiActions::BuyItems(id, params) => { ApiActions::BuyItems(id, params) => {
// TODO: check that player has enough money ! // TODO: check that player has enough money !
@@ -141,7 +142,7 @@ pub fn execute(
total_amount.to_gp() total_amount.to_gp()
)); ));
response.push_update(Update::Wealth(total_amount)); response.push_update(Update::Wealth(total_amount));
Some("Achat d'objets") Some((id, "Achat d'objets"))
} }
// 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.
@@ -184,7 +185,7 @@ pub fn execute(
response.push_update(Update::Wealth(total_amount)); response.push_update(Update::Wealth(total_amount));
} }
} }
Some("Vente d'objets") Some((id, "Vente d'objets"))
} }
ApiActions::ClaimItem(id, item) => { 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)?));
@@ -196,6 +197,13 @@ pub fn execute(
response.notify("Bof! Finalement non.".to_string()); response.notify("Bof! Finalement non.".to_string());
None 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 // Group actions
ApiActions::AddLoot(items) => { ApiActions::AddLoot(items) => {
let mut added_items = 0; let mut added_items = 0;
@@ -214,20 +222,23 @@ pub fn execute(
{ {
response.push_error(format!("Erreur durant la notification : {:?}", e)); 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); dbg!(&action_text);
Ok(response) if let Some((id, text)) = action_text {
} db::models::history::insert_event(
conn,
/// Reverts the last action stored for player id,
fn revert_last_action(conn: &DbConnection, id: i32) -> Result<ApiResponse, diesel::result::Error> { text,
let mut response = ApiResponse::default(); response
// 1. Load the last action .updates
// 2. Iterate trought updates and reverse them (in a single transaction) .as_ref()
// 3. Send nice message back ("'action desc text' annulé !") .expect("there should be updates in here !"),
)?;
}
// match _action_text -> Save updates in DB
Ok(response) Ok(response)
} }

View File

@@ -112,7 +112,14 @@ fn configure_app(config: &mut web::ServiceConfig) {
db_call(pool, Q::SellItems(*player, data.into_inner())) db_call(pool, Q::SellItems(*player, data.into_inner()))
}, },
)), )),
), )
.service(
web::scope("/events")
.route("/last", web::delete().to_async(|pool, player: PlayerId| {
db_call(pool, Q::UndoLastAction(*player))
}))
)
), ),
) )
.route("/claims", web::get().to_async(|pool| db_call(pool, Q::FetchClaims))) .route("/claims", web::get().to_async(|pool| db_call(pool, Q::FetchClaims)))