// ==UserScript==
// @name egovatwar
// @namespace egov4war
// @description Orders and shouts from Alliance Headquarters and Ministry of Defence
// @include *.erepublik.com/*
// @version 0.2.4
// @author Serj_LV (aVie)
// @grant GM_getValue
// @grant GM_setValue
// @downloadURL none
// ==/UserScript==
function egov4war_start () {
var egov4war = '0.2.4';
var scriptLink = 'http://openuserjs.org/';
var countries = {
"Albania": 167,
"Argentina": 27,
"Armenia": 169,
"Australia": 50,
"Austria": 33,
"Belarus": 83,
"Belgium": 32,
"Bolivia": 76,
"Bosnia-Herzegovina": 69,
"Bolivia": 76,
"Brazil": 9,
"Bulgaria": 42,
"Canada": 23,
"Chile": 64,
"China": 14,
"Colombia": 78,
"Croatia": 63,
"Cuba": 171,
"Cyprus": 82,
"Czech-Republic": 34,
"Denmark": 55,
"Egypt": 165,
"Estonia": 70,
"Finland": 39,
"France": 11,
"Georgia": 168,
"Germany": 12,
"Greece": 44,
"Hungary": 13,
"India": 48,
"Indonesia": 49,
"Iran": 56,
"Ireland": 54,
"Israel": 58,
"Italy": 10,
"Japan": 45,
"Latvia": 71,
"Lithuania": 72,
"Malaysia": 66,
"Mexico": 26,
"Moldova": 52,
"Montenegro": 80,
"Netherlands": 31,
"New-Zealand": 84,
"Nigeria": 170,
"North-Korea": 73,
"Norway": 37,
"Pakistan": 57,
"Paraguay": 75,
"Peru": 77,
"Philippines": 67,
"Poland": 35,
"Portugal": 53,
"Republic-of-China-Taiwan": 81,
"Republic-of-Macedonia-FYROM": 79,
"Romania": 1,
"Russia": 41,
"Saudi-Arabia": 164,
"Serbia": 65,
"Singapore": 68,
"Slovakia": 36,
"Slovenia": 61,
"South-Africa": 51,
"South-Korea": 47,
"Spain": 15,
"Sweden": 38,
"Switzerland": 30,
"Thailand": 59,
"Turkey": 43,
"Ukraine": 40,
"United-Arab-Emirates": 166,
"United-Kingdom": 29,
"Uruguay": 74,
"USA": 24,
"Venezuela": 28
}
function jsonRequest(url, onSuccess) {
var aR = new XMLHttpRequest();
aR.onreadystatechange = function() {
if( aR.readyState == 4 && (aR.status == 200 || aR.status == 304))
onSuccess(eval('(' + aR.responseText + ')'));
};
aR.open('GET', url, true);
aR.setRequestHeader('Accept', 'application/json, text/javascript, */*; q=0.01');
aR.send();
};
function $g(aID) {return (aID != '' ? document.getElementById(aID) : null);};
function $gt(str,m) { return (typeof m == 'undefined' ? document:m).getElementsByTagName(str); };
function $gc(str,m) { return (typeof m == 'undefined' ? document:m).getElementsByClassName(str); };
function $at(aElem, att) {if (att !== undefined) {for (var xi = 0; xi < att.length; xi++) {aElem.setAttribute(att[xi][0], att[xi][1]); if (att[xi][0].toUpperCase() == 'TITLE') aElem.setAttribute('alt', att[xi][1]);};};};//Acr111-addAttributes
function $t(iHTML) {return document.createTextNode(iHTML);};
function $e(nElem, att) {var Elem = document.createElement(nElem); $at(Elem, att); return Elem;};
function $ee(nElem, oElem, att) {var Elem = $e(nElem, att); if (oElem !== undefined) if( typeof(oElem) == 'object' ) Elem.appendChild(oElem); else Elem.innerHTML = oElem; return Elem;};
function $am(Elem, mElem) { if (mElem !== undefined) for(var i = 0; i < mElem.length; i++) { if( typeof(mElem[i]) == 'object' ) Elem.appendChild(mElem[i]); else Elem.appendChild($t(mElem[i])); } return Elem;};
function $em(nElem, mElem, att) {var Elem = $e(nElem, att); return $am(Elem, mElem);};
function insertAfter(node, rN) {rN.parentNode.insertBefore(node, rN.nextSibling);};
var useDOMs = typeof window.localStorage == 'undefined' ? false: true;
if ( ! (typeof GM_getValue == 'undefined' || /khtml/i.test(navigator.appVersion)) ) {
if( useDOMs )
if( GM_getValue('brokenFF',0) == 1 ) useDOMs = false;
else {
GM_setValue('brokenFF',1);
if( typeof window.localStorage.setItem != "undefined" ) GM_setValue('brokenFF',2);
}
}
function RB_getValue ( key, defaultValue ) {
if( useDOMs ) {
var value = window.localStorage.getItem(key);
if( value == null ) value = defaultValue;
return value;
} else return GM_getValue( key, defaultValue );
}
function RB_setValue( key, value ) {
if( useDOMs )
window.localStorage.setItem( key, value );
else
GM_setValue( key, value );
}
function getDivision(e) {
if (e < 35) return 1;
if (e < 50) return 2;
if (e < 70) return 3;
return 4
}
function showPoints(order, countryIdL, countryIdR, wallL, wallR, dominationL, dominationR, totalL, totalR) {
var str = '
'
+ '
'
+ '' + totalL + ''
+ '' + totalR + ''
+ '' + dominationL + ''
+ '' + dominationR + ''
+ '
'
+ '' + wallL + '%'
+ '' + wallR + '%'
+ '
';
$g("order" + order).innerHTML+=str;
}
function showWall(order, countryIdL, countryIdR, wallL, wallR, dominationL, dominationR, totalL, totalR) {
var str = ''
+ '

'
+ '

'
+ '
' + wallL + ''
+ '
' + wallR + ''
+ '
';
$g("order" + order).innerHTML+=str;
}
function getPointsWall(battleId, order, division) {
jsonRequest("/en/military/battle-status/" + battleId + "/1", function(json) {
if (json['message'] != 'BATTLE_CLOSED') {
var countryIdL = Object.getOwnPropertyNames(json['division'])[0];
var countryIdR = Object.getOwnPropertyNames(json['division'])[1];
var domination = Math.round(json['division']['domination'][division] * 1000) / 1000;
domination = domination.toFixed(3);
var bar = json['division']['bar'][division];
if (domination > 50) {
if (bar == countryIdL) {
var wallL = domination;
var wallR = 100 - domination
} else {
var wallR = domination;
var wallL = 100 - domination
}
} else {
if (bar == countryIdL) {
var wallR = domination;
var wallL = 100 - domination
} else {
var wallL = domination;
var wallR = 100 - domination
}
}
var dominationL = json['division'][countryIdL][division]['domination'];
var dominationR = json['division'][countryIdR][division]['domination'];
var totalL = json['division'][countryIdL]['total'];
var totalR = json['division'][countryIdR]['total'];
if (window.location.href.indexOf("/military/campaigns") > 0) {
wallL = Number(wallL).toFixed(3);
wallR = Number(wallR).toFixed(3);
showPoints(order, countryIdL, countryIdR, wallL, wallR, dominationL, dominationR, totalL, totalR)
} else {
wallL = Number(wallL).toFixed(2);
wallR = Number(wallR).toFixed(2);
showWall(order, countryIdL, countryIdR, wallL, wallR, dominationL, dominationR, totalL, totalR)
}
if (order == 6) {
$g("hq1").style.display="block";
$g("hq2").style.display="block";
} else {
$g("mod1").style.display="block";
$g("mod2").style.display="block";
$g("order" + order).style.display="block";
}
}
});
}
var sb = $gc('user_finances');
if( sb.length > 0 ) {
var egov4Country = RB_getValue('egov4Country',40);
var str = '';
str = 'egovatwar v' + egov4war + '';
insertAfter($ee('div',str,[['id',"egov4warSidebar"],['style',"font-size:10px;font-weight:bold;color:#333;"]]),sb[0]);
$g('selectCountry').addEventListener('change', function() {
RB_setValue('egov4Country', this.value);
window.location = location.href
},false);
jsonRequest('http://www.egov4you.info/orders/data/' + egov4Country, function(json) {
var division = getDivision(parseInt($gt('b',$gc("user_level")[0])[0].innerHTML));
var str = '';
if (json.target && json.target.battle != null && (json.target.div == null || json.target.div == division)) {
str = str + '' + json.alliance + ' HeadQuarters (updated in day ' + json.target.updated + ')
';
str = str + '';
str = str + '- ';
if (json.target.note.length == 0) {
json.target.note = 'Fight!'
}
for (i = 0; i < json.target.stars; i++) {
position = 4 + (i * 16);
str = str + '
'
}
str = str + '' + json.target.region + '';
str = str + '' + json.target.note + '';
if (json.target.side != null) {
str = str + 'Fight'
} else {
str = str + 'Fight'
}
str = str + ' ';
str = str + '
'
}
if (json.orders) {
var checkBattles = false;
for(var index in json.orders) {
if (json.orders[index].battle != null) {
checkBattles = true
}
}
if (checkBattles) {
str = str + 'Ministry of Defence (updated in day ' + json.updated + ')
';
str = str + '';
for(var index in json.orders) {
if (json.orders[index].battle != null && (json.orders[index].div == null || json.orders[index].div == division)) {
str = str + '- ';
if (json.orders[index].note.length == 0) {
json.orders[index].note = 'Fight!'
}
for (i = 0; i < json.orders[index].stars; i++) {
position = 4 + (i * 16);
str = str + '
'
}
str = str + '' + json.orders[index].region + '';
str = str + '' + json.orders[index].note + '';
if (json.orders[index].side != null) {
str = str + 'Fight'
} else {
str = str + 'Fight'
}
str = str + ' '
}
}
str = str + '
'
}
}
insertAfter($ee('div',str),$g("rw_pop"));
if (json.target && (json.target.div == null || json.target.div == division)) {
getPointsWall(json.target.battle, '6', division)
}
if (json.orders) {
if (checkBattles) {
for(var index in json.orders) {
if (json.orders[index].battle != null && (json.orders[index].div == null || json.orders[index].div == division)) {
getPointsWall(json.orders[index].battle, json.orders[index].order, division)
}
}
}
}
if (window.location.href.split("/").length == 4) {
if (json.shouts) {
var firstPost = $gc("wall_post")[0];
for(var index in json.shouts) {
if (json.shouts[index].text.length > 0) {
var sT = json.shouts[index].alliance != null ? ["/en/main/alliances",2]:["/en/military/campaigns",1];
firstPost.parentNode.insertBefore($em('li',[
$em('a',[
$e('span'),
$e('img',[['src',"http://cdn.egov4you.info/img/about"+sT[1]+".png"]])
],[['href',sT[0]],['class',"user_pic"],['target',"_blank"]]),
$em('div',[
$em('h6',[
$ee('a',(sT[1]==2?json.shouts[index].alliance + ' HeadQuarters':'Ministry of Defence'),[['href',sT[0]]]),
$ee('em','wrote in day ' + json.shouts[index].updated)
]),
$ee('p',json.shouts[index].text)
],[['class',"post_content"]])
],[['class',"wall_post"]]), firstPost);
}
}
}
}
});
}
}
if (window.location.href.split("/").length == 4) {
egov4war_start();
} else if (window.location.href.indexOf("/military/campaigns") > 0) {
egov4war_start();
}