moves shared store inside AppStorage.js

This commit is contained in:
2019-06-16 15:37:03 +02:00
parent d9b629116c
commit ef9124cc74
5 changed files with 75 additions and 59 deletions

View File

@@ -1,5 +1,5 @@
<template>
<main id="app" class="container is-fluid">
<main id="app" class="section">
<section id="content" class="columns is-desktop">
<Player></Player>
<div class="column">
@@ -12,47 +12,7 @@
<script>
import Player from './components/Player.vue'
import Chest from './components/Chest.vue'
export const store = {
debug: true,
state: {
player_id: 0,
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},
],
show_player_chest: false,
requests: { // TEST: must be initialised with players ids
0: [], 1: [], 4: [], 3: []
},
},
setActivePlayer (newPlayerId) {
if (this.debug) console.log('setActivePlayer to ', newPlayerId)
this.state.player_id = newPlayerId
document.cookie = `player_id=${newPlayerId};`;
},
switchPlayerChestVisibility () {
if (this.debug) console.log('switchPlayerChestVisibility', !this.state.show_player_chest)
this.state.show_player_chest = !this.state.show_player_chest
},
putRequest (itemId) {
const playerId = this.state.player_id
if (this.debug) console.log('newRequest from', playerId, 'on', itemId)
this.state.requests[playerId].push(itemId);
},
cancelRequest(itemId) {
const playerId = this.state.player_id
if (this.debug) console.log('cancelRequest of', playerId, 'on', itemId)
var idx = this.state.requests[playerId].indexOf(itemId);
if (idx > -1) {
this.state.requests[playerId].splice(idx, 1);
} else {
if (this.debug) console.log("cancel a non-existent request")
}
}
}
import { AppStorage } from './AppStorage'
function getCookie(cname) {
var name = cname + "=";
@@ -76,15 +36,16 @@ export default {
Player,
Chest
},
created () {
beforeCreate () {
const cookie = getCookie("player_id");
let playerId;
if (cookie == "") {
return;
playerId = 0;
} else {
var newPlayerId = Number(cookie);
console.log("initiated with id", newPlayerId);
store.setActivePlayer(newPlayerId);
playerId = Number(cookie);
}
AppStorage.initStorage(playerId);
return;
}
}
</script>
@@ -95,7 +56,5 @@ export default {
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>