Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* all fiddely bits */
if(!this.nameTemplate) nameTemplate = '{unit} {launched2} {coords} {player} {duration} {distance} {backtime} {attack_id}';
if(!this.tableTemplate) tableTemplate = '<tr><td rowspan="11">Additional Information</td><td>Unit:</td><td>{unit}</td></tr><tr><td>Launched (AttackID)</td><td>{launched1}</td></tr><tr><td>Launched (Corrected)</td><td>{launched2}</td></tr><tr><td>Coords</td><td>{coords}</td></tr><tr><td>Player</td><td>{player}</td></tr><tr><td>Duration</td><td>{duration}</td></tr><tr><td>Distance</td><td>{distance}</td></tr><tr><td>Return (Backtime)</td><td>{backtime}</td></tr><tr><td>attackId</td><td>{attack_id}</td></tr><tr><td>Change Name</td><td>{namechange}</td></tr><tr><td>Forum Post</td><td><input id="forumpost" value="{forumpost}" size="40"/></td></tr>';
if(!this.forumTemplate) forumTemplate = '[table][**]Attack On[||][img]http://en57.tribalwars.net/graphic/command/support.png[/img][player]{targetplayer}[/player][||]Origin[||][img]http://en57.tribalwars.net/graphic/command/attack.png[/img][player]{player}[/player][/**][*][|][village]{targetcoords}[/village][|][|][village]{coords}[/village][*][b]Unit[/b][|]{unit}[|][b]Launched[/b][|]{launched2}[*][b]Land Time[/b][|]{landing}[|][b]Back Time[/b][|]{backtime}[*][|][|][|][*]Distance[|]{distance}[|]Duration[|]{duration}[*]Attack ID[|]{attack_id}[|][|][/table]';
if(!this.timingTableTemplate) timingTableTemplate = '<tr><td rowspan="3">Additional Information</td><td>Launched</td><td>{launched2}</td></tr><tr><td>attackId</td><td>{attack_id}</td></tr><tr><td>Forum Entry</td><td>{forumentry}<form method="GET" target="_new" action="http://equinoxscripts.org/sandbox/attackID/callback.php"><input type="hidden" name="fname" value="w72sg"/><input type="hidden" name="data" value="{forumentry}"/><input type="submit" value="Insert"/></form></td></tr>';
var inputTemplate = '<tr><td rowspan="3">Import IDs</td><td>Enter ids<br><a href="http://code.google.com/p/tribalwarsscripts/wiki/attack_ID_lookup_and_tagging?ts=1318510402&updated=attack_ID_lookup_and_tagging#Manual_import_of_attackID\'s">more information...</a></td><td><textarea id="attackidlist"></textarea></td></tr><tr><td>&nbsp;</td><td><input type="button" value="Add" name="add" id="addAttackId"/><input type="button" value="Show all" name="show" id="showList"/></td></tr><tr><td>Current ID</td><td>{currentId}</td></tr>';
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
/* all regular expressions */
var urlReg = /[\?&]id=([^&#]*)/;
var listReg = /\s*(\d{1,2})\/(\d{1,2})\/(\d{2,4})\s*(\d{1,2}):(\d{1,2})\s*-\s*(\d+)/i;
var twfrReg = /Attack:\s*(\d+)\s*(\d{1,2})\/(\d{1,2})\/(\d{2,4})\s*(\d{1,2}):(\d{1,2}):\d{1,2}\s*([AaPp][Mm])/i;
var dateReg = /(\w{3})\s+(\d{1,2}),\s+(\d{4})\s+(\d{2}):(\d{2}):(\d{2}):(\d{0,3})/i;
var durReg = /(\d+):(\d{2}):(\d{2})/i;
var coordsReg = /\s\(((\d+)\|(\d+))\)\sK/i;
var url = window.location.href;
var id;
var config = null;
var textlist = null;
/* function */
function processTemplate(template, valueMap) {
for(name in valueMap) {
template = template.replace(new RegExp("{" + name + "}","g"), valueMap[name]);
}
return template;
}
function leadFormat(n) {
if(n < 10) {
return "0" + n;
}
return n;
}
function dateFormat(t) {
var d = new Date(t);
return months[d.getMonth()] + " " + leadFormat(d.getDate()) + ", " + d.getFullYear() + " " + leadFormat(d.getHours()) + ":" + leadFormat(d.getMinutes()) + ":" + leadFormat(d.getSeconds());
}
function dateFormat2(t) {
var d = new Date(t);
return leadFormat(d.getDate()) + "/" + leadFormat(d.getMonth() + 1) + "/" + d.getFullYear() + " " + leadFormat(d.getHours()) + ":" + leadFormat(d.getMinutes());
}
function findMonth(t) {
for(m in months)
if(months[m] == t)
return m;
}
function findLaunchtime(l) {
var previd = 0;
var prevtime = 0;
var nextid = 0;
for(var j = 0; j < l.length; j++) {
var t = listReg.exec(l[j]);
if (t != null) {
var d = new Date(t[3], t[2] - 1, t[1], t[4], t[5]);
var time = d.getTime();
previd = parseInt(nextid);
nextid = parseInt(t[6]);
if (previd != 0 && previd <= id && id <= nextid) {
return prevtime + (time - prevtime) * (id - previd) / (nextid - previd);
}
prevtime = time;
}
}
return -1;
}
function findCoords() {
var coords = Array();
$("#content_value table a").each(function(i, v) {
var c = coordsReg.exec($(v).html());
if (c != null) {
coords.push(c);
}
});
return coords;
}
function buildRenamer(launchtime) {
var attackData = { unit: "unknown", launched1: "unknown", launched2: "unknown", coords: "000|000", player: "unknown", duration: "0", distance: "0", backtime: "unknown", attack_id: "0", namechange: "", forumentry:""};
attackData.attack_id = id;
attackData.player = $("#content_value table:first tr:first").next().children("td:nth-child(3)").text();
attackData.targetplayer = $("#content_value table:first tr:nth-child(3)").next().children("td:nth-child(3)").text();
attackData.launched1 = dateFormat(launchtime);
var c = findCoords();
if (c.length > 0) {
var c1 = c[0];
var c2 = c[1];
var x = c1[2] - c2[2];
var y = c1[3] - c2[3];
attackData.coords = c1[1];
attackData.targetcoords = c2[1];
attackData.dist = Math.sqrt(x * x + y * y);
attackData.distance = Math.round(attackData.dist);
var ld = dateReg.exec($("#content_value table:first").t
/* all fiddely bits */
if(!this.nameTemplate) nameTemplate = '{unit} {launched2} {coords} {player} {duration} {distance} {backtime} {attack_id}';
if(!this.tableTemplate) tableTemplate = '<tr><td rowspan="11">Additional Information</td><td>Unit:</td><td>{unit}</td></tr><tr><td>Launched (AttackID)</td><td>{launched1}</td></tr><tr><td>Launched (Corrected)</td><td>{launched2}</td></tr><tr><td>Coords</td><td>{coords}</td></tr><tr><td>Player</td><td>{player}</td></tr><tr><td>Duration</td><td>{duration}</td></tr><tr><td>Distance</td><td>{distance}</td></tr><tr><td>Return (Backtime)</td><td>{backtime}</td></tr><tr><td>attackId</td><td>{attack_id}</td></tr><tr><td>Change Name</td><td>{namechange}</td></tr><tr><td>Forum Post</td><td><input id="forumpost" value="{forumpost}" size="40"/></td></tr>';
if(!this.timingTableTemplate) timingTableTemplate = '<tr><td rowspan="3">Additional Information</td><td>Launched</td><td>{launched2}</td></tr><tr><td>attackId</td><td>{attack_id}</td></tr><tr><td>Forum Entry</td><td>{forumentry}<form method="POST" target="_new" action="forum.php?&screen=view_thread&action=new_post&thread_id='+threadId+'&answer=true&page=last#"><input type="hidden" name="message" value="{forumentry}"/><input type="submit" value="Preview" name="preview"/></form></td></tr>';
var inputTemplate = '<tr><td rowspan="3">Import IDs</td><td>Enter ids<br><a href="http://code.google.com/p/tribalwarsscripts/wiki/attack_ID_lookup_and_tagging?ts=1318510402&updated=attack_ID_lookup_and_tagging#Manual_import_of_attackID\'s">more information...</a></td><td><textarea id="attackidlist"></textarea></td></tr><tr><td>&nbsp;</td><td><input type="button" value="Add" name="add" id="addAttackId"/><input type="button" value="Show all" name="show" id="showList"/></td></tr><tr><td>Current ID</td><td>{currentId}</td></tr>';
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
/* all regular expressions */
var urlReg = /[\?&]id=([^&#]*)/;
var listReg = /\s*(\d{1,2})\/(\d{1,2})\/(\d{2,4})\s*(\d{1,2}):(\d{1,2})\s*-\s*(\d+)/i;
var twfrReg = /Attack:\s*(\d+)\s*(\d{1,2})\/(\d{1,2})\/(\d{2,4})\s*(\d{1,2}):(\d{1,2}):\d{1,2}\s*([AaPp][Mm])/i;
var dateReg = /(\w{3})\s+(\d{1,2}),\s+(\d{4})\s+(\d{2}):(\d{2}):(\d{2}):(\d{0,3})/i;
var durReg = /(\d+):(\d{2}):(\d{2})/i;
var coordsReg = /\s\(((\d+)\|(\d+))\)\sK/i;
var url = window.location.href;
var id;
var config = null;
var textlist = null;
/* function */
function processTemplate(template, valueMap) {
for(name in valueMap) {
template = template.replace(new RegExp("{" + name + "}","g"), valueMap[name]);
}
return template;
}
function leadFormat(n) {
if(n < 10) {
return "0" + n;
}
return n;
}
function dateFormat(t) {
var d = new Date(t);
return months[d.getMonth()] + " " + leadFormat(d.getDate()) + ", " + d.getFullYear() + " " + leadFormat(d.getHours()) + ":" + leadFormat(d.getMinutes()) + ":" + leadFormat(d.getSeconds());
}
function dateFormat2(t) {
var d = new Date(t);
return leadFormat(d.getDate()) + "/" + leadFormat(d.getMonth() + 1) + "/" + d.getFullYear() + " " + leadFormat(d.getHours()) + ":" + leadFormat(d.getMinutes());
}
function findMonth(t) {
for(m in months)
if(months[m] == t)
return m;
}
function findLaunchtime(l) {
var previd = 0;
var prevtime = 0;
var nextid = 0;
for(var j = 0; j < l.length; j++) {
var t = listReg.exec(l[j]);
if (t != null) {
var d = new Date(t[3], t[2] - 1, t[1], t[4], t[5]);
var time = d.getTime();
previd = parseInt(nextid);
nextid = parseInt(t[6]);
if (previd != 0 && previd <= id && id <= nextid) {
return prevtime + (time - prevtime) * (id - previd) / (nextid - previd);
}
prevtime = time;
}
}
return -1;
}
function findCoords(element) {
return coordsReg.exec(element.innerText);
}
function getServerDate(daysFuture) {
currentDate = $('#serverDate').html();
splitDate = currentDate.split('/');
day = splitDate[0];
month = splitDate[1] - 1;
year = splitDate[2];
var date = new Date(new Date(year,month,day,0,0,0).getTime() + daysFuture * 24 * 60 * 60 * 1000);
return months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
}
function buildRenamer(element, launchtime) {
var attackData = { unit: "unknown", launched1: "unknown", launched2: "unknown", coords: "000|000", player: "unknown", duration: "0", distance: "0", backtime: "unknown", attack_id: "0", namechange: "", forumentry:""};
attackData.attack_id = id;
attackData.player = element.children[3].children[0].innerText;
attackData.targetplayer = game_data.player.name;
attackData.launched1 = dateFormat(launchtime);
var c1 = findCoords(element.children[2]);
var c2 = findCoords(element.children[1]);
var x = c1[2] - c2[2];
var y = c1[3] - c2[3];
attackData.coords = c1[1];
attackData.targetcoords = c2[1];
attackData.dist = Math.sqrt(x * x + y * y);
attackData.distance = Math.round(attackData.dist);
var dateText = element.children[4].innerText;
dateText = dateText.replace("today", getServerDate(0));
dateText = dateText.replace("tomorrow", getServerDate(1));
dateText = dateText.replace(" at ", " ");
var ld = dateReg.exec(dateText);
if (ld != null) {
attackData.landing = ld[0];
var landTime = new Date(ld[3], findMonth(ld[1]), ld[2], ld[4], ld[5], ld[6], ld[7]).getTime();
var duration = landTime - launchtime;
var dSec = duration / 1000;
attackData.duration = leadFormat(Math.floor(dSec / 3600)) + ':' + leadFormat(Math.floor(dSec % 3600 / 60)) + ':' + leadFormat(Math.floor(dSec % 60));
var closestTime = 99999999;
for(var n = 0; n < config.units.list.length; n++) {
var ut = Math.round(config.units.list[n].speed * 60 * attackData.dist * 1000);// / (config.speed * config.unit_speed)
if(Math.abs(duration - ut) < closestTime) {
closestTime = Math.abs(duration - ut);
attackData.unit = config.units.list[n].name;
attackData.launched2 = dateFormat(landTime - ut);
attackData.backtime = dateFormat(landTime + ut);
}
}
}
var nameString = processTemplate(nameTemplate, attackData);
element.children[0].children[2].children[1].click();
element.children[0].children[3].children[0].value=nameString;
}
function processForum(data) {
var ind1 = data.indexOf('[' + 'attackid]');
var ind2 = data.indexOf('[/' + 'attackid]');
if (ind1 == -1 || ind2 == -1) {
alert("The AttackID thread on the forum is not well formatted, empty or missing.");
return;
}
textlist = textlist.concat(data.substring(ind1 + 10, ind2).split('<br />'));
textlist = sortIDList(textlist);
localStorage.setItem('twAttackIDs', JSON.stringify(textlist));
var launchtime = findLaunchtime(textlist);
if(launchtime > 0) {
buildRenamer(launchtime);
} else {
var attackData = { unit: "unknown", launched1: "unknown", launched2: "unknown", coords: "000|000", player: "unknown", duration: "0", distance: "0", backtime: "unknown", attack_id: "0", namechange: "", forumentry:""};
var dl = $("#content_value table:first tr:nth-child(6)");
if(dl.children("td:first").text() == "Catapult target:") {
dl = dl.next();
}
var dt = dl.children("td:first").text();
var ds = dl.children("td:nth-child(2)").text();
var ls = dl.next().children("td:nth-child(2)").text();
if(dt == "Duration:") {
var dd = durReg.exec(ds);
var ld = dateReg.exec(ls);
if (ld != null && dd != null) {
var d = dd[1] * 3600 + dd[2] * 60 + dd[3] * 1;
var landTime = new Date(ld[3], findMonth(ld[1]), ld[2], ld[4], ld[5], ld[6], ld[7]).getTime();
var launchTime = landTime - d * 1000;
attackData.attack_id = id;
attackData.launched2 = dateFormat2(launchTime);
attackData.forumentry = attackData.launched2 + " - " + attackData.attack_id;
var tableString = processTemplate(timingTableTemplate, attackData);
$("#content_value table:first tr:last").before(tableString);
//
textlist.push(attackData.forumentry);
localStorage.setItem('twAttackIDs', JSON.stringify(textlist));
}
} else {
alert("The AttackID thread on the forum is out of date. Post a new attack id and ask a forum mod to update.");
}
}
}
function sortIDList(idList) {
idList.sort(sortFunction);
var prevId = "";
var newList = new Array();
for(var j = 0; j < idList.length; j++) {
var t = listReg.exec(idList[j]);
if(t) {
var d = new Date(t[3], t[2] - 1, t[1], t[4], t[5]);
var time = d.getTime();
var now = (new Date()).getTime();
if(t[6] != prevId && now - time < 432000000) {
newList.push($.trim(idList[j]));
}
prevId = t[6];
}
}
return newList;
}
function sortFunction(item1, item2) {
var t1 = listReg.exec(item1);
var t2 = listReg.exec(item2);
if(t1 != null && t2 != null && t1[6] == t2[6] || t1 == null && t2 == null) {
return 0;
} else if(t1 == null || t2 != null && t1[6] < t2[6]) {
return -1;
} else {
return 1;
}
}
function renameAll(){$("table#incomings_table tr td span input.btn").filter(function() { return $(this).parent().css("display") != "none" }).click();}
/* main statement */
function main() {
if (url.indexOf('screen=overview_villages') == -1 || url.indexOf('mode=incomings') == -1) {
alert('This script needs to be run from the bulk attack detail page.');
} else {
var launchtime = -1;
var attackIDString = localStorage.getItem('twAttackIDs');
if(attackIDString && attackIDString.length > 0) {
textlist = JSON.parse(attackIDString);
} else {
textlist = new Array();
}
var chunks = document.body.innerHTML.match(new RegExp("a href=\"(.*?screen=info_command.*?)\".*?>.*?Attack</span>","g"));
for (var i = 0; i<chunks.length; i++) {
var parts = chunks[i].match(new RegExp("a href=\"(.*?screen=info_command.*?)\".*?id=\"(.*?)\".*?>.*?Attack</span>"));
var loc = urlReg.exec(parts[1].replace( /\&amp;/g, '&' ));
var element = document.getElementById(parts[2]).parentNode.parentNode.parentNode.parentNode;
console.log(element);
id = parseInt(loc[1]);
launchtime = findLaunchtime(textlist);
if(launchtime > 0) {
buildRenamer(element, launchtime);
}
}
if (chunks.length > 0) {
$("#incomings_form").before("<a href='javascript:renameAll();'>Rename All</a>");
}
}
}
function loadSettings() {
config = new Config();
config.onloaded = function() {
main();
};
config.load();
}
function initSettings() {
var configString = localStorage.getItem('twConfig');
if(configString && configString.length > 0) {
config = JSON.parse(configString);
}
if(config == null || !config.time || (new Date().getTime() - config.time) > 1000 * 3600 * 24 * 7) {
$.getScript(scriptRoot+"settings.js", loadSettings);
} else {
main();
}
}
initSettings();
/* all regular expressions */
var listReg = /(\d{1,2})\/(\d{1,2})\/(\d{2,4})\s*(\d{1,2}):(\d{1,2})\s*-\s*(\d+)/ig;
var twfrReg = /Attack:\s*(\d+)\s*(\d{1,2})\/(\d{1,2})\/(\d{2,4})\s*(\d{1,2}):(\d{1,2}):\d{1,2}\s*([AaPp][Mm])/i;
var dateReg = /(\w{3})\s+(\d{1,2}),\s+(\d{4})\s+(\d{2}):(\d{2}):(\d{2}):(\d{0,3})/i;
var url = window.location.href;
var id;
var config = null;
var textlist = null;
function processForum() {
textlist = document.body.innerText.match(listReg);
textlist = sortIDList(textlist);
var ind1 = document.body.innerHTML.indexOf('[' + 'attackid]');
var ind2 = document.body.innerHTML.indexOf('[/' + 'attackid]');
if (ind1 == -1 || ind2 == -1) {
alert("The AttackID thread on the forum is not well formatted, empty or missing.");
return;
}
document.body.innerHTML = document.body.innerHTML.substr(0, ind1 + 10) + textlist.join("\n") + document.body.innerHTML.substr(ind2);
}
function sortIDList(idList) {
idList.sort(sortFunction);
var prevId = "";
var newList = new Array();
for(var j = 0; j < idList.length; j++) {
var t = listReg.exec(idList[j]);
if(t) {
var d = new Date(t[3], t[2] - 1, t[1], t[4], t[5]);
var time = d.getTime();
var now = (new Date()).getTime();
if(t[6] != prevId) {
var str = $.trim(idList[j]);
if (!(newList.indexOf(str) > 0)) {
newList.push(str);
}
}
prevId = t[6];
}
}
return newList;
}
function sortFunction(item1, item2) {
var t1 = listReg.exec(item1);
var t2 = listReg.exec(item2);
if(t1 != null && t2 != null && t1[6] == t2[6] || t1 == null && t2 == null) {
return 0;
} else if(t1 == null || t2 != null && t1[6] < t2[6]) {
return 1;
} else {
return -1;
}
}
processForum();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment