Files
lootalot/lootalot_front/src/components/PlayerView.js

108 lines
3.6 KiB
JavaScript

import { api } from '../lootalot.js'
export default {
props: ["id"],
data () { return {
player: {
name: "Loading",
id: 0,
cp: '-', sp: '-', gp: '-', pp: '-',
debt: 0,
},
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) {
var w = update.Wealth;
this.player.cp += w.cp;
this.player.sp += w.sp;
this.player.gp += w.gp;
this.player.pp += w.pp;
}
else if (update.ItemAdded) {
var i = update.ItemAdded;
this.loot.push(i);
}
else if (update.ItemRemoved) {
var i = update.ItemRemoved;
this.loot.splice(this.loot.indexOf(i), 1);
}
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) {
return api.fetch(`players/${this.id}/${resource}`, method, payload)
.then(response => {
if (response.notification) {
this.notifications.push(response.notification)
}
if (response.errors) {
this.notifications.push(response.errors)
}
if (response.updates) {
for (var idx in response.updates) {
this.parseUpdate(response.updates[idx]);
}
}
return response.value;
})
},
updateWealth (value) { this.call("wealth", "PUT", Number(value)) },
putClaim (itemId) { this.call("claims", "PUT", itemId) },
withdrawClaim (itemId) { this.call("claims", "DELETE", itemId) },
buyItems(items) { this.call("loot", "PUT", items) },
sellItems (items) { this.call("loot", "DELETE", items) },
},
watch: {
id: {
immediate: true,
handler: function(newId) {
this.call("", "GET", null)
.then(p => this.player = p)
this.call("loot", "GET", null)
.then(l => this.loot = l)
}
},
},
computed: {},
render () {
return this.$scopedSlots.default({
player: this.player,
loot: this.loot,
notifications: this.notifications,
actions: {
updateWealth: this.updateWealth,
putClaim: this.putClaim,
withdrawClaim: this.withdrawClaim,
buyItems: this.buyItems,
sellItems: this.sellItems,
},
claims: this.claims,
})
}
}