-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
13 changed files
with
297 additions
and
195 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.