Repository navigation
Expand file tree
/
Copy pathdownosaurBot.js
More file actions
149 lines (130 loc) · 4.08 KB
/
Copy pathdownosaurBot.js
File metadata and controls
149 lines (130 loc) · 4.08 KB
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
var context = null;
var isActing = false;
var isGameOver = false;
var obstacleCheckInterval = null;
var scoreTimes = [];
var cactusCount = 0;
var birdCount = 0;
function checkSite() {
var canvas = document.getElementsByClassName('runner-canvas');
if (canvas && canvas[0] && canvas[0].getContext) {
context = canvas[0].getContext('2d');
if (context != null) {
console.log('run downosaur, run!');
runDownosaurRun();
return;
}
}
console.log('downosaur extinct!');
}
function checkObstable() {
if (isActing) return;
birdCount = cactusCount = 0;
var cactus = context.getImageData(120, 120, 1, 1);
var brid = context.getImageData(120, 95, 1, 1);
var isCactus = cactus.data[0] != 0;
var isBird = brid.data[0] != 0;
if (isBird && !isCactus) {
isActing = true;
context.fillRect(120, 95, 5, 5);
console.log('bird!');
birdCount++;
dispatchEventDown();
} else if (isCactus) {
isActing = true;
context.fillRect(120, 120, 5, 5);
console.log('cactus!');
cactusCount++;
dispatchEventUp();
} else {
var game = context.getImageData(300, 100, 1, 1).data[0] != 0;
var oVer = context.getImageData(340, 47, 1, 1).data[0] != 0;
if (game && oVer) {
if (isGameOver) {
return;
}
isGameOver = true;
clearInterval(obstacleCheckInterval);
var timeRun = new Date() - startTime;
scoreTimes.push({ timeRun, cactusCount, birdCount });
console.log("game over. downosaur runned for: " + timeRun / 1000 + " sec. let him run again.");
setTimeout(runDownosaurRun, 4000);
}
}
}
function printScoreTimes() {
scoreTimes.forEach(function (item, idx) {
console.log(idx + ': ' + item.timeRun / 100 + ' sec. Jumped ' + item.cactusCount + ' cactus, ducked ' + tiem.birdCount + ' birds! ');
})
}
function stopIt() {
clearInterval(obstacleCheckInterval);
}
function runDownosaurRun() {
dispatchEventUp();
startTime = new Date();
setTimeout(fancyStartMove, 1200);
setTimeout(fancyStartMove, 1600);
setTimeout(fancyStartMove, 2000);
setTimeout(fancyStartMove, 2400);
setTimeout(fancyStartMove, 2800);
//give time for timeout to vanish
setTimeout(function () {
obstacleCheckInterval = setInterval(checkObstable, 20);
isGameOver = false;
}, 400);
}
function fancyStartMove() {
document.dispatchEvent(KeyDown_Press);
setTimeout(function () { document.dispatchEvent(KeyDown_Release) }, 100);
}
function dispatchEventUp() {
//console.log('press up');
document.dispatchEvent(KeyUp_Press);
setTimeout(dispatchEventUpEnd, 300);
}
function dispatchEventUpEnd() {
//console.log('release up');
document.dispatchEvent(KeyUp_Release);
isActing = false;
}
function dispatchEventDown() {
//console.log('press down');
document.dispatchEvent(KeyDown_Press);
setTimeout(dispatchEventDownEnd, 200);
}
function dispatchEventDownEnd() {
//console.log('release down');
document.dispatchEvent(KeyDown_Release);
isActing = false;
}
//http://stackoverflow.com/questions/10455626/keydown-simulation-in-chrome-fires-normally-but-not-the-correct-key/10520017#10520017
var createKeyboardEvent = function (k, keyboarEventName) {
var oEvent = document.createEvent('KeyboardEvent');
// Chromium Hack
Object.defineProperty(oEvent, 'keyCode', {
get: function () {
return this.keyCodeVal;
}
});
Object.defineProperty(oEvent, 'which', {
get: function () {
return this.keyCodeVal;
}
});
if (oEvent.initKeyboardEvent) {
oEvent.initKeyboardEvent(keyboarEventName, true, true, document.defaultView, false, false, false, false, k, k);
} else {
oEvent.initKeyEvent(keyboarEventName, true, true, document.defaultView, false, false, false, false, k, 0);
}
oEvent.keyCodeVal = k;
if (oEvent.keyCode !== k) {
alert("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
}
return oEvent;
}
var KeyDown_Press = createKeyboardEvent('40', 'keydown');
var KeyDown_Release = createKeyboardEvent('40', 'keyup');
var KeyUp_Press = createKeyboardEvent('38', 'keydown');
var KeyUp_Release = createKeyboardEvent('38', 'keyup');
setTimeout(checkSite, 1000);