|
|
|
|
import {createStore} from 'vuex'
|
|
|
|
|
import moment from "moment";
|
|
|
|
|
import {SeasonService} from "@/services/SeasonService";
|
|
|
|
|
import {ItemTypes} from "@/data/ItemTypes";
|
|
|
|
|
import createPersistedState from "vuex-persistedstate";
|
|
|
|
|
import ItemService from "@/services/ItemService";
|
|
|
|
|
import iziToast from "izitoast";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default createStore({
|
|
|
|
|
state: {
|
|
|
|
|
initialized: null,
|
|
|
|
|
player: {
|
|
|
|
|
// TODO: ADD PLAYER INTRODUCTION
|
|
|
|
|
name: 'Franz',
|
|
|
|
|
money: 1_000,
|
|
|
|
|
unlocked: {
|
|
|
|
|
fields: 48
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
time: {
|
|
|
|
|
stamp: null,
|
|
|
|
|
week: null,
|
|
|
|
|
season: null,
|
|
|
|
|
weekday: null,
|
|
|
|
|
weekdayNr: null
|
|
|
|
|
},
|
|
|
|
|
inventory: [],
|
|
|
|
|
cropField: [],
|
|
|
|
|
selections: {
|
|
|
|
|
field: null,
|
|
|
|
|
item: null
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
getters: {
|
|
|
|
|
selectedField(state) {
|
|
|
|
|
return state.cropField.find(function (field) {
|
|
|
|
|
return field.id === state.selections.field;
|
|
|
|
|
}) ?? {};
|
|
|
|
|
},
|
|
|
|
|
getInventoryWithItemData(state) {
|
|
|
|
|
return state.inventory.filter((inventoryItem) => {
|
|
|
|
|
return inventoryItem.quantity > 0;
|
|
|
|
|
}).map((inventoryItem) => {
|
|
|
|
|
let item = ItemService.getItemByID(inventoryItem.id);
|
|
|
|
|
|
|
|
|
|
item.quantity = inventoryItem.quantity;
|
|
|
|
|
return item;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
getSeedsInInventory(state) {
|
|
|
|
|
return state.inventory.filter((inventoryItem) => {
|
|
|
|
|
return inventoryItem.type === ItemTypes.Seeds && inventoryItem.quantity > 0;
|
|
|
|
|
}).map((inventoryItem) => {
|
|
|
|
|
let item = ItemService.getItemByID(inventoryItem.id);
|
|
|
|
|
|
|
|
|
|
item.quantity = inventoryItem.quantity;
|
|
|
|
|
return item;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mutations: {
|
|
|
|
|
initialize(state) {
|
|
|
|
|
this.commit('checkForSeasonChange');
|
|
|
|
|
|
|
|
|
|
state.time.week = moment().week();
|
|
|
|
|
state.time.season = SeasonService.getSeasonByWeek(state.time.week);
|
|
|
|
|
state.time.weekday = moment().weekday();
|
|
|
|
|
state.time.weekdayNr = moment().format('dddd');
|
|
|
|
|
state.initialized = moment();
|
|
|
|
|
|
|
|
|
|
this.commit('generateField');
|
|
|
|
|
},
|
|
|
|
|
updateTimestamp(state) {
|
|
|
|
|
state.time.stamp = moment().format('H:mm:ss')
|
|
|
|
|
},
|
|
|
|
|
generateField(state) {
|
|
|
|
|
let fieldIndex = 0;
|
|
|
|
|
|
|
|
|
|
while(state.cropField.length < state.player.unlocked.fields) {
|
|
|
|
|
fieldIndex++;
|
|
|
|
|
|
|
|
|
|
state.cropField.push({
|
|
|
|
|
id: fieldIndex
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
selectField(state, id) {
|
|
|
|
|
console.debug('select field with id '+id)
|
|
|
|
|
state.selections.field = id;
|
|
|
|
|
},
|
|
|
|
|
plantSeed(state, seed) {
|
|
|
|
|
let selectedField = state.cropField.find((field) => {
|
|
|
|
|
return field.id === state.selections.field;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/*** SEED PLANTING ***/
|
|
|
|
|
selectedField.data = {
|
|
|
|
|
planted: moment(),
|
|
|
|
|
readyForHarvest: moment().add(seed.cropData.timeToGrow, 'minutes'),
|
|
|
|
|
percentDone: 0,
|
|
|
|
|
canBeHarvested: false,
|
|
|
|
|
seed_id: seed.id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.commit('removeItemFromInventory', {
|
|
|
|
|
item: seed,
|
|
|
|
|
quantity: 1
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
harvestSeed(state, field) {
|
|
|
|
|
let seed = ItemService.getItemByID(field.data.seed_id);
|
|
|
|
|
let productFromSeed = ItemService.getItemByID(seed.cropData.product_id);
|
|
|
|
|
|
|
|
|
|
if (field.data.percentDone < 100 || !field.data.readyForHarvest) {
|
|
|
|
|
// bloody cheater
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.commit('addItemToInventory', {
|
|
|
|
|
item: productFromSeed,
|
|
|
|
|
quantity: 1
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
field.data = null;
|
|
|
|
|
},
|
|
|
|
|
checkForSeasonChange(state) {
|
|
|
|
|
let prevDate = state.initialized;
|
|
|
|
|
|
|
|
|
|
if (moment(prevDate).weekday() === 7 && moment().weekday() === 1) {
|
|
|
|
|
console.debug('SEASON CHANGE DETECTED');
|
|
|
|
|
|
|
|
|
|
// TODO: KILL ALL THE CROPS
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
buyItem(state, props = {item: Object, quantity: Number}) {
|
|
|
|
|
let purchasePrice = props.item.economy.buy * props.quantity;
|
|
|
|
|
|
|
|
|
|
if (purchasePrice > state.player.money) {
|
|
|
|
|
alert("Not enough money to buy "+props.quantity+" of "+props.item.name);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.commit('addItemToInventory', {
|
|
|
|
|
item: props.item,
|
|
|
|
|
quantity: props.quantity
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
state.player.money -= purchasePrice;
|
|
|
|
|
|
|
|
|
|
iziToast.success({
|
|
|
|
|
title: 'Purchased '+props.item.name+(props.quantity > 1 ? ' ('+props.quantity+')' : '')
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
addItemToInventory(state, props = {item: Object, quantity: Number}) {
|
|
|
|
|
let itemInInventory = state.inventory.find((inventoryItem) => {
|
|
|
|
|
return inventoryItem.id === props.item.id;
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (itemInInventory) {
|
|
|
|
|
itemInInventory.quantity = Number(itemInInventory.quantity) + Number(props.quantity);
|
|
|
|
|
} else {
|
|
|
|
|
/*** INVENTORY ITEM CREATION ***/
|
|
|
|
|
state.inventory.push({
|
|
|
|
|
id: props.item.id,
|
|
|
|
|
type: props.item.type,
|
|
|
|
|
quantity: props.quantity
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
removeItemFromInventory(state, props = {item: Object, quantity: Number}) {
|
|
|
|
|
let itemInInventory = state.inventory.find((inventoryItem) => {
|
|
|
|
|
return inventoryItem.id === props.item.id;
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (itemInInventory) {
|
|
|
|
|
itemInInventory.quantity -= props.quantity;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
updateCrops(state) {
|
|
|
|
|
state.cropField.filter((field) => {
|
|
|
|
|
return field.data != null && !field.data.canBeHarvested;
|
|
|
|
|
}).forEach(function (field) {
|
|
|
|
|
let seed = ItemService.getItemByID(field.data.seed_id);
|
|
|
|
|
|
|
|
|
|
if (field.data.readyForHarvest < moment()) {
|
|
|
|
|
field.data.canBeHarvested = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (field.data.percentDone <= 100) {
|
|
|
|
|
let minutesLeft = moment.duration(moment(field.data.readyForHarvest).diff(moment())).asMinutes();
|
|
|
|
|
field.data.percentDone = Number((Number(seed.cropData.timeToGrow) * 100) / Number(minutesLeft) - 100).toFixed(2);
|
|
|
|
|
if (field.data.percentDone >= 100) {
|
|
|
|
|
field.data.percentDone = 100;
|
|
|
|
|
field.data.canBeHarvested = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
field.data.canBeHarvested = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
actions: {},
|
|
|
|
|
modules: {},
|
|
|
|
|
plugins: [createPersistedState()],
|
|
|
|
|
});
|