Last active
January 9, 2019 01:25
-
-
Save Shibe/1419e4a33ced16cb299e43b8a0c7736d to your computer and use it in GitHub Desktop.
Mark enemy scrip for Fix.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Mark Enemy Rewrite | |
// @version 1.0 | |
// @author Orignal by : Martijn1279, Edited by: Thom (Rage Parrot) | |
// @include https://nl*.tribalwars.nl* | |
// @grant none | |
// ==/UserScript== | |
// RELEASE: 29-12-2018 | |
function main() { | |
/************ | |
* SETTINGS * | |
************/ | |
var baseLink = 'https://doge.beer/markenemy'; | |
var colors = { | |
'red' : '#FF0000', | |
'orange' : '#FFA500', | |
'green' : '#008000' | |
}; | |
var configurations = ""; | |
var screen = game_data.screen; | |
var showMsg = true; | |
/************ | |
* PAGES * | |
************/ | |
switch (screen) { | |
case 'report': | |
if (location.href.match(/view=/)) { | |
var coordAttack = $('#attack_info_att').find('.village_anchor').parent().text().match(/\050\d{1,3}\|\d{1,3}\051/)[0].slice(1, -1); | |
var coordDefence = $('#attack_info_def').find('.village_anchor').parent().text().match(/\050\d{1,3}\|\d{1,3}\051/)[0].slice(1, -1); | |
coord = [coordAttack, coordDefence]; | |
getDataFromCoord(coord, function(data) { | |
configurations = data; | |
createReportBoxes($('#attack_info_def'), true); | |
createReportBoxes($('#attack_info_att'), true); | |
$('.ME_Report').on('click', addToCoords); | |
}); | |
} | |
break; | |
case 'map': | |
getData(screen, function(data) { | |
configurations = data; | |
markCoords(); | |
}); | |
break; | |
case 'overview_villages': | |
getData(screen, function(data) { | |
configurations = data; | |
var mode = game_data.mode; | |
if (mode === 'incomings') { | |
var $table = $('#incomings_table'), | |
$rows = $table.find('td').closest('tr'); | |
lookForMatch($rows); | |
showIcons(); | |
addFilterForIcons($table); | |
} | |
}); | |
break; | |
case 'info_village': | |
var coord = $('#map_toggler').closest('table').find('td').eq(2).text().match(/\d{1,3}\|\d{1,3}/)[0]; | |
getDataFromCoord(coord, function(data) { | |
configurations = data; | |
var $content = $('#content_value'), | |
$header = $content.find('h2').first(); | |
for (var prop in configurations) { | |
if (configurations.hasOwnProperty(prop)) { | |
if (configurations[prop]['info'].hasOwnProperty(coord)) { | |
var img = '<img src="' + configurations[prop]['img'] + '" style="background-color:' + configurations[prop]['background'] + ';border-radius:8px;vertical-align:middle;margin-left:4px;">'; | |
$header.append($(img)); | |
} | |
} | |
} | |
$content.children('table').find('tr').first().children('td').last().prepend('<div id="ME_Info" style="float:left;"><table class="vis"><tbody><tr><th colspan="10">Groepen voor: <span class="village_anchor">' + coord + '</span></tr></tbody></table></div>'); | |
createReportBoxes($('#ME_Info'), false); | |
showMsg = false; | |
$('.ME_Report').on('click', addToCoords); | |
}); | |
break; | |
case 'info_player': | |
getData(screen, function(data) { | |
configurations = data; | |
var $table = $('#villages_list'), | |
$rows = $table.find('tr') | |
lookForMatchPlayer($rows); | |
showIcons(); | |
}); | |
break; | |
} | |
/****** | |
** FUNCTIONS * | |
******/ | |
function getData(screen, callback) { | |
var dataD = null; | |
$.ajax({ | |
type : 'GET', | |
dataType : 'json', | |
url : baseLink + '/world/' + game_data.world + '/tribe/' + game_data.player.ally, | |
success : function(data) { callback(data) }, | |
error : function() { | |
UI.ErrorMessage('Fout bij het ophalen van data !'); | |
} | |
}); | |
} | |
function getDataFromCoord(coords, callback) { | |
var dataD = null; | |
$.ajax({ | |
url : baseLink + '/world/' + game_data.world + '/tribe/' + game_data.player.ally, | |
data : { | |
'village' : coords | |
}, | |
success : function(data) { callback(data) }, | |
error : function() { | |
UI.ErrorMessage('Fout bij het ophalen van data !'); | |
} | |
}); | |
} | |
function lookForMatchPlayer($rows) { | |
var i = 1, | |
l = $rows.length, | |
allCoords = getAll(configurations); | |
for (i; i < l; i = i + 2) { | |
var $row = $rows.eq(i), | |
$a = $row.find('td').eq(3); | |
var coord = $a.text(); | |
if (allCoords.hasOwnProperty(coord)) { | |
for (var j = 0, k = allCoords[coord].length; j < k; j++) { | |
$a.addClass('ME_Inc_' + allCoords[coord][j]); | |
} | |
} | |
} | |
} | |
function lookForMatch($rows) { | |
var i = 0, | |
l = $rows.length, | |
allCoords = getAll(configurations); | |
for (i; i < l; i++) { | |
var $row = $rows.eq(i), | |
$a = $row.find('td').eq(2).find('a').first(); | |
coord = $a.text().match(/\050\d{1,3}\|\d{1,3}\051/)[0].slice(1, -1); | |
if (allCoords.hasOwnProperty(coord)) { | |
for (var j = 0, k = allCoords[coord].length; j < k; j++) { | |
$a.addClass('ME_Inc_' + allCoords[coord][j]); | |
} | |
} | |
} | |
} | |
function showIcons() { | |
for (var prop in configurations) { | |
if (configurations.hasOwnProperty(prop)) { | |
var $anchors = $('.ME_Inc_' + prop); | |
if ($anchors) { | |
var img = '<img src="' + configurations[prop]['img'] + '" style="background-color:' + configurations[prop]['background'] + ';border-radius:8px;vertical-align:middle;margin-left:4px;">'; | |
$anchors.closest('td').append($(img)); | |
} | |
} | |
} | |
} | |
function addFilterForIcons($table) { | |
var filters = ''; | |
for (var prop in configurations) { | |
if (configurations.hasOwnProperty(prop)) { | |
filters += '<img class="ME_filter_Icon" data-type="' + prop + '" src="' + configurations[prop]['img'] + '" style="background-color:' + configurations[prop]['background'] + ';border-radius:8px;vertical-align:middle;cursor:pointer;margin-left:4px;">'; | |
} | |
} | |
filters += '<a href="#" id="ME_Icon_Reset" style="margin-left: 4px">Reset</a>'; | |
$table.find('th').eq(2).append(filters); | |
var $rows = $table.find('td').closest('tr'); | |
$('.ME_filter_Icon').on('click', function() { | |
var type = $(this).data('type'); | |
$rows.hide().find('.ME_Inc_' + type).closest('tr').show(); | |
}); | |
$('#ME_Icon_Reset').on('click', function(e) { | |
e.preventDefault(); | |
e.stopPropagation(); | |
$rows.show(); | |
}); | |
} | |
function createReportBoxes(elem, prepend) { | |
var row = ''; | |
if (prepend) { | |
row = '<td><p><strong>ME: </strong><strong></strong></p></td>'; | |
elem.find('table').find('tbody').prepend('<tr id="ME"></tr>'); | |
} else { | |
elem.find('table').find('tbody').append('<tr id="ME"></tr>'); | |
} | |
if (screen == 'report') { | |
coord = elem.find('.village_anchor').parent().text().match(/\050\d{1,3}\|\d{1,3}\051/)[0].slice(1, -1); | |
} | |
else if (screen == 'info_village') { | |
coord = elem.find('.village_anchor').parent().text().match(/\d{1,3}\|\d{1,3}/)[0]; | |
} | |
for (var key in configurations) { | |
var arr = Object.keys(configurations[key]['info']); | |
var style = 'background-color:' + configurations[key]['background'] + ';pointer:cursor;margin-left:4px;border-radius:100%;'; | |
if (arr.indexOf(coord) != -1) { | |
style += 'border:2px black solid'; | |
} | |
row += '<td><img src="' + configurations[key]['img'] + '"style="' + style + '" id="ME_' + key + '" class="ME_Report"></td>'; | |
} | |
$('#ME').append(row); | |
} | |
function coordValidity(coord) { | |
var stringedConfigs = JSON.stringify(configurations), | |
spl = coord.split('|'), | |
re = new RegExp(spl[0] + '\\|' + spl[1], 'g'); | |
var amountOfMatches = stringedConfigs.match(re); | |
amountOfMatches = amountOfMatches !== null ? amountOfMatches.length : 0; | |
return amountOfMatches < 3; | |
} | |
function getOwnerOfVillage(coord, callback) { | |
TribalWars.get("api", { | |
ajax : "target_selection", | |
async : false, | |
input : coord, | |
limit : 1, | |
offset : 0, | |
request_id : 6, | |
type : "coord" | |
}, function(e) { | |
callback(e.villages[0].player_id); | |
}); | |
} | |
function addToCoords() { | |
$(this).parent().find('.ME_Report').css('border', 'none'); | |
var type = $(this).attr('id').split('_')[1]; | |
if (screen == 'report') { | |
coord = $(this).closest('table').parent().closest('table').find('.village_anchor').parent().text().match(/\050\d{1,3}\|\d{1,3}\051/)[0].slice(1, -1); | |
} | |
else if (screen == 'info_village') { | |
coord = $(this).closest('table').parent().closest('table').find('.village_anchor').parent().text().match(/\d{1,3}\|\d{1,3}/)[0]; | |
} | |
var arrToAddCoord = Object.keys(configurations[type]['info']); | |
var index = arrToAddCoord.indexOf(coord); | |
var $th = $(this); | |
if (index === -1) { | |
if (coordValidity(coord)) { | |
getOwnerOfVillage(coord, function(owner) { | |
addCoordToDatabase(coord, owner, type); | |
$th.css('border', 'black 2px solid'); | |
if (showMsg) { | |
$th.closest('tr').find('p').find('strong').last().text('Toegevoegd aan ' + type + '!'); | |
} | |
configurations[type]['info'][coord] = owner; | |
}); | |
} else { | |
alert(coord + ' zit al in 3 groepen!'); | |
} | |
} else { | |
removeCoordToDatabase(coord, type); | |
$(this).css('border', 'none'); | |
if (showMsg) { | |
$(this).closest('tr').find('p').find('strong').last().text('Verwijderd uit ' + type + '!'); | |
} | |
delete configurations[type]['info'][coord]; | |
} | |
} | |
function addCoordToDatabase(village_coords, owner_id, group_name) { | |
$.ajax({ | |
contentType : 'application/json', | |
url : baseLink + '/world/' + game_data.world + '/tribe/' + game_data.player.ally, | |
type : 'POST', | |
data : JSON.stringify({ | |
'village_coords' : village_coords, | |
'group_name' : group_name | |
}) | |
}); | |
} | |
function removeCoordToDatabase(village_coords, group_name) { | |
$.ajax({ | |
url : baseLink + '/world/' + game_data.world + '/tribe/' + game_data.player.ally + '?' + $.param({ 'village' : village_coords, 'group' : group_name }), | |
type : 'DELETE' | |
}); | |
} | |
function getAll(obj, count) { | |
var allCoords = {}; | |
for (var key in obj) { | |
if (obj.hasOwnProperty(key)) { | |
var coords = Object.keys(obj[key]['info']); | |
if (coords.length > 0) { | |
for (var i = 0, l = coords.length; i < l; i++) { | |
var c = coords[i]; | |
if (allCoords.hasOwnProperty(c)) { | |
allCoords[c].push(key); | |
} else { | |
allCoords[c] = [key]; | |
} | |
if (count) { | |
if (allCoords.hasOwnProperty('count')) { | |
allCoords['count'] += 1; | |
} else { | |
allCoords['count'] = 1; | |
} | |
} | |
} | |
} | |
} | |
} | |
return allCoords; | |
} | |
function markCoords() { | |
var totalCoords = getAll(configurations, true); | |
if (totalCoords['count'] > 0) { | |
delete totalCoords['count']; | |
var enemyMarkMap = TWMap; | |
function spawnSectorReplacer(data, sector) { | |
enemyMarkMap.mapHandler.enemyMarkMapspawnSector(data, sector); | |
var beginX = sector.x - data.x; | |
var endX = beginX + enemyMarkMap.mapSubSectorSize; | |
var beginY = sector.y - data.y; | |
var endY = beginY + enemyMarkMap.mapSubSectorSize; | |
for (var x in data.tiles) { | |
var x = parseInt(x, 10); | |
if (x < beginX || x >= endX) { | |
continue; | |
} | |
for (var y in data.tiles[x]) { | |
var y = parseInt(y, 10); | |
if (y < beginY || y >= endY) { | |
continue; | |
} | |
var xCoord = data.x + x; | |
var yCoord = data.y + y; | |
var v = enemyMarkMap.villages[(xCoord) * 1000 + yCoord]; | |
if (v) { | |
for (var key in totalCoords) { | |
if (totalCoords.hasOwnProperty(key)) { | |
if (key == (xCoord + "|" + yCoord)) { | |
for (var i = 0, l = totalCoords[key].length; i < l; i++) { | |
var img = document.createElement('img'); | |
img.src = configurations[totalCoords[key][i]]['img']; | |
img.style.position = 'absolute'; | |
img.style.margin = '17px 0 0 ' + (40 - (20 * i)) + 'px'; | |
img.style.zIndex = '50'; | |
img.style.borderRadius = '8px'; | |
img.style.background = configurations[totalCoords[key][i]]['background']; | |
sector.appendElement(img, x - beginX, y - beginY); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
enemyMarkMap.mapHandler.enemyMarkMapspawnSector = enemyMarkMap.mapHandler.spawnSector; | |
enemyMarkMap.mapHandler.spawnSector = spawnSectorReplacer; | |
enemyMarkMap.reload(); | |
} | |
} | |
} | |
(function() { | |
var script = document.createElement('script'); | |
script.textContent = '$(document).ready(' + main + ');'; | |
document.readyState === "complete" ? document.body.appendChild(script) : window.onload = main; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment