adds buy/sell endpoints

This commit is contained in:
2019-08-07 15:34:08 +02:00
parent 6e7a0f6211
commit cb98b97126
5 changed files with 149 additions and 47 deletions

View File

@@ -44,7 +44,16 @@ export const Api = {
updateWealth (player_id, value_in_gp) {
const payload = { player_id, value_in_gp: Number(value_in_gp) };
return this.__doFetch("players/update-wealth", 'PUT', payload);
}
},
buyItems (player_id, items) {
const payload = { player_id, items };
return this.__doFetch("players/buy", 'POST', payload);
},
sellItems (player_id, items) {
const payload = { player_id, items };
return this.__doFetch("players/sell", 'POST', payload);
},
};
@@ -117,15 +126,15 @@ export const AppStorage = {
},
updatePlayerWealth (goldValue) {
return Api.updateWealth(this.state.player_id, goldValue)
.then(response => {
// Update player wealth
var diff = response;
.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) {
@@ -136,6 +145,22 @@ export const AppStorage = {
this.state.player_claims[playerId].push(itemId);
});
},
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);
});
},
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