forked from RNabla/Tribalwars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HermitowskaGiełda.js
185 lines (184 loc) · 7.37 KB
/
HermitowskaGiełda.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
!(function () {
if (window.PremiumExchangeTracker) {
return;
}
window.PremiumExchangeTracker = {
resources: ['wood', 'stone', 'iron'],
dictionary: {
'wood' : '6e4dd7ce4ea3c1d4a90edb289e22da98',
'stone' : 'ed5eace1bd098cdced7685864b09c291',
'iron' : 'cefa8a9606819ed409dc761ca6080887'
},
ev : {
global: 0,
local : 0,
},
icon: '',
init: function () {
PremiumExchangeTracker.CheckPermission();
PremiumExchangeTracker.SetUpIcon();
},
SetUpIcon: function () {
let lvl = game_data.village.buildings.market;
let iconId = 1;
if (lvl >= 5)
iconId = 2;
else if (lvl >= 20)
iconId = 3;
PremiumExchangeTracker.icon = `${image_base}big_buildings/market${iconId}.png`;
},
SpawnNotification: function (theBody) {
let options = {
body: theBody,
icon: PremiumExchangeTracker.icon
};
let n = new Notification(`Hermitowska Giełda - ${game_data.world}`, options);
setTimeout(n.close.bind(n), 8000);
},
DisplaySample: function () {
PremiumExchangeTracker.SpawnNotification('Tak będzie wyglądać przykładowe okienko');
},
Inject: function () {
PremiumExchange.receiveData = (function () {
return function (arguments) {
if (arguments.hasOwnProperty('bot_protect') || !arguments.hasOwnProperty('stock') ) {
PremiumExchangeTracker.SpawnNotification('Wymagane zatwierdzenie captchy');
}
else{
PremiumExchange.data= arguments;
PremiumExchange.updateUI();
PremiumExchangeTracker.Analyze();
}
};
})();
},
Analyze: function () {
let gIncome = PremiumExchangeTracker.GlobalIncome();
let lIncome = PremiumExchangeTracker.OptimalTrade();
if (lIncome.totalIncome > 0) {
PremiumExchangeTracker.Announce(lIncome);
console.log(gIncome);
}
else {
if (PremiumExchangeTracker.Stock('wood') > 0 || PremiumExchangeTracker.Stock('iron') > 0 || PremiumExchangeTracker.Stock('stone') > 0) {
console.log('upsi');
}
}
},
Execute: function () {
PremiumExchangeTracker.Inject();
},
Exchanges: function (res, limit) {
let exchanges = [];
let stock_limit = PremiumExchange.data.capacity[res] - PremiumExchange.data.stock[res];
limit = Math.min(limit, stock_limit);
let lastIncome = -1;
for (let i = 0; i < limit; i++) {
let income = -Math.ceil(PremiumExchange.calculateCost(res, -i));
if (lastIncome < income) {
lastIncome = income;
exchanges.push(i);
}
}
if (limit === stock_limit) {
let income = PremiumExchangeTracker.VaBank(res);
exchanges[income] = stock_limit;
}
return exchanges;
},
GlobalIncome: function () {
let exchange = [];
let income = [];
for (const res of PremiumExchangeTracker.resources) {
exchange.push(PremiumExchangeTracker.Stock(res));
income.push(PremiumExchangeTracker.VaBank(res));
}
return {
totalIncome: income.reduce((a, b) => a + b, 0),
income: income,
exchange: exchange
};
},
VaBank: function (res) {
return -Math.floor(PremiumExchange.calculateCost(res, -PremiumExchangeTracker.Stock(res)));
},
Stock: function (res) {
return PremiumExchange.data.capacity[res] - PremiumExchange.data.stock[res];
},
BruteForce: function (woodCount, stoneCount, ironCount, transport) {
let wood = PremiumExchangeTracker.Exchanges('wood', woodCount);
let stone = PremiumExchangeTracker.Exchanges('stone', stoneCount);
let iron = PremiumExchangeTracker.Exchanges('iron', ironCount);
let max = -1;
let sum = -1;
let exchange = [];
let income = [];
for (let w = 0; w < wood.length; w++) {
sum = Math.ceil(wood[w]/1000)*1000;
for (let s = 0; s < stone.length; s++) {
sum += Math.ceil(stone[s]/1000)*1000;
if (sum > transport)
break;
for (let i = 0; i < iron.length; i++) {
sum += Math.ceil(iron[i]/1000)*1000;
if (sum > transport)
break;
let pp = w + s + i;
if (max < pp) {
income = [w, s, i];
exchange = [wood[w], stone[s], iron[i]];
max = pp;
}
sum -= Math.ceil(iron[i]/1000)*1000;
}
sum -= Math.ceil(stone[s]/1000)*1000;
}
}
return {
totalIncome: max,
income: income,
exchange: exchange
}
},
OptimalTrade: function () {
let available = [];
let transport = PremiumExchange.data.merchants * 1000;
for (const res of PremiumExchangeTracker.resources) {
available.push(Math.min(...[
transport,
game_data.village[res],
PremiumExchangeTracker.Stock(res)
]));
}
return PremiumExchangeTracker.BruteForce(...available, transport);
},
CheckPermission: function () {
if (window.Notification && window.Notification.permission !== 'granted') {
UI.InfoMessage('Powinno wyskoczyć okienko z pytaniem o możliwość wyświetlania powiadomień');
}
window.Notification.requestPermission().then(result => {
switch (result) {
case 'granted':
UI.SuccessMessage('Script online');
PremiumExchangeTracker.DisplaySample();
this.Execute();
break;
default:
UI.ErrorMessage('Brak pozwolenia. Kończę pracę');
break;
}
});
},
Announce: function (income) {
let text = `Możliwy zarobek: ${income.totalIncome}\r\n`;
for (const i in PremiumExchangeTracker.resources){
let res = window.lang[PremiumExchangeTracker.dictionary[PremiumExchangeTracker.resources[i]]];
let xchg = income.exchange[i];
let inc = income.income[i];
text += `${res} : ${xchg} -> ${inc} PP\r\n`;
}
PremiumExchangeTracker.SpawnNotification(text);
}
};
window.PremiumExchangeTracker.init();
})();