Commit 7174aa2c authored by Matthew Odle's avatar Matthew Odle

convert villageStore to an object

parent 47a85885
......@@ -3,10 +3,11 @@ import React from 'react';
export class StoreRow extends React.Component {
render(props) {
var item = this.props.item;
const item = this.props.item;
const count = this.props.count;
return <div className="storeRow">
<div className="storeName">{item.name}</div>
<div className={item.count <= 0 ? "storeAbsent storeCount" : "storePresent storeCount"}>{item.count}</div>
<div className="storeName">{item}</div>
<div className={count <= 0 ? "storeAbsent storeCount" : "storePresent storeCount"}>{count}</div>
<div className="clear"></div>
</div>;
}
......
......@@ -5,13 +5,13 @@ import { StoreRow } from './store-row';
export class Stores extends React.Component {
render(props) {
const stores = this.props.stores.slice();
const stores = this.props.stores;
return <div className="storeContainer">
<h3>Stores</h3>
<div className="stores">
<div className="resources">
{ stores.map( item =>
<StoreRow key={item.name} item={item} />
{ Array.from(Object.keys(stores)).map( item =>
<StoreRow key={item} item={item} count={stores[item].count} />
)}
</div>
</div>
......
......@@ -59,7 +59,7 @@ export class VillageManagement extends React.Component {
render = () => {
const statuses = this.props.villagerStatus;
const stores = this.props.villageStore.slice();
const stores = this.props.villageStore;
return <div className="management">
<Buildings buildings={this.props.buildings}
manageVillagerAssignments={this.manageVillagerAssignments}
......
......@@ -11,7 +11,7 @@ export class Village extends React.Component {
}
sortStores = (stores) => {
stores.sort((a, b) => a.name > b.name)
Array.from(Object.keys(stores)).sort((a, b) => a.name > b.name)
}
constructor(props) {
......@@ -19,7 +19,7 @@ export class Village extends React.Component {
this.state = {
buildings: this.sortBuildings(props.buildings.slice()),
score: 0,
villageStore: props.villageStore.slice(),
villageStore: props.villageStore,
villagers: props.villagers.slice(),
log: [],
villagerStatus: {naked: 0, toolless: 0, starving: 0, freezing: 0, unassigned: props.knobsAndLevers.startingVillagers},
......@@ -74,30 +74,23 @@ export class Village extends React.Component {
//----- cycle management functions -----//
canProduce = (building) => {
const theItems = this.state.villageStore.slice();
const theItems = this.state.villageStore;
let flag = true;
// FIXME: dictionary would be better, this is n^2
if (building.materials) {
building.materials.map( material =>
theItems.map( theItem =>
theItem.name === material
? (theItem.count > 0 && flag ? flag = true : flag = false)
: null,
)
);
theItems[material].count > 0 && flag ? flag = true : flag = false
)
}
return flag;
};
useMaterials = (building) => {
const villageStore = this.state.villageStore.slice();
const villageStore = this.state.villageStore;
building.materials.map(
material => villageStore.map(
villageStoreMaterial =>
villageStoreMaterial.name === material && villageStoreMaterial.count > 0
? (villageStoreMaterial.count -= 1)
material => villageStore[material] && villageStore[material].count > 0
? (villageStore[material].count -= 1)
: null
)
);
this.updateTheVillageStore(villageStore);
building.materialsInUse = true;
......@@ -105,11 +98,9 @@ export class Village extends React.Component {
};
addProduct = (buildingName, goods) => {
const villageStore = this.state.villageStore.slice();
const villageStore = this.state.villageStore;
goods.map(
aGood => villageStore.map(
storeItem => storeItem.name === aGood ? storeItem.count += 1 : null,
)
theGood => villageStore[theGood].count += 1
);
// holy crap this is confusing; basically, count the elements in the goods array and stuff them in an object
// then loop through the keys of the object and print the amounts produced
......@@ -383,7 +374,7 @@ export class Village extends React.Component {
};
render = () => {
const villageStore = this.props.villageStore.slice();
const villageStore = this.props.villageStore;
this.sortStores(villageStore);
const buildings = this.props.buildings.slice();
this.sortBuildings(buildings);
......
......@@ -56,15 +56,15 @@ const startingLog = [
"Key: TL=Tool, CL=Clothing, WM=Warmth, EN=Energy",
]
const villageStore = [
{name: 'wood', count: knobsAndLevers.defaultWoodStores, source: 'FORESTER_HUT'},
{name: 'tool', count: knobsAndLevers.defaultToolStores, source: 'SMITHY'},
{name: 'iron', count: knobsAndLevers.defaultIronStores, source: 'MINE'},
{name: 'firewood', count: knobsAndLevers.defaultFirewoodStores, source: 'WOODCUTTER_HUT'},
{name: 'food', count: knobsAndLevers.defaultFoodStores, source: 'GATHERER_HUT, HUNTER_LODGE'},
{name: 'hide', count: knobsAndLevers.defaultHideStores, source: 'HUNTER_LODGE'},
{name: 'clothing', count: knobsAndLevers.defaultClothingStores, source: 'TAILOR'},
];
const villageStore = {
wood: {count: knobsAndLevers.defaultWoodStores, source: 'FORESTER_HUT'},
tool: {count: knobsAndLevers.defaultToolStores, source: 'SMITHY'},
iron: {count: knobsAndLevers.defaultIronStores, source: 'MINE'},
firewood: {count: knobsAndLevers.defaultFirewoodStores, source: 'WOODCUTTER_HUT'},
food: {count: knobsAndLevers.defaultFoodStores, source: 'GATHERER_HUT, HUNTER_LODGE'},
hide: {count: knobsAndLevers.defaultHideStores, source: 'HUNTER_LODGE'},
clothing: {count: knobsAndLevers.defaultClothingStores, source: 'TAILOR'},
};
const addVillager = () => {
let nextId = villagers.length > 0 ? Math.max(...villagers.map(v => v.id)) + 1 : 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment