From cd0de901898039a0153ac88dc34fe58a5f363a86 Mon Sep 17 00:00:00 2001 From: pemcne Date: Wed, 4 Jul 2018 16:16:37 -0400 Subject: [PATCH] Market and Player inventory (#2) * WIP for store layout and two markets * More wip * View components are working * Initial layout for market buys and player inventory * Added store items and stub functions for buy and sell * Buy operations working * Sell functionality working * Put store inventories in a separate module * Namespaced the store and refactored to match * Removed old components * Reorganized the files to make more sense --- src/components/Market.vue | 44 ------------- src/components/ResourceItem.vue | 21 ------ src/components/ResourceList.vue | 43 ------------- src/main.js | 9 ++- src/modules/Items.js | 12 ++++ src/store/index.js | 86 +++---------------------- src/store/modules/inventory.js | 50 ++++++++++++++ src/store/modules/market.js | 33 ++++++++++ src/{ => view}/App.vue | 18 ++++-- src/view/components/Inventory.vue | 51 +++++++++++++++ src/view/components/Market.vue | 37 +++++++++++ src/view/components/MarketInventory.vue | 63 ++++++++++++++++++ src/view/components/Player.vue | 25 +++++++ 13 files changed, 297 insertions(+), 195 deletions(-) delete mode 100644 src/components/Market.vue delete mode 100644 src/components/ResourceItem.vue delete mode 100644 src/components/ResourceList.vue create mode 100644 src/modules/Items.js create mode 100644 src/store/modules/inventory.js create mode 100644 src/store/modules/market.js rename src/{ => view}/App.vue (54%) create mode 100644 src/view/components/Inventory.vue create mode 100644 src/view/components/Market.vue create mode 100644 src/view/components/MarketInventory.vue create mode 100644 src/view/components/Player.vue diff --git a/src/components/Market.vue b/src/components/Market.vue deleted file mode 100644 index b38da71..0000000 --- a/src/components/Market.vue +++ /dev/null @@ -1,44 +0,0 @@ - - - - - diff --git a/src/components/ResourceItem.vue b/src/components/ResourceItem.vue deleted file mode 100644 index 0841bae..0000000 --- a/src/components/ResourceItem.vue +++ /dev/null @@ -1,21 +0,0 @@ - - - - - diff --git a/src/components/ResourceList.vue b/src/components/ResourceList.vue deleted file mode 100644 index f26fd40..0000000 --- a/src/components/ResourceList.vue +++ /dev/null @@ -1,43 +0,0 @@ - - - - - diff --git a/src/main.js b/src/main.js index 01cf5de..96c5c6f 100644 --- a/src/main.js +++ b/src/main.js @@ -1,9 +1,8 @@ // The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' -import App from './App' -import store from './store' -// import EventBus from './EventBus.js' +import App from './view/App' +import store from '@/store' import Engine from '@/Engine.js' Vue.config.productionTip = false @@ -18,8 +17,8 @@ new Vue({ counter: 0 }, mounted () { - Engine.store = this.$store - Engine.start() + // Engine.store = this.$store + // Engine.start() }, beforeDestroy () { Engine.stop() diff --git a/src/modules/Items.js b/src/modules/Items.js new file mode 100644 index 0000000..9974eb6 --- /dev/null +++ b/src/modules/Items.js @@ -0,0 +1,12 @@ +export default { + 'wood': { + craftable: true, + weight: 1, + description: 'Wood' + }, + 'metal': { + craftable: true, + weight: 5, + description: 'Metal' + } +} diff --git a/src/store/index.js b/src/store/index.js index 43c45cf..26dd165 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,92 +1,22 @@ import Vue from 'vue' import Vuex from 'vuex' +import inventory from './modules/inventory' +import market from './modules/market' // import VuexPersist from 'vuex-persist' Vue.use(Vuex) -// Set up persistent storage -// Leave this commented out during development -// const vuexlocalstorage = new VuexPersist({ -// key: 'railway-simulator', -// storage: window.localStorage -// }) - -// The initial state of the application const state = { - money: 100, - inventory: { - worker: 0 - }, - resources: [{ - shortCode: 'worker', - name: 'Worker', - description: 'Basic worker', - income: 2, - cost: 10, - rate: 0.2 - }], - timestamp: Math.floor(new Date() / 1000) -} - -// Define getters here -// Use this.$store.getters. to access -const getters = { - money: state => state.money, - workers: state => state.inventory.worker, - timestamp: state => state.timestamp, - inventory: state => state.inventory, - resource (state) { - // This takes a shortCode parameter and returns the item in the resources - // list that corresponds with the shortCode - return shortCode => state.resources.filter(resource => { - return resource.shortCode === shortCode - })[0] - } -} - -// Define actions here -// Use this.$store.dispatch('foo', {}) to access -const actions = { - buy ({commit}, {item, amount, cost}) { - commit('BUY', { - item: item, - amount, - cost - }) - }, - income ({commit}, {amount}) { - commit('INCOME', {amount}) - }, - setTime ({commit}, {timestamp}) { - commit('SETTIME', {timestamp}) - } -} - -// Define the mutations here -// Ideally, this is what the actions will call to actually change the state -// Use this.$store.commit('FOO', {}) to access -const mutations = { - BUY (state, {item, amount, cost}) { - if (item in state.inventory) { - state.inventory[item] += amount - } else { - state.inventory[item] = amount - } - state.money -= cost - }, - INCOME (state, {amount}) { - state.money += amount - }, - SETTIME (state, {timestamp}) { - state.timestamp = timestamp + player: { + inventory: 'player' } } // Export store by default export default new Vuex.Store({ state, - getters, - mutations, - actions - // plugins: [vuexlocalstorage.plugin] + modules: { + inventory, + market + } }) diff --git a/src/store/modules/inventory.js b/src/store/modules/inventory.js new file mode 100644 index 0000000..485e01c --- /dev/null +++ b/src/store/modules/inventory.js @@ -0,0 +1,50 @@ +export default { + namespaced: true, + state: { + 'abc': { + items: ['wood'], + quantities: { + 'wood': 2 + } + }, + 'def': { + items: ['metal', 'wood'], + quantities: { + 'wood': 0, + 'metal': 2 + } + }, + 'player': { + items: ['wood'], + quantities: { + 'wood': 1 + } + } + }, + getters: { + getInventory: state => id => state[id] + }, + mutations: { + ADD_ITEM (state, {inv, item, quantity}) { + const inventory = state[inv] + // Make copy of an object + let quantities = Object.assign({}, inventory.quantities) + if (inventory.items.includes(item)) { + quantities[item] += quantity + } else { + inventory.items.push(item) + quantities[item] = quantity + } + // Set the copy back to the state + inventory.quantities = quantities + }, + REMOVE_ITEM (state, {inv, item, quantity}) { + const inventory = state[inv] + if (inventory.quantities[item] - quantity >= 0) { + inventory.quantities[item] -= quantity + } else { + throw new Error(`${inv} has less than ${quantity} of ${item}`) + } + } + } +} diff --git a/src/store/modules/market.js b/src/store/modules/market.js new file mode 100644 index 0000000..9d05e3d --- /dev/null +++ b/src/store/modules/market.js @@ -0,0 +1,33 @@ +export default { + namespaced: true, + state: { + 'market1': { + inventory: 'abc' + }, + 'market2': { + inventory: 'def' + } + }, + getters: { + getMarket: state => id => state[id] + }, + actions: { + // For cross module commits, put {root: true} on the commit call + buyItem ({commit}, {toinv, frominv, item, quantity}) { + commit('inventory/REMOVE_ITEM', { + inv: frominv, + item, + quantity + }, { + root: true + }) + commit('inventory/ADD_ITEM', { + inv: toinv, + item, + quantity + }, { + root: true + }) + } + } +} diff --git a/src/App.vue b/src/view/App.vue similarity index 54% rename from src/App.vue rename to src/view/App.vue index 53ea126..89b85d8 100644 --- a/src/App.vue +++ b/src/view/App.vue @@ -1,13 +1,18 @@ diff --git a/src/view/components/Inventory.vue b/src/view/components/Inventory.vue new file mode 100644 index 0000000..1171d3b --- /dev/null +++ b/src/view/components/Inventory.vue @@ -0,0 +1,51 @@ + + + + + diff --git a/src/view/components/MarketInventory.vue b/src/view/components/MarketInventory.vue new file mode 100644 index 0000000..99f5e6f --- /dev/null +++ b/src/view/components/MarketInventory.vue @@ -0,0 +1,63 @@ + + + diff --git a/src/view/components/Player.vue b/src/view/components/Player.vue new file mode 100644 index 0000000..cdb5c63 --- /dev/null +++ b/src/view/components/Player.vue @@ -0,0 +1,25 @@ + + +