Advertisement
Guest User

Powiadomienia o podbojach - wyświetlanie danych

a guest
Feb 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name          Powiadomienia o podbojach - wyświetlanie danych
  3. // @description   Skrypt wyświetla dane o niedawno przejętych wioskach spełniających dane kryteria
  4. // @author        stivens, Makak
  5. // @include       https://pl*.plemiona.pl/game.php?*
  6. // @version       1.1.2
  7. // @grant         none
  8. // ==/UserScript==
  9.  
  10.  
  11. (function() {
  12.     'use strict';
  13.  
  14.     var settings;
  15.     var image_base = window.image_base;
  16.     var UI = window.UI;
  17.     var game_data = window.game_data;
  18.     var player_id = (Number(game_data.player.sitter) !== 0) ? Number(game_data.player.sitter) : Number(game_data.player.id);
  19.     var t = (Number(game_data.player.sitter) !== 0) ? 't=' + game_data.player.id + '&' : '';
  20.     var reload_diplomacy = false;
  21.     var tooltip_options = {showURL: false, track: true, fade: true, delay: false, showBody: ' :: ', extraClass: 'tooltip-style'};
  22.  
  23.  
  24.  
  25.     /**
  26.      * Returns URL to a dot based on new village owner's tribe
  27.      * @param {String} tribe_tag
  28.      * @returns {String} url to dot image
  29.      */
  30.     function get_dot_url(tribe_tag) {
  31.         storage_get_settings();
  32.         var dot;
  33.  
  34.         if (settings.allies.indexOf(tribe_tag) !== -1) {
  35.             dot = 'green';
  36.         } else if (settings.own_tribe === tribe_tag) {
  37.             dot = 'green';
  38.         } else if (settings.enemies.indexOf(tribe_tag) !== -1) {
  39.             dot = 'red';
  40.         } else if (settings.interesting.indexOf(tribe_tag) !== -1) {
  41.             dot = 'yellow';
  42.         } else {
  43.             dot = 'blue';
  44.         }
  45.  
  46.         return image_base + '/dots/' + dot + '.png';
  47.     }
  48.  
  49.  
  50.     /**
  51.      * Creates HTML string with list of conquers
  52.      * @returns {String} content
  53.      */
  54.     function create_gui_content() {
  55.         var content = `<div id="conquers_script_search">
  56.                             <input type="text" placeholder="Szukaj">
  57.                             <div></div>
  58.                             <span></span>
  59.                         </div>
  60.                         <table class="vis" width="100%" id="conquers_script_conquers">
  61.                         <tbody>`;
  62.         var row_class = ['row_a', 'row_b'];
  63.         var row_number = 0;
  64.         var conquers = JSON.parse(localStorage.conquers_script_old_data);
  65.         var array_of_ids = get_objects_order_array(conquers);
  66.  
  67.         for (var i = 0, n = array_of_ids.length; i < n; i++) {
  68.             if (settings.settings_for_conquers === false || is_conquer_interesting(conquers[array_of_ids[i]])) {
  69.                 content += get_conquer_html(conquers[array_of_ids[i]], row_class[row_number%2]);
  70.                 row_number++;
  71.             }
  72.         }
  73.  
  74.         content += `</tbody></table>`;
  75.         return content;
  76.     }
  77.  
  78.  
  79.     /**
  80.      * Checks if player is in tribe and has access to tribe functions (in case of a sitter)
  81.      * @returns {Boolean}
  82.      */
  83.     function player_in_tribe() {
  84.         var is_in_tribe = (game_data.player.ally !== '0');
  85.         var has_tribe_access = ($('#tribe_forum_indicator').length === 1);
  86.  
  87.         return (is_in_tribe && has_tribe_access);
  88.     }
  89.  
  90.     /**
  91.      * Creates HTML string with script's settings
  92.      * @returns {String} content
  93.      */
  94.     function create_settings() {
  95.         storage_get_settings();
  96.  
  97.         var settings_content = `<h3>DYPLOMACJA</h3><p>Moje plemię: <a href="#">${settings.own_tribe}</a> ` + (player_in_tribe() ? '<button type="button" class="btn" style="float:right;" id="load_diplomacy">Wczytaj dyplomację plemienia</button>' : '' ) + `</p>
  98.                                     <span><button class="btn conquers_script_tribes_add" data-type="allies">+</button> <strong>Sojusznicy</strong>:`;
  99.  
  100.         for (var ally in settings.allies) {
  101.             if (settings.allies[ally] !== "") {
  102.                 settings_content += ` <span><a href="#" class="conquers_script_tribes_remove" data-type="allies">${settings.allies[ally]} <img src="${image_base}delete_small.png" ></a></span>`;
  103.             }
  104.         }
  105.  
  106.         settings_content += `</span><br><span><button class="btn conquers_script_tribes_add" data-type="enemies">+</button> <strong>Wrogowie</strong>:`;
  107.  
  108.  
  109.         for (var enemy in settings.enemies) {
  110.             if (settings.enemies[enemy] !== "") {
  111.                 settings_content += ` <span><a href="#" class="conquers_script_tribes_remove" data-type="enemies">${settings.enemies[enemy]} <img src="${image_base}delete_small.png" ></a></span>`;
  112.             }
  113.         }
  114.  
  115.         settings_content += `</span><br><span><button class="btn conquers_script_tribes_add" data-type="interesting">+</button> <strong>Interesujące</strong>:`;
  116.  
  117.         for (var inter in settings.interesting) {
  118.             if (settings.interesting[inter] !== "") {
  119.                 settings_content += ` <span><a href="#" class="conquers_script_tribes_remove" data-type="interesting">${settings.interesting[inter]} <img src="${image_base}delete_small.png" ></a></span>`;
  120.             }
  121.         }
  122.  
  123.         settings_content += `</span><br><br><br><br>
  124.                                 <h3>POWIADOMIENIA</h3>
  125.                                 <input type="checkbox" id="checkbox_notifications" ${(settings.show_notifications === true ? 'checked ' : '')}>Pokaż powiadomienia
  126.                                 <select id="notification_style">
  127.                                     <option value="classic">Makak\'s classic</option>
  128.                                     <option value="dark" ${(settings.notification_style === 'dark' ? 'selected' : '')}>stivens\'s dark</option>
  129.                                 </select><br><hr>Pokaż powiadomienia dla przejęć dokonanych przez<br>
  130.                                 <input type="checkbox" id="checkbox_own" ${(settings.show_own === true ? 'checked ' : '')}> własne plemię
  131.                                 <input type="checkbox" id="checkbox_allies" ${(settings.show_allies === true ? 'checked ' : '')}> sojuszników
  132.                                 <input type="checkbox" id="checkbox_enemies" ${(settings.show_enemies === true ? 'checked ' : '')}> wrogów <br>
  133.                                 <input type="checkbox" id="checkbox_inter" ${(settings.show_interesting === true ? 'checked ' : '')}>  interesujących
  134.                                 <span style="white-space: nowrap;"><input type="checkbox" id="checkbox_others" ${(settings.show_others === true ? 'checked ' : '')}> wszystkie plemiona</span><br> <br>
  135.                                 <input type="checkbox" id="checkbox_victims" ${(settings.show_victims === true ? 'checked ' : '')}>  Pokaż również powiadomienie w wypadku straty wioski<br><hr> <br>
  136.                                 <input type="checkbox" id="checkbox_range" ${(settings.show_range === true ? 'checked ' : '')}>Pokazuj tylko przejęcia max <input style="width:50px;" type="number" id="conquers_range" value="${settings.range}"> pól ode mnie <br>
  137.                                 <input type="checkbox" style="opacity:0;">Animacja powiadomień
  138.                                 <select id="animation_style">
  139.                                   <option value="0">Przenikanie</option>
  140.                                   <option value="a" ${(settings.animation_style === 'a' ? 'selected' : '')}>Skok</option>
  141.                                   <option value="b" ${(settings.animation_style === 'b' ? 'selected' : '')}>Wleć z prawej</option>
  142.                                   <option value="c" ${(settings.animation_style === 'c' ? 'selected' : '')}>Trzeci wymiar</option>
  143.                                   <option value="d" ${(settings.animation_style === 'd' ? 'selected' : '')}>Spadnij</option>
  144.                                 </select><br>
  145.                                 <input type="checkbox" style="opacity:0;">Powiadomienia nie starsze niż
  146.                                 <select id="notifications_lifetime">
  147.                                     <option value="1">1h</option>
  148.                                     <option value="2" ${(settings.notifications_lifetime === 2*3600 ? 'selected' : '')}>2h</option>
  149.                                     <option value="3" ${(settings.notifications_lifetime === 3*3600 ? 'selected' : '')}>3h</option>
  150.                                     <option value="4" ${(settings.notifications_lifetime === 4*3600 ? 'selected' : '')}>4h</option>
  151.                                     <option value="5" ${(settings.notifications_lifetime === 5*3600 ? 'selected' : '')}>5h</option>
  152.                                     <option value="6" ${(settings.notifications_lifetime === 6*3600 ? 'selected' : '')}>6h</option>
  153.                                     <option value="12" ${(settings.notifications_lifetime === 12*3600 ? 'selected' : '')}>12h</option>
  154.                                 </select><br><br><br><input type="checkbox" style="opacity:0;">Podboje nie starsze niż
  155.                                 <select id="conquers_lifetime">
  156.                                     <option value="12" ${(settings.conquers_lifetime === 12*3600 ? 'selected' : '')}>12h</option>
  157.                                     <option value="24" ${(settings.conquers_lifetime === 24*3600 ? 'selected' : '')}>24h</option>
  158.                                     <option value="36" ${(settings.conquers_lifetime === 36*3600 ? 'selected' : '')}>36h</option>
  159.                                     <option value="48" ${(settings.conquers_lifetime === 48*3600 ? 'selected' : '')}>48h</option>
  160.                                     <option value="72" ${(settings.conquers_lifetime === 72*3600 ? 'selected' : '')}>72h</option>
  161.                                 </select>
  162.                                 <br><input type="checkbox" id="checkbox_settings_for_conquers" ${(settings.settings_for_conquers === true ? 'checked ' : '')}>Zastosuj powyższe ustawienia dla przeglądu podbojów
  163.  
  164.  
  165.                                 <br><br><br>
  166.  
  167.                                 <span class="ui_tooltip
  168.                                 " title="Przy filtrowaniu odległości od Twoich wiosek możesz wkleić tutaj listę dodatkowych wiosek, wokół których chcesz widzieć podbicia.">Traktuj te wioski jak moje własne:</span> <br><br>
  169.                                 <textarea name="" id="additional_villages" cols="40" rows="5">${settings.additional_villages}</textarea>
  170.  
  171.                                 <br><br><br>
  172.  
  173.                                 <div style="font-size:0.9em;margin-bottom:5px;">
  174.                                     Skrypt autorstwa Makak & stivens. Po więcej zapraszamy na <a href="https://www.plemsy.pl/" target="_BLANK">plemsy.pl</a>
  175.                                 </div>`;
  176.  
  177.         return settings_content;
  178.     }
  179.  
  180.     /**
  181.      * Opens inlinePopup
  182.      * @param {Boolean} display_conquers Whether to display conquers or settings
  183.      * @param {Object} e Click event
  184.      * @returns {undefined}
  185.      */
  186.     function display_inline_popup(display_conquers, e = null) {
  187.         storage_get_settings();
  188.         var popup_position = {};
  189.  
  190.         if (e) {
  191.             popup_position.clientX = e.clientX;
  192.             popup_position.clientY = e.clientY;
  193.         }
  194.  
  195.  
  196.         if (display_conquers) {
  197.             inlinePopup(popup_position, 'conquers_script', null, {offset_x: 0, offset_y: 0}, create_gui_content(), 'Powiadomienia o podbojach');
  198.             $('span:contains(Powiadomienia o podbojach)').append(' <a href="#" id="conquers_script_settings" style="font-size:0.8em;">ustawienia</a>');
  199.         } else {
  200.             inlinePopup(popup_position, 'conquers_script', null, {offset_x: 0, offset_y: 0}, '<div style="padding:2%;box-sizing:border-box;">' + create_settings() + '</div>', 'Powiadomienia o podbojach');
  201.             $('span:contains(Powiadomienia o podbojach)').append(' <a href="#" id="conquers_script_settings" style="font-size:0.8em;">akceptuj</a>');
  202.         }
  203.  
  204.         refresh_counter();
  205.     }
  206.  
  207.  
  208.     /**
  209.      * Returns given conquer's html string
  210.      * @param {Object} conquer
  211.      * @param {String} class_name
  212.      * @returns {String} html
  213.      */
  214.     function get_conquer_html(conquer, class_name) {
  215.         var date = new Date(conquer.timestamp * 1000);
  216.         var hours = (date.getHours() < 10) ? '0'+date.getHours() : date.getHours();
  217.         var minutes = (date.getMinutes() < 10) ? '0'+date.getMinutes() : date.getMinutes();
  218.  
  219.         var text = `<tr class="${class_name}">
  220.                         <td style="padding: 8px; text-align: center; width: 40px;">${hours}:${minutes}</td>
  221.                             <td style="padding: 8px;">
  222.                                 <img src="${get_dot_url(conquer.owner.new.ally.tag)}">
  223.                                 <a href="/game.php?${t}screen=info_player&id=${conquer.owner.new.id}">${conquer.owner.new.name}</a> `;
  224.  
  225.         if (conquer.owner.new.ally.id !== 0) {
  226.             text += `<span style="white-space: nowrap;">(<a href="/game.php?${t}screen=info_ally&id=${conquer.owner.new.ally.id}">${conquer.owner.new.ally.tag}</a>)</span> `;
  227.         }
  228.  
  229.         text += `podbija <a href="/game.php?${t}screen=info_village&id=${conquer.village.id}">${conquer.village.name}</a> od `;
  230.  
  231.         if (conquer.owner.old.id !== 0) {
  232.             text += `<a href="/game.php?${t}screen=info_player&id=${conquer.owner.old.id}">${conquer.owner.old.name}</a> `;
  233.  
  234.             if (conquer.owner.old.ally.id !== 0) {
  235.                 text += `<span style="white-space: nowrap;">(<a href="/game.php?${t}screen=info_ally&id=${conquer.owner.old.ally.id}">${conquer.owner.old.ally.tag}</a>)</span>`;
  236.             }
  237.         } else {
  238.             text += `<i>wioska barbarzyńska</i>`;
  239.         }
  240.  
  241.         return text + `</td></tr>`;
  242.     }
  243.  
  244.     /**
  245.      * Returns notification's html string for given conquer
  246.      * @param {String} conquer_id
  247.      * @param {Object} conquer
  248.      * @returns {String} html
  249.      */
  250.     function get_notification_html(conquer_id, conquer) {
  251.         storage_get_settings();
  252.  
  253.         var text = `<div id="${conquer_id}" class="notification_style_${settings.notification_style}">
  254.                         <img src="${image_base}delete.png" height="14" class="notification_close">
  255.                         <img src="${image_base}new_mail.png" class="notification_send_pm">
  256.                         <img src="${get_dot_url(conquer.owner.new.ally.tag)}">
  257.                         <a href="/game.php?${t}screen=info_player&id=${conquer.owner.new.id}">${conquer.owner.new.name}</a>
  258.                         podbija <a href="/game.php?${t}screen=info_village&id=${conquer.village.id}">${conquer.village.name}</a> od `;
  259.  
  260.         if (conquer.owner.old.id !== 0) {
  261.             text += `<a href="/game.php?${t}screen=info_player&id=${conquer.owner.old.id}">${conquer.owner.old.name}</a>`;
  262.         } else {
  263.             text += `<i>wioska barbarzyńska</i>`;
  264.         }
  265.  
  266.         return text + '</div>';
  267.     }
  268.  
  269.     /**
  270.      * Checks if settings are stored in localStorage or not (first-time usage)
  271.      * @returns {Boolean} settings_exist
  272.      */
  273.     function storage_settings_exist() {
  274.         return (localStorage.getItem('conquers_script_settings') !== null);
  275.     }
  276.  
  277.     /**
  278.      * Saves settings in localStorage
  279.      * @returns {undefined}
  280.      */
  281.     function storage_set_settings() {
  282.         localStorage.conquers_script_settings = JSON.stringify(settings);
  283.     }
  284.  
  285.     /**
  286.      * Reads settings from localStorage
  287.      * @returns {undefined}
  288.      */
  289.     function storage_get_settings() {
  290.         settings = JSON.parse(localStorage.conquers_script_settings);
  291.     }
  292.  
  293.     /**
  294.      * Loads diplomacy data from localStorage to settings object
  295.      * @returns {undefined}
  296.      */
  297.     function load_diplomacy_to_settings() {
  298.         if (localStorage.getItem('conquers_script_own_tribe') !== null) {
  299.             settings.own_tribe = localStorage.getItem('conquers_script_own_tribe');
  300.         }
  301.  
  302.         if (localStorage.getItem('conquers_script_diplomacy') !== null) {
  303.             var diplomacy = JSON.parse(localStorage.conquers_script_diplomacy);
  304.             settings.allies = diplomacy.allies;
  305.             settings.enemies = diplomacy.enemies;
  306.         }
  307.     }
  308.  
  309.     /**
  310.      * Updates notifications data in localStorage
  311.      * @param {Object} notifications
  312.      * @returns {undefined}
  313.      */
  314.     function storage_set_notifications(notifications) {
  315.         localStorage.conquers_script_notifications = JSON.stringify(notifications);
  316.     }
  317.  
  318.     /**
  319.      * Reads notifications object from localStorage
  320.      * @returns {Object} notifications
  321.      */
  322.     function storage_get_notifications() {
  323.         return JSON.parse(localStorage.conquers_script_notifications);
  324.     }
  325.  
  326.     /**
  327.      * Updates notifications order data in localStorage
  328.      * @param {Array} sorted_array_of_ids
  329.      * @returns {undefined}
  330.      */
  331.     function storage_set_notifications_order(sorted_array_of_ids) {
  332.         localStorage.conquers_script_notifications_order = JSON.stringify(sorted_array_of_ids);
  333.     }
  334.  
  335.     /**
  336.      * Reads notifications order array from localStorage
  337.      * @returns {Array} notifications order
  338.      */
  339.     function storage_get_notifications_order() {
  340.         return JSON.parse(localStorage.conquers_script_notifications_order);
  341.     }
  342.  
  343.     /**
  344.      * Creates sorted array of conquers' ids
  345.      * @param {Object} object Conquers or notifications object
  346.      * @returns {Array} array of strings (conquers' ids) sorted by timestamp
  347.      */
  348.     function get_objects_order_array(object) {
  349.         var tmp = [];
  350.         for (var id in object) {
  351.             tmp.push({id: id, timestamp: object[id].timestamp});
  352.         }
  353.  
  354.         tmp.sort(function(a, b) {
  355.             return b.timestamp - a.timestamp;
  356.         });
  357.  
  358.         var array_of_ids = [];
  359.         for (var i = 0, n = tmp.length; i < n; i++) {
  360.             array_of_ids.push(tmp[i].id);
  361.         }
  362.  
  363.         return array_of_ids;
  364.     }
  365.  
  366.     /**
  367.      * Removes notifications older than X seconds and updates localStorage data
  368.      * @returns {undefined}
  369.      */
  370.     function remove_old_notifications() {
  371.         storage_get_settings();
  372.  
  373.         var timestamp = get_current_timestamp() - settings.notifications_lifetime;
  374.         var notifications = JSON.parse(localStorage.conquers_script_notifications);
  375.  
  376.         for (var conquer_id in notifications) {
  377.             if (notifications[conquer_id].timestamp < timestamp) {
  378.                 delete notifications[conquer_id];
  379.             }
  380.         }
  381.  
  382.         storage_set_notifications(notifications);
  383.         storage_set_notifications_order(get_objects_order_array(notifications));
  384.     }
  385.  
  386.     /**
  387.      * Removes data for conquers older than X seconds and updates localStorage data
  388.      * @returns {undefined}
  389.      */
  390.     function remove_old_conquers_data() {
  391.         storage_get_settings();
  392.  
  393.         var timestamp = get_current_timestamp() - settings.conquers_lifetime;
  394.         var conquers = JSON.parse(localStorage.conquers_script_old_data);
  395.  
  396.         for (var conquer_id in conquers) {
  397.             if (conquers[conquer_id].timestamp < timestamp) {
  398.                 delete conquers[conquer_id];
  399.             }
  400.         }
  401.  
  402.         localStorage.conquers_script_old_data = JSON.stringify(conquers);
  403.     }
  404.  
  405.     /**
  406.      * Creates an array with additional villages coordinates
  407.      * @returns {Array} list Array of {x, y} objects
  408.      */
  409.     function parse_additional_villages() {
  410.         var list = [];
  411.         var matches = settings.additional_villages.match(/\d{1,3}\|\d{1,3}/g);
  412.         if (matches !== null) {
  413.             for (var i = 0, n = matches.length; i < n; i++) {
  414.                 var coords = matches[i].split('|');
  415.                 list.push({
  416.                     x: Number(coords[0]),
  417.                     y: Number(coords[1])
  418.                 });
  419.             }
  420.         }
  421.  
  422.         return list;
  423.     }
  424.  
  425.     /**
  426.      * Checks if a village is within field range of player's villages
  427.      * @param {Number} x
  428.      * @param {Number} y
  429.      * @param {Number} range
  430.      * @returns {Boolean}
  431.      */
  432.     function is_village_within_range(x, y, range) {
  433.         storage_get_settings();
  434.  
  435.         var villages = JSON.parse(localStorage['conquers_script_player_' + player_id + '_villages']);
  436.         var additional_villages = parse_additional_villages();
  437.         for (var i = 0, n = additional_villages.length; i < n; i++) {
  438.             villages['additional_' + i] = additional_villages[i];
  439.         }
  440.  
  441.         var x_diff, y_diff, square_range = range*range;
  442.         for (var key in villages) {
  443.             if (villages.hasOwnProperty(key)) {
  444.                 x_diff = villages[key].x - x;
  445.                 y_diff = villages[key].y - y;
  446.  
  447.                 if (x_diff*x_diff + y_diff*y_diff <= square_range) {
  448.                     return true;
  449.                 }
  450.             }
  451.         }
  452.  
  453.         return false;
  454.     }
  455.  
  456.     /**
  457.      * Checks if a "group" (allies/enemies) contains given tribe tag
  458.      * @param {String} group_name
  459.      * @param {String} tribe_tag
  460.      * @returns {Boolean}
  461.      */
  462.     function group_contains_tribe(group_name, tribe_tag) {
  463.         if (tribe_tag === '') {
  464.             return false;
  465.         }
  466.  
  467.         var tribes = settings[group_name];
  468.         for (var i = 0, n = tribes.length; i < n; i++) {
  469.             tribes[i] = tribes[i].toLowerCase();
  470.         }
  471.  
  472.         return tribes.indexOf(tribe_tag.toLowerCase()) !== -1;
  473.     }
  474.  
  475.     /**
  476.      * Checks if conquer is interesting to the player (if is close to their villages etc)
  477.      * @param {Object} conquer
  478.      * @returns {Boolean} is_interesting
  479.      */
  480.     function is_conquer_interesting(conquer) {
  481.         storage_get_settings();
  482.  
  483.         // Conquer is NOT interesting if user chose to check range and village is outside the range (ally/enemies etc. settings don't matter in this case)
  484.         if (settings.show_range === true && is_village_within_range(conquer.village.x, conquer.village.y, settings.range) === false) {
  485.             return false;
  486.         }
  487.  
  488.         // User wants to see every tribe's conquers (also players not in tribe) so conquer is automatically interesting
  489.         if (settings.show_others) {
  490.             return true;
  491.         } else {
  492.             var old_tag = conquer.owner.old.ally.tag;
  493.             var new_tag = conquer.owner.new.ally.tag;
  494.  
  495.             // Attacker/defender is in user's tribe
  496.             if (settings.show_own && (settings.own_tribe === new_tag || (settings.show_victims && settings.own_tribe === old_tag))) {
  497.                 return true;
  498.             }
  499.  
  500.             // Try to find a group that user wants to check and which contains attacker's/defender's tribe tag
  501.             if (settings.show_allies && (group_contains_tribe('allies', new_tag) || (settings.show_victims && group_contains_tribe('allies', old_tag)))) {
  502.                 return true;
  503.             }
  504.             if (settings.show_enemies && (group_contains_tribe('enemies', new_tag) || (settings.show_victims && group_contains_tribe('enemies', old_tag)))) {
  505.                 return true;
  506.             }
  507.             if (settings.show_interesting && (group_contains_tribe('interesting', new_tag) || (settings.show_victims && group_contains_tribe('interesting', old_tag)))) {
  508.                 return true;
  509.             }
  510.  
  511.             // Did NOT find any group that contains attacker's/defender's tribe so conquer is NOT interesting
  512.             return false;
  513.         }
  514.     }
  515.  
  516.     /**
  517.      * Reads new_data and updates notifications list
  518.      * @returns {undefined}
  519.      */
  520.     function update_notifications_list() {
  521.         storage_get_settings();
  522.  
  523.         if (settings.show_notifications === false) {
  524.             localStorage.conquers_script_new_data = '{}';
  525.             return;
  526.         }
  527.  
  528.         var notifications = storage_get_notifications();
  529.         var conquers = JSON.parse(localStorage.conquers_script_new_data);
  530.         localStorage.conquers_script_new_data = '{}';
  531.  
  532.         var conquer_id;
  533.         var contains_old_conquers = false;
  534.         for (conquer_id in conquers) {
  535.             if (conquers[conquer_id].timestamp < get_current_timestamp()-310) {
  536.                 contains_old_conquers = true;
  537.             }
  538.         }
  539.  
  540.         for (conquer_id in conquers) {
  541.             if (conquers[conquer_id].timestamp > get_current_timestamp()-settings.notifications_lifetime && is_conquer_interesting(conquers[conquer_id]) && conquers[conquer_id].owner.new.id !== player_id && conquers[conquer_id].owner.old.id !== player_id) {
  542.                 notifications[conquer_id] = {
  543.                     is_new: !contains_old_conquers,
  544.                     timestamp: conquers[conquer_id].timestamp
  545.                 };
  546.             }
  547.         }
  548.  
  549.         storage_set_notifications(notifications);
  550.         storage_set_notifications_order(get_objects_order_array(notifications));
  551.     }
  552.  
  553.     /**
  554.      * Hides all notifications from the screen
  555.      * @returns {undefined}
  556.      */
  557.     function hide_all_notifications() {
  558.         $('#conquers_script_notifications_container').empty();
  559.     }
  560.  
  561.     /**
  562.      * Shows all notifications (without animation)
  563.      * @returns {undefined}
  564.      */
  565.     function display_all_notifications() {
  566.         storage_get_settings();
  567.  
  568.         if (settings.show_notifications === false) {
  569.             return;
  570.         }
  571.  
  572.         var conquers = JSON.parse(localStorage.conquers_script_old_data);
  573.         var notifications = storage_get_notifications();
  574.         var order = storage_get_notifications_order();
  575.  
  576.         var i, n = order.length;
  577.         for (i = 0; i < n; i++) {
  578.             var conquer_id = order[i];
  579.             $('#conquers_script_notifications_container').append(get_notification_html(conquer_id, conquers[conquer_id]));
  580.             notifications[conquer_id].is_new = false;
  581.         }
  582.  
  583.         toggle_close_all_button();
  584.         toggle_notifications_wrapper();
  585.         storage_set_notifications(notifications);
  586.         storage_set_notifications_order(get_objects_order_array(notifications));
  587.     }
  588.  
  589.     /**
  590.      * Animates notification by its id
  591.      * @param {String} id
  592.      * @returns {undefined}
  593.      */
  594.     function animate_notification(id) {
  595.         storage_get_settings();
  596.  
  597.         switch (settings.animation_style) {
  598.             case '0':
  599.                 $('#' + id).fadeIn(500);
  600.                 break;
  601.             case 'a':
  602.                 $('#' + id).show();
  603.                 $('#' + id).addClass('anim_scale');
  604.                 break;
  605.             case 'b':
  606.                 $('#' + id).show();
  607.                 $('#' + id).addClass('anim_fly-in');
  608.                 break;
  609.             case 'c':
  610.                 $('#' + id).show();
  611.                 $('#' + id).addClass('anim_third-dimension');
  612.                 break;
  613.             case 'd':
  614.                 $('#' + id).show();
  615.                 $('#' + id).addClass('anim_fall-down');
  616.                 break;
  617.         }
  618.     }
  619.  
  620.     /**
  621.      * Refreshes notifications - shows new ones (with animation) and hides the closed ones
  622.      * @returns {undefined}
  623.      */
  624.     function refresh_notifications() {
  625.         storage_get_settings();
  626.  
  627.         if (settings.show_notifications === false) {
  628.             return;
  629.         }
  630.  
  631.         var conquers = JSON.parse(localStorage.conquers_script_old_data);
  632.         var notifications = storage_get_notifications();
  633.         var order = storage_get_notifications_order();
  634.         var notifications_data_changed = false;
  635.  
  636.         $('#conquers_script_notifications_container > div').each(function() {
  637.             if (notifications.hasOwnProperty($(this).attr('id')) === false) {
  638.                 $(this).remove();
  639.             }
  640.         });
  641.  
  642.         var i, n = order.length;
  643.         for (i = 0; i < n; i++) {
  644.             var conquer_id = order[i];
  645.             if ($('#' + conquer_id).length === 0) {
  646.                 if (i === 0) {
  647.                     $('#conquers_script_notifications_container').prepend($(get_notification_html(conquer_id, conquers[conquer_id])).hide());
  648.                 } else {
  649.                     $('#conquers_script_notifications_container > div:eq(' + (i-1) + ')').after($(get_notification_html(conquer_id, conquers[conquer_id])).hide());
  650.                 }
  651.  
  652.                 if (notifications[conquer_id].is_new) {
  653.                     notifications[conquer_id].is_new = false;
  654.                     notifications_data_changed = true;
  655.                     animate_notification(conquer_id);
  656.                 } else {
  657.                     $('#' + conquer_id).show();
  658.                 }
  659.             }
  660.         }
  661.  
  662.         toggle_close_all_button();
  663.         toggle_notifications_wrapper();
  664.  
  665.         if (notifications_data_changed) {
  666.             storage_set_notifications(notifications);
  667.             storage_set_notifications_order(get_objects_order_array(notifications));
  668.         }
  669.     }
  670.  
  671.     /**
  672.      * Removes single notification data and hides it by its id
  673.      * @param {String} conquer_id
  674.      * @returns {undefined}
  675.      */
  676.     function close_notification(conquer_id) {
  677.         $('#'+conquer_id).addClass('anim_remove');
  678.         setTimeout(function() {
  679.             var notifications = storage_get_notifications();
  680.             delete notifications[conquer_id];
  681.             $('#' + conquer_id).remove();
  682.             toggle_close_all_button();
  683.             toggle_notifications_wrapper();
  684.             storage_set_notifications(notifications);
  685.             storage_set_notifications_order(get_objects_order_array(notifications));
  686.         }, 400);
  687.     }
  688.  
  689.     /**
  690.      * Removes all notifications from localStorage
  691.      * @returns {undefined}
  692.      */
  693.     function remove_all_notifications() {
  694.         storage_set_notifications({});
  695.         storage_set_notifications_order([]);
  696.     }
  697.  
  698.     /**
  699.      * Removes all notifications data from localStorage and hides all notifications
  700.      * @returns {undefined}
  701.      */
  702.     function close_all_notifications() {
  703.         hide_all_notifications();
  704.         toggle_close_all_button();
  705.         toggle_notifications_wrapper();
  706.         remove_all_notifications();
  707.     }
  708.  
  709.     /**
  710.      * Automatically fills the message title and body at "New message" page
  711.      * @param {String} conquer_id
  712.      * @returns {undefined}
  713.      */
  714.     function fill_new_pm_fields(conquer_id) {
  715.         var conquer = JSON.parse(localStorage.conquers_script_old_data)[conquer_id];
  716.  
  717.         if (conquer.owner.old.id === 0) {
  718.             $('[name=subject]').val(`${conquer.owner.new.name} przejął barbę na ${conquer.village.continent}`);
  719.         } else {
  720.             $('[name=subject]').val(`${conquer.owner.new.name} przejął wioskę od ${conquer.owner.old.name}`);
  721.         }
  722.  
  723.  
  724.         var date = new Date(conquer.timestamp * 1000);
  725.         var hours = (date.getHours() < 10) ? '0'+date.getHours() : date.getHours();
  726.         var minutes = (date.getMinutes() < 10) ? '0'+date.getMinutes() : date.getMinutes();
  727.  
  728.         var text = `${hours}:${minutes} - [player]${conquer.owner.new.name}[/player] `;
  729.  
  730.         if (conquer.owner.new.ally.id !== 0) {
  731.             text += `([ally]${conquer.owner.new.ally.tag}[/ally]) `;
  732.         }
  733.  
  734.         text += `podbija [coord]${conquer.village.x}|${conquer.village.y}[/coord] od `;
  735.  
  736.         if (conquer.owner.old.id !== 0) {
  737.             text += `[player]${conquer.owner.old.name}[/player] `;
  738.  
  739.             if (conquer.owner.old.ally.id !== 0) {
  740.                 text += `([ally]${conquer.owner.old.ally.tag}[/ally])`;
  741.             }
  742.         } else {
  743.             text += `[i]wioska barbarzyńska[/i]`;
  744.         }
  745.  
  746.         $('#message').val(text);
  747.     }
  748.  
  749.     /**
  750.      * Shows or hides "Close all" button based on the amount of notifications
  751.      * @returns {undefined}
  752.      */
  753.     function toggle_close_all_button() {
  754.         if ($('#conquers_script_notifications_container > div').length < 3 && $('#conquers_script_close_all_notifications').is(':visible')) {
  755.             $('#conquers_script_close_all_notifications').hide();
  756.         } else if ($('#conquers_script_notifications_container > div').length >= 3 && $('#conquers_script_close_all_notifications').is(':hidden')) {
  757.             $('#conquers_script_close_all_notifications').show();
  758.         }
  759.     }
  760.  
  761.     function toggle_notifications_wrapper() {
  762.         if ($('#conquers_script_notifications_container > div').length === 0 && $('#conquers_script_notifications_wrapper').is(':visible')) {
  763.             $('#conquers_script_notifications_wrapper').hide();
  764.         } else if ($('#conquers_script_notifications_container > div').length !== 0 && $('#conquers_script_notifications_wrapper').is(':hidden')) {
  765.             $('#conquers_script_notifications_wrapper').show();
  766.         }
  767.     }
  768.  
  769.     /**
  770.      * Returns current unix timestamp in seconds
  771.      * @returns {Number}
  772.      */
  773.     function get_current_timestamp() {
  774.         return Math.floor(Date.now() / 1000);
  775.     }
  776.  
  777.     function refresh_counter() {
  778.         var found = $('#conquers_script_conquers').find('tr:visible').length;
  779.         $('#conquers_script_search').find('span').text(`Podboje: ${found}`);
  780.     }
  781.  
  782.     /**
  783.      * Updates settings variable and settings in localStorage
  784.      * @returns {undefined}
  785.      */
  786.     function update_settings() {
  787.         var remove_old_data = false;
  788.         storage_get_settings();
  789.  
  790.         settings.show_own = $('#checkbox_own').prop('checked');
  791.         settings.show_allies = $('#checkbox_allies').prop('checked');
  792.         settings.show_enemies = $('#checkbox_enemies').prop('checked');
  793.         settings.show_interesting = $('#checkbox_inter').prop('checked');
  794.         settings.show_others = $('#checkbox_others').prop('checked');
  795.         settings.show_victims = $('#checkbox_victims').prop('checked');
  796.         settings.show_range = $('#checkbox_range').prop('checked');
  797.         settings.settings_for_conquers = $('#checkbox_settings_for_conquers').prop('checked');
  798.         settings.range = Number($('#conquers_range').val());
  799.         settings.show_notifications = $('#checkbox_notifications').prop('checked');
  800.         settings.animation_style = $('#animation_style').val();
  801.         settings.notification_style = $('#notification_style').val();
  802.         if (settings.notifications_lifetime !== Number($('#notifications_lifetime').val())*3600) {
  803.             settings.notifications_lifetime = Number($('#notifications_lifetime').val())*3600;
  804.             remove_old_data = true;
  805.         }
  806.         if (settings.conquers_lifetime !== Number($('#conquers_lifetime').val())*3600) {
  807.             settings.conquers_lifetime = Number($('#conquers_lifetime').val())*3600;
  808.             remove_old_data = true;
  809.         }
  810.         settings.additional_villages = $('#additional_villages').val();
  811.         storage_set_settings();
  812.  
  813.         if (remove_old_data) {
  814.             remove_old_notifications();
  815.             remove_old_conquers_data();
  816.         }
  817.  
  818.         hide_all_notifications();
  819.         display_all_notifications();
  820.  
  821.         if (settings.show_notifications === false) {
  822.             remove_all_notifications();
  823.             toggle_close_all_button();
  824.             toggle_notifications_wrapper();
  825.         }
  826.     }
  827.  
  828.     /**
  829.      * Initialization, sets onclick handlers, inserts CSS
  830.      * @returns {undefined}
  831.      */
  832.     function init() {
  833.         if (storage_settings_exist() === false) {
  834.             settings = {
  835.                 own_tribe: '',
  836.                 allies: [],
  837.                 enemies: [],
  838.                 interesting: [],
  839.                 show_own: false,
  840.                 show_allies: false,
  841.                 show_enemies: true,
  842.                 show_interesting: true,
  843.                 show_others: false,
  844.                 show_victims: false,
  845.                 show_range: true,
  846.                 range: 10,
  847.                 show_notifications: true,
  848.                 notification_style: "classic",
  849.                 animation_style: 'b',
  850.                 notifications_lifetime: 6*3600,
  851.                 conquers_lifetime: 24*3600,
  852.                 settings_for_conquers: false,
  853.                 additional_villages: ''
  854.             };
  855.  
  856.             load_diplomacy_to_settings();
  857.             storage_set_settings();
  858.         } else {
  859.             storage_get_settings();
  860.         }
  861.  
  862.  
  863.  
  864.         if (localStorage.getItem('conquers_script_notifications') === null) {
  865.             localStorage.setItem('conquers_script_notifications', '{}');
  866.         }
  867.  
  868.  
  869.  
  870.  
  871.  
  872.  
  873.  
  874.  
  875.         $('body').on('click', '#conquers_script_settings', function(e) {
  876.             e.preventDefault();
  877.  
  878.             if ($(this).text() === 'ustawienia') {
  879.                 $('#inline_popup_content').html('<div style="padding:2%;box-sizing:border-box;">' + create_settings() + '</div>');
  880.                 UI.ToolTip($('.ui_tooltip'));
  881.                 $(this).text('akceptuj');
  882.             } else {
  883.                 if (Number($('#conquers_range').val()) > 0) {
  884.                     update_settings();
  885.  
  886.                     $('#inline_popup_content').html(create_gui_content());
  887.                     refresh_counter();
  888.                     $(this).text('ustawienia');
  889.                 } else {
  890.                     alert('Odległość filtrowania przejęć musi być liczbą pozytywną.');
  891.                     $('#conquers_range').css('background', 'rgb(255,77,77)');
  892.                 }
  893.             }
  894.  
  895.         });
  896.  
  897.  
  898.  
  899.  
  900.  
  901.         $('body').on('keyup', '#conquers_script_search input', function() {
  902.             var search_for = $(this).val();
  903.  
  904.             if (search_for.trim() !== '') {
  905.                 $('#conquers_script_conquers').find('tr').each(function() {
  906.                     if ($(this).text().toLowerCase().includes(search_for.toLowerCase())) {
  907.                         $(this).show();
  908.                     } else {
  909.                         $(this).hide();
  910.                     }
  911.  
  912.                 });
  913.             } else {
  914.                 $('#conquers_script_conquers').find('tr').each(function() {
  915.                     $(this).show();
  916.                 });
  917.             }
  918.  
  919.             refresh_counter();
  920.         });
  921.  
  922.  
  923.  
  924.         $('body').on('click', '#load_diplomacy', function() {
  925.             update_settings();
  926.             delete localStorage.conquers_script_own_tribe;
  927.             delete localStorage.conquers_script_diplomacy;
  928.             reload_diplomacy = true;
  929.             inlinePopupClose();
  930.             UI.SuccessMessage('Skrypt pobiera informacje o dyplomacji Twojego plemienia. Potrwa to max kilka sekund.', 1000);
  931.         });
  932.  
  933.  
  934.  
  935.         $('body').on('click', '.conquers_script_tribes_add', function() {
  936.             var input_tribe = prompt('Wprowadź skrót plemienia:');
  937.             if (settings[$(this).attr('data-type')].indexOf(input_tribe) === -1 && input_tribe !== null && input_tribe.trim() !== '') {
  938.                 if (input_tribe.length > 6) {
  939.                     alert('Wprowadź skrót plemienia (max 6 znaków) a nie jego nazwę.');
  940.                 } else {
  941.                     settings[$(this).attr('data-type')].push(input_tribe); //type -> allies/enemies etc.
  942.  
  943.                     storage_set_settings();
  944.  
  945.                     $(this).parent().append(` <span><a href="#" class="conquers_script_tribes_remove" data-type="${$(this).attr('data-type')}">${input_tribe} <img src="${image_base}delete_small.png" ></a></span>`);
  946.                 }
  947.             }
  948.         });
  949.  
  950.         $('body').on('click', '.conquers_script_tribes_remove', function() {
  951.             var index = settings[$(this).attr('data-type')].indexOf($(this).parent().text().trim());
  952.             settings[$(this).attr('data-type')].splice(index, 1); //type -> allies/enemies etc.
  953.             storage_set_settings();
  954.             $(this).parent().remove();
  955.         });
  956.  
  957.  
  958.         var wrapper_top = $('#header_info').offset().top + $('#header_info').height() + 10 + 30; // +10px to be below the buttons and another +30px for "Close all" button
  959.         $('head').append(`
  960.             <style>
  961.                 #conquers_script_notifications_wrapper {
  962.                     position: fixed;
  963.                     top: ${wrapper_top}px;
  964.                     right: 20px;
  965.                     height: 60%;
  966.                     width: 350px;
  967.                     padding-right: 5px;
  968.                     overflow-y: auto;
  969.                     overflow-x: hidden;
  970.                     z-index: 100;
  971.                 }
  972.  
  973.  
  974.                 #conquers_script_search input {
  975.                     display: block;
  976.                     float: left;
  977.                     box-sizing: content-box;
  978.                     height: 18px;
  979.                     width: 150px;
  980.                     padding: 1px 6px 1px 6px;
  981.                     border: 1px solid rgb(129, 66, 2);
  982.                     background-color: white;
  983.                 }
  984.  
  985.                 #conquers_script_search div {
  986.                     background-image: url("`+image_base+`search.png");
  987.                     background-repeat: no-repeat;
  988.                     background-position: center;
  989.                     width: 18px;
  990.                     height: 22px;
  991.                     float: left;
  992.                 }
  993.                
  994.  
  995.                 #conquers_script_search span {
  996.                     line-height: 22px;
  997.                 }
  998.  
  999.  
  1000.  
  1001.                 #conquers_script_close_all_notifications {
  1002.                     position: fixed;
  1003.                     top: ${wrapper_top - 30}px;
  1004.                     right: 40px;
  1005.                     height: 25px;
  1006.                     width: 140px;
  1007.                     opacity: 0.5;
  1008.                 }
  1009.  
  1010.                 #conquers_script_close_all_notifications:hover {
  1011.                     opacity: 1;
  1012.                     cursor: pointer;
  1013.                 }
  1014.  
  1015.                 #conquers_script_notifications_container {
  1016.                     width: 100%;
  1017.                 }
  1018.  
  1019.                 #conquers_script_notifications_container > div {
  1020.                     padding-left: 25px;
  1021.                 }
  1022.  
  1023.                 .notification_style_dark {
  1024.                     background: rgba(0, 0, 0, 0.6); width:100%; box-sizing: border-box; margin-right: 5px; padding:15px; color:#fff; font-weight:700; border-radius:10px; margin-top:20px; position:relative; text-align:center;
  1025.                 }
  1026.  
  1027.                 .notification_style_dark a {
  1028.                     color: #c1a264;
  1029.                     white-space: nowrap;
  1030.                 }
  1031.  
  1032.                 .notification_style_classic {
  1033.                     position:relative;
  1034.                     background: #fff5da;
  1035.                     width:100%;
  1036.                     box-sizing: border-box;
  1037.                     margin-right: 5px;
  1038.                     padding:10px;
  1039.                     color: #000;
  1040.                     font-weight: 700;
  1041.                     border: 1px solid #804000;
  1042.                     margin-bottom: 20px;
  1043.                     text-align: center;
  1044.                 }
  1045.  
  1046.                 .notification_close {
  1047.                     position: absolute;
  1048.                     left: 8px;
  1049.                     cursor: pointer;
  1050.                 }
  1051.  
  1052.                 .notification_style_dark .notification_close {
  1053.                     visibility: hidden;
  1054.                 }
  1055.  
  1056.                 .notification_style_dark:hover .notification_close {
  1057.                     visibility: visible;
  1058.                 }
  1059.  
  1060.                 .notification_send_pm {
  1061.                     cursor: pointer;
  1062.                 }
  1063.  
  1064.  
  1065.                 .conquers_script_tribes_remove {
  1066.                     white-space: nowrap;
  1067.                 }
  1068.  
  1069.  
  1070.                 #inline_popup {
  1071.                     width: 400px;
  1072.                 }
  1073.  
  1074.                 #inline_popup_main span.ui_tooltip {
  1075.                     cursor: help;
  1076.                 }
  1077.  
  1078.  
  1079.                 .anim_scale {
  1080.                     animation-duration: 600ms;
  1081.                     animation-name: anim-scale;
  1082.                 }
  1083.  
  1084.                 .anim_fly-in {
  1085.                     animation-duration: 500ms;
  1086.                     animation-name: anim-fly-in;
  1087.                 }
  1088.  
  1089.                 .anim_third-dimension {
  1090.                     animation-duration: 600ms;
  1091.                     animation-name: anim-third-dimension;
  1092.                 }
  1093.  
  1094.                 .anim_remove {
  1095.                     animation-duration: 400ms;
  1096.                     animation-name: anim-remove;
  1097.                 }
  1098.  
  1099.                 .anim_fall-down {
  1100.                     transform-origin: bottom;
  1101.                     animation-duration: 500ms;
  1102.                     animation-name: anim-fall-down;
  1103.                 }
  1104.  
  1105.                 @keyframes anim-scale {
  1106.                     0% {
  1107.                         transform: scale(0);
  1108.                     }
  1109.                     60% {
  1110.                         transform: scale(1);
  1111.                     }
  1112.                     80% {
  1113.                         transform: scale(0.8);
  1114.                     }
  1115.                     100% {
  1116.                         transform: scale(1);
  1117.                     }
  1118.                 }
  1119.  
  1120.                 @keyframes anim-fly-in {
  1121.                     0% {
  1122.                         transform: translateX(500px);
  1123.                     }
  1124.                     60% {
  1125.                         transform: translateX(0px);
  1126.                     }
  1127.                     80% {
  1128.                         transform: translateX(20px);
  1129.                     }
  1130.                     100% {
  1131.                         transform: translateX(0px);
  1132.                     }
  1133.                 }
  1134.  
  1135.                 @keyframes anim-third-dimension {
  1136.                     0% {
  1137.                         transform: rotateX(90deg);
  1138.                     }
  1139.                     5% {
  1140.                         transform: rotateX(88deg);
  1141.                     }
  1142.                     15% {
  1143.                         transform: rotateX(88deg);
  1144.                     }
  1145.                     80% {
  1146.                         transform: rotateX(-25deg);
  1147.                     }
  1148.                     100% {
  1149.                         transform: rotateX(0deg);
  1150.                     }
  1151.                 }
  1152.  
  1153.                 @keyframes anim-remove {
  1154.                     0% {
  1155.                         transform: scale(1);
  1156.                     }
  1157.                     100% {
  1158.                         transform: scale(0);
  1159.                     }
  1160.                 }
  1161.  
  1162.                 @keyframes anim-fall-down {
  1163.                     0% {
  1164.                         transform: translateY(-500px) rotateX(0deg);
  1165.                     }
  1166.                     70% {
  1167.                         transform: translateY(0px) rotateX(50deg);
  1168.                     }
  1169.                     85% {
  1170.                         transform: translateY(0px) rotateX(-30deg);
  1171.                     }
  1172.                     100% {
  1173.                         transform: translateY(0px) rotateX(0deg);
  1174.                     }
  1175.                 }
  1176.             </style>
  1177.         `);
  1178.  
  1179.         $('body').append('<div id="conquers_script_notifications_wrapper"><div id="conquers_script_notifications_container"></div></div>');
  1180.         $('#conquers_script_notifications_container').on('click', '.notification_close', function() {
  1181.             close_notification($(this).parent().attr('id'));
  1182.         });
  1183.         $('#conquers_script_notifications_container').on('click', '.notification_send_pm', function() {
  1184.             window.open('/game.php?' + t + 'screen=mail&mode=new&conquer_id=' + $(this).parent().attr('id'));
  1185.         });
  1186.  
  1187.         if (game_data.hasOwnProperty('screen') && game_data.screen === 'mail' && game_data.hasOwnProperty('mode') && game_data.mode === 'new' && window.location.search.indexOf('conquer_id=') !== -1) {
  1188.             fill_new_pm_fields((new URLSearchParams(window.location.search)).get('conquer_id'));
  1189.         }
  1190.  
  1191.         $('body').append('<input type="button" id="conquers_script_close_all_notifications" class="btn" value="Zamknij wszystko" style="display: none;">');
  1192.         $('#conquers_script_close_all_notifications').click(function() {
  1193.             UI.ConfirmationBox('Czy na pewno chcesz usunąć wszystkie powiadomienia?', [{
  1194.                 text: "Usuń",
  1195.                 callback: close_all_notifications,
  1196.                 confirm: true
  1197.             }]);
  1198.         });
  1199.     }
  1200.  
  1201.     /**
  1202.      * Creates main "Conquers" button next to the village's coords
  1203.      * @returns {undefined}
  1204.      */
  1205.     function create_conquers_button() {
  1206.         $('#header_info td').first().after('<td><input id="conquers_script_button" type="button" class="btn btn-disabled" value="Podboje" style="position: relative; left: 5px; top: -3px;" title="Skrypt pobiera teraz dane o podbojach. Potrwa to kilka sekund."></td>'); //'main' button
  1207.         $('#conquers_script_button').tooltip(tooltip_options);
  1208.  
  1209.         $('#conquers_script_button').on('click', function(e) {
  1210.             if (is_downloader_working() === false) {
  1211.                 storage_get_settings();
  1212.                 display_inline_popup(true, e);
  1213.             }
  1214.         });
  1215.     }
  1216.  
  1217.     /**
  1218.      * Checks if downloader is currently downloading some data
  1219.      * @returns {Boolean}
  1220.      */
  1221.     function is_downloader_working() {
  1222.         return (localStorage.getItem('conquers_script_is_downloading_data') === null || localStorage.conquers_script_is_downloading_data === '1' || localStorage.getItem('conquers_script_last_download') === null || Number(localStorage.conquers_script_last_download)+2 > get_current_timestamp());
  1223.     }
  1224.  
  1225.     /**
  1226.      * Enables or disables main "Conquers" button based on whether some data is being downloaded or not
  1227.      * @returns {undefined}
  1228.      */
  1229.     function toggle_conquers_button() {
  1230.         if (is_downloader_working() && $('#conquers_script_button').hasClass('btn-disabled') === false) {
  1231.             $('#conquers_script_button').addClass('btn-disabled').prop('title', 'Skrypt pobiera teraz dane o podbojach. Potrwa to kilka sekund.').tooltip(tooltip_options);
  1232.         } else if (is_downloader_working() === false && $('#conquers_script_button').hasClass('btn-disabled')) {
  1233.             $('#conquers_script_button').removeClass('btn-disabled').prop('title', '').tooltip(tooltip_options);
  1234.         }
  1235.     }
  1236.  
  1237.  
  1238.  
  1239.  
  1240.     create_conquers_button();
  1241.  
  1242.     var interval_id = setInterval(function() {
  1243.         if (is_downloader_working() === false) {
  1244.             clearInterval(interval_id);
  1245.  
  1246.             init();
  1247.             remove_old_notifications();
  1248.             remove_old_conquers_data();
  1249.             display_all_notifications();
  1250.             update_notifications_list();
  1251.             refresh_notifications();
  1252.  
  1253.             setInterval(toggle_conquers_button, 100);
  1254.  
  1255.             setInterval(function() {
  1256.                 update_notifications_list();
  1257.                 refresh_notifications();
  1258.             }, 1000);
  1259.  
  1260.             setInterval(function() {
  1261.                 remove_old_notifications();
  1262.                 remove_old_conquers_data();
  1263.             }, 60000);
  1264.  
  1265.             setInterval(function() {
  1266.                 if (reload_diplomacy === true && storage_settings_exist() && localStorage.getItem('conquers_script_own_tribe') !== null && localStorage.getItem('conquers_script_diplomacy') !== null) {
  1267.                     reload_diplomacy = false;
  1268.                     storage_get_settings();
  1269.                     load_diplomacy_to_settings();
  1270.                     storage_set_settings();
  1271.                     hide_all_notifications();
  1272.                     display_all_notifications();
  1273.                     toggle_close_all_button();
  1274.                     toggle_notifications_wrapper();
  1275.                     display_inline_popup(false);
  1276.                 }
  1277.             }, 200);
  1278.         }
  1279.     }, 500);
  1280.  
  1281. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement