moves all api logic inside PlayerView, found weird bug in unpack_gold_value (floating error)

This commit is contained in:
2019-10-18 16:21:00 +02:00
parent 3fce1dc7d0
commit 61c9a4e6d4
7 changed files with 78 additions and 48 deletions

View File

@@ -50,7 +50,11 @@ impl<'q> AsPlayer<'q> {
.find(self.1) .find(self.1)
.select((cp, sp, gp, pp)) .select((cp, sp, gp, pp))
.first::<Wealth>(self.0)?; .first::<Wealth>(self.0)?;
let updated_wealth = Wealth::from_gp(current_wealth.to_gp() + value_in_gp); dbg!(&current_wealth);
dbg!(current_wealth.to_gp());
dbg!(value_in_gp);
let updated_wealth = Wealth::from_gp(dbg!((current_wealth.to_gp() * 100.0 + value_in_gp * 100.0) / 100.0));
dbg!(&updated_wealth);
// Difference in coins that is sent back // Difference in coins that is sent back
let difference = Wealth { let difference = Wealth {
cp: updated_wealth.cp - current_wealth.cp, cp: updated_wealth.cp - current_wealth.cp,
@@ -58,6 +62,7 @@ impl<'q> AsPlayer<'q> {
gp: updated_wealth.gp - current_wealth.gp, gp: updated_wealth.gp - current_wealth.gp,
pp: updated_wealth.pp - current_wealth.pp, pp: updated_wealth.pp - current_wealth.pp,
}; };
diesel::update(players) diesel::update(players)
.filter(id.eq(self.1)) .filter(id.eq(self.1))
.set(&updated_wealth) .set(&updated_wealth)
@@ -122,8 +127,8 @@ impl Wealth {
/// # Examples /// # Examples
/// ``` /// ```
/// # use lootalot_db::models::Wealth; /// # use lootalot_db::models::Wealth;
/// let wealth = Wealth{ pp: 4, gp: 3, sp: 2, cp: 1}; /// let wealth = Wealth{ pp: 4, gp: 5, sp: 8, cp: 4};
/// assert_eq!(wealth.to_gp(), 403.21); /// assert_eq!(wealth.to_gp(), 405.84);
/// ``` /// ```
pub fn to_gp(&self) -> f32 { pub fn to_gp(&self) -> f32 {
let i = self.pp * 100 + self.gp; let i = self.pp * 100 + self.gp;
@@ -182,12 +187,16 @@ mod tests {
fn test_unpack_gold_values() { fn test_unpack_gold_values() {
use super::unpack_gold_value; use super::unpack_gold_value;
let test_values = [ let test_values = [
(0.01, (1, 0, 0, 0)),
(0.1, (0, 1, 0, 0)),
(1.0, (0, 0, 1, 0)), (1.0, (0, 0, 1, 0)),
(1.23, (3, 2, 1, 0)), (1.23, (3, 2, 1, 0)),
(1.03, (3, 0, 1, 0)), (1.03, (3, 0, 1, 0)),
(100.23, (3, 2, 0, 1)), (100.23, (3, 2, 0, 1)),
(-100.23, (-3, -2, -0, -1)), (-100.23, (-3, -2, -0, -1)),
(10189.23, (3, 2, 89, 101)), (10189.23, (3, 2, 89, 101)),
(141805.9, (0, 9, 5, 1418)),
(123141805.9, (0, 9, 5, 1231418)),
(-8090.20, (0, -2, -90, -80)), (-8090.20, (0, -2, -90, -80)),
]; ];

View File

@@ -1,7 +1,7 @@
<template> <template>
<PlayerView <PlayerView
:id="state.player_id" :id="state.player_id"
v-slot="{ player, loot, notifications, actions }" v-slot="{ player, loot, notifications, actions, claims }"
> >
<main id="app" class="container"> <main id="app" class="container">
<header> <header>
@@ -62,12 +62,16 @@
@confirmAction="addNewLoot" @confirmAction="addNewLoot"
></Loot> ></Loot>
<AddingChest <AddingChest
:player="player.id"
:claims="claims"
:items="playerIsGroup ? pending_loot : state.inventory" :items="playerIsGroup ? pending_loot : state.inventory"
:perms="playerIsGroup ? {} : { canBuy: true }" :perms="playerIsGroup ? {} : { canBuy: true }"
@buy="(data) => { switchView('player'); actions.buyItems(data); }"> @buy="(data) => { switchView('player'); actions.buyItems(data); }">
</AddingChest> </AddingChest>
</template> </template>
<Chest v-else <Chest v-else
:player="player.id"
:claims="claims"
:items="showPlayerChest ? loot : state.group_loot" :items="showPlayerChest ? loot : state.group_loot"
:perms="{ :perms="{
canGrab: !(showPlayerChest || playerIsGroup), canGrab: !(showPlayerChest || playerIsGroup),

View File

@@ -120,18 +120,6 @@ export const AppStorage = {
if (this.debug) console.log('switchPlayerChestVisibility', !this.state.show_player_chest) if (this.debug) console.log('switchPlayerChestVisibility', !this.state.show_player_chest)
this.state.show_player_chest = !this.state.show_player_chest this.state.show_player_chest = !this.state.show_player_chest
}, },
updatePlayerWealth (goldValue) {
return Api.updateWealth(this.state.player_id, goldValue)
.then(diff => this.__updatePlayerWealth(diff));
},
// TODO: Weird private name denotes a conflict
__updatePlayerWealth (diff) {
if (this.debug) console.log('updatePlayerWealth', diff)
this.state.player_list[this.state.player_id].cp += diff[0];
this.state.player_list[this.state.player_id].sp += diff[1];
this.state.player_list[this.state.player_id].gp += diff[2];
this.state.player_list[this.state.player_id].pp += diff[3];
},
// Put a claim on an item from group chest. // Put a claim on an item from group chest.
putRequest (itemId) { putRequest (itemId) {
const playerId = this.state.player_id const playerId = this.state.player_id
@@ -141,20 +129,6 @@ export const AppStorage = {
this.state.player_claims[playerId].push(itemId); this.state.player_claims[playerId].push(itemId);
}); });
}, },
buyItems (items) {
return Api.buyItems(this.state.player_id, 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))
},
// Withdraws a claim. // Withdraws a claim.
cancelRequest(itemId) { cancelRequest(itemId) {
const playerId = this.state.player_id const playerId = this.state.player_id

View File

@@ -47,6 +47,8 @@
<td> <td>
<Request <Request
v-if="perms.canGrab" v-if="perms.canGrab"
:id="player"
:claims="claims"
:item="item.id" :item="item.id"
@claim="(data) => $emit('claim', data)" @claim="(data) => $emit('claim', data)"
@unclaim="(data) => $emit('unclaim', data)" @unclaim="(data) => $emit('unclaim', data)"
@@ -68,6 +70,7 @@
import Request from './Request.vue' import Request from './Request.vue'
import PercentInput from './PercentInput.vue' import PercentInput from './PercentInput.vue'
import Selector from './Selector.vue' import Selector from './Selector.vue'
import { api } from '../lootalot.js'
/* /*
The chest displays a collection of items. The chest displays a collection of items.
@@ -77,6 +80,10 @@
*/ */
export default { export default {
props: { props: {
player: {
type: Number,
required: true,
},
items: { items: {
type: Array, type: Array,
required: true, required: true,
@@ -85,6 +92,10 @@
type: Object, type: Object,
required: true, required: true,
}, },
claims: {
type: Object,
required: true,
},
}, },
components: { components: {
Request, Request,

View File

@@ -11,7 +11,20 @@ export default {
}, },
notifications: [], notifications: [],
loot: [], loot: [],
claims: {},
}}, }},
created () {
api.fetch("claims", "GET", null)
.then(r => {
for (var idx in r.value) {
var claim = r.value[idx];
if (!(claim.player_id in this.claims)) {
this.$set(this.claims, claim.player_id, []);
}
this.claims[claim.player_id].push(claim.loot_id);
}
});
},
methods: { methods: {
parseUpdate (update) { parseUpdate (update) {
if (update.Wealth) { if (update.Wealth) {
@@ -21,16 +34,24 @@ export default {
this.player.gp += w.gp; this.player.gp += w.gp;
this.player.pp += w.pp; this.player.pp += w.pp;
} }
if (update.ItemAdded) { else if (update.ItemAdded) {
var i = update.ItemAdded; var i = update.ItemAdded;
this.loot.push(i); this.loot.push(i);
} }
if (update.ItemRemoved) { else if (update.ItemRemoved) {
var i = update.ItemRemoved; var i = update.ItemRemoved;
this.loot.splice(this.loot.indexOf(i), 1); this.loot.splice(this.loot.indexOf(i), 1);
} }
if (update.ClaimAdded || update.ClaimRemoved) { else if (update.ClaimAdded) {
console.error("cannot handle claim !", update); var c = update.ClaimAdded;
this.claims[c.player_id].push(c.loot_id);
}
else if (update.ClaimRemoved) {
var c = update.ClaimRemoved;
this.claims[c.player_id].splice(
this.claims[c.player_id].indexOf(c.loot_id),
1
);
} }
}, },
call (resource, method, payload) { call (resource, method, payload) {
@@ -79,7 +100,8 @@ export default {
withdrawClaim: this.withdrawClaim, withdrawClaim: this.withdrawClaim,
buyItems: this.buyItems, buyItems: this.buyItems,
sellItems: this.sellItems, sellItems: this.sellItems,
} },
claims: this.claims,
}) })
} }
} }

View File

@@ -14,7 +14,7 @@
</span> </span>
</button> </button>
</template> </template>
<button class="button is-primary is-fullwidth" <button class="button is-primary"
@click="putRequest" @click="putRequest"
:disabled="isRequested"> :disabled="isRequested">
<span class="icon is-small"> <span class="icon is-small">
@@ -27,26 +27,36 @@
<script> <script>
import { AppStorage } from '../AppStorage' import { AppStorage } from '../AppStorage'
export default { export default {
props: ["item"], props: {
data () { // Id of active player
return AppStorage.state; id: {
type: Number,
required: true,
},
// Map of all claims
claims: {
type: Object,
required: true,
},
// Id of item we are bound to
item: {
type: Number,
required: true,
}
}, },
computed: { computed: {
// Check if item is requested by active player // Check if item is requested by active player
isRequested () { isRequested () {
const reqs = this.player_claims[this.player_id]; return this.claims[this.id].includes(this.item);
return reqs.includes(this.item);
}, },
// Check if item is requested by multiple players including active one // Check if item is requested by multiple players including active one
isInConflict () { isInConflict () {
const reqs = this.player_claims;
const playerId = this.player_id;
var reqByPlayer = false; var reqByPlayer = false;
var reqByOther = false; var reqByOther = false;
for (var key in reqs) { for (var id in this.claims) {
const isReq = reqs[key].includes(this.item); const isReq = this.claims[id].includes(this.item);
if (isReq) { if (isReq) {
if (key == playerId) { if (id == this.id) {
reqByPlayer = true; reqByPlayer = true;
} else { } else {
reqByOther = true; reqByOther = true;

View File

@@ -127,7 +127,7 @@ pub fn execute(
response.push_update(Update::Wealth( response.push_update(Update::Wealth(
db::AsPlayer(conn, id).update_wealth(amount)?, db::AsPlayer(conn, id).update_wealth(amount)?,
)); ));
response.notify(format!("Money updated !")); response.notify(format!("Mis à jour ({}po)!", amount));
} }
ApiActions::BuyItems(id, params) => { ApiActions::BuyItems(id, params) => {
let mut cumulated_diff: Vec<db::Wealth> = Vec::with_capacity(params.len()); let mut cumulated_diff: Vec<db::Wealth> = Vec::with_capacity(params.len());