// ==UserScript==
// @name egovatwar
// @namespace egov4war
// @description Orders and shouts from Alliance Headquarters and Ministry of Defence
// @include *.erepublik.com/*
// @version 0.2.5
// @author Serj_LV (aVie)
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
function egov4war_start (battleList) {
var egov4war = '0.2.5';
var scriptLink = 'https://greasyfork.org/ru/scripts/7333-egovatwar';
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,
"Macedonia-FYROM": 79,
"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,
"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,
"Taiwan": 81,
"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 wallStyle (wallL) {
return 'margin:0;padding:0;'
+ 'background: -moz-linear-gradient(left, #33cc33 0%, #33cc33 ' + wallL + '%, #cc3333 ' + wallL + '%, #cc3333 100%);'
+ 'background: -webkit-gradient(linear, left top, right top, color-stop(0%,#33cc33), color-stop(' + wallL + '%,#33cc33), color-stop(' + wallL + '%,#cc3333), color-stop(100%,#cc3333));'
+ 'background: -webkit-linear-gradient(left, #33cc33 0%,#33cc33 ' + wallL + '%,#cc3333 ' + wallL + '%,#cc3333 100%);'
+ 'background: -o-linear-gradient(left, #33cc33 0%,#33cc33 ' + wallL + '%,#cc3333 ' + wallL + '%,#cc3333 100%);'
+ 'background: -ms-linear-gradient(left, #33cc33 0%,#33cc33 ' + wallL + '%,#cc3333 ' + wallL + '%,#cc3333 100%);'
+ 'background: linear-gradient(to right, #33cc33 0%,#33cc33 ' + wallL + '%,#cc3333 ' + wallL + '%,#cc3333 100%);'
+ 'border:solid #666 1px;';
}
function showPoints(order, countryIdL, countryIdR, wallL, wallR, dominationL, dominationR, totalL, totalR) {
$am($g("order" + order),[
$e('img',[['style',"position:absolute;left:210px;top:2px;height:16px;border:solid #666 1px;"],['src',"http://cdn.egov4you.info/img/flags/" + countryIdL + ".gif"]]),
$e('img',[['style',"position:absolute;left:276px;top:2px;height:16px;border:solid #666 1px;"],['src',"http://cdn.egov4you.info/img/flags/" + countryIdR + ".gif"]]),
$ee('span',totalL,[['style',"width:14px;text-align:left;position:absolute;left:239px;top:17px;font-size:10px;line-height:10px;color:#333;font-weight:bold;"]]),
$ee('span',totalR,[['style',"width:14px;text-align:right;position:absolute;left:258px;top:17px;font-size:10px;line-height:10px;color:#333;font-weight:bold;"]]),
$ee('span',dominationL,[['style',"width:30px;text-align:right;position:absolute;left:315px;top:17px;font-size:10px;line-height:10px;color:#333;font-weight:bold;"]]),
$ee('span',dominationR,[['style',"width:30px;text-align:left;position:absolute;left:627px;top:17px;font-size:10px;line-height:10px;color:#333;font-weight:bold;"]]),
$em('div',[
$ee('span',wallL + '%',[['style',"width:45px;text-align:left;position:absolute;left:7px;bottom:6px;font-size:10px;line-height:10px;color:#fff;font-weight:bold;"]]),
$ee('span',wallR + '%',[['style',"width:45px;text-align:right;position:absolute;right:6px;bottom:6px;font-size:10px;line-height:10px;color:#fff;font-weight:bold;"]]),
Math.max(dominationL,dominationR) < 1800 ? "":
$ee('span','Wait',[['style',"width:30px;text-align:center;position:absolute;left:120px;bottom:3px;font-size:16px;line-height:16px;color:#ff3;font-weight:bold;"]])
],[['style',"position:absolute;left:350px;top:10px;width:270px;height:22px;" + wallStyle(wallL)]])
]);
}
function showWall(order, countryIdL, countryIdR, wallL, wallR, dominationL, dominationR, totalL, totalR) {
var bPercent = Math.floor(Math.max(dominationL,dominationR)/18);
bPercent = bPercent >= 100 ? "wait": bPercent + "%";
$am($g("order" + order),[
$ee('span',totalL,[['style',"width:15px;text-align:right;position:absolute;left:98px;top:30px;font-size:10px;line-height:10px;color:#333;font-weight:bold;z-index:100;"]]),
$ee('span',totalR,[['style',"width:15px;text-align:left;position:absolute;left:229px;top:30px;font-size:10px;line-height:10px;color:#333;font-weight:bold;"]]),
$ee('span',bPercent,[['style',"width:30px;text-align:right;position:absolute;left:194px;top:10px;font-size:10px;line-height:10px;color:#333;font-weight:bold;"]]),
$em('div',[
$e('img',[['style',"position:absolute;left:-16px;bottom:0;width:12px;height:9px;border:solid #666 1px;"],['src',"http://cdn.egov4you.info/img/flags/" + countryIdL + ".gif"]]),
$e('img',[['style',"position:absolute;right:-16px;bottom:0;width:12px;height:9px;border:solid #666 1px;"],['src',"http://cdn.egov4you.info/img/flags/" + countryIdR + ".gif"]]),
$ee('span',wallL,[['style',"width:40px;text-align:left;position:absolute;left:3px;bottom:1px;font-size:10px;line-height:10px;color:#fff;font-weight:bold;"]]),
$ee('span',wallR,[['style',"width:40px;text-align:right;position:absolute;right:2px;bottom:1px;font-size:10px;line-height:10px;color:#fff;font-weight:bold;"]]),
],[['style',"position:absolute;left:130px;top:29px;width:80px;height:11px;" + wallStyle(wallL)]])
]);
}
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 (battleList) {
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";
}
}
});
}
function drawStars(n) {
var str = '';
for (i = 0; i < n; i++) {
var position = 4 + (i * 16);
str += '<img style="position:absolute;left:' + position + 'px;top:26px;width:16px;margin:0;" alt="*" src="http://cdn.egov4you.info/img/star.png"></div>'
}
return str;
}
var sb = $gc('user_finances');
if( sb.length == 0 ) return;
var egov4Country = RB_getValue('egov4Country',40);
var str = '';
str = 'egovatwar <a target="_blank" href="'+scriptLink+'">v' + egov4war + '</a><select id="selectCountry" style="margin-top:3px;width:150px;font-size:10px;">';
for( var country in countries )
str += '<option value="' + countries[country] + '"' + (countries[country] == egov4Country ?' selected="selected">':'>') + country + '</option>';
str += '</select>';
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));
str = '';
if (json.target && json.target.battle != null && (json.target.div == null || json.target.div == division)) {
if (json.target.note.length == 0) {
json.target.note = 'Fight!';
}
str += '<h4 id="hq1" style="display:none;">' + json.alliance + ' HeadQuarters <span style="color:#b6b6b6;font-size:90%;font-weight:normal;">(updated in day ' + json.target.updated + ')</span></h4>'
+ '<ul id="hq2" style="display:none;">'
+ '<li id="order6">'
+ drawStars(json.target.stars)
+ '<span style="position:absolute;left:7px;top:6px;font-size:12px;line-height:12px;color:#333;font-weight:bold;">' + json.target.region + '</span>'
+ '<span style="position:absolute;left:7px;top:17px;font-size:10px;line-height:10px;color:#333;width:195px;overflow:hidden;white-space:nowrap;">' + json.target.note + '</span>'
+ (json.target.side != null ?
'<a title="" href="/en/military/battlefield-choose-side/' + json.target.battle + '/' + json.target.side + '"><span>Fight</span></a>':
'<a title="" href="/en/military/battlefield/' + json.target.battle + '"><span>Fight</span></a>')
+ '</li>'
+ '</ul>';
}
if (json.orders) {
var checkBattles = false;
for(var index in json.orders) {
if (json.orders[index].battle != null) {
checkBattles = true
}
}
if (checkBattles) {
str += '<h4 id="mod1" style="display:none;">Ministry of Defence <span style="color:#b6b6b6;font-size:90%;font-weight:normal;">(updated in day ' + json.updated + ')</span></h4>'
+ '<ul id="mod2" style="display:none;">';
for(var index in json.orders) {
if (json.orders[index].battle != null && (json.orders[index].div == null || json.orders[index].div == division)) {
if (json.orders[index].note.length == 0) {
json.orders[index].note = 'Fight!';
}
str += '<li id="order' + json.orders[index].order + '" style="display:none;">'
+ drawStars(json.orders[index].stars)
+ '<span style="position:absolute;left:7px;top:6px;font-size:12px;line-height:12px;color:#333;font-weight:bold;">' + json.orders[index].region + '</span>'
+ '<span style="position:absolute;left:7px;top:17px;font-size:10px;line-height:10px;color:#333;width:195px;overflow:hidden;white-space:nowrap;">' + json.orders[index].note + '</span>'
+ (json.orders[index].side != null ?
'<a title="" href="/en/military/battlefield-choose-side/' + json.orders[index].battle + '/' + json.orders[index].side + '"><span>Fight</span></a>':
'<a title="" href="/en/military/battlefield/' + json.orders[index].battle + '"><span>Fight</span></a>')
+ '</li>';
}
}
str += '</ul>';
}
}
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 ( ! battleList ) {
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(false);
} else if (window.location.href.indexOf("/military/campaigns") > 0) {
egov4war_start(true);
}