removes obsolete AppStorage.js

This commit is contained in:
2019-10-19 14:23:55 +02:00
parent b0441f3402
commit 622d746446
5 changed files with 44 additions and 185 deletions

View File

@@ -1,11 +1,11 @@
<template> <template>
<PlayerView <PlayerView
:id="state.player_id" :id="player_id"
v-slot="{ player, loot, notifications, actions, claims }" v-slot="{ player, loot, notifications, actions, claims }"
> >
<main id="app" class="container"> <main id="app" class="container">
<header> <header>
<HeaderBar :app_state="state"> <HeaderBar>
<template v-slot:title> <template v-slot:title>
{{ player.name }} {{ player.name }}
</template> </template>
@@ -19,7 +19,7 @@
</template> </template>
<hr class="navbar-divider"> <hr class="navbar-divider">
<div class="navbar-item heading">Changer</div> <div class="navbar-item heading">Changer</div>
<a v-for="(p,i) in state.player_list" :key="i" <a v-for="(p,i) in playerList" :key="i"
@click="setActivePlayer(i)" @click="setActivePlayer(i)"
href="#" class="navbar-item"> href="#" class="navbar-item">
{{ p.name }}</a> {{ p.name }}</a>
@@ -57,14 +57,14 @@
<main class="section"> <main class="section">
<template v-if="isAdding"> <template v-if="isAdding">
<Loot v-if="playerIsGroup" <Loot v-if="playerIsGroup"
:inventory="state.inventory" :inventory="itemsInventory"
@addItem="item => pending_loot.push(item)" @addItem="item => pending_loot.push(item)"
@confirmAction="addNewLoot" @confirmAction="addNewLoot"
></Loot> ></Loot>
<AddingChest <AddingChest
:player="player.id" :player="player.id"
:claims="claims" :claims="claims"
:items="playerIsGroup ? pending_loot : state.inventory" :items="playerIsGroup ? pending_loot : itemsInShop"
:perms="playerIsGroup ? {} : { canBuy: true }" :perms="playerIsGroup ? {} : { canBuy: true }"
@buy="(data) => { switchView('player'); actions.buyItems(data); }"> @buy="(data) => { switchView('player'); actions.buyItems(data); }">
</AddingChest> </AddingChest>
@@ -72,7 +72,7 @@
<Chest v-else <Chest v-else
:player="player.id" :player="player.id"
:claims="claims" :claims="claims"
:items="showPlayerChest ? loot : state.group_loot" :items="showPlayerChest ? loot : groupLoot"
:perms="{ :perms="{
canGrab: !(showPlayerChest || playerIsGroup), canGrab: !(showPlayerChest || playerIsGroup),
canSell: showPlayerChest || playerIsGroup canSell: showPlayerChest || playerIsGroup
@@ -92,7 +92,7 @@ import HeaderBar from './components/HeaderBar.vue'
import Wealth from './components/Wealth.vue' import Wealth from './components/Wealth.vue'
import Chest from './components/Chest.vue' import Chest from './components/Chest.vue'
import Loot from './components/Loot.vue' import Loot from './components/Loot.vue'
import { Api, AppStorage } from './AppStorage' import { api } from './lootalot.js'
function getCookie(cname) { function getCookie(cname) {
var name = cname + "="; var name = cname + "=";
@@ -114,10 +114,14 @@ export default {
name: 'app', name: 'app',
data () { data () {
return { return {
state: AppStorage.state, player_id: 0,
playerList: [],
activeView: 'group', activeView: 'group',
shopInventory: [{id: 1, name: "Item from shop #1", base_price: 2000}], groupLoot: [],
itemsInventory: [],
itemsInShop: [{id: 1, name: "Item from shop #1", base_price: 2000}],
pending_loot: [], pending_loot: [],
initiated: false,
}; };
}, },
components: { components: {
@@ -129,21 +133,26 @@ export default {
Loot, Loot,
}, },
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; this.player_id = cookie ? Number(cookie) : 0;
if (cookie == "") { Promise.all([
playerId = 0; api.fetch("players/", "GET", null),
} else { api.fetch("players/0/loot", "GET", null),
playerId = Number(cookie); api.fetch("items", "GET", null),
} ])
AppStorage.initStorage(playerId); .then(([players, loot, items]) => {
this.playerList = players.value;
this.groupLoot = loot.value;
this.itemsInventory = items.value;
})
.catch(r => alert("Error ! \n" + r))
.then(() => this.initiated = true);
}, },
methods: { methods: {
setActivePlayer (idx) { setActivePlayer (idx) {
if (idx == 0) this.switchView('group'); if (idx == 0) this.switchView('group');
AppStorage.setActivePlayer(idx); this.player_id = Number(idx)
document.cookie = `player_id=${idx};`;
}, },
switchView (viewId) { switchView (viewId) {
if (!['group', 'player', 'adding'].includes(viewId)) { if (!['group', 'player', 'adding'].includes(viewId)) {
@@ -151,21 +160,19 @@ export default {
} }
this.activeView = viewId; this.activeView = viewId;
}, },
switchPlayerChestVisibility () {
AppStorage.switchPlayerChestVisibility();
},
addNewLoot () { addNewLoot () {
Api.newLoot(this.pending_loot) api.fetch("admin/add-loot", "POST", this.pending_loot)
.then(_ => { .then(() => {
this.pending_loot = [] this.pending_loot = []
this.switchView('group'); this.switchView('group');
}); })
.catch(r => alert("Error: " + r));
} }
}, },
computed: { computed: {
showPlayerChest () { return this.activeView == 'player' }, showPlayerChest () { return this.activeView == 'player' },
isAdding () { return this.activeView == 'adding' }, isAdding () { return this.activeView == 'adding' },
playerIsGroup () { return this.state.player_id == 0 }, playerIsGroup () { return this.player_id == 0 },
} }
} }
</script> </script>

View File

@@ -1,149 +0,0 @@
import Vue from 'vue'
const API_BASEURL = "http://localhost:8088/api/"
const API_ENDPOINT = function (tailString) {
return API_BASEURL + tailString;
}
export const Api = {
__doFetch (endpoint, method, payload) {
return fetch(API_ENDPOINT(endpoint),
{
method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
})
.then(r => r.json())
},
fetchPlayerList () {
return fetch(API_ENDPOINT("players/"))
.then(r => r.json())
},
fetchInventory () {
return fetch(API_ENDPOINT("items"))
.then(r => r.json())
},
fetchClaims () {
return fetch(API_ENDPOINT("claims"))
.then(r => r.json())
},
fetchLoot (playerId) {
return fetch(API_ENDPOINT("players/" + playerId + "/loot"))
.then(r => r.json())
},
putClaim (player_id, item_id) {
const payload = item_id;
return this.__doFetch("players/" + player_id + "/claims", 'PUT', payload);
},
unClaim (player_id, item_id) {
const payload = item_id;
return this.__doFetch("players/" + player_id + "/claims", 'DELETE', payload);
},
updateWealth (player_id, value_in_gp) {
const payload = Number(value_in_gp);
return this.__doFetch("players/" + player_id + "/wealth", 'PUT', payload);
},
buyItems (player_id, items) {
const payload = items;
return this.__doFetch("players/" + player_id + "/loot", 'PUT', payload);
},
sellItems (player_id, items) {
const payload = items;
return this.__doFetch("players/" + player_id + "/loot", 'DELETE', payload);
},
newLoot (items) {
return this.__doFetch("admin/add-loot", 'POST', items);
},
};
export const AppStorage = {
debug: true,
state: {
player_id: 0,
player_list: {},
group_loot: [],
player_claims: {},
inventory: [],
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.fetchInventory(),
Api.fetchLoot(0)
])
.then(data => {
const [players, claims, inventory, group_loot] = data;
this.__initPlayerList(players.value);
this.__initClaimsStore(claims.value);
Vue.set(this.state, 'group_loot', group_loot.value);
Vue.set(this.state, 'inventory', inventory.value);
})
.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 = Number(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
},
// 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(_ => {
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")
}
});
},
addNewLoot (items) {
return Api.newLoot(items);
},
}

View File

@@ -25,7 +25,6 @@
</template> </template>
<script> <script>
import { AppStorage } from '../AppStorage'
export default { export default {
props: { props: {
// Id of active player // Id of active player
@@ -47,7 +46,11 @@
computed: { computed: {
// Check if item is requested by active player // Check if item is requested by active player
isRequested () { isRequested () {
if (this.claims[this.id]) {
return this.claims[this.id].includes(this.item); return this.claims[this.id].includes(this.item);
} else {
return false;
}
}, },
// Check if item is requested by multiple players including active one // Check if item is requested by multiple players including active one
isInConflict () { isInConflict () {

View File

@@ -1,5 +1,3 @@
import Vue from 'vue'
const API_BASEURL = "http://localhost:8088/api/" const API_BASEURL = "http://localhost:8088/api/"
const API_ENDPOINT = function (tailString) { const API_ENDPOINT = function (tailString) {
return API_BASEURL + tailString; return API_BASEURL + tailString;