Commit 55bce7b7 authored by Matthew Odle's avatar Matthew Odle

refactor add/remove villager to DRY them up; combine building types

parent f0cf3975
......@@ -20,4 +20,5 @@ Reorg of structure; moved materials and producedGoods counters into a state vari
Did a code review, deleted some dead code
Fixed a bug where if first materials needed to produce is missing, but second is available, material is still produced
Added villager list generation
Next step: villager assignment handling
\ No newline at end of file
Next step: villager assignment handling
import React from 'react';
export class ProductionBuilding extends React.Component {
import { Materials } from './materials';
import { Goods } from './goods';
export class Building extends React.Component {
addMaterials(material, amount) {
this.materials[material] += amount;
......@@ -9,10 +12,10 @@ export class ProductionBuilding extends React.Component {
this.materials[material] -= amount;
};
addProducedItems(item, amount) {
this.producedItems[item] += amount;
this.goods[item] += amount;
};
removeProducedItems(item, amount) {
this.producedItems[item] -= amount;
this.goods[item] -= amount;
};
render() {
var building = this.props.building;
......@@ -21,14 +24,12 @@ export class ProductionBuilding extends React.Component {
<div className='tooltip'>{building.label}
<span className='tooltiptext'>{building.description}</span>
</div>
<button type="button" id="assignVillager" onClick={() => this.props.assignVillager(building, 1)}>+</button>
<button type="button" id="removeVillager" onClick={() => this.props.removeVillager(building, 1)}>-</button>
<button type="button" id="assignVillager" onClick={() => this.props.assignVillager(building, 10)}>++</button>
<button type="button" id="removeVillager" onClick={() => this.props.removeVillager(building, 10)}>--</button>
<div className='producedItems'>
<span>Produces Items: </span>
<span>{building.producedItems.toString()}</span>
</div>
<button type="button" id="addAVillager" onClick={() => this.props.manageVillagers(building, 1, 'assign')}>+</button>
<button type="button" id="removeAVillger" onClick={() => this.props.manageVillagers(building, 1, 'unassign')}>-</button>
<button type="button" id="addManyVillagers" onClick={() => this.props.manageVillagers(building, 10, 'assign')}>++</button>
<button type="button" id="removeManyVillagers" onClick={() => this.props.manageVillagers(building, 10, 'unassign')}>--</button>
{ building.goods ? <Goods building={building} /> : null }
{ building.materials ? <Materials building={building} /> : null }
<div>Active: {building.producing ? "Yes" : "No" }</div>
<div>Progress: {building.progress}</div>
<div>Villagers Assigned: {building.assignedVillagers}</div>
......
import React from 'react';
export class ExtractionBuilding extends React.Component {
addProducedItems(item, amount) {
this.producedItems[item] += amount;
};
removeProducedItems(item, amount) {
this.producedItems[item] -= amount;
};
render() {
var building = this.props.building;
return (
<div className='building'>
<div className='tooltip'>{building.label}
<span className='tooltiptext'>{building.description}</span>
</div>
<button type="button" id="assignVillager" onClick={() => this.props.assignVillager(building, 1)}>+</button>
<button type="button" id="removeVillager" onClick={() => this.props.removeVillager(building, 1)}>-</button>
<button type="button" id="assignVillager" onClick={() => this.props.assignVillager(building, 10)}>++</button>
<button type="button" id="removeVillager" onClick={() => this.props.removeVillager(building, 10)}>--</button>
<div className='producedItems'>
<span>Produces Items: </span>
<span>{building.producedItems.toString()}</span>
</div>
<div>Active: {building.producing ? "Yes" : "No" }</div>
<div>Progress: {building.progress}</div>
<div>Villagers Assigned: {building.assignedVillagers}</div>
</div>
);
}
}
import React from 'react';
export class Goods extends React.Component {
render() {
var building = this.props.building;
return (
<div className='goods'>
<span>Goods produced: </span>
<span>{building.goods.toString()}</span>
</div>
);
}
}
import React from 'react';
export class Materials extends React.Component {
render() {
var building = this.props.building;
return (
<div className='materials'>
<span>Materials needed: </span>
<span>{building.materials.toString()}</span>
</div>
);
}
}
import React from 'react';
import { ProductionBuilding } from './production-building';
import { Building } from './building';
import { Stock } from './stock';
import { ExtractionBuilding } from './extraction-building';
export class Village extends React.Component {
constructor(props) {
......@@ -15,58 +14,56 @@ export class Village extends React.Component {
};
}
assignVillager = (updateBuilding, amount) => {
const buildings = this.props.buildings.slice();
canAdjust = (building, amount, operation) => {
let unassignedVillagers = this.state.unassignedVillagers;
if (operation === 'assign' && (building.assignedVillagers >= building.capacity || unassignedVillagers <= 0)) {
return false;
}
if (operation === 'unassign' && building.assignedVillagers <= 0) {
building.assignedVillagers = 0;
return false;
}
return true;
}
// this block is unique to addVillager; the rest is shared with removeVillager; DRY it up
if (updateBuilding.assignedVillagers >= updateBuilding.capacity || unassignedVillagers <= 0) {
return;
getValues = (building, operation) => {
let unassignedVillagers = this.state.unassignedVillagers;
let checkValue = 0;
let signMultiplier = 1;
if (operation === 'assign') {
checkValue = unassignedVillagers;
} else if (operation === 'unassign') {
checkValue = building.assignedVillagers;
signMultiplier = -1;
}
let adjustmentValue = unassignedVillagers - amount > 0 ? amount : unassignedVillagers;
updateBuilding.assignedVillagers += adjustmentValue;
unassignedVillagers -= adjustmentValue;
return {checkValue: checkValue, signMultiplier: signMultiplier}
}
const buildingIndex = buildings.findIndex(building => building.id === updateBuilding.id);
updateTheVillage = (building, unassignedVillagers) => {
const buildings = this.props.buildings.slice();
const buildingIndex = buildings.findIndex(indexBuilding => indexBuilding.id === building.id);
this.setState({
unassignedVillagers: unassignedVillagers,
buildings: [
...buildings.slice(0, buildingIndex),
updateBuilding,
building,
...buildings.slice(buildingIndex + 1),
]
});
};
removeVillager = (updateBuilding, amount) => {
const buildings = this.props.buildings.slice();
let unassignedVillagers = this.state.unassignedVillagers;
}
// this block is unique to removeVillager; the rest is shared with addVillager; DRY it up
if (updateBuilding.assignedVillagers <= 0) {
updateBuilding.assignedVillagers = 0;
manageVillagers = (building, amount, operation) => {
if (!this.canAdjust(building, amount, operation)) {
return;
}
let adjustmentValue = updateBuilding.assignedVillagers - amount > 0 ? amount : updateBuilding.assignedVillagers;
updateBuilding.assignedVillagers -= adjustmentValue;
unassignedVillagers += adjustmentValue;
let unassignedVillagers = this.state.unassignedVillagers;
const buildingIndex = buildings.findIndex(building => building.id === updateBuilding.id);
this.setState({
unassignedVillagers: unassignedVillagers,
buildings: [
...buildings.slice(0, buildingIndex),
updateBuilding,
...buildings.slice(buildingIndex + 1),
]
});
};
let values = this.getValues(building, operation);
let adjustmentValue = values.checkValue - amount > 0 ? amount : values.checkValue;
building.assignedVillagers += values.signMultiplier * adjustmentValue;
unassignedVillagers -= values.signMultiplier * adjustmentValue;
componentDidMount = () => {
this.timer = setInterval(() => this.tick(), 1000);
};
componentWillUnmount = () => {
clearInterval(this.timer);
this.updateTheVillage(building, unassignedVillagers);
};
canProduce = (building) => {
......@@ -98,16 +95,16 @@ export class Village extends React.Component {
building.materialsInUse = true;
};
addProduct = (producedItems) => {
const theItems = this.state.villageStore.slice();
producedItems.map(
item => theItems.map(
theItem => theItem.name === item ? theItem.count += 1 : null,
addProduct = (goods) => {
const villageStore = this.state.villageStore.slice();
goods.map(
aGood => villageStore.map(
storeItem => storeItem.name === aGood ? storeItem.count += 1 : null,
)
);
this.setState({
villageStore: theItems,
villageStore: villageStore,
});
};
......@@ -118,7 +115,7 @@ export class Village extends React.Component {
building.progress = 0;
building.materialsInUse = false;
}
this.addProduct(building.producedItems);
this.addProduct(building.goods);
};
startNextProductionCycle = (building) => {
......@@ -154,6 +151,13 @@ export class Village extends React.Component {
});
};
componentDidMount = () => {
this.timer = setInterval(() => this.tick(), 1000);
};
componentWillUnmount = () => {
clearInterval(this.timer);
};
tick = () => {
const buildings = this.props.buildings.slice();
this.manageProductionBuildings(buildings);
......@@ -177,32 +181,9 @@ export class Village extends React.Component {
</div>
<h3>Buildings</h3>
<div className="buildings">
{
buildings.map( building =>
{return building.type === 'production'
? <ProductionBuilding
key={building.id}
building={building}
assignVillager={this.assignVillager}
removeVillager={this.removeVillager}
/>
: null
}
)
}
{
buildings.map( building =>
{return building.type === 'extraction'
? <ExtractionBuilding
key={building.id}
building={building}
assignVillager={this.assignVillager}
removeVillager={this.removeVillager}
/>
: null
}
)
}
{ buildings.map( building =>
<Building key={building.id} building={building} manageVillagers={this.manageVillagers} />
)}
</div>
</div>
)
......
......@@ -35,7 +35,7 @@ buildings.push({type: 'production',
description: 'workers are more efficient with the right tools',
priority: 0,
materials: ['wood', 'iron'],
producedItems: ['tool'],
goods: ['tool'],
assignedVillagers: 0,
capacity: defaultCapacity,
progress: 0,
......@@ -48,7 +48,7 @@ buildings.push({type: 'extraction',
label: "Forester's Hut",
description: "it's big, it's heavy, it's wood",
priority: 0,
producedItems: ['wood'],
goods: ['wood'],
assignedVillagers: 0,
capacity: defaultCapacity,
progress: 0,
......@@ -61,7 +61,7 @@ buildings.push({type: 'extraction',
label: "Mine",
description: "the strength of the earth to forge the tools",
priority: 0,
producedItems: ['iron'],
goods: ['iron'],
assignedVillagers: 0,
capacity: defaultCapacity,
progress: 0,
......@@ -75,7 +75,7 @@ buildings.push({type: 'production',
description: "it's big, it's heavy, it's wood",
priority: 0,
materials: ['wood'],
producedItems: ['firewood'],
goods: ['firewood'],
assignedVillagers: 0,
capacity: defaultCapacity,
progress: 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