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

View File

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

View File

@@ -120,18 +120,6 @@ export const AppStorage = {
if (this.debug) console.log('switchPlayerChestVisibility', !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.
putRequest (itemId) {
const playerId = this.state.player_id
@@ -141,20 +129,6 @@ export const AppStorage = {
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.
cancelRequest(itemId) {
const playerId = this.state.player_id

View File

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

View File

@@ -11,7 +11,20 @@ export default {
},
notifications: [],
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: {
parseUpdate (update) {
if (update.Wealth) {
@@ -21,16 +34,24 @@ export default {
this.player.gp += w.gp;
this.player.pp += w.pp;
}
if (update.ItemAdded) {
else if (update.ItemAdded) {
var i = update.ItemAdded;
this.loot.push(i);
}
if (update.ItemRemoved) {
else if (update.ItemRemoved) {
var i = update.ItemRemoved;
this.loot.splice(this.loot.indexOf(i), 1);
}
if (update.ClaimAdded || update.ClaimRemoved) {
console.error("cannot handle claim !", update);
else if (update.ClaimAdded) {
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) {
@@ -79,7 +100,8 @@ export default {
withdrawClaim: this.withdrawClaim,
buyItems: this.buyItems,
sellItems: this.sellItems,
}
},
claims: this.claims,
})
}
}

View File

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

View File

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