import {createStore} from 'vuex' import axios from "axios"; export default createStore({ state: { player: null, inventory: [] }, getters: {}, mutations: { fetchPlayer(state) { let vuex = this; let existingUserUuid = localStorage.getItem('farmfresh_uuid'); if (existingUserUuid != null) { axios.get( 'http://api.luna-development.net/api/player/fetch/'+existingUserUuid ).then((response) => { if (response.data && response.data.uuid) { state.player = response.data; vuex.commit('fetchInventory'); } else { alert('User not found'); } }).catch((error) => { alert('User not found'); console.log(error); }); } }, fetchInventory(state) { axios.get( 'http://api.luna-development.net/api/inventory/fetch/'+state.player.uuid ).then((response) => { if (response.data.success) { state.inventory = response.data.inventory; } }); }, createNewPlayer(state, userName) { axios.post('http://api.luna-development.net/api/player/create', { name: userName.trim(), game_id: 1 }).then((response) => { if (response.data.success) { state.player = response.data.player; localStorage.setItem('farmfresh_uuid', response.data.player.uuid); } }); }, destroyPlayer(state) { axios.post('http://api.luna-development.net/api/player/destroy', { name: state.player.name, uuid: state.player.uuid }).then((response) => { if (response.data.success) { state.player = null; localStorage.removeItem('farmfresh_uuid'); } }); } }, actions: {}, modules: {} });