2222 * @typedef {Object } Game
2323 * @property {function } registerMod
2424 * @property {function } Has
25+ * @property {function } CalculateGains
2526 * @property {Object[] } GrandmaSynergies
2627 * @property {Object } GrandmaSynergies.buildingTie
2728 * @property {number } GrandmaSynergies.buildingTie.storedTotalCps
3334 * @property {number } unbuffedCps
3435 * @property {number } globalCpsMult
3536 * @property {number } storedCps
37+ * @property {number } cookies
38+ * @property {number } cookiesPs
3639 */
3740/**
3841 * @typedef {Object } App
@@ -53,6 +56,7 @@ let BestDealHelper = {
5356
5457 isLoaded : false ,
5558 load_chroma : false ,
59+ loopCount : 0 ,
5660
5761 register : function ( ) {
5862 Game . registerMod ( this . name , this ) ;
@@ -74,7 +78,7 @@ let BestDealHelper = {
7478 "load" : function ( str ) {
7579 const config = JSON . parse ( str ) ;
7680 for ( const c in config ) MOD . config [ c ] = config [ c ] ;
77- MOD . sortBuildings ( ) ;
81+ MOD . sortDeals ( ) ;
7882 } ,
7983
8084 "save" : function ( ) {
@@ -92,36 +96,19 @@ let BestDealHelper = {
9296 } ,
9397
9498 logicLoop : function ( ) {
99+ MOD . loopCount ++ ;
95100 if (
96- MOD . last_cps !== Game . unbuffedCps
101+ MOD . loopCount >= 10
102+ || MOD . last_cps !== Game . cookiesPs
97103 || MOD . config . sortbuildings !== MOD . last_config_sortbuildings
98104 || ! document . querySelector ( "#normalizedCpspb0" )
99105 ) {
100- MOD . sortBuildings ( ) ;
106+ MOD . sortDeals ( ) ;
107+ MOD . loopCount = 0 ;
101108 MOD . last_config_sortbuildings = MOD . config . sortbuildings ;
102- MOD . last_cps = Game . unbuffedCps ;
109+ MOD . last_cps = Game . cookiesPs ;
103110 }
104111 } ,
105- boosted : function ( me ) {
106- let boost ;
107- let other ;
108- let synergyBoost = 0 ;
109- if ( me . name === "Grandma" ) {
110- for ( const i in Game . GrandmaSynergies ) {
111- if ( Game . Has ( Game . GrandmaSynergies [ i ] ) ) {
112- other = Game . Upgrades [ Game . GrandmaSynergies [ i ] ] . buildingTie ;
113- const mult = me . amount * 0.01 * ( 1 / ( other . id - 1 ) ) ;
114- boost = ( other . storedTotalCps * Game . globalCpsMult ) - ( other . storedTotalCps * Game . globalCpsMult ) / ( 1 + mult ) ;
115- synergyBoost += boost ;
116- }
117- }
118- } else if ( me . name === "Portal" && Game . Has ( "Elder Pact" ) ) {
119- other = Game . Objects [ "Grandma" ] ;
120- boost = ( me . amount * 0.05 * other . amount ) * Game . globalCpsMult ;
121- synergyBoost += boost ;
122- }
123- return me . storedCps * Game . globalCpsMult + synergyBoost / Math . max ( me . amount , 1 ) ;
124- } ,
125112 median : function ( values ) {
126113 if ( values . length === 0 ) return 0 ;
127114
@@ -139,15 +126,34 @@ let BestDealHelper = {
139126 referenceNode . parentNode . insertBefore ( newNode , referenceNode . nextSibling ) ;
140127 } ,
141128
142- sortBuildings : function ( ) {
143- let buildings = [ ...Game . ObjectsById ] ;
144- buildings . forEach ( e => e . cpsPerCookie = MOD . boosted ( e ) / e . price ) ;
129+ sortDeals : function ( ) {
130+ let enabledBuildings = Game . ObjectsById . map ( e => + ! e . locked ) . reduce ( ( a , b ) => a + b ) + 2 ;
131+ let buildings = [ ...Game . ObjectsById ] . filter ( o => o . id < enabledBuildings ) ;
132+
133+ buildings . forEach ( function ( me ) {
134+ Game . timedout = true ;
135+ me . amount ++ ;
136+ Game . CalculateGains ( ) ;
137+ let newCookiesPs = Game . cookiesPs ;
138+ me . amount -- ;
139+ Game . CalculateGains ( ) ;
140+ Game . timedout = false ;
141+
142+ let deltaTime ;
143+ if ( me . price > Game . cookies ) {
144+ deltaTime = ( me . price - Game . cookies ) / Game . cookiesPs + Game . cookies / newCookiesPs ;
145+ } else {
146+ deltaTime = me . price / newCookiesPs ;
147+ }
148+
149+ let deltaCps = newCookiesPs - Game . cookiesPs ;
150+ return me . cpsAcceleration = deltaCps / deltaTime ;
151+ } ) ;
145152
146153 // Sort buildings or leave them to default
147154 if ( MOD . config . sortbuildings ) {
148155 buildings . sort ( function ( a , b ) {
149- if ( a . locked ) return 1 ;
150- return ( a . cpsPerCookie === b . cpsPerCookie ) ? 0 : ( a . cpsPerCookie < b . cpsPerCookie ? 1 : - 1 ) ;
156+ return ( a . cpsAcceleration === b . cpsAcceleration ) ? 0 : ( a . cpsAcceleration < b . cpsAcceleration ? 1 : - 1 ) ;
151157 } ) ;
152158 }
153159
@@ -163,13 +169,12 @@ let BestDealHelper = {
163169 }
164170
165171 // Normalization by Mean
166- buildings = buildings . filter ( o => o . locked === 0 ) ;
167- const cpsPerCookieArr = buildings . map ( e => e . cpsPerCookie ) ;
168- const avg = cpsPerCookieArr . reduce ( ( a , b ) => a + b , 0 ) / buildings . length ;
172+ const cpsAccelerationArr = buildings . map ( e => e . cpsAcceleration ) ;
173+ const avg = cpsAccelerationArr . reduce ( ( a , b ) => a + b , 0 ) / buildings . length ;
169174 let color = (
170175 chroma . scale ( [ "red" , "yellow" , "lightgreen" ] )
171176 . mode ( "lrgb" )
172- . domain ( [ Math . min ( ...cpsPerCookieArr ) , MOD . median ( cpsPerCookieArr ) , Math . max ( ...cpsPerCookieArr ) ] )
177+ . domain ( [ Math . min ( ...cpsAccelerationArr ) , MOD . median ( cpsAccelerationArr ) , Math . max ( ...cpsAccelerationArr ) ] )
173178 ) ;
174179
175180 for ( const i in buildings ) {
@@ -181,8 +186,8 @@ let BestDealHelper = {
181186 cpspb . style . fontWeight = "bolder" ;
182187 MOD . insertAfter ( cpspb , l ( "productPrice" + me . id ) ) ;
183188 }
184- cpspb . textContent = "( 💹" + Beautify ( me . cpsPerCookie * 100 / avg , 1 ) + "%) " ;
185- cpspb . style . color = color ( me . cpsPerCookie ) ;
189+ cpspb . textContent = " 💹" + Beautify ( me . cpsAcceleration * 100 / avg , 2 ) + "%" ;
190+ cpspb . style . color = color ( me . cpsAcceleration ) ;
186191
187192 }
188193 } ,
0 commit comments