egovatwar

Orders and shouts from Alliance Headquarters and Ministry of Defence

  1. // ==UserScript==
  2. // @name egovatwar
  3. // @namespace egov4war
  4. // @description Orders and shouts from Alliance Headquarters and Ministry of Defence
  5. // @include *.erepublik.com/*
  6. // @version 0.2.10
  7. // @author Serj_LV (aVie)
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @grant GM_xmlhttpRequest
  11. // ==/UserScript==
  12.  
  13. function egov4war_start (battleList) {
  14. var egov4war = '0.2.10';
  15. var scriptLink = 'https://greasyfork.org/ru/scripts/7333-egovatwar';
  16. var countries = {
  17. "Albania": 167,
  18. "Argentina": 27,
  19. "Armenia": 169,
  20. "Australia": 50,
  21. "Austria": 33,
  22. "Belarus": 83,
  23. "Belgium": 32,
  24. "Bolivia": 76,
  25. "Bosnia-Herzegovina": 69,
  26. "Brazil": 9,
  27. "Bulgaria": 42,
  28. "Canada": 23,
  29. "Chile": 64,
  30. "China": 14,
  31. "Colombia": 78,
  32. "Croatia": 63,
  33. "Cuba": 171,
  34. "Cyprus": 82,
  35. "Czech-Republic": 34,
  36. "Denmark": 55,
  37. "Egypt": 165,
  38. "Estonia": 70,
  39. "Finland": 39,
  40. "France": 11,
  41. "Georgia": 168,
  42. "Germany": 12,
  43. "Greece": 44,
  44. "Hungary": 13,
  45. "India": 48,
  46. "Indonesia": 49,
  47. "Iran": 56,
  48. "Ireland": 54,
  49. "Israel": 58,
  50. "Italy": 10,
  51. "Japan": 45,
  52. "Latvia": 71,
  53. "Lithuania": 72,
  54. "Macedonia-FYROM": 79,
  55. "Malaysia": 66,
  56. "Mexico": 26,
  57. "Moldova": 52,
  58. "Montenegro": 80,
  59. "Netherlands": 31,
  60. "New-Zealand": 84,
  61. "Nigeria": 170,
  62. "North-Korea": 73,
  63. "Norway": 37,
  64. "Pakistan": 57,
  65. "Paraguay": 75,
  66. "Peru": 77,
  67. "Philippines": 67,
  68. "Poland": 35,
  69. "Portugal": 53,
  70. "Romania": 1,
  71. "Russia": 41,
  72. "Saudi-Arabia": 164,
  73. "Serbia": 65,
  74. "Singapore": 68,
  75. "Slovakia": 36,
  76. "Slovenia": 61,
  77. "South-Africa": 51,
  78. "South-Korea": 47,
  79. "Spain": 15,
  80. "Sweden": 38,
  81. "Switzerland": 30,
  82. "Taiwan": 81,
  83. "Thailand": 59,
  84. "Turkey": 43,
  85. "Ukraine": 40,
  86. "United-Arab-Emirates": 166,
  87. "United-Kingdom": 29,
  88. "Uruguay": 74,
  89. "USA": 24,
  90. "Venezuela": 28
  91. };
  92. function jsonRequest(u, onSuccess) {
  93. if ( typeof GM_xmlhttpRequest == 'undefined' ) {
  94. var aR = new XMLHttpRequest();
  95. aR.onreadystatechange = function() {
  96. if( aR.readyState == 4 && (aR.status == 200 || aR.status == 304))
  97. onSuccess(eval('(' + aR.responseText + ')'));
  98. };
  99. aR.open('GET', u, true);
  100. aR.setRequestHeader('Accept', 'application/json, text/javascript, */*; q=0.01');
  101. aR.send();
  102. } else GM_xmlhttpRequest({
  103. method: "GET",
  104. url: u,
  105. onload: function(e) {
  106. onSuccess(JSON.parse(e.responseText));
  107. }
  108. });
  109. }
  110. function $g(aID) {return (aID !== '' ? document.getElementById(aID) : null);}
  111. function $gt(str,m) { return (typeof m == 'undefined' ? document:m).getElementsByTagName(str); }
  112. function $gc(str,m) { return (typeof m == 'undefined' ? document:m).getElementsByClassName(str); }
  113. function $at(aElem, att) {if (att !== undefined) {for (var xi in att) {aElem.setAttribute(xi, att[xi]); if (xi.toUpperCase() == 'TITLE') aElem.setAttribute('alt', att[xi]);}}}
  114. function $t(iHTML) {return document.createTextNode(iHTML);}
  115. function $e(nElem, att) {var Elem = document.createElement(nElem); $at(Elem, att); return Elem;}
  116. 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;}
  117. 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;}
  118. function $em(nElem, mElem, att) {var Elem = $e(nElem, att); return $am(Elem, mElem);}
  119. function insertAfter(node, rN) {rN.parentNode.insertBefore(node, rN.nextSibling);}
  120. var useDOMs = typeof window.localStorage == 'undefined' ? false: true;
  121. if ( ! (typeof GM_getValue == 'undefined' || /khtml/i.test(navigator.appVersion)) ) {
  122. if( useDOMs )
  123. if( GM_getValue('brokenFF',0) == 1 ) useDOMs = false;
  124. else {
  125. GM_setValue('brokenFF',1);
  126. if( typeof window.localStorage.setItem != "undefined" ) GM_setValue('brokenFF',2);
  127. }
  128. }
  129. function RB_getValue ( key, defaultValue ) {
  130. if( useDOMs ) {
  131. var value = window.localStorage.getItem(key);
  132. if( value === null ) value = defaultValue;
  133. return value;
  134. } else return GM_getValue( key, defaultValue );
  135. }
  136. function RB_setValue( key, value ) {
  137. if( useDOMs )
  138. window.localStorage.setItem( key, value );
  139. else
  140. GM_setValue( key, value );
  141. }
  142. if (typeof GM_addStyle == 'undefined' ) {
  143. function GM_addStyle(css) {
  144. var head = document.getElementsByTagName('head')[0];
  145. if (head) {
  146. var style = document.createElement("style");
  147. style.type = "text/css";
  148. style.appendChild($t(css));
  149. head.appendChild(style);
  150. }
  151. }
  152. }
  153. function getDivision(e) {
  154. if (e < 35) return 1;
  155. if (e < 50) return 2;
  156. if (e < 70) return 3;
  157. return 4;
  158. }
  159. function wallStyle (wallL) {
  160. return 'margin:0;padding:0;'
  161. + 'background: -moz-linear-gradient(left, #33cc33 0%, #33cc33 ' + wallL + '%, #cc3333 ' + wallL + '%, #cc3333 100%);'
  162. + 'background: -webkit-gradient(linear, left top, right top, color-stop(0%,#33cc33), color-stop(' + wallL + '%,#33cc33), color-stop(' + wallL + '%,#cc3333), color-stop(100%,#cc3333));'
  163. + 'background: -webkit-linear-gradient(left, #33cc33 0%,#33cc33 ' + wallL + '%,#cc3333 ' + wallL + '%,#cc3333 100%);'
  164. + 'background: -o-linear-gradient(left, #33cc33 0%,#33cc33 ' + wallL + '%,#cc3333 ' + wallL + '%,#cc3333 100%);'
  165. + 'background: -ms-linear-gradient(left, #33cc33 0%,#33cc33 ' + wallL + '%,#cc3333 ' + wallL + '%,#cc3333 100%);'
  166. + 'background: linear-gradient(to right, #33cc33 0%,#33cc33 ' + wallL + '%,#cc3333 ' + wallL + '%,#cc3333 100%);'
  167. + 'border:solid #666 1px;';
  168. }
  169. function showPoints(order, countryIdL, countryIdR, wallL, wallR, dominationL, dominationR, totalL, totalR) {
  170. $am($g("order" + order),[
  171. $e('img',{class:"egov_img egov_iL", src:"http://cdn.egov4you.org/img/flags/" + countryIdL + ".gif"}),
  172. $e('img',{class:"egov_img egov_iR", src:"http://cdn.egov4you.org/img/flags/" + countryIdR + ".gif"}),
  173. $ee('span', totalL, {class:"egov_total egov_tL"}),
  174. $ee('span', totalR, {class:"egov_total egov_tR"}),
  175. $ee('span', dominationL, {class:"egov_domination egov_dL"}),
  176. $ee('span', dominationR, {class:"egov_domination egov_dR"}),
  177. $em('div',[
  178. $ee('span', wallL + '%', {class:"egov_wall egov_wL"}),
  179. $ee('span', wallR + '%', {class:"egov_wall egov_wR"}),
  180. Math.max(dominationL,dominationR) < 1800 ? "":
  181. $ee('span', 'Waiting', {class:"egov_percent"})
  182. ],{style:"position:absolute;left:350px;top:10px;width:270px;height:22px;" + wallStyle(wallL)})
  183. ]);
  184. }
  185. function showWall(order, countryIdL, countryIdR, wallL, wallR, dominationL, dominationR, totalL, totalR) {
  186. var bPercent = Math.floor(Math.max(dominationL,dominationR)/18);
  187. bPercent = bPercent >= 100 ? "waiting": bPercent + "%";
  188. $am($g("order" + order),[
  189. $ee('span', totalL, {class:"egov_total egov_tL"}),
  190. $ee('span', totalR, {class:"egov_total egov_tR"}),
  191. $ee('span', bPercent, {class:"egov_percent"}),
  192. $em('div',[
  193. $e('img', {class:"egov_img egov_iL", src:"http://cdn.egov4you.org/img/flags/" + countryIdL + ".gif"}),
  194. $e('img', {class:"egov_img egov_iR", src:"http://cdn.egov4you.org/img/flags/" + countryIdR + ".gif"}),
  195. $ee('span', wallL, {class:"egov_wall egov_wL"}),
  196. $ee('span', wallR, {class:"egov_wall egov_wR"}),
  197. ],{style:"position:absolute;left:145px;top:31px;width:80px;height:11px;" + wallStyle(wallL)})
  198. ]);
  199. }
  200. function getPointsWall(battleId, order, division) {
  201. // jsonRequest("/en/military/battle-status/" + battleId + "/1", function(json) {
  202. jsonRequest("/en/military/nbp-stats/" + battleId + "/"+division, function(json) {
  203. if (json.message != 'BATTLE_CLOSED') {
  204. var countryIdL = Object.getOwnPropertyNames(json.division)[0];
  205. var countryIdR = Object.getOwnPropertyNames(json.division)[1];
  206. if( json.division.domination[division] == 50 && json.division.domination[11] != 50 )
  207. division = 11;
  208. var domination = Math.round(json.division.domination[division] * 1000) / 1000;
  209. domination = domination.toFixed(3);
  210. var bar = json.division.bar[division];
  211. var wallL = domination > 50 ? (
  212. bar == countryIdL ? domination: 100 - domination
  213. ):(
  214. bar == countryIdL ? 100 - domination: domination
  215. );
  216. var wallR = 100 - wallL;
  217. var dominationL = json.division[countryIdL][division].domination;
  218. var dominationR = json.division[countryIdR][division].domination;
  219. var totalL = json.division[countryIdL].total;
  220. var totalR = json.division[countryIdR].total;
  221. if (battleList) {
  222. wallL = Number(wallL).toFixed(3);
  223. wallR = Number(wallR).toFixed(3);
  224. showPoints(order, countryIdL, countryIdR, wallL, wallR, dominationL, dominationR, totalL, totalR);
  225. } else {
  226. wallL = Number(wallL).toFixed(2);
  227. wallR = Number(wallR).toFixed(2);
  228. showWall(order, countryIdL, countryIdR, wallL, wallR, dominationL, dominationR, totalL, totalR);
  229. }
  230. if( totalL < 94 && totalR < 94 ) {
  231. if (order == 6) {
  232. $g("hq1").style.display="block";
  233. $g("hq2").style.display="block";
  234. } else {
  235. $g("mod1").style.display="block";
  236. $g("mod2").style.display="block";
  237. }
  238. $g("order" + order).style.display="block";
  239. }
  240. }
  241. });
  242. }
  243. function drawStars2(n) {
  244. var stars = $e('div', {class:"egov_stars"});
  245. for (var i = 0; i < n; i++) {
  246. stars.appendChild($e('img', {style:"width:" + (battleList ? 18:16) + "px;margin:0;", alt:"*", src:"http://cdn.egov4you.org/img/star.png"}));
  247. }
  248. return stars;
  249. }
  250. function showOrder ( order, fl ) {
  251. return $em('li',[
  252. drawStars2(order.stars),
  253. $ee('span', order.region, {class:"egov_region"}),
  254. $ee('span', order.note, {class:"egov_note"}),
  255. $ee('span', $ee('a', 'Fight', {title:"", href:(order.side !== null ?
  256. '/en/military/battlefield-choose-side/' + order.battle + '/' + order.side:
  257. '/en/military/battlefield/' + order.battle), class:"button_body colour_"+(order.stars>4?"red":"blue")}), {class:"button_bkg fight_button egov_button"})
  258. ],{id:"order" + (fl ? order.order: 6), class:"egov_li", style:"display:none;"});
  259. }
  260. var egov4Country = RB_getValue('egov4Country',40);
  261. if( ! battleList ) {
  262. var sb = $gc('user_finances');
  263. if( sb.length == 0 ) return;
  264. var str = 'egovatwar <a target="_blank" href="'+scriptLink+'">v' + egov4war + '</a><select id="selectCountry" style="margin-top:3px;width:150px;font-size:10px;">';
  265. for( var country in countries )
  266. str += '<option value="' + countries[country] + '"' + (countries[country] == egov4Country ?' selected="selected">':'>') + country + '</option>';
  267. str += '</select>';
  268. insertAfter($ee('div', str, {id:"egov4warSidebar", style:"font-size:10px;font-weight:bold;color:#333;"}),sb[0]);
  269. $g('selectCountry').addEventListener('change', function() {
  270. RB_setValue('egov4Country', this.value);
  271. window.location = location.href;
  272. },false);
  273. }
  274. GM_addStyle("#battle_listing li.egov_li {height: auto; padding: 5px 4px 3px;position:relative;} "+
  275. ".egov_region {position:absolute;left:6px;top:5px;font-size:12px;line-height:14px;color:#333;font-weight:bold;} "+
  276. ".egov_note {position:absolute;left:6px;top:18px;font-size:10px;line-height:12px;color:#333;max-width:205px;overflow:hidden;white-space:nowrap;} "+
  277. ".egov_stars {white-space:nowrap;position:absolute;left:2px;top:29px;} "+
  278. ".egov_updated {color:#b6b6b6;font-size:90%;font-weight:normal;} "+
  279. (battleList?
  280. ".egov_button a {margin-left: 700px;margin-right:20px;} "+
  281. "#battle_listing .egov_domination {width:30px;position:absolute;top:17px;font-size:10px;line-height:10px;color:#333;font-weight:bold;} "+
  282. ".egov_dL {text-align:right;left:315px;} .egov_dR {text-align:left;left:627px;} "+
  283. "#battle_listing .egov_img {position:absolute;top:13px;height:16px;border:solid #666 1px;} "+
  284. ".egov_iL {left:210px;} .egov_iR {left:276px;} "+
  285. "#battle_listing .egov_total {width:14px;position:absolute;top:17px;font-size:10px;line-height:10px;color:#333;font-weight:bold;} "+
  286. ".egov_tL {left:239px;text-align:left;} .egov_tR {left:258px;text-align:right;} "+
  287. "#battle_listing .egov_wall {width:45px;position:absolute;bottom:6px;font-size:10px;line-height:10px;color:#fff;font-weight:bold;} "+
  288. ".egov_wL {text-align:left;left:7px;} .egov_wR {text-align:right;right:6px;} "+
  289. "#battle_listing .egov_percent {width:60px;text-align:center;position:absolute;left:105px;bottom:3px;font-size:16px;line-height:16px;color:#ff3;font-weight:bold;}"
  290. :
  291. ".egov_button {padding: 6px 10px 6px 257px;}"+
  292. ".egov_button a {padding: 1px 15px 0px !important;} "+
  293. "#battle_listing .egov_img {position:absolute;bottom:0;width:12px;height:9px;border:solid #666 1px;} "+
  294. ".egov_iL {left:-16px;} .egov_iR {right:-16px;} "+
  295. "#battle_listing .egov_total {width:15px;position:absolute;top:32px;font-size:10px;line-height:10px;color:#333;font-weight:bold;z-index:100;} "+
  296. ".egov_tL {left:112px;text-align:right;} .egov_tR {left:245px;text-align:left;} "+
  297. "#battle_listing .egov_wall {width:40px;position:absolute;bottom:1px;font-size:10px;line-height:10px;color:#fff;font-weight:bold;} "+
  298. ".egov_wL {text-align:left;left:3px;} .egov_wR {text-align:right;right:2px;} "+
  299. "#battle_listing .egov_percent {width:50px;text-align:right;position:absolute;left:180px;top:12px;font-size:10px;line-height:10px;color:#333;font-weight:bold;}"
  300. )
  301. );
  302. jsonRequest('http://www.egov4you.org/orders/data/' + egov4Country,
  303. function(json) {
  304. var division = getDivision(parseInt($gt('b',$gc("user_level")[0])[0].innerHTML));
  305. str = '';
  306. var ordersDiv = $e('div', {style:"clear:both;float:left;"});
  307. insertAfter(ordersDiv,battleList?$g("campaignsSearchContainer"):$g("battle_listing").childNodes[1]);
  308. if (json.target && json.target.battle !== null && (json.target.div === null || json.target.div == division)) {
  309. if (json.target.note.length == 0) {
  310. json.target.note = 'Fight!';
  311. }
  312. $am(ordersDiv,[
  313. $em('h4',[
  314. json.alliance + ' HeadQuarters ',
  315. $ee('span','(updated in day ' + json.target.updated + ')', {class:"egov_updated"})
  316. ],{id:"hq1", style:"display:none;float:none;"}),
  317. $ee('ul', showOrder(json.target, false), {id:"hq2", style:"display:none;"})
  318. ]);
  319. getPointsWall(json.target.battle, '6', division);
  320. }
  321. if (json.orders) {
  322. var checkBattles = false;
  323. for(var index in json.orders) {
  324. if (json.orders[index].battle !== null) {
  325. checkBattles = true;
  326. }
  327. }
  328. if (checkBattles) {
  329. $am(ordersDiv,[
  330. $em('h4',[
  331. 'Ministry of Defence ',
  332. $ee('span', '(updated in day ' + json.updated + ')', {class:"egov_updated"})
  333. ],{id:"mod1", style:"display:none;float:none;"}),
  334. $e('ul',{id:"mod2", style:"display:none;"})
  335. ]);
  336. for(var index in json.orders) {
  337. if (json.orders[index].battle !== null && (json.orders[index].div === null || json.orders[index].div == division)) {
  338. if (json.orders[index].note.length == 0) {
  339. json.orders[index].note = 'Fight!';
  340. }
  341. $g('mod2').appendChild( showOrder(json.orders[index], true) );
  342. getPointsWall(json.orders[index].battle, json.orders[index].order, division);
  343. }
  344. }
  345. }
  346. }
  347. if ( ! battleList ) {
  348. if (json.shouts) {
  349. var firstPost = $gc("wall_post")[0];
  350. for(var index in json.shouts) {
  351. if (json.shouts[index].text.length > 0) {
  352. var sT = json.shouts[index].alliance !== null ? ["/en/main/alliances",2]:["/en/military/campaigns",1];
  353. firstPost.parentNode.insertBefore($em('li',[
  354. $em('a',[
  355. $e('span'),
  356. $e('img',{src:"http://cdn.egov4you.org/img/about"+sT[1]+".png"})
  357. ],{href:sT[0], class:"user_pic", target:"_blank"}),
  358. $em('div',[
  359. $em('h6',[
  360. $ee('a', (sT[1]==2?json.shouts[index].alliance + ' HeadQuarters':'Ministry of Defence'), {href:sT[0]}),
  361. $ee('em','wrote in day ' + json.shouts[index].updated)
  362. ]),
  363. $ee('p',json.shouts[index].text.replace('="www','="http://www'))
  364. ],{class:"post_content"})
  365. ],{class:"wall_post"}), firstPost);
  366. }
  367. }
  368. }
  369. }
  370. });
  371. }
  372.  
  373. if (window.location.href.split("/").length == 4) {
  374. egov4war_start(false);
  375. } else if (window.location.href.indexOf("/military/campaigns") > 0) {
  376. egov4war_start(true);
  377. }