more refactoring, removing imports

This commit is contained in:
2019-07-31 21:48:59 +02:00
parent dae7633c11
commit 15d87e3b47
5 changed files with 74 additions and 73 deletions

View File

@@ -5,36 +5,30 @@ const API_ENDPOINT = function (tailString) {
return API_BASEURL + tailString;
}
const Api = {
export const Api = {
fetchPlayerList () {
return fetch(API_ENDPOINT("players"))
.then(r => r.json())
.catch(e => console.error("Fetch error ", e));
},
fetchClaims () {
return fetch(API_ENDPOINT("claims"))
.then(r => r.json())
.catch(e => console.error("Fetch error ", e));
},
fetchLoot (playerId) {
return fetch(API_ENDPOINT(playerId + "/loot"))
.then(r => r.json())
.catch(e => console.error("Fetch error", e));
},
putClaim (playerId, itemId) {
return fetch(API_ENDPOINT(playerId + "/claim/" + itemId))
.then(r => r.json())
.catch(e => console.error("Fetch error", e));
},
unClaim (playerId, itemId) {
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))
.then(r => r.json())
.catch(e => console.error("Fetch error", e));
}
};
@@ -44,7 +38,7 @@ export const AppStorage = {
state: {
player_id: 0,
player_list: {},
player_loot: {},
group_loot: [],
player_claims: {},
initiated: false,
show_player_chest: false,
@@ -55,14 +49,19 @@ export const AppStorage = {
this.state.player_id = playerId;
// Fetch initial data
return Promise
.all([ Api.fetchPlayerList(), Api.fetchClaims(), ])
.all([
Api.fetchPlayerList(),
Api.fetchClaims(),
Api.fetchLoot(0)
])
.then(data => {
const [players, claims] = data;
const [players, claims, group_loot] = data;
this.__initPlayerList(players);
this.__initClaimsStore(claims);
Vue.set(this.state, 'group_loot', group_loot);
})
// TODO: when __initPlayerList won't use promises
//.then(_ => this.state.initiated = true);
.then(_ => this.state.initiated = true)
.catch(e => { alert(e); this.state.initiated = false });
},
__initClaimsStore(data) {
for (var idx in data) {
@@ -77,25 +76,8 @@ export const AppStorage = {
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_loot, playerId, []);
Vue.set(this.state.player_claims, playerId, []);
}
// Hack for now !!
// Fetch all players loot and wait to set initiated to true
var promises = [];
for (var idx in data) {
const playerId = data[idx].id;
var promise = Api.fetchLoot(playerId)
.then(data => data.forEach(
item => {
if (this.debug) console.log("add looted item", item, playerId)
this.state.player_loot[playerId].push(item)
}
));
promises.push(promise);
}
Promise.all(promises)
.then(_ => this.state.initiated = true)
},
// User actions
// Sets a new active player by id