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

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -308,8 +308,9 @@ const Config = {
'Sort buildings: default', 'Sort buildings: default',
'Sort buildings: PP of x1 purchase', 'Sort buildings: PP of x1 purchase',
'Sort buildings: PP of selected bulk mode', '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, false,
() => { () => {
UpdateBuildings(); UpdateBuildings();

View File

@@ -3,6 +3,7 @@ import {
CacheObjects1, CacheObjects1,
CacheObjects10, CacheObjects10,
CacheObjects100, CacheObjects100,
CacheObjectsNextAchievement,
} from '../../Cache/VariablesAndData'; } from '../../Cache/VariablesAndData';
import { CMOptions } from '../../Config/VariablesAndData'; import { CMOptions } from '../../Config/VariablesAndData';
import BuildingSell from '../../Sim/SimulationEvents/SellBuilding'; 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 // Build array of pointers and sort according to the user's configured sort option.
// (grid rows are 1-based indexing, and row 1 is the bulk buy/sell options) // This regulates sorting of buildings.
// This regulates sorting of buildings let arr;
if (Game.buyMode === 1 && CMOptions.SortBuildings) { if (Game.buyMode !== 1 || !CMOptions.SortBuildings) {
let arr; arr = Object.keys(CacheObjects1).map(k => {
if (CMOptions.SortBuildings === 1) { const o = {};
arr = Object.keys(CacheObjects1).map(k => { o.name = k;
const o = {}; o.id = Game.Objects[k].id;
o.name = k; return o;
o.pp = CacheObjects1[k].pp; });
o.color = CacheObjects1[k].color; // Sort using default order.
return o; arr.sort((a, b) => a.id - b.id);
}); } else if (CMOptions.SortBuildings === 1) {
} else if (CMOptions.SortBuildings === 2) { arr = Object.keys(CacheObjects1).map(k => {
arr = Object.keys(target).map(k => { const o = {};
const o = {}; o.name = k;
o.name = k; o.pp = CacheObjects1[k].pp;
o.pp = target[k].pp; o.color = CacheObjects1[k].color;
o.color = target[k].color; return o;
return o; });
});
}
// Sort by pp colour group, then by pp. // Sort by pp colour group, then by pp.
arr.sort((a, b) => arr.sort((a, b) =>
ColoursOrdering.indexOf(a.color) === ColoursOrdering.indexOf(b.color) ColoursOrdering.indexOf(a.color) === ColoursOrdering.indexOf(b.color)
? a.pp - b.pp ? a.pp - b.pp
: ColoursOrdering.indexOf(a.color) - ColoursOrdering.indexOf(b.color) : ColoursOrdering.indexOf(a.color) - ColoursOrdering.indexOf(b.color)
); );
for (let x = 0; x < arr.length; x++) { } else if (CMOptions.SortBuildings === 2) {
Game.Objects[arr[x].name].l.style.gridRow = `${x + 2}/${x + 2}`; arr = Object.keys(target).map(k => {
}
} else {
const arr = Object.keys(CacheObjects1).map(k => {
const o = {}; const o = {};
o.name = k; o.name = k;
o.id = Game.Objects[k].id; o.pp = target[k].pp;
o.color = target[k].color;
return o; return o;
}); });
arr.sort((a, b) => a.id - b.id); // Sort by pp colour group, then by pp.
for (let x = 0; x < arr.length; x++) { arr.sort((a, b) =>
Game.Objects[arr[x].name].l.style.gridRow = `${x + 2}/${x + 2}`; 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}`;
} }
} }