diff --git a/lootalot_db/src/lib.rs b/lootalot_db/src/lib.rs index 92ad60d..83a1d20 100644 --- a/lootalot_db/src/lib.rs +++ b/lootalot_db/src/lib.rs @@ -131,30 +131,37 @@ impl<'q> AsPlayer<'q> { /// /// # Returns /// Result containing the difference in coins after operation - pub fn buy<'a>(self, params: &Vec<(i32, Option)>) -> ActionResult<(i32, i32, i32, i32)> { - let mut all_results: Vec<(i32, i32, i32, i32)> = Vec::with_capacity(params.len()); + pub fn buy<'a>(self, params: &Vec<(i32, Option)>) -> ActionResult<(Vec, (i32, i32, i32, i32))> { + let mut cumulated_diff: Vec<(i32, i32, i32, i32)> = Vec::with_capacity(params.len()); + let mut added_items: Vec = Vec::with_capacity(params.len()); for (item_id, price_mod) in params.into_iter() { - let res = self.conn.transaction(|| { + if let Ok((item, diff)) = self.conn.transaction(|| { + use schema::looted::dsl::*; let item = schema::items::table.find(item_id).first::(self.conn)?; let new_item = models::item::NewLoot::to_player(self.id, (&item.name, item.base_price)); - let _item_added = diesel::insert_into(schema::looted::table) + diesel::insert_into(schema::looted::table) .values(&new_item) - .execute(self.conn) - .map(|rows_updated| match rows_updated { - 1 => (), - _ => panic!("RuntimeError: Buy made no changes at all"), - })?; + .execute(self.conn)?; + let added_item = models::Item::owned_by(self.id) + .order(id.desc()) + .first(self.conn)?; let sell_price = match price_mod { Some(modifier) => item.base_price as f32 * modifier, None => item.base_price as f32 }; - DbApi::with_conn(self.conn).as_player(self.id).update_wealth(-sell_price) - }); - if let Ok(diff) = res { all_results.push(diff); } + DbApi::with_conn(self.conn) + .as_player(self.id) + .update_wealth(-sell_price) + .map(|diff| (added_item, diff)) + }) { + cumulated_diff.push(diff); + added_items.push(item); + } } - Ok(all_results.into_iter().fold((0,0,0,0), |sum, diff| { + let all_diff = cumulated_diff.into_iter().fold((0,0,0,0), |sum, diff| { (sum.0 + diff.0, sum.1 + diff.1, sum.2 + diff.2, sum.3 + diff.3) - })) + }); + Ok((added_items, all_diff)) } /// Sell a set of items from this player chest /// diff --git a/lootalot_front/src/AppStorage.js b/lootalot_front/src/AppStorage.js index a20bf88..9e1c432 100644 --- a/lootalot_front/src/AppStorage.js +++ b/lootalot_front/src/AppStorage.js @@ -31,7 +31,7 @@ export const Api = { }, fetchLoot (playerId) { return fetch(API_ENDPOINT("players/loot/" + playerId)) - .then(r => r.json()) + .then(r => r.json()) }, putClaim (player_id, item_id) { const payload = { player_id, item_id }; @@ -117,12 +117,6 @@ export const AppStorage = { switchPlayerChestVisibility () { if (this.debug) console.log('switchPlayerChestVisibility', !this.state.show_player_chest) this.state.show_player_chest = !this.state.show_player_chest - }, - // TODO - // get the content of a player Chest, retrieve form cache or fetched - // will replace hack that loads *all* chest... - getPlayerLoot (playerId) { - }, updatePlayerWealth (goldValue) { return Api.updateWealth(this.state.player_id, goldValue) @@ -147,25 +141,23 @@ export const AppStorage = { }, buyItems (items) { return Api.buyItems(this.state.player_id, items) - .then(diff => this.__updatePlayerWealth(diff)) - .then(() => { - // Add items to the player loot - console.log(items); - }); + .then(([items, diff]) => { + this.__updatePlayerWealth(diff) + // Add items to the player loot + // TODO: needs refactoring because player mutation happens in + // 2 different places + return items; + }); }, sellItems (items) { return Api.sellItems(this.state.player_id, items) .then(diff => this.__updatePlayerWealth(diff)) - .then(() => { - // Remove items from player chest - console.log(items); - }); }, // Withdraws a claim. cancelRequest(itemId) { const playerId = this.state.player_id return Api.unClaim(playerId, itemId) - .then(done => { + .then(_ => { var idx = this.state.player_claims[playerId].indexOf(itemId); if (idx > -1) { this.state.player_claims[playerId].splice(idx, 1); diff --git a/lootalot_front/src/components/Chest.vue b/lootalot_front/src/components/Chest.vue index 22cf801..f068767 100644 --- a/lootalot_front/src/components/Chest.vue +++ b/lootalot_front/src/components/Chest.vue @@ -103,10 +103,10 @@ this.is_selling = false; if (this.selected_items.length > 0) { const items = this.items.filter(i => this.selected_items.includes(i.id)); - var payload = []; - items.forEach(item => { - payload.push([item.id, null]); - }); + var payload = []; + items.forEach(item => { + payload.push([item.id, null]); + }); this.$emit("sell", payload); this.selected_items = []; } diff --git a/lootalot_front/src/components/PlayerView.js b/lootalot_front/src/components/PlayerView.js index d512050..cd76e0f 100644 --- a/lootalot_front/src/components/PlayerView.js +++ b/lootalot_front/src/components/PlayerView.js @@ -23,11 +23,20 @@ export default { }, buyItems(items) { AppStorage.buyItems(items) - .then(_ => this.notifications.push(`Bought ${items.length} items`)) + .then((items) => { + this.notifications.push(`Bought ${items.length} items`) + this.loot = this.loot.concat(items); + }) }, sellItems (items) { AppStorage.sellItems(items) - .then(_ => this.notifications.push(`Sold ${items.length} items`)) + .then(_ => { + this.notifications.push(`Sold ${items.length} items`) + for (var idx in items) { + var to_remove = items[idx][0]; + this.loot = this.loot.filter((item) => item.id != to_remove); + } + }) }, parseLoot (items) { this.loot = []; diff --git a/lootalot_front/src/components/Wealth.vue b/lootalot_front/src/components/Wealth.vue index 3f64b37..5cebccb 100644 --- a/lootalot_front/src/components/Wealth.vue +++ b/lootalot_front/src/components/Wealth.vue @@ -56,8 +56,8 @@ props: ["wealth", "debt"], data () { return { - editing: false, - edit_value: 0, + editing: false, + edit_value: 0, }; }, methods: { @@ -66,8 +66,8 @@ this.resetValues(); }, resetValues () { - this.editing = false; - this.edit_value = 0; + this.editing = false; + this.edit_value = 0; } }, computed: { diff --git a/src/server.rs b/src/server.rs index deb8488..76e7aa1 100644 --- a/src/server.rs +++ b/src/server.rs @@ -84,7 +84,7 @@ pub(crate) fn serve() -> std::io::Result<()> { .data(pool.clone()) .wrap( Cors::new() - .allowed_origin("http://localhost:8088") + .allowed_origin("http://localhost:8080") .allowed_methods(vec!["GET", "POST", "PUT", "DELETE", "OPTIONS"]) .max_age(3600), )