· 8 years ago · May 02, 2018, 08:46 PM
1js_strict(true);
2
3include('gamemanager.dbl');
4
5
6
7var _badGames = ["cha","kha","cbaal","kaos","c baal","k baal","d2baal","diabaal","koas","coas",
8 "caos","cnbaal","knbaal","c-baal","cs"];
9
10function main()
11{
12 var gameCount = 1,
13 db = new SQLite('runstats.sqlite'),
14 profile = '',
15 gamePrefix = '',
16 password = '',
17 mode = '',
18 gamesPerHour = 0,
19 difficulty = 0;
20
21 db.execute('CREATE TABLE IF NOT EXISTS "main"."runstats" (' +
22 '"start" DATETIME NOT NULL,' +
23 '"end" DATETIME,' +
24 '"status" TEXT NOT NULL,' +
25 '"game" TEXT NOT NULL,' +
26 '"pass" TEXT NOT NULL,' +
27 '"profile" TEXT NOT NULL);');
28 db.execute('CREATE TABLE IF NOT EXISTS "main"."config" (' +
29 '"name" TEXT NOT NULL, "value" TEXT, "profile" TEXT NOT NULL);');
30
31 var startrun = db.query('INSERT INTO runstats (start, status, profile, game, pass) VALUES (?, ?, ?, ?, ?);');
32 var newrun = db.query('UPDATE runstats SET start=?, status=? WHERE _ROWID_=last_insert_rowid();');
33 var endrun = db.query('UPDATE runstats SET end=?, status=? WHERE _ROWID_=last_insert_rowid();');
34 var firstrun = db.query("SELECT strftime('%s', start, 'utc'), _ROWID_ AS id FROM runstats WHERE start>=? AND start<=? AND profile=? ORDER BY start DESC LIMIT 1;");
35 var numruns = db.query('SELECT _ROWID_+1 AS run FROM runstats ORDER BY run DESC LIMIT 1;');
36
37 addEventListener("scriptmsg", function (check, p) {
38 if(check == 'profile' && p != undefined && typeof(p) == 'string' && p != '') {
39 profile = p;
40 removeEventListener("scriptmsg", arguments.callee);
41 }
42 });
43 GameManager.requestProfile();
44 sendCopyData(null, "OOG", 0,"ShowWindow 6" ); //OOG compatablity,
45 while(profile == ''){
46 locationAction( getLocation() );
47 sendCopyData(null, "OOG", 0,"Move "+getLocation() );
48 delay(1000);
49 }
50
51 var q = db.query('SELECT name, value FROM config WHERE profile = ?');
52 q.bindAll(profile);
53 while(q.next())
54 {
55 switch(q.getColumnValue(0))
56 {
57 case 'gamePrefix': gamePrefix = q.getColumnValue(1); break;
58 case 'password': password = q.getColumnValue(1); break;
59 case 'mode': mode = q.getColumnValue(1); break;
60 case 'gamesPerHour': gamesPerHour = q.getColumnValue(1); break;
61 case 'difficulty': difficulty = q.getColumnValue(1); break;
62 }
63 }
64
65 while(true)
66 {
67 // wake up and check every 1 second for out of game
68 while(me.ingame) delay(1000);
69 delay(3000); // let the game catch up
70
71 // if we finished a run, update the db
72 if(db.lastRowId != 0)
73 {
74 // we finished a run
75 endrun.bindAll(new Date(), 'finished');
76 endrun.next();
77 endrun.reset();
78 }
79
80 switch(getLocation())
81 {
82 case 1: // lobby
83 case 3: // chat
84 makeGame();
85 break;
86 case 24: case 26: case 28: // failed to join of some form or another
87 GameManager.sendMessage('Failed to join the game, waiting 5 minutes');
88 delay(300000);
89 break;
90 case 13: // realm down
91 GameManager.sendMessage('Got restricted, waiting 2 hours');
92 restart(120);
93 break;
94 case 8: // main menu
95 case 18: // splash screen
96 login(profile);
97 break;
98 case 21: // "Connecting" screen
99 case 25: // "please wait" screen
100 while(getLocation() == 25 || getLocation() == 21)
101 delay(350);
102 break;
103 case 22: // invalid cd key
104 GameManager.sendMessage('Your CD Key appears to be invalid, restarting in 5 minutes');
105 restart(5);
106 break;
107 case 16: // "please wait" on char select screen
108 case 9: // login screen
109 case 10: // bailed out during login()
110 GameManager.sendMessage('Failed to log in! Check your profile settings and try again.');
111 GameManager.reload = false;
112 restart(1);
113 break;
114 default: // anywhere else
115 // are we really lost, or just think we're out of game?
116 if(me.ingame)
117 break;
118
119 var control = getControl();
120 var msg = 'The bot got lost? Location is ' + getLocation() + ', first control is ' +
121 (control == undefined || control == null ? 'undefined' : control.toSource()) +
122 '. Please report this as an error (copy/paste this entire line with your report).';
123 GameManager.sendMessage(msg);
124 GameManager.sendMessage('Restarting...');
125 restart(1);
126 break;
127 }
128 }
129
130 function makeGame() {
131 if(gameCount % gamesPerHour == 0)
132 {
133 var time = getRemainingTime();
134 GameManager.sendMessage('Waiting for ' + parseInt(time/1000, 10) +
135 ' seconds before going to the next game (games per hour delay).');
136 delay(time);
137 }
138 switch(mode)
139 {
140 case 'create':
141 var wait = rand(10, 15);
142 GameManager.sendMessage('Waiting ' + wait + ' seconds before making the next game');
143 delay(wait*1000);
144 var name = (gamePrefix == 'random' ? makerand() : gamePrefix + '-' + getRunNumber());
145 var pass = (password == 'random' ? makerand() : password);
146 GameManager.sendMessage('Creating game ' + name + '/' + pass);
147 startrun.bindAll(new Date(), 'creating', profile, name, pass);
148 startrun.next();
149 startrun.reset();
150 createGame(name, pass, difficulty);
151 gameCount++;
152 break;
153 case 'follow':
154 print('Waiting for a game to join');
155 addEventListener("scriptmsg", function (check, game, pass) {
156 if(check != 'joingame' &&
157 game != undefined && typeof(game) == 'string' && game != '' &&
158 pass != undefined && typeof(pass) == 'string')
159 {
160 GameManager.sendMessage('Joining game ' + name + '/' + pass);
161 startrun.bindAll(new Date(), 'joining', profile, name, pass);
162 startrun.next();
163 startrun.reset();
164 joinGame(game, pass);
165 removeEventListener("scriptmsg", arguments.callee);
166 }
167 });
168 while(!me.ingame) delay(1000);
169 break;
170 default:
171 GameManager.sendMessage('Invalid mode!');
172 break;
173 }
174
175 // wait for the potential line
176 while(getLocation() == 2)
177 delay(500);
178
179 // wait for game join
180 for(let i = 0; i < 30; i++)
181 {
182 delay(1000)
183 if(me.ingame)
184 {
185 newrun.bindAll(new Date(), 'joined');
186 newrun.next();
187 newrun.reset();
188 GameManager.sendMessage('Game joined successfully');
189 break;
190 }
191 }
192 if(!me.ingame)
193 {
194 newrun.bindAll(new Date(), 'failed to join');
195 newrun.next();
196 newrun.reset();
197 GameManager.sendMessage('Waiting 5 minutes due to failed join');
198 delay(300000);
199 }
200 }
201
202 function makerand() { return md5(getTickCount()+rand(1,150000)).substr(rand(0, 17), rand(5, 15)); }
203 function getRemainingTime() {
204 // find the first run this hour so we can determine how long to delay for
205 var now = new Date();
206 var y = now.getFullYear(),
207 m = now.getMonth(),
208 d = now.getDate(),
209 h = now.getHours();
210 var begin = new Date(y, m, d, h),
211 end = new Date(y, m, d, h);
212 end.setHours(h+1);
213
214 firstrun.bindAll(begin, end, profile);
215 if(!firstrun.next()) {
216 GameManager.sendMessage('No rows found?! Waiting an hour, then...');
217 firstrun.reset();
218 return 3600000;
219 }
220
221 var start = new Date(parseInt(firstrun.getColumnValue(0), 10)*1000);
222 firstrun.reset();
223 start.setHours(start.getHours()+1);
224 now = new Date();
225 if(start.getTime() > now.getTime())
226 return (start - now);
227 GameManager.sendMessage('The delay is in the past?! Waiting an hour, then...');
228 return 3600000;
229 }
230 function getRunNumber() {
231 if(!numruns.next())
232 return 1;
233 var num = numruns.getColumnValue(0);
234 numruns.reset();
235 return num;
236 }
237 function restart(duration) {
238 delay(duration*60*1000);
239 quitGame();
240 }
241}
242
243DBStatement.prototype.bindAll = function bindAll() {
244 for(var i = 0; i < arguments.length; i++)
245 if(arguments[i] instanceof Date)
246 this.bind(i+1, arguments[i].toDBString());
247 else
248 this.bind(i+1, arguments[i]);
249};
250
251Date.prototype.toDBString = function () {
252 function prefix(x) { return (x < 10 ? "0"+x : x); }
253
254 var year = this.getFullYear(),
255 month = prefix(this.getMonth()+1),
256 day = prefix(this.getDate()),
257 hour = prefix(this.getHours()),
258 min = prefix(this.getMinutes()),
259 sec = prefix(this.getSeconds()),
260 msec = this.getMilliseconds();
261 msec = (msec < 10 ? "00" + msec : (msec < 100 ? "0" + msec : msec));
262 return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec + "." + msec;
263};
264function locationAction( location ) { // oog location actions
265 switch (location) {
266
267 case 3: // Chat
268 var con =getControl(4,28,410,354,298);
269 if(con){
270 var lin=con.getText();
271 sendCopyData(null, "OOG", 0,"Chat "+lin );
272 }
273 break;
274
275 case 5: //Join Game
276
277 delay(5000);
278
279 var con =getControl(4,432,393,160,173);
280 if (con){
281 var lin=con.getText();
282
283 var data;
284 var arr = [];
285 var _lin = "";
286
287 var _cur;
288 var _shouldAdd = true;
289 var _x = 0;
290
291 if(lin && lin.length > 0){
292
293 data = new String(lin);
294 arr = data.split(",");
295
296 for(var _a = 0; _a<arr.length;_a++){
297
298 _cur = arr[_a].toLowerCase();
299 _shouldAdd = true;
300
301 for(_x = 0; _x < _badGames.length && _shouldAdd; _x++){
302 if(_badGames[_x] != "" && _cur.indexOf(_badGames[_x]) != -1){
303 _shouldAdd = false;
304 }
305 }
306
307 if(_shouldAdd)
308 _lin += ((_a+1)<arr.length) ? arr[_a] + "," : arr[_a];
309
310 }
311
312 lin = _lin;
313
314 }
315
316 sendCopyData(null, "OOG", 0,"Join "+lin );
317 }
318 break;
319
320 case 11: // unable to connect
321 //delay(300000); //5 min delay
322 //quitGame();
323
324 }
325}