LukasRiedel

Untitled

Mar 13th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. javascript:
  2.  
  3. var win = (window.frames.length > 0) ? window.main : window;
  4.  
  5. function fnAjaxRequest(url, sendMethod, params, type) {
  6.     var error = null, paylod = null;
  7.     win.$.ajax({
  8.         "async": false, "url": url, "data": params, "dataType": type, "type": String(sendMethod || "GET").toUpperCase(),
  9.         "error": function (req, status, err) {
  10.             error = "ajax: " + status;
  11.         }, "success": function (data, status, req) {
  12.             payload = data;
  13.         }
  14.     });
  15.     if (error) throw (error);
  16.     return payload;
  17. }
  18.  
  19. function fnCreateConfig(name) {
  20.     return win.$(fnAjaxRequest("/interface.php", "GET", { "func": name }, "xml")).find("config");
  21. }
  22.  
  23. function fnCreateUnitConfig() {
  24.     return fnCreateConfig("get_unit_info");
  25. }
  26.  
  27. function getUnitSpeed(unit) {
  28.     var unitConfig = fnCreateUnitConfig();
  29.     var info = [];
  30.  
  31.     win.$(unitConfig).children(unit).each(function (i, e) { info.push(win.$(e).find("speed").text()); });
  32.  
  33.     return parseFloat(info) * 60 * 1000;
  34. }
  35.  
  36. function setCookie(cname, cvalue) {
  37.     var d = new Date();
  38.     d.setTime(d.getTime() + (365 * 24 * 60 * 60 * 1000));
  39.     var expires = "expires=" + d.toUTCString();
  40.     document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
  41. }
  42.  
  43. function getCookie(cname) {
  44.     var name = cname + "=";
  45.     var decodedCookie = decodeURIComponent(document.cookie);
  46.     var ca = decodedCookie.split(';');
  47.     for (var i = 0; i < ca.length; i++) {
  48.         var c = ca[i];
  49.         while (c.charAt(0) == ' ') {
  50.             c = c.substring(1);
  51.         }
  52.         if (c.indexOf(name) == 0) {
  53.             return c.substring(name.length, c.length);
  54.         }
  55.     }
  56.     return "";
  57. }
  58.  
  59. function add() {
  60.     var sum = getCookie("sum");
  61.     setCookie("X" + sum, $('#X').val());
  62.     setCookie("Y" + sum, $('#Y').val());
  63.     setCookie("D" + sum, $('#D').val());
  64.     setCookie("sum", parseInt(sum) + 1);
  65. }
  66.  
  67. function remove(i) {
  68.     var sum = getCookie("sum");
  69.  
  70.     if (i == (parseInt(sum) - 1)) {
  71.         setCookie("sum", parseInt(sum) - 1);
  72.     }
  73.  
  74.     else {
  75.         setCookie("X" + i, getCookie("X" + (parseInt(sum) - 1)));
  76.         setCookie("Y" + i, getCookie("Y" + (parseInt(sum) - 1)));
  77.         setCookie("D" + i, getCookie("D" + (parseInt(sum) - 1)));
  78.         setCookie("sum", parseInt(sum) - 1);
  79.     }
  80. }
  81.  
  82. function getDate(date) {
  83.     var currDate = new Date();
  84.  
  85.     switch (date.slice(6, 11)) {
  86.         case 'dnes ':
  87.             currDate.setHours(parseInt(date.slice(13, 15)), parseInt(date.slice(16, 18)), parseInt(date.slice(19, 21)));
  88.             break;
  89.         case 'zítra':
  90.             currDate.setHours(parseInt(date.slice(14, 16)), parseInt(date.slice(17, 19)), parseInt(date.slice(20, 22)));
  91.             currDate.setDate(currDate.getDate() + 1);
  92.             break;
  93.         default:
  94.             currDate.setHours(parseInt(date.slice(22, 24)), parseInt(date.slice(25, 27)), parseInt(date.slice(28, 30)));
  95.             currDate.setDate(parseInt(date.slice(10, 12)));
  96.             currDate.setMonth(parseInt(date.slice(13, 15)) - 1);
  97.     }
  98.  
  99.     return currDate;
  100. }
  101.  
  102. function parseDate(string) {
  103.     var day = string.toString().split(" ")[2];
  104.  
  105.     var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  106.  
  107.     var i = 0;
  108.  
  109.     while (true) {
  110.         if (months[i] == string.toString().split(" ")[1]) { break; }
  111.         i++;
  112.     }
  113.  
  114.     var time = string.toString().split(" ")[4];
  115.  
  116.     return day + "." + parseInt(i + 1) + ". v/ve " + time + " hodin";
  117. }
  118.  
  119. function rename(name) {  
  120.     if (name.indexOf('Beranidlo') != -1) { return 'ram'; }
  121.     else if (name.indexOf('Špeh') != -1) { return 'spy'; }
  122.     else if (name.indexOf('LK') != -1) { return 'light'; }
  123.     else if (name.indexOf('TK') != -1) { return 'heavy'; }
  124.     else if (name.indexOf('Sekera') != -1) { return 'axe'; }
  125.     else if (name.indexOf('Šlechta') != -1) { return 'snob'; }
  126.     else if (name.indexOf('Meč') != -1) { return 'sword'; }
  127.     return false;
  128. }
  129.  
  130. function shown(name) {
  131.     if (name.indexOf('odhalen') != -1) { return true; }
  132.     else { return false; }
  133. }
  134.  
  135. function calculate() {
  136.     $('#incomings_table th:last-child').after('<th><a>Bude odhalen</a></th>');
  137.     $('#incomings_table td:last-child').after('<td class="watchtower" style="text-align:center"></td>');
  138.  
  139.     $("tr.nowrap").each(function () {
  140.         var originCoords = $(this).find("td:eq(2)").text();
  141.         originCoords = originCoords.trim();
  142.         originCoords = originCoords.slice(originCoords.length - 12, originCoords.length - 5);
  143.         var originAxis = originCoords.split('|');
  144.  
  145.         var targetCoords = $(this).find("td:eq(1)").text();
  146.         targetCoords = targetCoords.trim();
  147.         targetCoords = targetCoords.slice(targetCoords.length - 12, targetCoords.length - 5);
  148.         var targetAxis = targetCoords.split('|');
  149.  
  150.         var date = $(this).find("td:eq(5)").text();
  151.  
  152.         var maxX = 0;
  153.         var maxY = 0;
  154.         var a = targetAxis[1] - originAxis[1];
  155.         var b = originAxis[0] - targetAxis[0];
  156.         var c = (-1) * (a * originAxis[0] + b * originAxis[1]);
  157.  
  158.         for (var i = 0; i < parseInt(getCookie("sum")); i++) {
  159.             var m = parseInt(getCookie("X" + i));
  160.             var n = parseInt(getCookie("Y" + i));
  161.             var r = parseFloat(getCookie("D" + i));
  162.  
  163.             if (Math.sqrt(Math.pow(m - originAxis[0], 2) + Math.pow(n - originAxis[1], 2)) < r) {
  164.                 break;
  165.             }
  166.  
  167.             else if (b == 0) {
  168.                 var x1 = targetAxis[0];
  169.                 var x2 = targetAxis[0];
  170.                 var temp = Math.sqrt((-1) * Math.pow(m, 2) + 2 * m * x1 + Math.pow(r, 2) - Math.pow(x1, 2));
  171.                 var y1 = n - temp;
  172.                 var y2 = n + temp;
  173.             }
  174.  
  175.             else if (a == 0) {
  176.                 var y1 = targetAxis[1];
  177.                 var y2 = targetAxis[1];
  178.                 var temp = Math.sqrt((-1) * Math.pow(n, 2) + 2 * n * y1 + Math.pow(r, 2) - Math.pow(y1, 2));
  179.                 var x1 = m - temp;
  180.                 var x2 = m + temp;
  181.             }
  182.  
  183.             else {
  184.                 var temp = Math.sqrt(Math.pow((-2) * Math.pow(a, 2) * n + 2 * a * b * m + 2 * b * c, 2) - 4 * (Math.pow(a, 2) + Math.pow(b, 2)) * (Math.pow(a, 2) * Math.pow(m, 2) + Math.pow(a, 2) * Math.pow(n, 2) - Math.pow(a, 2) * Math.pow(r, 2) + 2 * a * c * m + Math.pow(c, 2))) + 2 * Math.pow(a, 2) * n - 2 * a * b * m - 2 * b * c;
  185.                 var y1 = (1 / (2 * (Math.pow(a, 2) + Math.pow(b, 2)))) * temp;
  186.                 var y2 = (1 / (2 * (Math.pow(a, 2) + Math.pow(b, 2)))) * (-1) * temp;
  187.                 var x1 = ((-1) * (b * y1) - c) / a;
  188.                 var x2 = ((-1) * (b * y2) - c) / a;
  189.             }
  190.            
  191.             if (Math.sqrt(Math.pow((x1 - originAxis[0]), 2) + Math.pow((y1 - originAxis[1]), 2)) < Math.sqrt(Math.pow((maxX - originAxis[0]), 2) + Math.pow((maxY - originAxis[1]), 2))) {
  192.                 maxX = x1;
  193.                 maxY = y1;
  194.             }
  195.  
  196.  
  197.             else if (Math.sqrt(Math.pow((x2 - originAxis[0]), 2) + Math.pow((y2 - originAxis[1]), 2)) < Math.sqrt(Math.pow((maxX - originAxis[0]), 2) + Math.pow((maxY - originAxis[1]), 2))) {
  198.                 maxX = x2;
  199.                 maxY = y2;
  200.             }            
  201.         }
  202.  
  203.         var command = $(this).find("span.quickedit-label").text().trim();
  204.         var distance = Math.sqrt(Math.pow((targetAxis[0] - maxX), 2) + Math.pow((targetAxis[1] - maxY), 2)) * getUnitSpeed(rename(command));
  205.  
  206.         if (i != parseInt(getCookie("sum"))) {
  207.             $(this).find("td.watchtower").html(parseDate(new Date()));
  208.             distance = r;
  209.         }
  210.  
  211.         else if (maxX == 0 && maxY == 0) {
  212.             $(this).find("td.watchtower").html("-----");
  213.             return;
  214.         }
  215.        
  216.         if (rename(command)) {
  217.             if (distance > r) { var currDate = new Date(getDate(date) - new Date(Math.floor(distance))); }
  218.             else { var currDate = new Date(); }
  219.             $(this).find("td.watchtower").html(parseDate(currDate));
  220.             if (!shown(command)) {
  221.                 $(this).find(".rename-icon").click();
  222.                 $(this).find("input[type=text]").val(command.trim() + " - odhalen dne " + parseDate(currDate));
  223.                 $(this).find("input[type=button]").click();
  224.             }            
  225.         }
  226.  
  227.         else {
  228.             $(this).find("td.watchtower").html("-----");
  229.         }
  230.     });  
  231. }
  232.  
  233. function main() {
  234.     $('.dynamicallyCreated').html("");
  235.  
  236.     if (getCookie("sum") == "") {
  237.         alert("Dle směrnice EU 2002/58/ES jsem povinen vás informovat, že tento script využívá soubory cookies. Používáním tohoto scriptu souhlasíte s používáním cookies.");
  238.         setCookie("sum", 0);
  239.     }
  240.  
  241.     var div = '<div class="dynamicallyCreated"><table align="center" style="text-align:center"><tr><th style="text-align:center">Vesnice</th><th style="text-align:center">Dosah</th><th></th></tr>';
  242.  
  243.     for (var i = 0; i < parseInt(getCookie("sum")); i++) {
  244.         div += '<tr><td>' + getCookie("X" + i) + '|' + getCookie("Y" + i) + '</td><td>' + getCookie("D" + i) + '</td><td><input type="submit" class="btn" onClick="remove(' + i + '); main();" value="Odebrat"></td></tr>';
  245.     }
  246.  
  247.     div += '<tr><td><input type="text" id="X" maxlength="3" size="3"> | <input type="text" id="Y" maxlength="3" size="3"></td><td><input type="text" id="D" maxlength="4" size="4"></td><td><input type="submit" onClick="add(); main();" class="btn" value="Přidat"></td></td></tr></table>';
  248.  
  249.     div += '<table align="center"><tr><td><input type="submit" class="btn" onClick="calculate();" value="Vypočítej"></td></tr></table></div>';
  250.  
  251.     $(div).insertBefore('#paged_view_content');
  252. }
  253.  
  254. $(document).ready(main());
Advertisement
Add Comment
Please, Sign In to add comment