Files
lootalot/lootalot_front/src/AppStorage.js

136 lines
4.5 KiB
JavaScript

import Vue from 'vue'
const API_BASEURL = "http://localhost:8088/api/"
const API_ENDPOINT = function (tailString) {
return API_BASEURL + tailString;
}
export const Api = {
fetchPlayerList () {
return fetch(API_ENDPOINT("players"))
.then(r => r.json())
},
fetchClaims () {
return fetch(API_ENDPOINT("claims"))
.then(r => r.json())
},
fetchLoot (playerId) {
return fetch(API_ENDPOINT(playerId + "/loot"))
.then(r => r.json())
},
putClaim (playerId, itemId) {
return fetch(API_ENDPOINT(playerId + "/claim/" + itemId))
.then(r => r.json())
},
unClaim (playerId, itemId) {
return fetch(API_ENDPOINT(playerId + "/unclaim/" + itemId))
.then(r => r.json())
},
updateWealth (playerId, goldValue) {
return fetch(API_ENDPOINT(playerId + "/update-wealth/" + goldValue))
.then(r => r.json())
}
};
export const AppStorage = {
debug: true,
state: {
player_id: 0,
player_list: {},
group_loot: [],
player_claims: {},
initiated: false,
show_player_chest: false,
},
// Initiate the state
initStorage (playerId) {
if (this.debug) console.log('Initiates with player : ', playerId)
this.state.player_id = playerId;
// Fetch initial data
return Promise
.all([
Api.fetchPlayerList(),
Api.fetchClaims(),
Api.fetchLoot(0)
])
.then(data => {
const [players, claims, group_loot] = data;
this.__initPlayerList(players);
this.__initClaimsStore(claims);
Vue.set(this.state, 'group_loot', group_loot);
})
.then(_ => this.state.initiated = true)
.catch(e => { alert(e); this.state.initiated = false });
},
__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];
const playerId = Number(playerDesc.id);
if (this.debug) console.log("Creates", playerId, playerDesc.name)
// Initiate data for a single Player.
Vue.set(this.state.player_list, playerId, playerDesc);
Vue.set(this.state.player_claims, playerId, []);
}
},
// User actions
// Sets a new active player by id
setActivePlayer (newPlayerId) {
if (this.debug) console.log('setActivePlayer to ', newPlayerId)
this.state.player_id = newPlayerId
document.cookie = `player_id=${newPlayerId};`;
},
// Show/Hide player's chest
switchPlayerChestVisibility () {
if (this.debug) console.log('switchPlayerChestVisibility', !this.state.show_player_chest)
this.state.show_player_chest = !this.state.show_player_chest
},
// TODO
// get the content of a player Chest, retrieve form cache or fetched
// will replace hack that loads *all* chest...
getPlayerLoot (playerId) {
},
updatePlayerWealth (goldValue) {
return Api.updateWealth(this.state.player_id, goldValue)
.then(response => {
// Update player wealth
var diff = 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];
});
},
// Put a claim on an item from group chest.
putRequest (itemId) {
const playerId = this.state.player_id
return Api.putClaim(playerId, itemId)
.then(done => {
// Update cliend-side state
this.state.player_claims[playerId].push(itemId);
});
},
// Withdraws a claim.
cancelRequest(itemId) {
const playerId = this.state.player_id
return Api.unClaim(playerId, itemId)
.then(done => {
var idx = this.state.player_claims[playerId].indexOf(itemId);
if (idx > -1) {
this.state.player_claims[playerId].splice(idx, 1);
} else {
if (this.debug) console.log("cancel a non-existent request")
}
});
}
}