Adding option to sort buildings by price until next achievement

This commit is contained in:
Tom
2021-06-06 17:32:37 -04:00
parent 4fb58719d3
commit 6ab791d762
4 changed files with 54 additions and 35 deletions

View File

@@ -308,8 +308,9 @@ const Config = {
'Sort buildings: default',
'Sort buildings: PP of x1 purchase',
'Sort buildings: PP of selected bulk mode',
'Sort buildings: price until next achievement',
],
'Sort the display of buildings in either default order or by PP',
'Sort the display of buildings in default order, by PP, or until next achievement',
false,
() => {
UpdateBuildings();

View File

@@ -3,6 +3,7 @@ import {
CacheObjects1,
CacheObjects10,
CacheObjects100,
CacheObjectsNextAchievement,
} from '../../Cache/VariablesAndData';
import { CMOptions } from '../../Config/VariablesAndData';
import BuildingSell from '../../Sim/SimulationEvents/SellBuilding';
@@ -63,47 +64,64 @@ export default function UpdateBuildings() {
});
}
// Build array of pointers, sort by pp, use array index (+2) as the grid row number
// (grid rows are 1-based indexing, and row 1 is the bulk buy/sell options)
// This regulates sorting of buildings
if (Game.buyMode === 1 && CMOptions.SortBuildings) {
let arr;
if (CMOptions.SortBuildings === 1) {
arr = Object.keys(CacheObjects1).map(k => {
const o = {};
o.name = k;
o.pp = CacheObjects1[k].pp;
o.color = CacheObjects1[k].color;
return o;
});
} else if (CMOptions.SortBuildings === 2) {
arr = Object.keys(target).map(k => {
const o = {};
o.name = k;
o.pp = target[k].pp;
o.color = target[k].color;
return o;
});
}
// Build array of pointers and sort according to the user's configured sort option.
// This regulates sorting of buildings.
let arr;
if (Game.buyMode !== 1 || !CMOptions.SortBuildings) {
arr = Object.keys(CacheObjects1).map(k => {
const o = {};
o.name = k;
o.id = Game.Objects[k].id;
return o;
});
// Sort using default order.
arr.sort((a, b) => a.id - b.id);
} else if (CMOptions.SortBuildings === 1) {
arr = Object.keys(CacheObjects1).map(k => {
const o = {};
o.name = k;
o.pp = CacheObjects1[k].pp;
o.color = CacheObjects1[k].color;
return o;
});
// Sort by pp colour group, then by pp.
arr.sort((a, b) =>
ColoursOrdering.indexOf(a.color) === ColoursOrdering.indexOf(b.color)
? a.pp - b.pp
: ColoursOrdering.indexOf(a.color) - ColoursOrdering.indexOf(b.color)
);
for (let x = 0; x < arr.length; x++) {
Game.Objects[arr[x].name].l.style.gridRow = `${x + 2}/${x + 2}`;
}
} else {
const arr = Object.keys(CacheObjects1).map(k => {
} else if (CMOptions.SortBuildings === 2) {
arr = Object.keys(target).map(k => {
const o = {};
o.name = k;
o.id = Game.Objects[k].id;
o.pp = target[k].pp;
o.color = target[k].color;
return o;
});
arr.sort((a, b) => a.id - b.id);
for (let x = 0; x < arr.length; x++) {
Game.Objects[arr[x].name].l.style.gridRow = `${x + 2}/${x + 2}`;
}
// Sort by pp colour group, then by pp.
arr.sort((a, b) =>
ColoursOrdering.indexOf(a.color) === ColoursOrdering.indexOf(b.color)
? a.pp - b.pp
: ColoursOrdering.indexOf(a.color) - ColoursOrdering.indexOf(b.color)
);
} else if (CMOptions.SortBuildings === 3) {
arr = Object.keys(CacheObjectsNextAchievement).map(k => {
const o = {};
o.name = k;
o.amountUntilNext = CacheObjectsNextAchievement[k].AmountNeeded;
o.priceUntilNext = CacheObjectsNextAchievement[k].price;
return o;
});
// Sort by price until next achievement. Buildings that aren't close to an achievement are always last.
arr.sort((a, b) =>
(a.amountUntilNext !== 101 ? a.priceUntilNext : Infinity) -
(b.amountUntilNext !== 101 ? b.priceUntilNext : Infinity)
);
}
// Use array index (+2) as the grid row number.
// (grid rows are 1-based indexing, and row 1 is the bulk buy/sell options)
for (let x = 0; x < arr.length; x++) {
Game.Objects[arr[x].name].l.style.gridRow = `${x + 2}/${x + 2}`;
}
}