Skip to content

Commit b4fce29

Browse files
committed
Add: upgrades!
1 parent e556f8c commit b4fce29

File tree

3 files changed

+139
-50
lines changed

3 files changed

+139
-50
lines changed

CHANGELOG.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
9+
10+
## [2042.7] - 2021-09-18
11+
### Added
12+
- Include upgrades now!
13+
814
### Changed
9-
- Improve: The metric of deal quality from `Price–PerBuildingCPS ratio` to `cpsAcceleration ratio`, in order to gain most cps in the shortest time.
10-
### Fixed
11-
- The color range is mistakenly include hidden building, thus colors are not much varies as expect.
15+
- Improve: change The metric of deal quality from `Price–PerBuildingCPS ratio` to `cpsAcceleration ratio`, in order to gain most cps in the shortest time.
16+
- Improve: hue for notations.
1217

1318
## [2042.6] - 2021-09-15
1419
### Fixed
@@ -19,7 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1924
- Colors!
2025

2126
### Changed
22-
- Efficiency: Sort buildings only if the order has changed
27+
- Improve efficiency of sorting buildings. Only sort them if the order has changed.
2328

2429
## [2042.4] - 2021-09-13
2530
### Fixed

RELEASE.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
## [2042.6] - 2021-09-15
2-
### Fixed
3-
- Loading error about CCSE init
1+
## [2042.7] - 2021-09-18
2+
### Added
3+
- Include upgrades now!
4+
5+
### Changed
6+
- Improve: change The metric of deal quality from `Price–PerBuildingCPS ratio` to `cpsAcceleration ratio`, in order to gain most cps in the shortest time.
7+
- Improve: hue for notations.

main.js

Lines changed: 123 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
/**
22
* @typedef {function} LoadScript
33
*/
4-
/**
5-
* @typedef {Object} Building
6-
* @property {number} price
7-
* @property {DocumentFragment} l
8-
*/
94
/**
105
* @function l
116
* @param {string} id
@@ -18,8 +13,19 @@
1813
/**
1914
* @function PlaySound
2015
*/
16+
/**
17+
* @typedef {Object} Building
18+
* @property {number} price
19+
* @property {DocumentFragment} l
20+
*/
21+
/**
22+
* @typedef {Object} Upgrade
23+
* @property {function} getPrice
24+
* @property {number} bought
25+
*/
2126
/**
2227
* @typedef {Object} Game
28+
* @property {function} Logic
2329
* @property {function} registerMod
2430
* @property {function} Has
2531
* @property {function} CalculateGains
@@ -28,6 +34,7 @@
2834
* @property {number} GrandmaSynergies.buildingTie.storedTotalCps
2935
* @property {Object.<string, Building>} Objects
3036
* @property {Building[]} ObjectsById
37+
* @property {Upgrade[]} UpgradesInStore
3138
* @property {Array} customOptionsMenu
3239
* @property {Array} Upgrades
3340
* @property {string} clickStr
@@ -63,6 +70,10 @@ let BestDealHelper = {
6370
},
6471

6572
"init": function () {
73+
// change building layout
74+
[...document.getElementsByClassName("product")].forEach(e => e.style.lineHeight = "18px");
75+
[...document.getElementsByClassName("productName")].forEach(e => e.style.fontSize = "26px");
76+
6677
MOD = this;
6778
Game.customOptionsMenu.push(MOD.addOptionsMenu);
6879
MOD.last_cps = 0;
@@ -72,6 +83,12 @@ let BestDealHelper = {
7283
setInterval(MOD.logicLoop, 200);
7384
}, 500);
7485

86+
const GameRebuildUpgrades = Game.RebuildUpgrades;
87+
Game.RebuildUpgrades = function () {
88+
GameRebuildUpgrades();
89+
MOD.logicLoop();
90+
};
91+
7592
MOD.isLoaded = true;
7693
},
7794

@@ -98,10 +115,11 @@ let BestDealHelper = {
98115
logicLoop: function () {
99116
MOD.loopCount++;
100117
if (
101-
MOD.loopCount >= 10
118+
MOD.loopCount >= 5
102119
|| MOD.last_cps !== Game.cookiesPs
103120
|| MOD.config.sortbuildings !== MOD.last_config_sortbuildings
104-
|| !document.querySelector("#normalizedCpspb0")
121+
|| !document.querySelector("#productAcc0")
122+
|| (document.querySelector("#upgrade0") && !document.querySelector("#upgradeAcc0"))
105123
) {
106124
MOD.sortDeals();
107125
MOD.loopCount = 0;
@@ -129,19 +147,25 @@ let BestDealHelper = {
129147
sortDeals: function () {
130148
let enabledBuildings = Game.ObjectsById.map(e => +!e.locked).reduce((a, b) => a + b) + 2;
131149
let buildings = [...Game.ObjectsById].filter(o => o.id < enabledBuildings);
150+
let upgrades = [...Game.UpgradesInStore];
151+
let all = [...buildings, ...upgrades];
132152

133-
buildings.forEach(function (me) {
134-
Game.timedout = true;
135-
me.amount++;
153+
// Calculate cpsAcceleration
154+
all.forEach(function (me) {
155+
Game.Logic_ = Game.Logic;
156+
Game.Logic = function () {
157+
};
158+
if (me.type === "upgrade") me.bought++; else me.amount++;
136159
Game.CalculateGains();
137160
let newCookiesPs = Game.cookiesPs;
138-
me.amount--;
161+
if (me.type === "upgrade") me.bought--; else me.amount--;
139162
Game.CalculateGains();
140-
Game.timedout = false;
163+
Game.Logic = Game.Logic_;
141164

142165
let deltaTime;
166+
if (me.type === "upgrade") me.price = me.getPrice();
143167
if (me.price > Game.cookies) {
144-
deltaTime = (me.price - Game.cookies) / Game.cookiesPs + Game.cookies / newCookiesPs;
168+
deltaTime = (me.price - Game.cookies) / Game.cookiesPs + me.price / newCookiesPs;
145169
} else {
146170
deltaTime = me.price / newCookiesPs;
147171
}
@@ -150,45 +174,97 @@ let BestDealHelper = {
150174
return me.cpsAcceleration = deltaCps / deltaTime;
151175
});
152176

153-
// Sort buildings or leave them to default
154-
if (MOD.config.sortbuildings) {
155-
buildings.sort(function (a, b) {
156-
return (a.cpsAcceleration === b.cpsAcceleration) ? 0 : (a.cpsAcceleration < b.cpsAcceleration ? 1 : -1);
157-
});
177+
// determine colors
178+
all.sort((a, b) => a.cpsAcceleration - b.cpsAcceleration);
179+
all.forEach((e, index) => e.accRank = index);
180+
let palette = ["#00ffff"];
181+
let rank = all.length - 1;
182+
let domain = [all[rank].cpsAcceleration];
183+
rank--;
184+
if (rank >= 0) {
185+
palette.unshift("#00ff00");
186+
domain.unshift(all[rank].cpsAcceleration);
187+
}
188+
rank -= 6;
189+
if (rank >= 0) {
190+
palette.unshift("#ffd939");
191+
domain.unshift(all[rank].cpsAcceleration);
192+
}
193+
rank -= 8;
194+
if (rank >= 0) {
195+
palette.unshift("#ff0000");
196+
domain.unshift(all[rank].cpsAcceleration);
197+
}
198+
rank--;
199+
if (rank >= 0) {
200+
palette.unshift("#64007c");
201+
domain.unshift(all[0].cpsAcceleration);
158202
}
159203

160-
// Sort buildings only if the order has changed
161-
let buildings_order = buildings.map(e => e.id);
162-
if (!buildings_order.every((value, index) => value === MOD.last_buildings_order[index])) {
163-
let store = document.querySelector("#products");
164-
for (let i = 0; i < buildings.length; ++i) {
165-
store.appendChild(buildings[i].l);
204+
let color = chroma.scale(palette).mode("lab").domain(domain);
205+
206+
// Normalized Notation by Mean
207+
const avg = all.map(e => e.cpsAcceleration).reduce((a, b) => a + b, 0) / all.length;
208+
209+
// Notation for upgrades
210+
for (const i in upgrades) {
211+
let me = upgrades[i];
212+
me.l = document.querySelector("#upgrade" + i);
213+
let span = document.querySelector("#upgradeAcc" + i);
214+
if (!span) {
215+
span = document.createElement("span");
216+
span.id = "upgradeAcc" + i;
217+
span.style.fontWeight = "bolder";
218+
span.style.position = "absolute";
219+
span.style.bottom = "0px";
220+
span.style.left = "-2px";
221+
span.style.textShadow = "0px 2px 6px #000, 0px 1px 1px #000";
222+
span.style.transform = "scale(0.8,1)";
223+
l("upgrade" + i).appendChild(span);
224+
}
225+
span.textContent = Beautify(me.cpsAcceleration * 100 / avg, 1) + "%";
226+
span.style.color = color(me.cpsAcceleration);
227+
}
228+
// Sort upgrades (or leave them as default)
229+
upgrades.sort((a, b) => b.cpsAcceleration - a.cpsAcceleration);
230+
// Only sort when the order is different
231+
let upgrades_order = upgrades.map(e => e.l.id);
232+
let current_upgrades_order = [...document.querySelector("#upgrades").children].map(e => e.id);
233+
if (!upgrades_order.every((value, index) => value === current_upgrades_order[index])) {
234+
let store = document.querySelector("#upgrades");
235+
for (let i = 0; i < upgrades.length; ++i) {
236+
store.appendChild(upgrades[i].l);
166237
}
167-
MOD.last_buildings_order = buildings_order;
168-
// Game.Notify(`Buildings are sorted!`, ``, [16, 5], 1.5, 1);
169238
}
170239

171-
// Normalization by Mean
172-
const cpsAccelerationArr = buildings.map(e => e.cpsAcceleration);
173-
const avg = cpsAccelerationArr.reduce((a, b) => a + b, 0) / buildings.length;
174-
let color = (
175-
chroma.scale(["red", "yellow", "lightgreen"])
176-
.mode("lrgb")
177-
.domain([Math.min(...cpsAccelerationArr), MOD.median(cpsAccelerationArr), Math.max(...cpsAccelerationArr)])
178-
);
179-
240+
// Notation for buildings
180241
for (const i in buildings) {
181242
let me = buildings[i];
182-
let cpspb = document.querySelector("#normalizedCpspb" + me.id);
183-
if (!cpspb) {
184-
cpspb = document.createElement("span");
185-
cpspb.setAttribute("id", "normalizedCpspb" + me.id);
186-
cpspb.style.fontWeight = "bolder";
187-
MOD.insertAfter(cpspb, l("productPrice" + me.id));
243+
let span = document.querySelector("#productAcc" + me.id);
244+
if (!span) {
245+
span = document.createElement("span");
246+
span.id = "productAcc" + me.id;
247+
span.style.fontWeight = "bolder";
248+
span.style.display = "block";
249+
MOD.insertAfter(span, l("productPrice" + me.id));
188250
}
189-
cpspb.textContent = " 💹" + Beautify(me.cpsAcceleration * 100 / avg, 2) + "%";
190-
cpspb.style.color = color(me.cpsAcceleration);
251+
span.textContent = " 💹" + Beautify(me.cpsAcceleration * 100 / avg, 2) + "%";
252+
span.style.color = color(me.cpsAcceleration);
253+
254+
}
191255

256+
// Sort buildings (or leave them as default)
257+
if (MOD.config.sortbuildings) {
258+
buildings.sort((a, b) => b.cpsAcceleration - a.cpsAcceleration);
259+
}
260+
// Only sort when the order is different
261+
let buildings_order = buildings.map(e => e.id);
262+
if (!buildings_order.every((value, index) => value === MOD.last_buildings_order[index])) {
263+
let store = document.querySelector("#products");
264+
for (let i = 0; i < buildings.length; ++i) {
265+
store.appendChild(buildings[i].l);
266+
}
267+
MOD.last_buildings_order = buildings_order;
192268
}
193269
},
194270

@@ -206,6 +282,8 @@ let BestDealHelper = {
206282
l(button).className = value ? "option" : "option off";
207283
PlaySound("snd/tick.mp3");
208284
},
285+
286+
209287
};
210288

211289
// Bind methods
@@ -214,6 +292,7 @@ for (func of Object.getOwnPropertyNames(BestDealHelper).filter(m => typeof BestD
214292
* @typedef {string} func
215293
*/
216294
BestDealHelper[func] = BestDealHelper[func].bind(BestDealHelper);
295+
217296
}
218297

219298
// Load mod
@@ -228,3 +307,4 @@ if (!BestDealHelper.isLoaded) {
228307
CCSE.postLoadHooks.push(BestDealHelper.register);
229308
}
230309
}
310+

0 commit comments

Comments
 (0)