OGame: Messages

Messages espionage table

当前为 2016-04-09 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name OGame: Messages
  3. // @description Messages espionage table
  4. // @namespace OGame: Messages
  5. // @version 1.0.4
  6. // @creator ter3ter
  7. // @include *://*/game/index.php?page=messages*
  8. // @include *://*/game/index.php?page=research*
  9. // @grant GM_openInTab
  10. // ==/UserScript==
  11.  
  12. function strtotime(text, now) {
  13.  
  14. // Unecessary spaces
  15. text = text.replace(/^\s+|\s+$/g, '')
  16. .replace(/\s{2,}/g, ' ')
  17. .replace(/[\t\r\n]/g, '')
  18. .toLowerCase();
  19.  
  20. match = text.match(
  21. /^(\d{1,4})([\-\.\/\:])(\d{1,2})([\-\.\/\:])(\d{1,4})(?:\s(\d{1,2}):(\d{2})?:?(\d{2})?)?(?:\s([A-Z]+)?)?$/);
  22. return new Date(match[5], parseInt(match[3], 10) - 1, match[1],
  23. match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
  24. }
  25.  
  26. function shipCount (m, k, d, cargo)
  27. {
  28. return Math.ceil (Math.ceil (Math.max (m + k + d, Math.min (0.75 * (m * 2 + k + d), m * 2 + d))) / cargo);
  29. }
  30.  
  31. function norm_val(s) {
  32. var mn = 1;
  33. s = s.replace(/,/, '.');
  34. if (s.match(/M/)) {
  35. var rpos = s.length - s.indexOf('.');
  36. if (rpos == 3) {
  37. mn = 100000;
  38. } else if (rpos == 4) {
  39. mn = 10000;
  40. } else if (rpos == 5) {
  41. mn = 1000;
  42. } else {
  43. mn = 1000000;
  44. }
  45. }
  46. s = s.replace(/[M,\.]/g, "");
  47. s *= mn;
  48. return s;
  49. }
  50.  
  51. var messages = [];
  52.  
  53. function parse_messages() {
  54. var options = loadOptions();
  55. var v = '';
  56. if (options.show_all_table == 'no') {
  57. messages = [];
  58. }
  59. $('li.msg').each(function() {
  60. if ($(this).find('.msg_content>div').length == 4) {
  61. var coords = $(this).find('.msg_head>span>a').closest('span');
  62. var met = $(this).find('.msg_content>div.compacting:eq(1)>span.ctn4>span.resspan:eq(0)').text().substring(1).replace(/М/, 'M').replace(/[^0-9M,\.]/g,"");
  63. met = norm_val(met);
  64. var kris = $(this).find('.msg_content>div.compacting:eq(1)>span.ctn4>span.resspan:eq(1)').text().replace(/М/, 'M').replace(/[^0-9M,\.]/g,"");
  65. kris = norm_val(kris);
  66. var deut = $(this).find('.msg_content>div.compacting:eq(1)>span.ctn4>span.resspan:eq(2)').text().replace(/М/, 'M').replace(/[^0-9M,\.]/g,"");
  67. deut = norm_val(deut);
  68. var fleet = $(this).find('.msg_content>div.compacting:eq(3)>span.ctn.ctn4.tooltipLeft').text().replace(/М/, 'M').replace(/[^0-9M,\.]/g,"");
  69. if (!fleet) {
  70. fleet = 'Н/Д';
  71. }
  72. var def = $(this).find('.msg_content>div.compacting:eq(3)>span.ctn.ctn4.tooltipRight').text().replace(/М/, 'M').replace(/[^0-9M,\.]/g,"");
  73. if (!def) {
  74. def = 'Н/Д';
  75. }
  76. var link = $(this).find('.msg_actions>a.msg_action_link').attr('href');
  77. var time = $(this).find('.msg_head>span.msg_date.fright').text();
  78. var nick;
  79. var status;
  80. if ($(this).find('.msg_content>div>span.status_abbr_honorableTarget').length) {
  81. nick = $(this).find('.msg_content>div>span.status_abbr_honorableTarget').text().substr(2);
  82. status = 'status_abbr_honorableTarget';
  83. } else if ($(this).find('.msg_content>div>span.status_abbr_longinactive').length) {
  84. nick = $(this).find('.msg_content>div>span.status_abbr_longinactive').text().substr(2);
  85. status = 'status_abbr_longinactive';
  86. } else if ($(this).find('.msg_content>div>span.status_abbr_inactive').length) {
  87. nick = $(this).find('.msg_content>div>span.status_abbr_inactive').text().substr(2);
  88. status = 'status_abbr_inactive';
  89. } else if ($(this).find('.msg_content>div>span.status_abbr_outlaw').length) {
  90. nick = $(this).find('.msg_content>div>span.status_abbr_outlaw').text().substr(2);
  91. status = 'status_abbr_outlaw';
  92. } else if ($(this).find('.msg_content>div>span.status_abbr_noob').length) {
  93. nick = $(this).find('.msg_content>div>span.status_abbr_noob').text().substr(2);
  94. status = 'status_abbr_noob';
  95. } else if ($(this).find('.msg_content>div>span.status_abbr_active').length) {
  96. nick = $(this).find('.msg_content>div>span.status_abbr_active').text().substr(2);
  97. status = 'status_abbr_active';
  98. }
  99. var loot =$(this).find('.msg_content>div.compacting:eq(2)>span.ctn.ctn4').text().replace(/\D+/g,"");
  100. var activ = ' (' + $(this).find('.msg_content>div.compacting:eq(0)>span.fright').text().replace(/\D+/g,"") + ')';
  101. var all_res = met*1 + kris*1 + deut*1;
  102. all_res *= loot/100;
  103. all_res = Math.round(all_res);
  104. kris *= loot/100;
  105. kris = Math.round(kris);
  106. met *= loot/100;
  107. met = Math.round(met);
  108. deut *= loot/100;
  109. deut = Math.round(deut);
  110. var coords_array = $(this).find('.msg_head>span>a').text().match(/\[([0-9]):([0-9]+):([0-9]+)\]/);
  111. var api = $('<b>'+$(this).find('.msg_actions .icon_apikey').attr('title')+'</b>').find('input').attr('value');
  112. if (!api) {
  113. api = $(this).find('.ago_act_buttons a').attr('href');
  114. api = api.match(/SR_KEY=([^&]+)/);
  115. api = api[1];
  116. }
  117. if (options.table_time == 0 || strtotime(time) > ((new Date()/1000) - 60*options.table_time)) {
  118. messages.push({
  119. coords: coords,
  120. coords_array: coords_array[0].match(/[0-9]+/g),
  121. met: met,
  122. kris: kris,
  123. deut: deut,
  124. fleet: fleet,
  125. def: def,
  126. loot: loot,
  127. all_res: all_res,
  128. nick: nick,
  129. link: link,
  130. time: time,
  131. activ: activ,
  132. mt_count: shipCount(met, kris, deut, 5000) + options.add_mt*1,
  133. bt_count: shipCount(met, kris, deut, 25000) + options.add_bt*1,
  134. api: api
  135. });
  136. }
  137. }
  138. });
  139. }
  140.  
  141. function sort_by_res() {
  142. messages.sort(function(a, b) {
  143. return b.all_res - a.all_res;
  144. });
  145. }
  146. function sort_by_time() {
  147. messages.sort(function(a, b) {
  148. var d1 = strtotime(a.time);
  149. var d2 = strtotime(b.time);
  150. return d2 - d1;
  151. });
  152. }
  153. function sort_by_def() {
  154. messages.sort(function(a, b) {
  155. if (b.def == 'Н/Д') {
  156. return -1;
  157. }
  158. if (a.def == 'Н/Д') {
  159. return 1;
  160. }
  161. console.log(b.def);
  162. return norm_val(b.def) - norm_val(a.def);
  163. });
  164. }
  165. function sort_by_fleet() {
  166. messages.sort(function(a, b) {
  167. if (b.fleet == 'Н/Д') {
  168. return -1;
  169. }
  170. if (a.fleet == 'Н/Д') {
  171. return 1;
  172. }
  173. return norm_val(b.fleet) - norm_val(a.fleet);
  174. });
  175. }
  176.  
  177. function show_table() {
  178. if ($('#scan_table').length > 0) {
  179. $('#scan_table').remove();
  180. }
  181. var cont = $('.pagination:first');
  182. var s = '<li id="scan_table"><table style="width: 100%;padding:5px">';
  183. s += '<tr><th></th><th id="order_time">Время</th><th>Ник</th><th>Координаты</th><th id="order_res">Добыча</th><th id="order_fleet">Флот</th><th id="order_def">Оборона</th><th></th><th>МТ</th><th>БТ</th></tr>';
  184. var options = loadOptions();
  185. for(var i in messages) {
  186. var msg = messages[i];
  187. s += '<tr>';
  188. s += '<td style="padding:3px"><input type="checkbox"></td>';
  189. s += '<td>' + msg.time.substr(11) + '</td>';
  190. s += '<td><a class="txt_link msg_action_link overlay" href="'+msg.link+'">' + msg.nick + msg.activ + '</a></td>';
  191. s += '<td><a href="' + msg.coords.find('a').attr('href') + '">'+msg.coords.find('a').text().match(/\[[0-9]:[0-9]+:[0-9]+\]/)+'</a></td>';
  192. s += '<td title="met: '+msg.met+' kris:'+msg.kris+' deut:'+msg.deut+'">' + msg.all_res.toString().replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ') + '</td>';
  193. s += '<td>' + msg.fleet + '</td>';
  194. s += '<td>' + msg.def + '</td>';
  195. if (options.sim_type == 'speedsim') {
  196. s += '<td><a href="#none" class="open_websim" lootp="'+msg.loot+'">sim</a></td>';
  197. } else {
  198. var tech110 = getCookie('ogmessages_tech110');
  199. var tech111 = getCookie('ogmessages_tech111');
  200. var tech115 = getCookie('ogmessages_tech115');
  201. var tech117 = getCookie('ogmessages_tech117');
  202. var tech118 = getCookie('ogmessages_tech118');
  203. }
  204. if (options.sim_type == 'toprider') {
  205. var url = 'http://topraider.eu/index.php?SR_KEY='+msg.api+'&speed='+document.getElementsByName('ogame-universe-speed-fleet')[0].content;
  206. var tech109 = getCookie('ogmessages_tech109');
  207. if (tech109) {
  208. url += '&arme='+tech109;
  209. }
  210. if (tech110) {
  211. url += '&bouclier='+tech110;
  212. }
  213. if (tech111) {
  214. url += '&protect='+tech111;
  215. }
  216. if (tech115) {
  217. url += '&combu='+tech115;
  218. }
  219. if (tech117) {
  220. url += '&impu='+tech117;
  221. }
  222. if (tech118) {
  223. url += '&prop='+tech118;
  224. }
  225. s += '<td><a target="_blank" href="'+url+'">sim</a></td>';
  226. }
  227. s += '<td><a href="/game/index.php?page=fleet1&galaxy='+msg.coords_array[0]+'&system='+msg.coords_array[1]+'&position='+msg.coords_array[2]+'&type=1&mission=1&am202='+msg.mt_count+'">'+msg.mt_count+'</a></td>';
  228. s += '<td><a href="/game/index.php?page=fleet1&galaxy='+msg.coords_array[0]+'&system='+msg.coords_array[1]+'&position='+msg.coords_array[2]+'&type=1&mission=1&am203='+msg.bt_count+'">' + msg.bt_count + '</a></td>';
  229. }
  230. s += '</table></li>';
  231. cont.after(s);
  232. $('#order_time').click(function() {
  233. sort_by_time();
  234. show_table();
  235. });
  236. $('#order_res').click(function() {
  237. sort_by_res();
  238. show_table();
  239. });
  240. $('#order_def').click(function() {
  241. sort_by_def();
  242. show_table();
  243. });
  244. $('#order_fleet').click(function() {
  245. sort_by_fleet();
  246. show_table();
  247. });
  248. $('.open_websim').click(function() {
  249. var tr = $(this).closest('tr');
  250. var full_link = tr.find('td>a.msg_action_link').attr('href');
  251. $.get(full_link, function(data) {
  252. var dom = $(data);
  253. var ships = {};
  254. var url = 'http://websim.speedsim.net/?';
  255. for (i = 202;i<216;i++) {
  256. if (dom.find('.tech'+i).length == 1) {
  257. var num = i - 202;
  258. url += 'ship_d0_'+num+'_b='+dom.find('.tech'+i).closest('.detail_list_el').find('span.fright').text().replace(/\D+/g,"")+'&';
  259. }
  260. }
  261. for (i = 401;i<409;i++) {
  262. if (dom.find('.defense'+i).length == 1) {
  263. var num = i - 401 + 14;
  264. url += 'ship_d0_'+num+'_b='+dom.find('.defense'+i).closest('.detail_list_el').find('span.fright').text().replace(/\D+/g,"")+'&';
  265. }
  266. }
  267. url += 'enemy_metal=' + norm_val(dom.find('.resourceIcon.metal').closest('.resource_list_el').find('.res_value').text());
  268. url += '&enemy_crystal=' + norm_val(dom.find('.resourceIcon.crystal').closest('.resource_list_el').find('.res_value').text());
  269. url += '&enemy_deut=' + norm_val(dom.find('.resourceIcon.deuterium').closest('.resource_list_el').find('.res_value').text());
  270. url += '&enemy_pos=' + tr.find('td:eq(3)>a').text().replace(/[\[\]]/g,'');
  271. var options = loadOptions();
  272. if (options.sim_send_tech == 'yes') {
  273. if (options.sim_need_tech == 'no' || dom.find('.research109').length == 1 || dom.find('.research110').length == 1 || dom.find('.research111').length == 1)
  274. {
  275. if (dom.find('.research109').length == 1) {
  276. url += '&tech_d0_0='+dom.find('.research109').closest('.detail_list_el').find('.fright').text();
  277. }
  278. if (dom.find('.research110').length == 1) {
  279. url += '&tech_d0_1='+dom.find('.research110').closest('.detail_list_el').find('.fright').text();
  280. }
  281. if (dom.find('.research111').length == 1) {
  282. url += '&tech_d0_2='+dom.find('.research111').closest('.detail_list_el').find('.fright').text();
  283. }
  284. var tech109 = getCookie('ogmessages_tech109');
  285. if (tech109) {
  286. url += '&tech_a0_0='+tech109;
  287. }
  288. var tech110 = getCookie('ogmessages_tech110');
  289. if (tech110) {
  290. url += '&tech_a0_1='+tech110;
  291. }
  292. var tech111 = getCookie('ogmessages_tech111');
  293. if (tech111) {
  294. url += '&tech_a0_2='+tech111;
  295. }
  296. var tech115 = getCookie('ogmessages_tech115');
  297. if (tech115) {
  298. url += '&engine0_0='+tech115;
  299. }
  300. var tech117 = getCookie('ogmessages_tech117');
  301. if (tech117) {
  302. url += '&engine0_1='+tech117;
  303. }
  304. var tech118 = getCookie('ogmessages_tech118');
  305. if (tech118) {
  306. url += '&engine0_2='+tech118;
  307. }
  308. }
  309. }
  310. url += '&uni_speed=' + $("meta[name=ogame-universe-speed-fleet]").attr('content');
  311. url += '&start_pos=' + $("meta[name=ogame-planet-coordinates]").attr('content');
  312. url += '&plunder_perc=' + tr.find('a.open_websim').attr('lootp');
  313. var api_url = 'http://' + $("meta[name=ogame-universe]").attr('content') + '/api/serverData.xml';
  314. $.get(api_url, function(data) {
  315. url += '&perc-df='+$(data).find('debrisFactor').text()*100;
  316. url += '&def_to_df='+$(data).find('defToTF').text();
  317. if (navigator.userAgent.search(/Firefox/) > -1) {
  318. unsafeWindow.open(url, '_blank');
  319. } else {
  320. GM_openInTab(url);
  321. }
  322. });
  323. return false;
  324. });
  325.  
  326. });
  327. }
  328.  
  329. function do_msg() {
  330. parse_messages();
  331.  
  332. show_table();
  333. }
  334.  
  335. function setCookie(name, value) {
  336. options = {};
  337.  
  338. var d = new Date();
  339. d.setTime(d.getTime() + 3600*24*365 * 1000);
  340. options.expires = d.toUTCString();
  341.  
  342. value = encodeURIComponent(value);
  343.  
  344. var updatedCookie = name + "=" + value;
  345.  
  346. for (var propName in options) {
  347. updatedCookie += "; " + propName;
  348. var propValue = options[propName];
  349. if (propValue !== true) {
  350. updatedCookie += "=" + propValue;
  351. }
  352. }
  353. document.cookie = updatedCookie;
  354. }
  355.  
  356. function getCookie(name) {
  357. var matches = document.cookie.match(new RegExp(
  358. "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
  359. ));
  360. return matches ? decodeURIComponent(matches[1]) : undefined;
  361. }
  362.  
  363. function update_tech() {
  364. [109, 110, 111, 115, 117, 118].forEach(function(item, i, arr) {
  365. if ($('a#details'+item+' span.level').length == 1) {
  366. var tech = $('a#details'+item+' span.level').text().replace(/[^0-9M]/g,"");
  367. setCookie('ogmessages_tech'+item, tech);
  368. }
  369. });
  370. }
  371.  
  372. function loadOptions() {
  373. var options = {};
  374. if (getCookie('ogmessages_option_send_tech') == 'no') {
  375. options.sim_send_tech = 'no';
  376. } else {
  377. options.sim_send_tech = 'yes';
  378. }
  379. if (getCookie('ogmessages_option_need_tech') == 'no') {
  380. options.sim_need_tech = 'no';
  381. } else {
  382. options.sim_need_tech = 'yes';
  383. }
  384. if (getCookie('ogmessages_option_show_all_table') == 'no') {
  385. options.show_all_table = 'no';
  386. } else {
  387. options.show_all_table = 'yes';
  388. }
  389. if (getCookie('ogmessages_option_show_all_messages') == 'no') {
  390. options.show_all_messages = 'no';
  391. } else {
  392. options.show_all_messages = 'yes';
  393. }
  394. options.sim_type = 'speedsim';
  395. if (getCookie('ogmessages_option_sim_type') != undefined) {
  396. options.sim_type = getCookie('ogmessages_option_sim_type');
  397. }
  398. options.add_mt = 0;
  399. options.add_bt = 0;
  400. options.table_time = 0;
  401. if (getCookie('ogmessages_option_add_mt') != undefined) {
  402. options.add_mt = getCookie('ogmessages_option_add_mt');
  403. }
  404. if (getCookie('ogmessages_option_add_bt') != undefined) {
  405. options.add_bt = getCookie('ogmessages_option_add_bt');
  406. }
  407. if (getCookie('ogmessages_option_table_time') != undefined) {
  408. options.table_time = getCookie('ogmessages_option_table_time');
  409. }
  410. return options;
  411. }
  412.  
  413. function saveOptions(options) {
  414. setCookie('ogmessages_option_send_tech', options.sim_send_tech);
  415. setCookie('ogmessages_option_need_tech', options.sim_need_tech);
  416. setCookie('ogmessages_option_sim_type', options.sim_type);
  417. setCookie('ogmessages_option_add_bt', options.add_bt);
  418. setCookie('ogmessages_option_add_mt', options.add_mt);
  419. setCookie('ogmessages_option_show_all_table', options.show_all_table);
  420. setCookie('ogmessages_option_show_all_messages', options.show_all_messages);
  421. setCookie('ogmessages_option_table_time', options.table_time);
  422. }
  423.  
  424. var savedContent = false;
  425. function showOptions() {
  426. if (!savedContent) {
  427. savedContent = $('#contentWrapper').html();
  428. $('#contentWrapper').html('<div id="oGameVersionCheck">' +
  429. '<div style="color: rgb(132, 132, 132); margin: 0px; padding: 17px 0px 10px; width: 100%; text-align: center; background: 5px 0px repeat-y scroll black;">' +
  430. '<table style="width: 100%">'+
  431. '<tr><td>Отправлять в сим техи</td><td><input type="checkbox" id="ogmessages_sim_send_tech"></td>' +
  432. '<tr><td>Отправлять в сим свои техи только если известны техи цели(работает только с парсингом доклада)</td><td><input type="checkbox" id="ogmessages_sim_need_tech"></td>' +
  433. '<tr><td>Используемый симулятор</td><td><select id="ogmessages_sim_type" style="visibility:visible">' +
  434. '<option value="toprider">Открыть через topraider(api)</option>' +
  435. '<option value="speedsim">SpeedSim(парсинг доклада)</option>' +
  436. '</select></td>' +
  437. '<tr><td colspan=2><hr></td></tr>' +
  438. '<tr><td>Добавлять МТ к расчётным</td><td><input type="text" id="ogmessages_add_mt" value="0"></td>' +
  439. '<tr><td>Добавлять БТ к расчётным</td><td><input type="text" id="ogmessages_add_bt" value="0"></td>' +
  440. '<tr><td colspan=2><hr></td></tr>' +
  441. '<tr><td>Показывать в таблице все уже загруженные сообщения</td><td><input type="checkbox" id="ogmessages_show_all_table"></td>' +
  442. '<tr><td>Показывать сообщения не старше, минут(0 - показывать все)</td><td><input type="text" id="ogmessages_table_time" value="0"></td>' +
  443. '<tr><td colspan="2"><input type="button" value="Сохранить" id="ogmessages_save_options"></td>' +
  444. '</table></div></div>');
  445. var options = loadOptions();
  446. if (options.sim_send_tech == 'yes') {
  447. $('#ogmessages_sim_send_tech').attr('checked', 'checked');
  448. }
  449. if (options.sim_need_tech == 'yes') {
  450. $('#ogmessages_sim_need_tech').attr('checked', 'checked');
  451. }
  452. if (options.show_all_table == 'yes') {
  453. $('#ogmessages_show_all_table').attr('checked', 'checked');
  454. }
  455. if (options.sim_type) {
  456. $("#ogmessages_sim_type option[value='"+options.sim_type+"']").prop("selected", true);
  457. }
  458. $('#ogmessages_add_mt').val(options.add_mt);
  459. $('#ogmessages_add_bt').val(options.add_bt);
  460. $('#ogmessages_table_time').val(options.table_time);
  461. $('#ogmessages_save_options').click(function() {
  462. var options = {};
  463. options.sim_send_tech = 'no';
  464. options.sim_need_tech = 'no';
  465. options.show_all_table = 'no';
  466. options.show_all_messages = 'no';
  467. if ($('#ogmessages_sim_send_tech').prop('checked') == true) {
  468. options.sim_send_tech = 'yes';
  469. }
  470. if ($('#ogmessages_sim_need_tech').prop('checked') == true) {
  471. options.sim_need_tech = 'yes';
  472. }
  473. if ($('#ogmessages_show_all_table').prop('checked') == true) {
  474. options.show_all_table = 'yes';
  475. }
  476. options.sim_type = $("#ogmessages_sim_type").val();
  477. options.add_mt = $("#ogmessages_add_mt").val();
  478. options.add_bt = $("#ogmessages_add_bt").val();
  479. options.table_time = $("#ogmessages_table_time").val();
  480. saveOptions(options);
  481. showOptions();
  482. });
  483. } else {
  484. $('#contentWrapper').html(savedContent);
  485. savedContent = false;
  486. }
  487. }
  488.  
  489. $(document).ready(function() {
  490. $(document).ajaxSuccess(function(e,xhr,settings) {
  491. if (!settings.url.indexOf("page=messages&tab=20&ajax=1") || !(settings.url.indexOf("page=messages") && settings.data && settings.data.indexOf('tabid=20'))) return;
  492. if ($('.msg_status').length > 0) {
  493. do_msg();
  494. }
  495. });
  496. if ($('#links').length > 0) {
  497. $('#links').append('<ul class="leftmenu">' +
  498. '<li><a id="ogameMassagesOptionsButton" class="menubutton" href="javascript:void(0)" style="color: rgb(255, 75, 0);"><span class="textlabel">OG: Messages</span></a></li>'+
  499. '</ul>');
  500. }
  501. $(document).on('click', '#ogameMassagesOptionsButton', function() {
  502. showOptions();
  503. });
  504. if ($('a#details109').length == 1) {
  505. update_tech();
  506. }
  507. //alert(getCookie('ogmessages_tech111'));
  508. });