|
|
|
|
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.success) {
|
|
|
|
|
state.player = response.data.player;
|
|
|
|
|
state.inventory = response.data.player.inventory;
|
|
|
|
|
vuex.commit('fetchInventory');
|
|
|
|
|
} else {
|
|
|
|
|
alert('User not found');
|
|
|
|
|
}
|
|
|
|
|
}).catch((error) => {
|
|
|
|
|
alert('User not found');
|
|
|
|
|
console.log(error);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
|
state.inventory = response.data.player.inventory;
|
|
|
|
|
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: {}
|
|
|
|
|
});
|