-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookieClickerScript.js
135 lines (117 loc) · 3.17 KB
/
cookieClickerScript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var _BIG_COOKIE = $("#bigCookie")
var _CURRENT_PRODUCT_STATE = undefined
var _NEXT_POWER = $(".crate.upgrade")
var minute = 1000 * 60
function clickIfNotNull(el) {
if(el) el.click()
}
function getBigCookie(jq) {
if (!_BIG_COOKIE) {
_BIG_COOKIE = jq("#bigCookie")
}
return _BIG_COOKIE
}
function clickCookie(jq) {
getBigCookie().click()
setTimeout(function(){
clickCookie(jq),
19
})
}
function buyProducts() {
setTimeout(function(){buyNextItem()})
setTimeout(function(){buyProducts()}, 50)
}
function buyPowers(jq) {
setTimeout(function(){clickIfNotNull(_NEXT_POWER)})
setTimeout(function(){buyPowers(jq)}, 45)
}
function clickShimmer(jq) {
clickIfNotNull(jq(".shimmer"))
clickIfNotNull(jq("#shimmer"))
setTimeout(function(){clickShimmer(jq)}, 14)
}
function powerPoll(jq) {
_NEXT_POWER = jq(".crate.upgrade")
setTimeout(function(){powerPoll(jq)}, 2000)
}
function productPoll(jq) {
findNextProductBuy(jq)
setTimeout(function(){productPoll(jq)}, 2000)
}
function getChildren(jq) {
return [].slice.call(jq("#products").children)
}
function findNextProductBuy(jq) {
// get all child elements in the buildings list
var children = getChildren(jq)
var price2indexAndMin = makePrice2IndexAndMin(jq, children)
var price2index = price2indexAndMin.price2index
var min = price2indexAndMin.min
var validProducts = filterProducts(price2index, min)
nextBuy = getBestBuy(validProducts, price2index)
setCurrentState(min, nextBuy, price2index, children)
}
function setCurrentState(min, nextBuy, price2index, children) {
_CURRENT_PRODUCT_STATE = {
min: min,
nextBuy: nextBuy,
price2index: price2index,
children: children,
}
}
function buyNextItem() {
if(_CURRENT_PRODUCT_STATE) {
var children = _CURRENT_PRODUCT_STATE.children
var nextBuy = _CURRENT_PRODUCT_STATE.nextBuy
var price2index = _CURRENT_PRODUCT_STATE.price2index
children[price2index[nextBuy]].click()
}
}
function makePrice2IndexAndMin(jq, children) {
var price2index = {}
var min = -1
// start at 1 to skip first child
for(var i = 1; i < children.length; i++) {
var price = getProductPrice(jq, i)
price2index[price] = i
if (price < min || min == -1) {
min = price
}
}
return {price2index: price2index, min: min}
}
function getProductPrice(jq, index) {
return Number(jq("#productPrice" + String(index-1)).textContent.split(",").join(""))
}
function filterProducts(price2index, min) {
var validValues = []
for (key in price2index) {
if (min * 10 > key) validValues.push(key)
}
return validValues
}
function getBestBuy(validProducts, price2index) {
bestBuy = undefined
// oops! my buys are all out of order,
// I'll be getting clickers forever at this rate
// buy the highest index of any item cheaper than the
// selected item
for (idx in validProducts) {
var price = validProducts[idx]
if (!bestBuy) bestBuy = price
if (price2index[price] > price2index[bestBuy]) {
bestBuy = price
}
}
return bestBuy
}
function getProducts(jq) {
return jq("#products").children
}
productPoll($)
powerPoll($)
clickCookie($)
buyProducts($)
buyPowers($)
clickShimmer($)