Skip to content

Commit

Permalink
Market and Player inventory (#2)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pemcne authored Jul 4, 2018
1 parent 4416c07 commit cd0de90
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 195 deletions.
44 changes: 0 additions & 44 deletions src/components/Market.vue

This file was deleted.

21 changes: 0 additions & 21 deletions src/components/ResourceItem.vue

This file was deleted.

43 changes: 0 additions & 43 deletions src/components/ResourceList.vue

This file was deleted.

9 changes: 4 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,8 +17,8 @@ new Vue({
counter: 0
},
mounted () {
Engine.store = this.$store
Engine.start()
// Engine.store = this.$store
// Engine.start()
},
beforeDestroy () {
Engine.stop()
Expand Down
12 changes: 12 additions & 0 deletions src/modules/Items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
'wood': {
craftable: true,
weight: 1,
description: 'Wood'
},
'metal': {
craftable: true,
weight: 5,
description: 'Metal'
}
}
86 changes: 8 additions & 78 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -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.<foo> 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
}
})
50 changes: 50 additions & 0 deletions src/store/modules/inventory.js
Original file line number Diff line number Diff line change
@@ -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}`)
}
}
}
}
33 changes: 33 additions & 0 deletions src/store/modules/market.js
Original file line number Diff line number Diff line change
@@ -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
})
}
}
}
18 changes: 14 additions & 4 deletions src/App.vue → src/view/App.vue
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
<template>
<div id="app">
<Market/>
<ResourceList/>
<Market v-for="market in markets" :key="market" v-bind:name="market"/>
<Player />
</div>
</template>

<script>
import Market from './components/Market'
import ResourceList from './components/ResourceList'
import Player from './components/Player'
import {createNamespacedHelpers} from 'vuex'
// Set up the helpers
// TODO: move this to a MarketList
const { mapState } = createNamespacedHelpers('market')
// Parent root element for the entire application
// Does nothing but display the children
export default {
name: 'App',
components: {
Market,
ResourceList
Player
},
computed: {
...mapState({
markets: state => Object.keys(state)
})
}
}
</script>
Expand Down
Loading

0 comments on commit cd0de90

Please sign in to comment.