Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error prevention management #476 #487

Merged
merged 14 commits into from
Sep 10, 2024
110 changes: 74 additions & 36 deletions source/core/source/arena.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,43 @@ var gca_arena = {
},

// Show Simulator
show_simulator : function(){
let link = document.createElement('a');
link.className = "gca_arena-simulator-link";
link.href = gca_links.get('gladiatus-simulator');
link.setAttribute("target","_blank");
document.getElementById('content').getElementsByTagName('article')[0].parentNode.insertBefore(link, document.getElementById('content').getElementsByTagName('article')[0]);

let image = document.createElement('div');
image.className = "gca_arena-simulator-img";
link.appendChild(image);

let text = document.createElement('div');
text.textContent = "Before attacking, use the...";
image.appendChild(text);
show_simulator: function() {
let content = document.getElementById('content');

// Check if the 'content' element and the first 'article' element exist
if (content) {
let article = content.getElementsByTagName('article')[0];

if (article && article.parentNode) {
// Create the simulator link
let link = document.createElement('a');
link.className = "gca_arena-simulator-link";
link.href = gca_links.get('gladiatus-simulator');
link.setAttribute("target", "_blank");

// Insert the link before the article
article.parentNode.insertBefore(link, article);

// Create the image container
let image = document.createElement('div');
image.className = "gca_arena-simulator-img";
link.appendChild(image);

// Create and append the text
let text = document.createElement('div');
text.textContent = "Before attacking, use the...";
image.appendChild(text);
}
}
},

// Overhaul Arena and Circus tables
overhaul_tables : function(){
document.getElementById("arenaPage").classList.add("overhaul_tables");
overhaul_tables: function() {
let arenaPage = document.getElementById("arenaPage");

if (arenaPage) {
arenaPage.classList.add("overhaul_tables");
}
},

// GCA Global Arena
Expand All @@ -117,7 +135,21 @@ var gca_arena = {
},

getInfo : function() {
let arena_rows = document.getElementById('content').getElementsByTagName('article')[0].getElementsByClassName('right')[0].getElementsByTagName('tr');
// Check if 'content' exists
let content = document.getElementById('content');
if (!content) return;

// Check if there are any 'article' elements and get the first one
let article = content.getElementsByTagName('article')[0];
if (!article) return;

// Check if there are any elements with class 'right' and get the first one
let rightSection = article.getElementsByClassName('right')[0];
if (!rightSection) return;

// Check if there are any 'tr' elements
let arena_rows = rightSection.getElementsByTagName('tr');
if (!arena_rows) return;

this.info = {};
this.info.locale_position = arena_rows[0].getElementsByTagName('th')[0].textContent.trim();
Expand All @@ -129,41 +161,44 @@ var gca_arena = {
this.info.guild_name = gca_data.section.get("guild", "name", "-").trim();
},

create : function() {
let article = document.getElementById('content').getElementsByTagName('article')[0];
create: function() {
let article = document.getElementById('content')?.getElementsByTagName('article')[0];

// Check if article exists before performing any operations
if (!article) return;

// Add br
// Add <br> element
article.appendChild(document.createElement('br'));

// Add header
let header = document.createElement('h2');
header.className = "section-header global_arena_header";
header.textContent = gca_locale.get("arena", "global_arena_title") + ' ' + '(Crazy Addon)';
article.appendChild(header);

// Add box
let box = document.createElement('section');
box.id = "global_arena_box";
box.style.display = 'block';
article.appendChild(box);
this.box = box;

// Add text
// Add description
let description = document.createElement('p');
description.textContent = gca_locale.get("arena", "global_arena_description")+" ";
description.textContent = gca_locale.get("arena", "global_arena_description") + " ";
description.style = "text-align: center;";
box.appendChild(description);

// Add link to highscore
// Add highscore link
let highscore_link = document.createElement('a');
highscore_link.className = "awesome-button";
highscore_link.textContent = gca_locale.get("arena", "global_highscore") + ' 🔗';
highscore_link.style = "margin-bottom: 15px;padding: 2px 6px;margin-right: 20px;";
highscore_link.href = gca_links.get('addon-page') + "/global-arena.php";
highscore_link.setAttribute("target","_blank");
highscore_link.setAttribute("target", "_blank");
box.appendChild(highscore_link);

// Add button
// Add load button
let load_btn = document.createElement('input');
load_btn.type = "button";
load_btn.className = "awesome-button";
Expand All @@ -176,41 +211,44 @@ var gca_arena = {
box.appendChild(load_btn);
this.load_btn = load_btn;

// Status
// Add status
let status = document.createElement('p');
status.id = 'alert_box';
status.style.display = 'none';
box.appendChild(status);
this.status = status;
// Spinner

// Add spinner
let spinner = document.createElement('div');
spinner.id = 'spiner_box';
spinner.style.display = 'none';
box.appendChild(spinner);
this.spinner = spinner;

let img = document.createElement('img');
img.src = gca_tools.img.cdn('img/ui/spinner.gif');
spinner.appendChild(img);


// Add rankings table
let rankings_table = document.createElement("table");
rankings_table.width = "100%";
rankings_table.style.display = "none";
rankings_table.style.marginBottom = '15px';
rankings_table.style.wordBreak = 'break-word';
box.appendChild(rankings_table);
this.rankings_table = rankings_table;


// Add main table
let table = document.createElement("table");
table.width = "100%";
table.style.border = "0px";
table.style.marginBottom = '15px';
table.style.wordBreak = 'break-word';
box.appendChild(table);
this.table = table;
// if link opens global arena
if ( document.location.href.match("#global_arena_box") ){

// If link opens global arena
if (document.location.href.match("#global_arena_box")) {
this.loadList();
header.scrollIntoView();
}
Expand Down
8 changes: 4 additions & 4 deletions source/core/source/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,10 +575,10 @@ var gca_global = {
}

// Get recovery rate
let recover_rate = tooltip[0][1][0].match(/\d+/);
if (!recover_rate) return; // Exit if recovery rate is not found
recover_rate = parseInt(recover_rate[0], 10);
recover_rate = (recover_rate + 100) / 100;
let recover_rate = tooltip?.[0]?.[1]?.[0]?.match(/\d+/);
if (!recover_rate) return;
recover_rate = parseInt(recover_rate[0], 10);
recover_rate = (recover_rate + 100) / 100;

// Get points left to fill
let point_max_elem = document.getElementById(type + 'points_value_pointmax');
Expand Down
58 changes: 35 additions & 23 deletions source/core/source/guild.bank.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,41 @@ var gca_guild_bank = {
);
},

// Bank Improve
donateLayout : {
improve : function(self){
// Get wrappers
var wrapper = document.getElementById("content").getElementsByTagName("article");
if(!wrapper) return;

// Improve gold in bank
this.goldInBank(self, wrapper[0].getElementsByTagName("table")[0]);
// Improve donate all my gold
this.insertAllPlayersGold(self, wrapper[0].getElementsByTagName("table")[1]);

// Set gold input to number type
var input = wrapper[0].getElementsByTagName("table")[1].getElementsByTagName("input")[0];
input.style.width = "128px";
input.type = "number";

// Change donations book link to show the detailed donations of only 1 day
// this speeds up loading and off-loads the server
let headerTabs = document.getElementById('mainnav').getElementsByTagName('a');
if(!headerTabs || headerTabs.length < 2) return;
headerTabs[1].href += '&l2=2';
},
// Bank Improve
donateLayout: {
improve: function (self) {
// Get wrappers
var wrapper = document.getElementById("content")?.getElementsByTagName("article");
if (!wrapper || wrapper.length === 0) return;

// Ensure the first article contains tables
var tables = wrapper[0]?.getElementsByTagName("table");
if (!tables || tables.length === 0) return;

// Improve gold in bank (ensure first table exists)
if (tables[0]) {
this.goldInBank(self, tables[0]);
}

// Improve donate all my gold (ensure second table exists)
if (tables[1]) {
this.insertAllPlayersGold(self, tables[1]);

// Set gold input to number type (ensure first input in second table exists)
var input = tables[1]?.getElementsByTagName("input")[0];
if (input) {
input.style.width = "128px";
input.type = "number";
}
}

// Change donations book link to show the detailed donations of only 1 day
// Ensure the navigation exists and has enough tabs
let headerTabs = document.getElementById('mainnav')?.getElementsByTagName('a');
if (headerTabs && headerTabs.length > 1) {
headerTabs[1].href += '&l2=2';
}
},

// Improve gold in bank
goldInBank : function(self, wrapper){
Expand Down
35 changes: 31 additions & 4 deletions source/core/source/guild.jail.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,27 @@ var gca_guild_jail = {
var jailTableRows = document.getElementById('content').getElementsByTagName('table')[0].getElementsByTagName('tr').length;

// If you have the rights to free monsters
var admin = ( document.getElementById('content').getElementsByTagName('table')[0].getElementsByTagName('tr')[1].getElementsByTagName('td')[3].getElementsByTagName('a')[1] )?true:false;
if(!admin) document.getElementById('content').className('noJailRights');
let contentElement = document.getElementById('content');
let admin = false;

if (contentElement) {
let table = contentElement.getElementsByTagName('table')[0];
if (table) {
let tr = table.getElementsByTagName('tr')[1];
if (tr) {
let td = tr.getElementsByTagName('td')[3];
if (td) {
let a = td.getElementsByTagName('a')[1];
admin = !!a; // Set admin to true if the anchor element exists
}
}
}
}

// If not an admin, add the 'noJailRights' class
if (!admin) {
contentElement?.classList.add('noJailRights');
}

// For every prisoner row
for(var i=1;i<jailTableRows;i++){
Expand Down Expand Up @@ -186,8 +205,16 @@ var gca_guild_jail = {
}

// Hide old prisoners layout
document.getElementById('content').getElementsByTagName('article')[0].style = 'display:none';
document.getElementById('content').className = 'gca-jail-content';
let content = document.getElementById('content');
// Check if the content and article exist
if (content) {
let article = content.getElementsByTagName('article')[0];
if (article) {
article.style.display = 'none';
}
// Set class name for content
content.className = 'gca-jail-content';
}
// Calculate empty cells
jailCells-=prisoners.length;

Expand Down
22 changes: 16 additions & 6 deletions source/core/source/guild.library.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@ var gca_guild_library = {
container.id = 'gca-library-container';

// Get guild gold
var guildGold = container.parentNode.getElementsByClassName('span_right')[1].textContent;
let guildGoldElement = container?.parentNode?.getElementsByClassName('span_right')[1];
// Ensure the element exists and has text content, otherwise set a default value
let guildGold = guildGoldElement ? guildGoldElement.textContent : '0';
// Parse guild gold (using the default value if necessary)
guildGold = gca_tools.strings.parseGold(guildGold);

// Get recipes
var recipes = container.getElementsByTagName('tr');
// Header style fix
recipes[0].getElementsByTagName('td')[4].style = 'width: 15%;';
var recipes = container?.getElementsByTagName('tr') || [];

// Check
if (recipes.length > 0 && recipes[0].getElementsByTagName('td').length > 4) {
recipes[0].getElementsByTagName('td')[4].style = 'width: 15%;';
}

// For each recipe
for (var i = recipes.length - 1; i >= 1; i--) {
Expand Down Expand Up @@ -139,9 +145,13 @@ var gca_guild_library = {
container.id = 'gca-library-container';

// Get guild gold
var guildGold = container.parentNode.getElementsByClassName('span_right')[1].textContent;
let guildGoldElement = container?.parentNode?.getElementsByClassName('span_right')[1];
// Ensure the element exists and has text content, otherwise set a default value
let guildGold = guildGoldElement ? guildGoldElement.textContent : '0';
// Parse guild gold
guildGold = gca_tools.strings.parseGold(guildGold);
if(guildGold === null) return;
// If guildGold is still null after parsing, return
if (guildGold === null) return;

// Get recipes
var recipes = container.getElementsByTagName('tr');
Expand Down
Loading