-
Notifications
You must be signed in to change notification settings - Fork 0
/
maindeal.js
401 lines (371 loc) · 14.8 KB
/
maindeal.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// ==UserScript==
// @name Mah Darn MiniMap
// @version 1.69.420
// @description Pixelcanvas Minimap for the Bot Bruddas
// @author Crunchy Potato
// @match https://pixelcanvas.io/*
// @match http://pixelcanvas.io/*
// @grant none
// @updateURL https://raw.githubusercontent.com/TukKeedar/TukKeedarMap/master/maindeal.js
// @downloadURL https://raw.githubusercontent.com/TukKeedar/TukKeedarMap/master/maindeal.js
// ==/UserScript==
window.baseTepmlateUrl = 'https://raw.githubusercontent.com/TukKeedar/TukKeedarMap/master';
window.addEventListener('load', function () {
//Regular Expression to get coordinates out of URL
re = /(.*)@(.*),(.*)/g;
//Regular Expression to get coordinates from cursor
rec = /\((.*), (.*)\)/g;
gameWindow = document.getElementById("gameWindow");
//DOM element of the displayed X, Y variables
coorDOM = null;
findCoor();
//coordinates of the middle of the window
x_window = 0;
y_window = 0;
//coordinates of cursor
x = 0;
y = 0;
//list of all available templates
template_list = null;
zoomlevel = 9;
//toggle options
toggle_show = true;
toggle_follow = true; //if minimap is following window, x_window = x and y_window = y;
zooming_in = false;
zooming_out = false;
zoom_time = 100;
//array with all loaded template-images
image_list = [];
counter = 0;
//templates which are needed in the current area
needed_templates = null;
//Cachebreaker to force refresh
cachebreaker = null;
var div = document.createElement('div');
div.setAttribute('class', 'post block bc2');
div.innerHTML = '<div id="minimapbg" style="position: absolute; right: 1em; bottom: 1em;">' +
'<div class="posy" id="posyt" style="background-color: rgba(0, 0, 0, 0.75); color: rgb(250, 250, 250); text-align: center; line-height: 42px; vertical-align: middle; width: auto; height: auto; border-radius: 21px; padding: 6px;">' +
'<div id="minimap-text" style="display: none;"></div>' +
'<div id="minimap-box" style="position: relative;width:420px;height:300px">' +
'<canvas id="minimap" style="width: 100%; height: 100%;z-index:1;position:absolute;top:0;left:0;"></canvas>' +
'<canvas id="minimap-board" style="width: 100%; height: 100%;z-index:2;position:absolute;top:0;left:0;"></canvas>' +
'<canvas id="minimap-cursor" style="width: 100%; height: 100%;z-index:3;position:absolute;top:0;left:0;"></canvas>' +
'</div><div id="minimap-config" style="line-height:20px;">' +
'<span id="hide-map" style="cursor:pointer;">Hide Map' +
'</span> | <span id="follow-mouse" style="cursor:pointer;">Follow Mouse' +
'</span> | Zoom: <span id="zoom-plus" style="cursor:pointer;font-weight:bold;">+</span> / ' +
'<span id="zoom-minus" style="cursor:pointer;font-weight:bold;">-</span>' +
'</div>' +
'</div>';
document.body.appendChild(div);
minimap = document.getElementById("minimap");
minimap_board = document.getElementById("minimap-board");
minimap_cursor = document.getElementById("minimap-cursor");
minimap.width = minimap.offsetWidth;
minimap_board.width = minimap_board.offsetWidth;
minimap_cursor.width = minimap_cursor.offsetWidth;
minimap.height = minimap.offsetHeight;
minimap_board.height = minimap_board.offsetHeight;
minimap_cursor.height = minimap_cursor.offsetHeight;
ctx_minimap = minimap.getContext("2d");
ctx_minimap_board = minimap_board.getContext("2d");
ctx_minimap_cursor = minimap_cursor.getContext("2d");
//No Antialiasing when scaling!
ctx_minimap.mozImageSmoothingEnabled = false;
ctx_minimap.webkitImageSmoothingEnabled = false;
ctx_minimap.msImageSmoothingEnabled = false;
ctx_minimap.imageSmoothingEnabled = false;
drawBoard();
drawCursor();
document.getElementById("hide-map").onclick = function () {
// console.log("This should do something, but it doesn't");
toggle_show = false;
document.getElementById("minimap-box").style.display = "none";
document.getElementById("minimap-config").style.display = "none";
document.getElementById("minimap-text").style.display = "block";
document.getElementById("minimap-text").innerHTML = "Show Map";
document.getElementById("minimap-text").style.cursor = "pointer";
};
document.getElementById("minimap-text").onclick = function () {
toggle_show = true;
document.getElementById("minimap-box").style.display = "block";
document.getElementById("minimap-config").style.display = "block";
document.getElementById("minimap-text").style.display = "none";
document.getElementById("minimap-text").style.cursor = "default";
loadTemplates();
};
document.getElementById("zoom-plus").addEventListener('mousedown', function (e) {
e.preventDefault();
zooming_in = true;
zooming_out = false;
zoomIn();
}, false);
document.getElementById("zoom-minus").addEventListener('mousedown', function (e) {
e.preventDefault();
zooming_out = true;
zooming_in = false;
zoomOut();
}, false);
document.getElementById("zoom-plus").addEventListener('mouseup', function (e) {
zooming_in = false;
}, false);
document.getElementById("zoom-minus").addEventListener('mouseup', function (e) {
zooming_out = false;
}, false);
document.getElementById("follow-mouse").onclick = function () {
toggle_follow = !toggle_follow;
if (toggle_follow) {
this.innerHTML = "Follow Mouse";
loadTemplates();
x_window = x;
y_window = y;
drawCursor();
} else {
this.innerHTML = "Follow Screen";
getCenter();
}
};
gameWindow.addEventListener('mouseup', function (evt) {
if (!toggle_show)
return;
if (!toggle_follow)
setTimeout(getCenter, 100);
}, false);
gameWindow.addEventListener('mousemove', function (evt) {
if (!toggle_show)
return;
x_new = coorDOM.innerHTML.replace(rec, '$1');
y_new = coorDOM.innerHTML.replace(rec, '$2');
if (x != x_new || y != y_new) {
x = x_new;
y = y_new;
if (toggle_follow) {
x_window = x;
y_window = y;
} else {
drawCursor();
}
loadTemplates();
}
}, false);
updateloop();
}, false);
function exportMd() {
var ttlpx = 0;
var mdstr = "";
Object.keys(template_list).map(function (index, ele) {
var eles = template_list[index];
mdstr += '\n#### ' + index;
mdstr += '\n[![](https://raw.githubusercontent.com/TukKeedar/TukKeedarMap/master/images' + eles.name + ')]';
mdstr += '(http://pixelcanvas.io/@' + Math.floor(eles.x + eles.width / 2) + ',' + Math.floor(eles.y + eles.height / 2) + ')'
mdstr += '\n';
ttlpx += eles.width * eles.height;
});
mdstr = '### Toplam pixel sayısı =' + ttlpx + '\n' + mdstr;
console.log(mdstr);
}
function updateloop() {
// console.log("Updating Template List");
// Get JSON of available templates
var xmlhttp = new XMLHttpRequest();
var url = window.baseTepmlateUrl + "/templates/data.json";
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
template_list = JSON.parse(this.responseText);
//exportMd();
if (!toggle_follow)
getCenter();
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
// console.log("Refresh got forced.");
image_list = [];
loadTemplates();
setTimeout(updateloop, 60000)
}
function toggleShow() {
toggle_show = !toggle_show;
if (toggle_show) {
document.getElementById("minimap-box").style.display = "block";
document.getElementById("minimap-config").style.display = "block";
document.getElementById("minimap-text").style.display = "none";
document.getElementById("minimapbg").onclick = function () {
};
loadTemplates();
} else {
document.getElementById("minimap-box").style.display = "none";
document.getElementById("minimap-config").style.display = "none";
document.getElementById("minimap-text").style.display = "block";
document.getElementById("minimap-text").innerHTML = "Show Minimap";
document.getElementById("minimapbg").onclick = function () {
toggleShow()
};
}
}
function zoomIn() {
if (!zooming_in)
return;
zoomlevel = zoomlevel * 1.1;
if (zoomlevel > 45) {
zoomlevel = 45;
return;
}
drawBoard();
drawCursor();
loadTemplates();
setTimeout(zoomIn, zoom_time);
}
function zoomOut() {
if (!zooming_out)
return;
zoomlevel = zoomlevel / 1.1;
if (zoomlevel < 1) {
zoomlevel = 1;
return;
}
drawBoard();
drawCursor();
loadTemplates();
setTimeout(zoomOut, zoom_time);
}
function loadTemplates() {
if (!toggle_show)
return;
if (template_list == null)
return;
var x_left = x_window * 1 - minimap.width / zoomlevel / 2;
var x_right = x_window * 1 + minimap.width / zoomlevel / 2;
var y_top = y_window * 1 - minimap.height / zoomlevel / 2;
var y_bottom = y_window * 1 + minimap.height / zoomlevel / 2;
// console.log("x_left : " + x_left);
// console.log("x_right : " + x_right);
// console.log("y_top : " + y_top);
// console.log("y_bottom : " + y_bottom);
// console.log(template_list);
var keys = [];
for (var k in template_list) keys.push(k);
needed_templates = [];
var i;
for (i = 0; i < keys.length; i++) {
template = keys[i];
var temp_x = template_list[template]["x"] * 1;
var temp_y = template_list[template]["y"] * 1;
var temp_xr = template_list[template]["x"] * 1 + template_list[template]["width"] * 1;
var temp_yb = template_list[template]["y"] * 1 + template_list[template]["height"] * 1;
if (temp_xr <= x_left || temp_yb <= y_top || temp_x >= x_right || temp_y >= y_bottom)
continue
// console.log(" Template " + template + " is in range!");
needed_templates.push(template);
}
if (needed_templates.length == 0) {
if (zooming_in == false && zooming_out == false) {
document.getElementById("minimap-box").style.display = "none";
document.getElementById("minimap-text").style.display = "block";
document.getElementById("minimap-text").innerHTML = "No Template For This Area";
}
} else {
document.getElementById("minimap-box").style.display = "block";
document.getElementById("minimap-text").style.display = "none";
counter = 0;
for (i = 0; i < needed_templates.length; i++) {
if (image_list[needed_templates[i]] == null) {
loadImage(needed_templates[i]);
} else {
counter += 1;
//if last needed image loaded, start drawing
if (counter == needed_templates.length)
drawTemplates();
}
}
}
}
function loadImage(imagename) {
// console.log(" Load image " + imagename);
image_list[imagename] = new Image();
if (cachebreaker != null)
image_list[imagename].src = window.baseTepmlateUrl + "/images/" + template_list[imagename].name;
else
image_list[imagename].src = window.baseTepmlateUrl + "/images/" + template_list[imagename].name;
image_list[imagename].onload = function () {
counter += 1;
//if last needed image loaded, start drawing
if (counter == needed_templates.length)
drawTemplates();
}
}
function drawTemplates() {
ctx_minimap.clearRect(0, 0, minimap.width, minimap.height);
var x_left = x_window * 1 - minimap.width / zoomlevel / 2;
var y_top = y_window * 1 - minimap.height / zoomlevel / 2;
var i;
for (i = 0; i < needed_templates.length; i++) {
var template = needed_templates[i];
var xoff = (template_list[template]["x"] * 1 - x_left * 1) * zoomlevel;
var yoff = (template_list[template]["y"] * 1 - y_top * 1) * zoomlevel;
var newwidth = zoomlevel * image_list[template].width;
var newheight = zoomlevel * image_list[template].height;
var img = image_list[template];
ctx_minimap.drawImage(img, xoff, yoff, newwidth, newheight);
}
}
function drawBoard() {
ctx_minimap_board.clearRect(0, 0, minimap_board.width, minimap_board.height);
if (zoomlevel <= 4.6)
return;
ctx_minimap_board.beginPath();
var bw = minimap_board.width + zoomlevel;
var bh = minimap_board.height + zoomlevel;
var xoff_m = (minimap.width / 2) % zoomlevel - zoomlevel;
var yoff_m = (minimap.height / 2) % zoomlevel - zoomlevel;
var z = 1 * zoomlevel;
for (var x = 0; x <= bw; x += z) {
ctx_minimap_board.moveTo(x + xoff_m, yoff_m);
ctx_minimap_board.lineTo(x + xoff_m, bh + yoff_m);
}
for (var x = 0; x <= bh; x += z) {
ctx_minimap_board.moveTo(xoff_m, x + yoff_m);
ctx_minimap_board.lineTo(bw + xoff_m, x + yoff_m);
}
ctx_minimap_board.strokeStyle = "black";
ctx_minimap_board.stroke();
}
function drawCursor() {
var x_left = x_window * 1 - minimap.width / zoomlevel / 2;
var x_right = x_window * 1 + minimap.width / zoomlevel / 2;
var y_top = y_window * 1 - minimap.height / zoomlevel / 2;
var y_bottom = y_window * 1 + minimap.height / zoomlevel / 2;
ctx_minimap_cursor.clearRect(0, 0, minimap_cursor.width, minimap_cursor.height);
if (x < x_left || x > x_right || y < y_top || y > y_bottom)
return
xoff_c = x - x_left;
yoff_c = y - y_top;
ctx_minimap_cursor.beginPath();
ctx_minimap_cursor.lineWidth = zoomlevel / 3;
ctx_minimap_cursor.strokeStyle = "red";
ctx_minimap_cursor.rect(zoomlevel * xoff_c, zoomlevel * yoff_c, zoomlevel, zoomlevel);
ctx_minimap_cursor.stroke();
}
function getCenter() {
var url = window.location.href;
x_window = url.replace(re, '$2');
y_window = url.replace(re, '$3');
if (x_window == url || y_window == url) {
x_window = 0;
y_window = 0;
}
loadTemplates();
}
function findCoor() {
//all elements with style attributes
var elms = document.querySelectorAll("*[style]");
// Loop and find the element with the right style attributes
Array.prototype.forEach.call(elms, function (elm) {
var style = elm.style.cssText;
if (style == "position: absolute; left: 1em; bottom: 1em;") {
// console.log("Found It!");
coorDOM = elm.firstChild;
// console.log(coorDOM.innerHTML);
}
});
}