Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Powiadomienia o podbojach - wyświetlanie danych
- // @description Skrypt wyświetla dane o niedawno przejętych wioskach spełniających dane kryteria
- // @author stivens, Makak
- // @include https://pl*.plemiona.pl/game.php?*
- // @version 1.1.2
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- var settings;
- var image_base = window.image_base;
- var UI = window.UI;
- var game_data = window.game_data;
- var player_id = (Number(game_data.player.sitter) !== 0) ? Number(game_data.player.sitter) : Number(game_data.player.id);
- var t = (Number(game_data.player.sitter) !== 0) ? 't=' + game_data.player.id + '&' : '';
- var reload_diplomacy = false;
- var tooltip_options = {showURL: false, track: true, fade: true, delay: false, showBody: ' :: ', extraClass: 'tooltip-style'};
- /**
- * Returns URL to a dot based on new village owner's tribe
- * @param {String} tribe_tag
- * @returns {String} url to dot image
- */
- function get_dot_url(tribe_tag) {
- storage_get_settings();
- var dot;
- if (settings.allies.indexOf(tribe_tag) !== -1) {
- dot = 'green';
- } else if (settings.own_tribe === tribe_tag) {
- dot = 'green';
- } else if (settings.enemies.indexOf(tribe_tag) !== -1) {
- dot = 'red';
- } else if (settings.interesting.indexOf(tribe_tag) !== -1) {
- dot = 'yellow';
- } else {
- dot = 'blue';
- }
- return image_base + '/dots/' + dot + '.png';
- }
- /**
- * Creates HTML string with list of conquers
- * @returns {String} content
- */
- function create_gui_content() {
- var content = `<div id="conquers_script_search">
- <input type="text" placeholder="Szukaj">
- <div></div>
- <span></span>
- </div>
- <table class="vis" width="100%" id="conquers_script_conquers">
- <tbody>`;
- var row_class = ['row_a', 'row_b'];
- var row_number = 0;
- var conquers = JSON.parse(localStorage.conquers_script_old_data);
- var array_of_ids = get_objects_order_array(conquers);
- for (var i = 0, n = array_of_ids.length; i < n; i++) {
- if (settings.settings_for_conquers === false || is_conquer_interesting(conquers[array_of_ids[i]])) {
- content += get_conquer_html(conquers[array_of_ids[i]], row_class[row_number%2]);
- row_number++;
- }
- }
- content += `</tbody></table>`;
- return content;
- }
- /**
- * Checks if player is in tribe and has access to tribe functions (in case of a sitter)
- * @returns {Boolean}
- */
- function player_in_tribe() {
- var is_in_tribe = (game_data.player.ally !== '0');
- var has_tribe_access = ($('#tribe_forum_indicator').length === 1);
- return (is_in_tribe && has_tribe_access);
- }
- /**
- * Creates HTML string with script's settings
- * @returns {String} content
- */
- function create_settings() {
- storage_get_settings();
- 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>
- <span><button class="btn conquers_script_tribes_add" data-type="allies">+</button> <strong>Sojusznicy</strong>:`;
- for (var ally in settings.allies) {
- if (settings.allies[ally] !== "") {
- 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>`;
- }
- }
- settings_content += `</span><br><span><button class="btn conquers_script_tribes_add" data-type="enemies">+</button> <strong>Wrogowie</strong>:`;
- for (var enemy in settings.enemies) {
- if (settings.enemies[enemy] !== "") {
- 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>`;
- }
- }
- settings_content += `</span><br><span><button class="btn conquers_script_tribes_add" data-type="interesting">+</button> <strong>Interesujące</strong>:`;
- for (var inter in settings.interesting) {
- if (settings.interesting[inter] !== "") {
- 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>`;
- }
- }
- settings_content += `</span><br><br><br><br>
- <h3>POWIADOMIENIA</h3>
- <input type="checkbox" id="checkbox_notifications" ${(settings.show_notifications === true ? 'checked ' : '')}>Pokaż powiadomienia
- <select id="notification_style">
- <option value="classic">Makak\'s classic</option>
- <option value="dark" ${(settings.notification_style === 'dark' ? 'selected' : '')}>stivens\'s dark</option>
- </select><br><hr>Pokaż powiadomienia dla przejęć dokonanych przez<br>
- <input type="checkbox" id="checkbox_own" ${(settings.show_own === true ? 'checked ' : '')}> własne plemię
- <input type="checkbox" id="checkbox_allies" ${(settings.show_allies === true ? 'checked ' : '')}> sojuszników
- <input type="checkbox" id="checkbox_enemies" ${(settings.show_enemies === true ? 'checked ' : '')}> wrogów <br>
- <input type="checkbox" id="checkbox_inter" ${(settings.show_interesting === true ? 'checked ' : '')}> interesujących
- <span style="white-space: nowrap;"><input type="checkbox" id="checkbox_others" ${(settings.show_others === true ? 'checked ' : '')}> wszystkie plemiona</span><br> <br>
- <input type="checkbox" id="checkbox_victims" ${(settings.show_victims === true ? 'checked ' : '')}> Pokaż również powiadomienie w wypadku straty wioski<br><hr> <br>
- <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>
- <input type="checkbox" style="opacity:0;">Animacja powiadomień
- <select id="animation_style">
- <option value="0">Przenikanie</option>
- <option value="a" ${(settings.animation_style === 'a' ? 'selected' : '')}>Skok</option>
- <option value="b" ${(settings.animation_style === 'b' ? 'selected' : '')}>Wleć z prawej</option>
- <option value="c" ${(settings.animation_style === 'c' ? 'selected' : '')}>Trzeci wymiar</option>
- <option value="d" ${(settings.animation_style === 'd' ? 'selected' : '')}>Spadnij</option>
- </select><br>
- <input type="checkbox" style="opacity:0;">Powiadomienia nie starsze niż
- <select id="notifications_lifetime">
- <option value="1">1h</option>
- <option value="2" ${(settings.notifications_lifetime === 2*3600 ? 'selected' : '')}>2h</option>
- <option value="3" ${(settings.notifications_lifetime === 3*3600 ? 'selected' : '')}>3h</option>
- <option value="4" ${(settings.notifications_lifetime === 4*3600 ? 'selected' : '')}>4h</option>
- <option value="5" ${(settings.notifications_lifetime === 5*3600 ? 'selected' : '')}>5h</option>
- <option value="6" ${(settings.notifications_lifetime === 6*3600 ? 'selected' : '')}>6h</option>
- <option value="12" ${(settings.notifications_lifetime === 12*3600 ? 'selected' : '')}>12h</option>
- </select><br><br><br><input type="checkbox" style="opacity:0;">Podboje nie starsze niż
- <select id="conquers_lifetime">
- <option value="12" ${(settings.conquers_lifetime === 12*3600 ? 'selected' : '')}>12h</option>
- <option value="24" ${(settings.conquers_lifetime === 24*3600 ? 'selected' : '')}>24h</option>
- <option value="36" ${(settings.conquers_lifetime === 36*3600 ? 'selected' : '')}>36h</option>
- <option value="48" ${(settings.conquers_lifetime === 48*3600 ? 'selected' : '')}>48h</option>
- <option value="72" ${(settings.conquers_lifetime === 72*3600 ? 'selected' : '')}>72h</option>
- </select>
- <br><input type="checkbox" id="checkbox_settings_for_conquers" ${(settings.settings_for_conquers === true ? 'checked ' : '')}>Zastosuj powyższe ustawienia dla przeglądu podbojów
- <br><br><br>
- <span class="ui_tooltip
- " 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>
- <textarea name="" id="additional_villages" cols="40" rows="5">${settings.additional_villages}</textarea>
- <br><br><br>
- <div style="font-size:0.9em;margin-bottom:5px;">
- Skrypt autorstwa Makak & stivens. Po więcej zapraszamy na <a href="https://www.plemsy.pl/" target="_BLANK">plemsy.pl</a>
- </div>`;
- return settings_content;
- }
- /**
- * Opens inlinePopup
- * @param {Boolean} display_conquers Whether to display conquers or settings
- * @param {Object} e Click event
- * @returns {undefined}
- */
- function display_inline_popup(display_conquers, e = null) {
- storage_get_settings();
- var popup_position = {};
- if (e) {
- popup_position.clientX = e.clientX;
- popup_position.clientY = e.clientY;
- }
- if (display_conquers) {
- inlinePopup(popup_position, 'conquers_script', null, {offset_x: 0, offset_y: 0}, create_gui_content(), 'Powiadomienia o podbojach');
- $('span:contains(Powiadomienia o podbojach)').append(' <a href="#" id="conquers_script_settings" style="font-size:0.8em;">ustawienia</a>');
- } else {
- 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');
- $('span:contains(Powiadomienia o podbojach)').append(' <a href="#" id="conquers_script_settings" style="font-size:0.8em;">akceptuj</a>');
- }
- refresh_counter();
- }
- /**
- * Returns given conquer's html string
- * @param {Object} conquer
- * @param {String} class_name
- * @returns {String} html
- */
- function get_conquer_html(conquer, class_name) {
- var date = new Date(conquer.timestamp * 1000);
- var hours = (date.getHours() < 10) ? '0'+date.getHours() : date.getHours();
- var minutes = (date.getMinutes() < 10) ? '0'+date.getMinutes() : date.getMinutes();
- var text = `<tr class="${class_name}">
- <td style="padding: 8px; text-align: center; width: 40px;">${hours}:${minutes}</td>
- <td style="padding: 8px;">
- <img src="${get_dot_url(conquer.owner.new.ally.tag)}">
- <a href="/game.php?${t}screen=info_player&id=${conquer.owner.new.id}">${conquer.owner.new.name}</a> `;
- if (conquer.owner.new.ally.id !== 0) {
- 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> `;
- }
- text += `podbija <a href="/game.php?${t}screen=info_village&id=${conquer.village.id}">${conquer.village.name}</a> od `;
- if (conquer.owner.old.id !== 0) {
- text += `<a href="/game.php?${t}screen=info_player&id=${conquer.owner.old.id}">${conquer.owner.old.name}</a> `;
- if (conquer.owner.old.ally.id !== 0) {
- 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>`;
- }
- } else {
- text += `<i>wioska barbarzyńska</i>`;
- }
- return text + `</td></tr>`;
- }
- /**
- * Returns notification's html string for given conquer
- * @param {String} conquer_id
- * @param {Object} conquer
- * @returns {String} html
- */
- function get_notification_html(conquer_id, conquer) {
- storage_get_settings();
- var text = `<div id="${conquer_id}" class="notification_style_${settings.notification_style}">
- <img src="${image_base}delete.png" height="14" class="notification_close">
- <img src="${image_base}new_mail.png" class="notification_send_pm">
- <img src="${get_dot_url(conquer.owner.new.ally.tag)}">
- <a href="/game.php?${t}screen=info_player&id=${conquer.owner.new.id}">${conquer.owner.new.name}</a>
- podbija <a href="/game.php?${t}screen=info_village&id=${conquer.village.id}">${conquer.village.name}</a> od `;
- if (conquer.owner.old.id !== 0) {
- text += `<a href="/game.php?${t}screen=info_player&id=${conquer.owner.old.id}">${conquer.owner.old.name}</a>`;
- } else {
- text += `<i>wioska barbarzyńska</i>`;
- }
- return text + '</div>';
- }
- /**
- * Checks if settings are stored in localStorage or not (first-time usage)
- * @returns {Boolean} settings_exist
- */
- function storage_settings_exist() {
- return (localStorage.getItem('conquers_script_settings') !== null);
- }
- /**
- * Saves settings in localStorage
- * @returns {undefined}
- */
- function storage_set_settings() {
- localStorage.conquers_script_settings = JSON.stringify(settings);
- }
- /**
- * Reads settings from localStorage
- * @returns {undefined}
- */
- function storage_get_settings() {
- settings = JSON.parse(localStorage.conquers_script_settings);
- }
- /**
- * Loads diplomacy data from localStorage to settings object
- * @returns {undefined}
- */
- function load_diplomacy_to_settings() {
- if (localStorage.getItem('conquers_script_own_tribe') !== null) {
- settings.own_tribe = localStorage.getItem('conquers_script_own_tribe');
- }
- if (localStorage.getItem('conquers_script_diplomacy') !== null) {
- var diplomacy = JSON.parse(localStorage.conquers_script_diplomacy);
- settings.allies = diplomacy.allies;
- settings.enemies = diplomacy.enemies;
- }
- }
- /**
- * Updates notifications data in localStorage
- * @param {Object} notifications
- * @returns {undefined}
- */
- function storage_set_notifications(notifications) {
- localStorage.conquers_script_notifications = JSON.stringify(notifications);
- }
- /**
- * Reads notifications object from localStorage
- * @returns {Object} notifications
- */
- function storage_get_notifications() {
- return JSON.parse(localStorage.conquers_script_notifications);
- }
- /**
- * Updates notifications order data in localStorage
- * @param {Array} sorted_array_of_ids
- * @returns {undefined}
- */
- function storage_set_notifications_order(sorted_array_of_ids) {
- localStorage.conquers_script_notifications_order = JSON.stringify(sorted_array_of_ids);
- }
- /**
- * Reads notifications order array from localStorage
- * @returns {Array} notifications order
- */
- function storage_get_notifications_order() {
- return JSON.parse(localStorage.conquers_script_notifications_order);
- }
- /**
- * Creates sorted array of conquers' ids
- * @param {Object} object Conquers or notifications object
- * @returns {Array} array of strings (conquers' ids) sorted by timestamp
- */
- function get_objects_order_array(object) {
- var tmp = [];
- for (var id in object) {
- tmp.push({id: id, timestamp: object[id].timestamp});
- }
- tmp.sort(function(a, b) {
- return b.timestamp - a.timestamp;
- });
- var array_of_ids = [];
- for (var i = 0, n = tmp.length; i < n; i++) {
- array_of_ids.push(tmp[i].id);
- }
- return array_of_ids;
- }
- /**
- * Removes notifications older than X seconds and updates localStorage data
- * @returns {undefined}
- */
- function remove_old_notifications() {
- storage_get_settings();
- var timestamp = get_current_timestamp() - settings.notifications_lifetime;
- var notifications = JSON.parse(localStorage.conquers_script_notifications);
- for (var conquer_id in notifications) {
- if (notifications[conquer_id].timestamp < timestamp) {
- delete notifications[conquer_id];
- }
- }
- storage_set_notifications(notifications);
- storage_set_notifications_order(get_objects_order_array(notifications));
- }
- /**
- * Removes data for conquers older than X seconds and updates localStorage data
- * @returns {undefined}
- */
- function remove_old_conquers_data() {
- storage_get_settings();
- var timestamp = get_current_timestamp() - settings.conquers_lifetime;
- var conquers = JSON.parse(localStorage.conquers_script_old_data);
- for (var conquer_id in conquers) {
- if (conquers[conquer_id].timestamp < timestamp) {
- delete conquers[conquer_id];
- }
- }
- localStorage.conquers_script_old_data = JSON.stringify(conquers);
- }
- /**
- * Creates an array with additional villages coordinates
- * @returns {Array} list Array of {x, y} objects
- */
- function parse_additional_villages() {
- var list = [];
- var matches = settings.additional_villages.match(/\d{1,3}\|\d{1,3}/g);
- if (matches !== null) {
- for (var i = 0, n = matches.length; i < n; i++) {
- var coords = matches[i].split('|');
- list.push({
- x: Number(coords[0]),
- y: Number(coords[1])
- });
- }
- }
- return list;
- }
- /**
- * Checks if a village is within field range of player's villages
- * @param {Number} x
- * @param {Number} y
- * @param {Number} range
- * @returns {Boolean}
- */
- function is_village_within_range(x, y, range) {
- storage_get_settings();
- var villages = JSON.parse(localStorage['conquers_script_player_' + player_id + '_villages']);
- var additional_villages = parse_additional_villages();
- for (var i = 0, n = additional_villages.length; i < n; i++) {
- villages['additional_' + i] = additional_villages[i];
- }
- var x_diff, y_diff, square_range = range*range;
- for (var key in villages) {
- if (villages.hasOwnProperty(key)) {
- x_diff = villages[key].x - x;
- y_diff = villages[key].y - y;
- if (x_diff*x_diff + y_diff*y_diff <= square_range) {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * Checks if a "group" (allies/enemies) contains given tribe tag
- * @param {String} group_name
- * @param {String} tribe_tag
- * @returns {Boolean}
- */
- function group_contains_tribe(group_name, tribe_tag) {
- if (tribe_tag === '') {
- return false;
- }
- var tribes = settings[group_name];
- for (var i = 0, n = tribes.length; i < n; i++) {
- tribes[i] = tribes[i].toLowerCase();
- }
- return tribes.indexOf(tribe_tag.toLowerCase()) !== -1;
- }
- /**
- * Checks if conquer is interesting to the player (if is close to their villages etc)
- * @param {Object} conquer
- * @returns {Boolean} is_interesting
- */
- function is_conquer_interesting(conquer) {
- storage_get_settings();
- // 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)
- if (settings.show_range === true && is_village_within_range(conquer.village.x, conquer.village.y, settings.range) === false) {
- return false;
- }
- // User wants to see every tribe's conquers (also players not in tribe) so conquer is automatically interesting
- if (settings.show_others) {
- return true;
- } else {
- var old_tag = conquer.owner.old.ally.tag;
- var new_tag = conquer.owner.new.ally.tag;
- // Attacker/defender is in user's tribe
- if (settings.show_own && (settings.own_tribe === new_tag || (settings.show_victims && settings.own_tribe === old_tag))) {
- return true;
- }
- // Try to find a group that user wants to check and which contains attacker's/defender's tribe tag
- if (settings.show_allies && (group_contains_tribe('allies', new_tag) || (settings.show_victims && group_contains_tribe('allies', old_tag)))) {
- return true;
- }
- if (settings.show_enemies && (group_contains_tribe('enemies', new_tag) || (settings.show_victims && group_contains_tribe('enemies', old_tag)))) {
- return true;
- }
- if (settings.show_interesting && (group_contains_tribe('interesting', new_tag) || (settings.show_victims && group_contains_tribe('interesting', old_tag)))) {
- return true;
- }
- // Did NOT find any group that contains attacker's/defender's tribe so conquer is NOT interesting
- return false;
- }
- }
- /**
- * Reads new_data and updates notifications list
- * @returns {undefined}
- */
- function update_notifications_list() {
- storage_get_settings();
- if (settings.show_notifications === false) {
- localStorage.conquers_script_new_data = '{}';
- return;
- }
- var notifications = storage_get_notifications();
- var conquers = JSON.parse(localStorage.conquers_script_new_data);
- localStorage.conquers_script_new_data = '{}';
- var conquer_id;
- var contains_old_conquers = false;
- for (conquer_id in conquers) {
- if (conquers[conquer_id].timestamp < get_current_timestamp()-310) {
- contains_old_conquers = true;
- }
- }
- for (conquer_id in conquers) {
- 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) {
- notifications[conquer_id] = {
- is_new: !contains_old_conquers,
- timestamp: conquers[conquer_id].timestamp
- };
- }
- }
- storage_set_notifications(notifications);
- storage_set_notifications_order(get_objects_order_array(notifications));
- }
- /**
- * Hides all notifications from the screen
- * @returns {undefined}
- */
- function hide_all_notifications() {
- $('#conquers_script_notifications_container').empty();
- }
- /**
- * Shows all notifications (without animation)
- * @returns {undefined}
- */
- function display_all_notifications() {
- storage_get_settings();
- if (settings.show_notifications === false) {
- return;
- }
- var conquers = JSON.parse(localStorage.conquers_script_old_data);
- var notifications = storage_get_notifications();
- var order = storage_get_notifications_order();
- var i, n = order.length;
- for (i = 0; i < n; i++) {
- var conquer_id = order[i];
- $('#conquers_script_notifications_container').append(get_notification_html(conquer_id, conquers[conquer_id]));
- notifications[conquer_id].is_new = false;
- }
- toggle_close_all_button();
- toggle_notifications_wrapper();
- storage_set_notifications(notifications);
- storage_set_notifications_order(get_objects_order_array(notifications));
- }
- /**
- * Animates notification by its id
- * @param {String} id
- * @returns {undefined}
- */
- function animate_notification(id) {
- storage_get_settings();
- switch (settings.animation_style) {
- case '0':
- $('#' + id).fadeIn(500);
- break;
- case 'a':
- $('#' + id).show();
- $('#' + id).addClass('anim_scale');
- break;
- case 'b':
- $('#' + id).show();
- $('#' + id).addClass('anim_fly-in');
- break;
- case 'c':
- $('#' + id).show();
- $('#' + id).addClass('anim_third-dimension');
- break;
- case 'd':
- $('#' + id).show();
- $('#' + id).addClass('anim_fall-down');
- break;
- }
- }
- /**
- * Refreshes notifications - shows new ones (with animation) and hides the closed ones
- * @returns {undefined}
- */
- function refresh_notifications() {
- storage_get_settings();
- if (settings.show_notifications === false) {
- return;
- }
- var conquers = JSON.parse(localStorage.conquers_script_old_data);
- var notifications = storage_get_notifications();
- var order = storage_get_notifications_order();
- var notifications_data_changed = false;
- $('#conquers_script_notifications_container > div').each(function() {
- if (notifications.hasOwnProperty($(this).attr('id')) === false) {
- $(this).remove();
- }
- });
- var i, n = order.length;
- for (i = 0; i < n; i++) {
- var conquer_id = order[i];
- if ($('#' + conquer_id).length === 0) {
- if (i === 0) {
- $('#conquers_script_notifications_container').prepend($(get_notification_html(conquer_id, conquers[conquer_id])).hide());
- } else {
- $('#conquers_script_notifications_container > div:eq(' + (i-1) + ')').after($(get_notification_html(conquer_id, conquers[conquer_id])).hide());
- }
- if (notifications[conquer_id].is_new) {
- notifications[conquer_id].is_new = false;
- notifications_data_changed = true;
- animate_notification(conquer_id);
- } else {
- $('#' + conquer_id).show();
- }
- }
- }
- toggle_close_all_button();
- toggle_notifications_wrapper();
- if (notifications_data_changed) {
- storage_set_notifications(notifications);
- storage_set_notifications_order(get_objects_order_array(notifications));
- }
- }
- /**
- * Removes single notification data and hides it by its id
- * @param {String} conquer_id
- * @returns {undefined}
- */
- function close_notification(conquer_id) {
- $('#'+conquer_id).addClass('anim_remove');
- setTimeout(function() {
- var notifications = storage_get_notifications();
- delete notifications[conquer_id];
- $('#' + conquer_id).remove();
- toggle_close_all_button();
- toggle_notifications_wrapper();
- storage_set_notifications(notifications);
- storage_set_notifications_order(get_objects_order_array(notifications));
- }, 400);
- }
- /**
- * Removes all notifications from localStorage
- * @returns {undefined}
- */
- function remove_all_notifications() {
- storage_set_notifications({});
- storage_set_notifications_order([]);
- }
- /**
- * Removes all notifications data from localStorage and hides all notifications
- * @returns {undefined}
- */
- function close_all_notifications() {
- hide_all_notifications();
- toggle_close_all_button();
- toggle_notifications_wrapper();
- remove_all_notifications();
- }
- /**
- * Automatically fills the message title and body at "New message" page
- * @param {String} conquer_id
- * @returns {undefined}
- */
- function fill_new_pm_fields(conquer_id) {
- var conquer = JSON.parse(localStorage.conquers_script_old_data)[conquer_id];
- if (conquer.owner.old.id === 0) {
- $('[name=subject]').val(`${conquer.owner.new.name} przejął barbę na ${conquer.village.continent}`);
- } else {
- $('[name=subject]').val(`${conquer.owner.new.name} przejął wioskę od ${conquer.owner.old.name}`);
- }
- var date = new Date(conquer.timestamp * 1000);
- var hours = (date.getHours() < 10) ? '0'+date.getHours() : date.getHours();
- var minutes = (date.getMinutes() < 10) ? '0'+date.getMinutes() : date.getMinutes();
- var text = `${hours}:${minutes} - [player]${conquer.owner.new.name}[/player] `;
- if (conquer.owner.new.ally.id !== 0) {
- text += `([ally]${conquer.owner.new.ally.tag}[/ally]) `;
- }
- text += `podbija [coord]${conquer.village.x}|${conquer.village.y}[/coord] od `;
- if (conquer.owner.old.id !== 0) {
- text += `[player]${conquer.owner.old.name}[/player] `;
- if (conquer.owner.old.ally.id !== 0) {
- text += `([ally]${conquer.owner.old.ally.tag}[/ally])`;
- }
- } else {
- text += `[i]wioska barbarzyńska[/i]`;
- }
- $('#message').val(text);
- }
- /**
- * Shows or hides "Close all" button based on the amount of notifications
- * @returns {undefined}
- */
- function toggle_close_all_button() {
- if ($('#conquers_script_notifications_container > div').length < 3 && $('#conquers_script_close_all_notifications').is(':visible')) {
- $('#conquers_script_close_all_notifications').hide();
- } else if ($('#conquers_script_notifications_container > div').length >= 3 && $('#conquers_script_close_all_notifications').is(':hidden')) {
- $('#conquers_script_close_all_notifications').show();
- }
- }
- function toggle_notifications_wrapper() {
- if ($('#conquers_script_notifications_container > div').length === 0 && $('#conquers_script_notifications_wrapper').is(':visible')) {
- $('#conquers_script_notifications_wrapper').hide();
- } else if ($('#conquers_script_notifications_container > div').length !== 0 && $('#conquers_script_notifications_wrapper').is(':hidden')) {
- $('#conquers_script_notifications_wrapper').show();
- }
- }
- /**
- * Returns current unix timestamp in seconds
- * @returns {Number}
- */
- function get_current_timestamp() {
- return Math.floor(Date.now() / 1000);
- }
- function refresh_counter() {
- var found = $('#conquers_script_conquers').find('tr:visible').length;
- $('#conquers_script_search').find('span').text(`Podboje: ${found}`);
- }
- /**
- * Updates settings variable and settings in localStorage
- * @returns {undefined}
- */
- function update_settings() {
- var remove_old_data = false;
- storage_get_settings();
- settings.show_own = $('#checkbox_own').prop('checked');
- settings.show_allies = $('#checkbox_allies').prop('checked');
- settings.show_enemies = $('#checkbox_enemies').prop('checked');
- settings.show_interesting = $('#checkbox_inter').prop('checked');
- settings.show_others = $('#checkbox_others').prop('checked');
- settings.show_victims = $('#checkbox_victims').prop('checked');
- settings.show_range = $('#checkbox_range').prop('checked');
- settings.settings_for_conquers = $('#checkbox_settings_for_conquers').prop('checked');
- settings.range = Number($('#conquers_range').val());
- settings.show_notifications = $('#checkbox_notifications').prop('checked');
- settings.animation_style = $('#animation_style').val();
- settings.notification_style = $('#notification_style').val();
- if (settings.notifications_lifetime !== Number($('#notifications_lifetime').val())*3600) {
- settings.notifications_lifetime = Number($('#notifications_lifetime').val())*3600;
- remove_old_data = true;
- }
- if (settings.conquers_lifetime !== Number($('#conquers_lifetime').val())*3600) {
- settings.conquers_lifetime = Number($('#conquers_lifetime').val())*3600;
- remove_old_data = true;
- }
- settings.additional_villages = $('#additional_villages').val();
- storage_set_settings();
- if (remove_old_data) {
- remove_old_notifications();
- remove_old_conquers_data();
- }
- hide_all_notifications();
- display_all_notifications();
- if (settings.show_notifications === false) {
- remove_all_notifications();
- toggle_close_all_button();
- toggle_notifications_wrapper();
- }
- }
- /**
- * Initialization, sets onclick handlers, inserts CSS
- * @returns {undefined}
- */
- function init() {
- if (storage_settings_exist() === false) {
- settings = {
- own_tribe: '',
- allies: [],
- enemies: [],
- interesting: [],
- show_own: false,
- show_allies: false,
- show_enemies: true,
- show_interesting: true,
- show_others: false,
- show_victims: false,
- show_range: true,
- range: 10,
- show_notifications: true,
- notification_style: "classic",
- animation_style: 'b',
- notifications_lifetime: 6*3600,
- conquers_lifetime: 24*3600,
- settings_for_conquers: false,
- additional_villages: ''
- };
- load_diplomacy_to_settings();
- storage_set_settings();
- } else {
- storage_get_settings();
- }
- if (localStorage.getItem('conquers_script_notifications') === null) {
- localStorage.setItem('conquers_script_notifications', '{}');
- }
- $('body').on('click', '#conquers_script_settings', function(e) {
- e.preventDefault();
- if ($(this).text() === 'ustawienia') {
- $('#inline_popup_content').html('<div style="padding:2%;box-sizing:border-box;">' + create_settings() + '</div>');
- UI.ToolTip($('.ui_tooltip'));
- $(this).text('akceptuj');
- } else {
- if (Number($('#conquers_range').val()) > 0) {
- update_settings();
- $('#inline_popup_content').html(create_gui_content());
- refresh_counter();
- $(this).text('ustawienia');
- } else {
- alert('Odległość filtrowania przejęć musi być liczbą pozytywną.');
- $('#conquers_range').css('background', 'rgb(255,77,77)');
- }
- }
- });
- $('body').on('keyup', '#conquers_script_search input', function() {
- var search_for = $(this).val();
- if (search_for.trim() !== '') {
- $('#conquers_script_conquers').find('tr').each(function() {
- if ($(this).text().toLowerCase().includes(search_for.toLowerCase())) {
- $(this).show();
- } else {
- $(this).hide();
- }
- });
- } else {
- $('#conquers_script_conquers').find('tr').each(function() {
- $(this).show();
- });
- }
- refresh_counter();
- });
- $('body').on('click', '#load_diplomacy', function() {
- update_settings();
- delete localStorage.conquers_script_own_tribe;
- delete localStorage.conquers_script_diplomacy;
- reload_diplomacy = true;
- inlinePopupClose();
- UI.SuccessMessage('Skrypt pobiera informacje o dyplomacji Twojego plemienia. Potrwa to max kilka sekund.', 1000);
- });
- $('body').on('click', '.conquers_script_tribes_add', function() {
- var input_tribe = prompt('Wprowadź skrót plemienia:');
- if (settings[$(this).attr('data-type')].indexOf(input_tribe) === -1 && input_tribe !== null && input_tribe.trim() !== '') {
- if (input_tribe.length > 6) {
- alert('Wprowadź skrót plemienia (max 6 znaków) a nie jego nazwę.');
- } else {
- settings[$(this).attr('data-type')].push(input_tribe); //type -> allies/enemies etc.
- storage_set_settings();
- $(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>`);
- }
- }
- });
- $('body').on('click', '.conquers_script_tribes_remove', function() {
- var index = settings[$(this).attr('data-type')].indexOf($(this).parent().text().trim());
- settings[$(this).attr('data-type')].splice(index, 1); //type -> allies/enemies etc.
- storage_set_settings();
- $(this).parent().remove();
- });
- 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
- $('head').append(`
- <style>
- #conquers_script_notifications_wrapper {
- position: fixed;
- top: ${wrapper_top}px;
- right: 20px;
- height: 60%;
- width: 350px;
- padding-right: 5px;
- overflow-y: auto;
- overflow-x: hidden;
- z-index: 100;
- }
- #conquers_script_search input {
- display: block;
- float: left;
- box-sizing: content-box;
- height: 18px;
- width: 150px;
- padding: 1px 6px 1px 6px;
- border: 1px solid rgb(129, 66, 2);
- background-color: white;
- }
- #conquers_script_search div {
- background-image: url("`+image_base+`search.png");
- background-repeat: no-repeat;
- background-position: center;
- width: 18px;
- height: 22px;
- float: left;
- }
- #conquers_script_search span {
- line-height: 22px;
- }
- #conquers_script_close_all_notifications {
- position: fixed;
- top: ${wrapper_top - 30}px;
- right: 40px;
- height: 25px;
- width: 140px;
- opacity: 0.5;
- }
- #conquers_script_close_all_notifications:hover {
- opacity: 1;
- cursor: pointer;
- }
- #conquers_script_notifications_container {
- width: 100%;
- }
- #conquers_script_notifications_container > div {
- padding-left: 25px;
- }
- .notification_style_dark {
- 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;
- }
- .notification_style_dark a {
- color: #c1a264;
- white-space: nowrap;
- }
- .notification_style_classic {
- position:relative;
- background: #fff5da;
- width:100%;
- box-sizing: border-box;
- margin-right: 5px;
- padding:10px;
- color: #000;
- font-weight: 700;
- border: 1px solid #804000;
- margin-bottom: 20px;
- text-align: center;
- }
- .notification_close {
- position: absolute;
- left: 8px;
- cursor: pointer;
- }
- .notification_style_dark .notification_close {
- visibility: hidden;
- }
- .notification_style_dark:hover .notification_close {
- visibility: visible;
- }
- .notification_send_pm {
- cursor: pointer;
- }
- .conquers_script_tribes_remove {
- white-space: nowrap;
- }
- #inline_popup {
- width: 400px;
- }
- #inline_popup_main span.ui_tooltip {
- cursor: help;
- }
- .anim_scale {
- animation-duration: 600ms;
- animation-name: anim-scale;
- }
- .anim_fly-in {
- animation-duration: 500ms;
- animation-name: anim-fly-in;
- }
- .anim_third-dimension {
- animation-duration: 600ms;
- animation-name: anim-third-dimension;
- }
- .anim_remove {
- animation-duration: 400ms;
- animation-name: anim-remove;
- }
- .anim_fall-down {
- transform-origin: bottom;
- animation-duration: 500ms;
- animation-name: anim-fall-down;
- }
- @keyframes anim-scale {
- 0% {
- transform: scale(0);
- }
- 60% {
- transform: scale(1);
- }
- 80% {
- transform: scale(0.8);
- }
- 100% {
- transform: scale(1);
- }
- }
- @keyframes anim-fly-in {
- 0% {
- transform: translateX(500px);
- }
- 60% {
- transform: translateX(0px);
- }
- 80% {
- transform: translateX(20px);
- }
- 100% {
- transform: translateX(0px);
- }
- }
- @keyframes anim-third-dimension {
- 0% {
- transform: rotateX(90deg);
- }
- 5% {
- transform: rotateX(88deg);
- }
- 15% {
- transform: rotateX(88deg);
- }
- 80% {
- transform: rotateX(-25deg);
- }
- 100% {
- transform: rotateX(0deg);
- }
- }
- @keyframes anim-remove {
- 0% {
- transform: scale(1);
- }
- 100% {
- transform: scale(0);
- }
- }
- @keyframes anim-fall-down {
- 0% {
- transform: translateY(-500px) rotateX(0deg);
- }
- 70% {
- transform: translateY(0px) rotateX(50deg);
- }
- 85% {
- transform: translateY(0px) rotateX(-30deg);
- }
- 100% {
- transform: translateY(0px) rotateX(0deg);
- }
- }
- </style>
- `);
- $('body').append('<div id="conquers_script_notifications_wrapper"><div id="conquers_script_notifications_container"></div></div>');
- $('#conquers_script_notifications_container').on('click', '.notification_close', function() {
- close_notification($(this).parent().attr('id'));
- });
- $('#conquers_script_notifications_container').on('click', '.notification_send_pm', function() {
- window.open('/game.php?' + t + 'screen=mail&mode=new&conquer_id=' + $(this).parent().attr('id'));
- });
- if (game_data.hasOwnProperty('screen') && game_data.screen === 'mail' && game_data.hasOwnProperty('mode') && game_data.mode === 'new' && window.location.search.indexOf('conquer_id=') !== -1) {
- fill_new_pm_fields((new URLSearchParams(window.location.search)).get('conquer_id'));
- }
- $('body').append('<input type="button" id="conquers_script_close_all_notifications" class="btn" value="Zamknij wszystko" style="display: none;">');
- $('#conquers_script_close_all_notifications').click(function() {
- UI.ConfirmationBox('Czy na pewno chcesz usunąć wszystkie powiadomienia?', [{
- text: "Usuń",
- callback: close_all_notifications,
- confirm: true
- }]);
- });
- }
- /**
- * Creates main "Conquers" button next to the village's coords
- * @returns {undefined}
- */
- function create_conquers_button() {
- $('#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
- $('#conquers_script_button').tooltip(tooltip_options);
- $('#conquers_script_button').on('click', function(e) {
- if (is_downloader_working() === false) {
- storage_get_settings();
- display_inline_popup(true, e);
- }
- });
- }
- /**
- * Checks if downloader is currently downloading some data
- * @returns {Boolean}
- */
- function is_downloader_working() {
- 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());
- }
- /**
- * Enables or disables main "Conquers" button based on whether some data is being downloaded or not
- * @returns {undefined}
- */
- function toggle_conquers_button() {
- if (is_downloader_working() && $('#conquers_script_button').hasClass('btn-disabled') === false) {
- $('#conquers_script_button').addClass('btn-disabled').prop('title', 'Skrypt pobiera teraz dane o podbojach. Potrwa to kilka sekund.').tooltip(tooltip_options);
- } else if (is_downloader_working() === false && $('#conquers_script_button').hasClass('btn-disabled')) {
- $('#conquers_script_button').removeClass('btn-disabled').prop('title', '').tooltip(tooltip_options);
- }
- }
- create_conquers_button();
- var interval_id = setInterval(function() {
- if (is_downloader_working() === false) {
- clearInterval(interval_id);
- init();
- remove_old_notifications();
- remove_old_conquers_data();
- display_all_notifications();
- update_notifications_list();
- refresh_notifications();
- setInterval(toggle_conquers_button, 100);
- setInterval(function() {
- update_notifications_list();
- refresh_notifications();
- }, 1000);
- setInterval(function() {
- remove_old_notifications();
- remove_old_conquers_data();
- }, 60000);
- setInterval(function() {
- if (reload_diplomacy === true && storage_settings_exist() && localStorage.getItem('conquers_script_own_tribe') !== null && localStorage.getItem('conquers_script_diplomacy') !== null) {
- reload_diplomacy = false;
- storage_get_settings();
- load_diplomacy_to_settings();
- storage_set_settings();
- hide_all_notifications();
- display_all_notifications();
- toggle_close_all_button();
- toggle_notifications_wrapper();
- display_inline_popup(false);
- }
- }, 200);
- }
- }, 500);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement