Merge pull request #848 from tsr488/achievementsort

Adding option to sort buildings by price until next achievement
This commit is contained in:
Daniël van Noord
2021-06-08 08:57:33 +02:00
committed by GitHub
4 changed files with 57 additions and 33 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,12 +64,19 @@ 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
if (Game.buyMode === 1 && CMOptions.SortBuildings) {
let arr; let arr;
if (CMOptions.SortBuildings === 1) { 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 => { arr = Object.keys(CacheObjects1).map(k => {
const o = {}; const o = {};
o.name = k; o.name = k;
@@ -76,6 +84,12 @@ export default function UpdateBuildings() {
o.color = CacheObjects1[k].color; o.color = CacheObjects1[k].color;
return o; 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)
);
} else if (CMOptions.SortBuildings === 2) { } else if (CMOptions.SortBuildings === 2) {
arr = Object.keys(target).map(k => { arr = Object.keys(target).map(k => {
const o = {}; const o = {};
@@ -84,26 +98,35 @@ export default function UpdateBuildings() {
o.color = target[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 === 3) {
Game.Objects[arr[x].name].l.style.gridRow = `${x + 2}/${x + 2}`; arr = Object.keys(CacheObjectsNextAchievement).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.id = Game.Objects[k].id;
o.amountUntilNext = CacheObjectsNextAchievement[k].AmountNeeded;
o.priceUntilNext = CacheObjectsNextAchievement[k].price;
return o; return o;
}); });
// First, sort using default order.
arr.sort((a, b) => a.id - b.id); arr.sort((a, b) => a.id - b.id);
// Sort by price until next achievement.
// Buildings that aren't within 100 of an achievement are placed at the end, still in
// default order relative to each other because sort() is guaranteed stable.
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++) { for (let x = 0; x < arr.length; x++) {
Game.Objects[arr[x].name].l.style.gridRow = `${x + 2}/${x + 2}`; Game.Objects[arr[x].name].l.style.gridRow = `${x + 2}/${x + 2}`;
} }
}
} }