From bcef69a344d2f32a81e103f6ab59f6be355e1d20 Mon Sep 17 00:00:00 2001 From: Fabian Date: Mon, 23 Nov 2020 16:41:46 +0100 Subject: [PATCH] Sort buildings and upgrades by PP (#253) --- CookieMonster.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++-- src/Config.js | 3 ++- src/Disp.js | 47 +++++++++++++++++++++++++++++++++++++++++ src/Main.js | 4 +++- 4 files changed, 104 insertions(+), 4 deletions(-) diff --git a/CookieMonster.js b/CookieMonster.js index 8d71f0a..030470a 100644 --- a/CookieMonster.js +++ b/CookieMonster.js @@ -589,7 +589,8 @@ CM.ConfigData.TimeFormat = {label: ['Time XXd, XXh, XXm, XXs', 'Time XX:XX:XX:XX CM.ConfigData.SayTime = {label: ['Format Time OFF', 'Format Time ON'], desc: 'Change how time is displayed in statistics', toggle: true, func: function() {CM.Disp.ToggleSayTime();}}; CM.ConfigData.GrimoireBar = {label: ['Grimoire Magic Meter Timer OFF', 'Grimoire Magic Meter Timer ON'], desc: 'A timer on how long before the Grimoire magic meter is full', toggle: true}; CM.ConfigData.Scale = {label: ['Game\'s Setting Scale', 'Metric', 'Short Scale', 'Scientific Notation', 'Engineering Notation'], desc: 'Change how long numbers are handled', toggle: false, func: function() {CM.Disp.RefreshScale();}}; - +CM.ConfigData.SortBuildings = {label: ['Sort Buildings: Default', 'Sort Buildings: PP'], desc: 'Sort the display of buildings in either default order or by PP', toggle: false, func: function () { CM.Disp.UpdateBuildings();}}; +CM.ConfigData.SortUpgrades = {label: ['Sort Upgrades: Default', 'Sort Upgrades: PP'], desc: 'Sort the display of upgrades in either default order or by PP', toggle: false, func: function () { CM.Disp.UpdateUpgrades();}}; /******** * Data * ********/ @@ -831,6 +832,14 @@ CM.Disp.CreateCssArea = function() { CM.Disp.Css.type = 'text/css'; document.head.appendChild(CM.Disp.Css); + + // given the architecture of your code, you probably want these lines somewhere else, + // but I stuck them here for convenience + l("products").style.display = "grid"; + l("storeBulk").style.gridRow = "1/1"; + + l("upgrades").style.display = "flex"; + l("upgrades").style["flex-wrap"] = "wrap"; } CM.Disp.CreateBotBar = function() { @@ -1218,6 +1227,24 @@ CM.Disp.UpdateBuildings = function() { l('productPrice' + Game.Objects[i].id).style.color = ''; } } + + // 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) + var arr = Object.keys(CM.Cache.Objects).map(k => + { + var o = CM.Cache.Objects[k]; + o.name = k; + o.id = Game.Objects[k].id; + return o; + }); + + if (CM.Config.SortBuildings) + arr.sort((a, b) => a.pp - b.pp); + else + arr.sort((a, b) => a.id - b.id); + + for (var x = 0; x < arr.length; x++) + Game.Objects[arr[x].name].l.style.gridRow = (x + 2) + "/" + (x + 2); } CM.Disp.CreateUpgradeBar = function() { @@ -1345,6 +1372,25 @@ CM.Disp.UpdateUpgrades = function() { l('CMUpgradeBarPurple').textContent = purple; l('CMUpgradeBarGray').textContent = gray; } + + // Build array of pointers, sort by pp, set flex positions + var arr = []; + for (var x = 0; x < Game.UpgradesInStore.length; x++){ + var o = {}; + o.name = Game.UpgradesInStore[x].name; + o.price = Game.UpgradesInStore[x].basePrice; + o.pp = CM.Cache.Upgrades[o.name].pp; + arr.push(o); + } + + if (CM.Config.SortUpgrades) + arr.sort((a, b) => a.pp - b.pp); + else + arr.sort((a, b) => a.price - b.price); + + for (var x = 0; x < Game.UpgradesInStore.length; x++){ + l("upgrade" + x).style.order = arr.findIndex(e => e.name === Game.UpgradesInStore[x].name) + 1 + } } CM.Disp.UpdateColors = function() { @@ -1733,6 +1779,8 @@ CM.Disp.AddMenuPref = function(title) { frag.appendChild(listing('BotBar')); frag.appendChild(listing('TimerBar')); frag.appendChild(listing('TimerBarPos')); + frag.appendChild(listing('SortBuildings')); + frag.appendChild(listing('SortUpgrades')); frag.appendChild(listing('BuildColor')); frag.appendChild(listing('BulkBuildColor')); frag.appendChild(listing('UpBarColor')); @@ -3027,7 +3075,9 @@ CM.ConfigDefault = { GrimoireBar: 1, Scale: 2, StatsPref: {Lucky: 1, Chain: 1, Prestige: 1, Wrink: 1, Sea: 1, Misc: 1}, - Colors : {Blue: '#4bb8f0', Green: '#00ff00', Yellow: '#ffff00', Orange: '#ff7f00', Red: '#ff0000', Purple: '#ff00ff', Gray: '#b3b3b3', Pink: '#ff1493', Brown: '#8b4513'} + Colors : {Blue: '#4bb8f0', Green: '#00ff00', Yellow: '#ffff00', Orange: '#ff7f00', Red: '#ff0000', Purple: '#ff00ff', Gray: '#b3b3b3', Pink: '#ff1493', Brown: '#8b4513'}, + SortBuildings: 0, + SortUpgrades: 0 }; CM.ConfigPrefix = 'CMConfig'; diff --git a/src/Config.js b/src/Config.js index 907a8f7..56974a8 100644 --- a/src/Config.js +++ b/src/Config.js @@ -183,4 +183,5 @@ CM.ConfigData.TimeFormat = {label: ['Time XXd, XXh, XXm, XXs', 'Time XX:XX:XX:XX CM.ConfigData.SayTime = {label: ['Format Time OFF', 'Format Time ON'], desc: 'Change how time is displayed in statistics', toggle: true, func: function() {CM.Disp.ToggleSayTime();}}; CM.ConfigData.GrimoireBar = {label: ['Grimoire Magic Meter Timer OFF', 'Grimoire Magic Meter Timer ON'], desc: 'A timer on how long before the Grimoire magic meter is full', toggle: true}; CM.ConfigData.Scale = {label: ['Game\'s Setting Scale', 'Metric', 'Short Scale', 'Scientific Notation', 'Engineering Notation'], desc: 'Change how long numbers are handled', toggle: false, func: function() {CM.Disp.RefreshScale();}}; - +CM.ConfigData.SortBuildings = {label: ['Sort Buildings: Default', 'Sort Buildings: PP'], desc: 'Sort the display of buildings in either default order or by PP', toggle: false, func: function () { CM.Disp.UpdateBuildings();}}; +CM.ConfigData.SortUpgrades = {label: ['Sort Upgrades: Default', 'Sort Upgrades: PP'], desc: 'Sort the display of upgrades in either default order or by PP', toggle: false, func: function () { CM.Disp.UpdateUpgrades();}}; diff --git a/src/Disp.js b/src/Disp.js index 8e65ec9..5f5fe96 100644 --- a/src/Disp.js +++ b/src/Disp.js @@ -206,6 +206,14 @@ CM.Disp.CreateCssArea = function() { CM.Disp.Css.type = 'text/css'; document.head.appendChild(CM.Disp.Css); + + // given the architecture of your code, you probably want these lines somewhere else, + // but I stuck them here for convenience + l("products").style.display = "grid"; + l("storeBulk").style.gridRow = "1/1"; + + l("upgrades").style.display = "flex"; + l("upgrades").style["flex-wrap"] = "wrap"; } CM.Disp.CreateBotBar = function() { @@ -593,6 +601,24 @@ CM.Disp.UpdateBuildings = function() { l('productPrice' + Game.Objects[i].id).style.color = ''; } } + + // 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) + var arr = Object.keys(CM.Cache.Objects).map(k => + { + var o = CM.Cache.Objects[k]; + o.name = k; + o.id = Game.Objects[k].id; + return o; + }); + + if (CM.Config.SortBuildings) + arr.sort((a, b) => a.pp - b.pp); + else + arr.sort((a, b) => a.id - b.id); + + for (var x = 0; x < arr.length; x++) + Game.Objects[arr[x].name].l.style.gridRow = (x + 2) + "/" + (x + 2); } CM.Disp.CreateUpgradeBar = function() { @@ -720,6 +746,25 @@ CM.Disp.UpdateUpgrades = function() { l('CMUpgradeBarPurple').textContent = purple; l('CMUpgradeBarGray').textContent = gray; } + + // Build array of pointers, sort by pp, set flex positions + var arr = []; + for (var x = 0; x < Game.UpgradesInStore.length; x++){ + var o = {}; + o.name = Game.UpgradesInStore[x].name; + o.price = Game.UpgradesInStore[x].basePrice; + o.pp = CM.Cache.Upgrades[o.name].pp; + arr.push(o); + } + + if (CM.Config.SortUpgrades) + arr.sort((a, b) => a.pp - b.pp); + else + arr.sort((a, b) => a.price - b.price); + + for (var x = 0; x < Game.UpgradesInStore.length; x++){ + l("upgrade" + x).style.order = arr.findIndex(e => e.name === Game.UpgradesInStore[x].name) + 1 + } } CM.Disp.UpdateColors = function() { @@ -1108,6 +1153,8 @@ CM.Disp.AddMenuPref = function(title) { frag.appendChild(listing('BotBar')); frag.appendChild(listing('TimerBar')); frag.appendChild(listing('TimerBarPos')); + frag.appendChild(listing('SortBuildings')); + frag.appendChild(listing('SortUpgrades')); frag.appendChild(listing('BuildColor')); frag.appendChild(listing('BulkBuildColor')); frag.appendChild(listing('UpBarColor')); diff --git a/src/Main.js b/src/Main.js index 55c5de3..2d3b3e8 100644 --- a/src/Main.js +++ b/src/Main.js @@ -295,7 +295,9 @@ CM.ConfigDefault = { GrimoireBar: 1, Scale: 2, StatsPref: {Lucky: 1, Chain: 1, Prestige: 1, Wrink: 1, Sea: 1, Misc: 1}, - Colors : {Blue: '#4bb8f0', Green: '#00ff00', Yellow: '#ffff00', Orange: '#ff7f00', Red: '#ff0000', Purple: '#ff00ff', Gray: '#b3b3b3', Pink: '#ff1493', Brown: '#8b4513'} + Colors : {Blue: '#4bb8f0', Green: '#00ff00', Yellow: '#ffff00', Orange: '#ff7f00', Red: '#ff0000', Purple: '#ff00ff', Gray: '#b3b3b3', Pink: '#ff1493', Brown: '#8b4513'}, + SortBuildings: 0, + SortUpgrades: 0 }; CM.ConfigPrefix = 'CMConfig';