enhance responses from API, integrates with frontend

This commit is contained in:
2019-07-11 14:54:18 +02:00
parent bc65dfcd78
commit e08cd64743
9 changed files with 130 additions and 26 deletions

View File

@@ -22,12 +22,14 @@ const Api = {
.catch(e => console.error("Fetch error", e));
},
putClaim (playerId, itemId) {
console.log('newRequest from', playerId, 'on', itemId);
return Promise.resolve(true);
return fetch(API_ENDPOINT(playerId + "/claim/" + itemId))
.then(r => r.json())
.catch(e => console.error("Fetch error", e));
},
unClaim (playerId, itemId) {
console.log('cancelRequest of', playerId, 'on', itemId);
return Promise.resolve(true);
return fetch(API_ENDPOINT(playerId + "/unclaim/" + itemId))
.then(r => r.json())
.catch(e => console.error("Fetch error", e));
},
updateWealth (playerId, goldValue) {
return fetch(API_ENDPOINT(playerId + "/update-wealth/" + goldValue))
@@ -57,9 +59,15 @@ export const AppStorage = {
.then(data => {
const [players, claims] = data;
this.__initPlayerList(players);
console.log("claims", claims);
this.__initClaimsStore(claims);
});
},
__initClaimsStore(data) {
for (var idx in data) {
var claimDesc = data[idx];
this.state.player_claims[claimDesc.player_id].push(claimDesc.loot_id);
}
},
__initPlayerList(data) {
for (var idx in data) {
var playerDesc = data[idx];
@@ -105,14 +113,18 @@ export const AppStorage = {
},
updatePlayerWealth (goldValue) {
if (this.debug) console.log('updatePlayerWealth', goldValue, this.state.player_id)
return Api.updateWealth(this.state.player_id, goldValue)
.then(done => {
if (done) {
if (done.executed) {
// Update player wealth
this.state.player_list[this.state.player_id].cp += 1;
var diff = done.response;
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];
}
return done;
return done.executed;
});
},
// Put a claim on an item from group chest.
@@ -120,7 +132,7 @@ export const AppStorage = {
const playerId = this.state.player_id
Api.putClaim(playerId, itemId)
.then(done => {
if (done) {
if (done.executed) {
// Update cliend-side state
this.state.player_claims[playerId].push(itemId);
} else {
@@ -133,7 +145,7 @@ export const AppStorage = {
const playerId = this.state.player_id
Api.unClaim(playerId, itemId)
.then(done => {
if (done) {
if (done.executed) {
var idx = this.state.player_claims[playerId].indexOf(itemId);
if (idx > -1) {
this.state.player_claims[playerId].splice(idx, 1);