updates frontend to use DB api

This commit is contained in:
2019-07-04 14:53:50 +02:00
parent 4e28eb4159
commit 503ebf7b6b
5 changed files with 103 additions and 61 deletions

View File

@@ -1,9 +1,9 @@
<template> <template>
<main id="app" class="section"> <main id="app" class="section">
<section id="content" class="columns is-desktop"> <section id="content" class="columns is-desktop">
<Player v-if="fullyLoaded"></Player> <Player v-if="state.initiated"></Player>
<div class="column"> <div class="column">
<Chest :player="0" v-if="fullyLoaded"></Chest> <Chest :player="0" v-if="state.initiated"></Chest>
</div> </div>
</section> </section>
</main> </main>
@@ -32,14 +32,18 @@ function getCookie(cname) {
export default { export default {
name: 'app', name: 'app',
data () { return { data () {
fullyLoaded: false, return {
};}, state: AppStorage.state,
};
},
components: { components: {
Player, Player,
Chest Chest
}, },
created () { created () {
// Initiate with active player set to value found in cookie
// or as group by default.
const cookie = getCookie("player_id"); const cookie = getCookie("player_id");
let playerId; let playerId;
if (cookie == "") { if (cookie == "") {
@@ -47,8 +51,7 @@ export default {
} else { } else {
playerId = Number(cookie); playerId = Number(cookie);
} }
AppStorage.initStorage(playerId) AppStorage.initStorage(playerId);
.then(_ => this.fullyLoaded = true);
}, },
} }
</script> </script>

View File

@@ -1,45 +1,84 @@
const PLAYER_LIST = [
{id: 0, name: "Groupe", wealth: [0,0,0,0], debt: 0},
{id: 1, name: "Lomion", wealth: [0,0,0,0], debt: 0},
{id: 4, name: "Oilosse", wealth: [0,0,0,0], debt: 0},
{id: 3, name: "Fefi", wealth: [0,0,0,0], debt: 0},
];
const __fetchPlayerList = function() { const Api = {
fetchPlayerList () {
return fetch("http://localhost:8088/players") return fetch("http://localhost:8088/players")
.then(r => r.json()) .then(r => r.json())
.catch(e => console.error("Fetch error ", e));
},
fetchClaims () {
return fetch("http://localhost:8088/claims")
.then(r => r.json())
.catch(e => console.error("Fetch error ", e));
},
fetchLoot (playerId) {
const url = "http://localhost:8088/loot/" + playerId;
return fetch(url)
.then(r => r.json())
.catch(e => console.error("Fetch error", e));
},
putClaim (playerId, itemId) {
console.log('newRequest from', playerId, 'on', itemId);
return true;
},
unClaim (playerId, itemId) {
console.log('cancelRequest of', playerId, 'on', itemId);
return true;
},
updateWealth (playerId, goldValue) {
console.log('Update wealth', goldValue);
return true;
}
}; };
export const AppStorage = { export const AppStorage = {
debug: true, debug: true,
state: { state: {
player_id: 0, player_id: 0,
player_list: [], player_list: {},
show_player_chest: false, player_loot: {},
requests: {}, requests: {},
initiated: false,
show_player_chest: false,
}, },
// Initiate the state // Initiate the state
initStorage (playerId) { initStorage (playerId) {
if (this.debug) console.log('Initiate with player : ', playerId) if (this.debug) console.log('Initiates with player : ', playerId)
this.state.player_id = playerId; this.state.player_id = playerId;
return Promise.all([ // Fetch initial data
fetch("http://localhost:8088/players") return Promise
.then(r => r.json()), .all([ Api.fetchPlayerList(), Api.fetchClaims(), ])
])
.then(data => { .then(data => {
console.log("initiate ", data); const [players, claims] = data;
this.initPlayerList(data[0]); this.initPlayerList(players);
}) console.log("claims", claims);
.catch(e => console.log("Fetch error ", e)); });
}, },
initPlayerList(data) { initPlayerList(data) {
for (var idx in data) { for (var idx in data) {
var p = data[idx]; var playerDesc = data[idx];
this.state.player_list.push(p); const playerId = Number(playerDesc.id);
this.state.requests[p.id] = [5,]; if (this.debug) console.log("Creates", playerId, playerDesc.name)
this.state.player_list[playerId] = playerDesc;
this.state.player_loot[playerId] = [];
this.state.requests[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);
}, },
// Player actions // Player actions
// Sets a new active player by id // Sets a new active player by id
@@ -56,19 +95,28 @@ export const AppStorage = {
// Put a claim on an item from group chest. // Put a claim on an item from group chest.
putRequest (itemId) { putRequest (itemId) {
const playerId = this.state.player_id const playerId = this.state.player_id
if (this.debug) console.log('newRequest from', playerId, 'on', itemId) const done = Api.putClaim(playerId, itemId);
if (done) {
// Update cliend-side state
this.state.requests[playerId].push(itemId); this.state.requests[playerId].push(itemId);
} else {
if (this.debug) console.log("API responded with 'false'")
}
}, },
// Withdraws a claim. // Withdraws a claim.
cancelRequest(itemId) { cancelRequest(itemId) {
const playerId = this.state.player_id const playerId = this.state.player_id
if (this.debug) console.log('cancelRequest of', playerId, 'on', itemId) const done = Api.unClaim(playerId, itemId);
if (done) {
var idx = this.state.requests[playerId].indexOf(itemId); var idx = this.state.requests[playerId].indexOf(itemId);
if (idx > -1) { if (idx > -1) {
this.state.requests[playerId].splice(idx, 1); this.state.requests[playerId].splice(idx, 1);
} else { } else {
if (this.debug) console.log("cancel a non-existent request") if (this.debug) console.log("cancel a non-existent request")
} }
} else {
if (this.debug) console.log("API responded with 'false'")
}
} }
} }

View File

@@ -72,7 +72,7 @@
id="`item-${idx}`" id="`item-${idx}`"
:value="item.id" :value="item.id"
v-model="sell_selected"> v-model="sell_selected">
{{item.sell_value}} GP {{item.base_price / 2}} GP
</label> </label>
</div> </div>
<PercentInput></PercentInput> <PercentInput></PercentInput>
@@ -118,7 +118,6 @@
data () { data () {
return { return {
app_state: AppStorage.state, app_state: AppStorage.state,
content: [],
is_selling: false, is_selling: false,
is_adding: false, is_adding: false,
sell_selected: [], sell_selected: [],
@@ -126,27 +125,25 @@
}, },
methods: { methods: {
fetchLoot () { fetchLoot () {
fetch(`http://localhost:8088/loot/${this.player}`)
.then(r => r.json())
.then(data => {
data.forEach(item => this.content.push(item));
})
.then(_ => console.log("Loot loaded !"))
} }
}, },
computed: { computed: {
content () {
const playerId = this.player;
console.log("Refresh chest of", playerId);
return this.app_state.player_loot[playerId];
},
// Can the active user sell items from this chest ? // Can the active user sell items from this chest ?
canSell () { canSell () {
return this.player == this.app_state.player_id; return this.player == this.app_state.player_id;
}, },
totalSellValue () { totalSellValue () {
const selected = this.sell_selected; const selected = this.sell_selected;
var value = this.content return this.content
.filter(item => selected.includes(item.id)) .filter(item => selected.includes(item.id))
.map(item => item.sell_value) .map(item => item.base_price / 2)
.reduce((total,value) => total + value, 0); .reduce((total,value) => total + value, 0);
return value;
}, },
// Can the user grab items from this chest ? // Can the user grab items from this chest ?
canGrab () { canGrab () {
@@ -163,9 +160,6 @@
&& !this.is_adding); && !this.is_adding);
} }
}, },
mounted () {
this.fetchLoot();
}
} }
</script> </script>

View File

@@ -82,8 +82,7 @@
}, },
computed: { computed: {
player () { player () {
const id = this.state.player_id; const idx = this.state.player_id;
const idx = this.state.player_list.findIndex(p => p.id == id);
return this.state.player_list[idx]; return this.state.player_list[idx];
}, },
wealth () { wealth () {
@@ -104,10 +103,9 @@
} }
}, },
setActivePlayer (playerIdx) { setActivePlayer (playerIdx) {
const newId = this.state.player_list[playerIdx].id; var playerIdx = Number(playerIdx);
AppStorage.setActivePlayer(newId); AppStorage.setActivePlayer(playerIdx);
if (newId == 0) { this.hidePlayerChest() } if (playerIdx == 0) { this.hidePlayerChest() }
this.player.name = this.state.player_list[playerIdx].name
}, },
closeDropdown () { closeDropdown () {
this.show_dropdown = false this.show_dropdown = false

View File

@@ -35,7 +35,6 @@
data () { data () {
return { return {
state: AppStorage.state, state: AppStorage.state,
_requested: false, // Dummy state
}; };
}, },
computed: { computed: {