Commit 2e95a76b authored by Matthew Odle's avatar Matthew Odle

priority adjustment now appropriately finds the next closest value when...

priority adjustment now appropriately finds the next closest value when adjusting, even if priority values are not in sequence
parent 5e3e1c6b
......@@ -31,3 +31,4 @@ update:
* Tools now degrade and break
* Added pretty colors to indicate production status
* Added sorting and priority adjustment; it's somewhat fragile: if there is no building with a priority 1 higher, the building will not move; this means if building a has priority 5 and building b has priority 10, nothing will happen when the up/down buttons are pushed. If there is any break in the sequence of priorities in the buildings array, reassignment of priority will not work. A better approach would be to find the next highest value.
* Fixed this by using filter and reduce
......@@ -14,7 +14,7 @@ export class Building extends React.Component {
<div className='tooltip'>{building.label}
<span className='tooltiptext'>{building.description}</span>
</div>
{ building.priority != undefined ? <Priority building={building} increasePriority={this.props.increasePriority} decreasePriority={this.props.decreasePriority} /> : null }
{ building.priority !== undefined ? <Priority building={building} increasePriority={this.props.increasePriority} decreasePriority={this.props.decreasePriority} /> : null }
{ building.goods ? <Goods building={building} /> : null }
{ building.materials ? <Materials building={building} /> : null }
<div>Active: {building.producing ? "Yes" : "No" }</div>
......
......@@ -99,15 +99,14 @@ export class Village extends React.Component {
increasePriority = (building) => {
console.log('increase. beep boop beep.')
if (building.priority === 0) {
return;
}
const buildings = this.state.buildings.slice();
const buildingToSwapWith = buildings.find( otherBuilding => otherBuilding.priority === building.priority - 1 );
if (buildingToSwapWith) {
const buildingsWithLowerPriority = buildings.filter( otherBuilding => otherBuilding.priority < building.priority );
if (buildingsWithLowerPriority.length > 0) {
building.priority = building.priority -= 1;
this.updateTheBuildings(building);
const buildingToSwapWith = buildingsWithLowerPriority.reduce((a, b) => a.priority > b.priority ? a : b)
buildingToSwapWith.priority += 1;
this.updateTheBuildings(buildingToSwapWith);
}
......@@ -115,10 +114,13 @@ export class Village extends React.Component {
decreasePriority = (building) => {
const buildings = this.state.buildings.slice();
const buildingToSwapWith = buildings.find( otherBuilding => otherBuilding.priority === building.priority + 1 );
if (buildingToSwapWith) {
const buildingsWithHigherPriority = buildings.filter( otherBuilding => otherBuilding.priority > building.priority );
if (buildingsWithHigherPriority.length > 0) {
building.priority += 1;
this.updateTheBuildings(building);
const buildingToSwapWith = buildingsWithHigherPriority.reduce((a, b) => a.priority < b.priority ? a : b)
buildingToSwapWith.priority -= 1;
this.updateTheBuildings(buildingToSwapWith);
}
......
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