Mission Queue

Put "Mission Commands" in the fleet notes.

当前为 2018-11-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Mission Queue
  3. // @namespace http://bitbucket/odahviing
  4. // @description Put "Mission Commands" in the fleet notes.
  5. // @include http://*.war-facts.com/fleet.php*
  6. // @grant GM_xmlhttpRequest
  7. // @version 2.0
  8. // ==/UserScript==
  9.  
  10. // Version 1.0, 1.1, 1.2 - Carabas Amazing Script
  11. // Version 2.0 - Cohenman - Basic support in the new user interface, many bugs still
  12.  
  13. /*
  14. To create a mission, start with the MISSION tags in the fleet notes.
  15. <MISSION step=1>
  16. </MISSION>
  17.  
  18. They must both be on seperate lines. The mission tags are there to
  19. a) let you have other notes
  20. b) give a place for the script to save the current "step".
  21.  
  22. The step=# is the current "step" of the mission queue.
  23. There are two ways to manually set the step. The first, obviously,
  24. is to edit the <MISSION> tag and press the "Take note!" button.
  25. The second, is to enter step:# into the current mission text box and press the button.
  26.  
  27. There are two mission types. They are "mission" and "trade".
  28. Basic syntax:
  29.  
  30. mission:[[restart]transfer|explore|colonize|jump];[planet|system|global|local]:[planetid|systemid|globalcoords|localcoords]
  31. trade:[buy|sell|buyall|sellall|refuel];outpost:outpostid;ship:shipid[;resource:amount[;resource:amount]etc]
  32. trade:buyall, trade:sellall and trade:refuel do not require the resource:amount.
  33.  
  34. outpost and ship are always require for resource trades, however only outpost is
  35. required for the refuel command. The resource:all syntax is also supported.
  36.  
  37. ex: trade:sell;outpost:123;ship:122456;oil:all;iron:50000;gold:all
  38.  
  39. Please pay extra attention to the syntax. Each command is made up of several
  40. field:value pairs. Each part of the pair is seperated by a colon. Each pair is
  41. seperated by a semi-colon.
  42.  
  43. Here are some examples:
  44.  
  45. Basic trade mission:
  46. <MISSION step=1>
  47. mission:transfer;planet:12234
  48. trade:buyall;outpost:123;ship:293458
  49. mission:transfer;planet:12322
  50. trade:sellall;outpost:123;ship:293458
  51. mission:restart
  52. </MISSION>
  53.  
  54. Colonize mission passing through a wormhole, refueling at a warpnet, and colonizing a planet:
  55. <MISSION step=1>
  56. mission:transfer;planet:12345
  57. mission:transfer;planet:12346
  58. mission:transfer;planet:12347
  59. mission:jump;local:85,-20,6
  60. mission:transfer;global:12234,-32468,560
  61. trade:refuel;outpost:123
  62. mission:colonize;planet:12348
  63. </MISSION>
  64. */
  65.  
  66. var fleetid = window.location.href.match(/fleet=(\d+)/);
  67. if (fleetid) fleetid = fleetid[1];
  68. else return;
  69.  
  70. var notes = document.getElementById('fleetNoteContent');
  71. var url = 'http://' + window.location.hostname + '/fleet.php?fleet=' + fleetid;
  72.  
  73. var missions = [];
  74. var currentMission = 0;
  75.  
  76. function parseNote() {
  77. let allLines = notes.innerHTML.split('\n');
  78. if (allLines[0].indexOf('&lt;MISSION') != 0)
  79. return undefined;
  80.  
  81. for (let i = 1; i < allLines.length -1; i++)
  82. {
  83. missions.push({
  84. topic: allLines[i].split(';')[0].split(':')[0],
  85. type: allLines[i].split(';')[0].split(':')[1],
  86. details: allLines[i].split(';')[1].split(':')
  87. });
  88. }
  89.  
  90. let getNum = allLines[0].substring(allLines[0].indexOf('step')+5, allLines[0].indexOf('&gt'));
  91. currentMission = parseInt(getNum);
  92. return true;
  93. }
  94.  
  95. function doMission() {
  96. let Mission = missions[currentMission];
  97. if (Mission == undefined)
  98. {
  99. console.log('No Mission');
  100. return;
  101. }
  102.  
  103. console.log('Topic:' + Mission.topic);
  104. if (Mission.topic == 'mission')
  105. {
  106. let action = Mission.details[0];
  107. document.getElementById('objective').value = Mission.type;
  108.  
  109. console.log('action:' + action);
  110. switch (action)
  111. {
  112. case 'planet':
  113. runExploreMission(Mission.details[1]);
  114. break;
  115. case 'system':
  116. runMissionbyUrl(Mission.type, '&tsystem=' + Mission.details[1]);
  117. break;
  118. case 'global':
  119. break;
  120. case 'local':
  121. break;
  122. }
  123. // updateMissionFinish();
  124. }
  125. }
  126.  
  127. function updateMissionFinish()
  128. {
  129. currentMission++;
  130. var newNotes = notes.value.replace(/step=\d+/, 'step=' + currentMission);
  131. updateNoteServer(newNotes);
  132. }
  133.  
  134. function updateNoteServer(textNotes)
  135. {
  136. var url = 'http://' + window.location.hostname + '/notemanager.php';
  137. var pars = 'type=fleet&id=' + fleetid + '&note=' + encodeURIComponent(textNotes);
  138. GM_xmlhttpRequest({
  139. method : 'POST',
  140. url : url,
  141. headers : {
  142. 'User-agent' : 'Mozilla/4.0 (compatible) Greasemonkey',
  143. 'Accept' : 'application/atom+xml,application/xml,text/xml,text/html',
  144. 'Content-Type' : 'application/x-www-form-urlencoded'
  145. },
  146. data : pars,
  147. onload : function () {
  148. notes.value = textNotes
  149. }
  150. });
  151. }
  152.  
  153.  
  154.  
  155.  
  156. function runExploreMission(wantedPlanet)
  157. {
  158. let optionsElements = document.getElementById('target1')
  159. optionsElements.value='tworld,' + wantedPlanet;
  160. getMission("verify", "target1")
  161. setTimeout(getMission('launch'),200);
  162. }
  163.  
  164. function loadButtons()
  165. {
  166. let newButton = document.createElement('input');
  167. newButton.type = 'button'
  168. newButton.style = 'width: 130px;'
  169. newButton.className = 'darkbutton dangerbutton';
  170. newButton.addEventListener("click", doMission);
  171. document.getElementsByClassName('iBlock tbborder padding5 fullwidth light')[0].insertBefore(newButton, null);
  172. }
  173.  
  174. function runMissionbyUrl(type, Url)
  175. {
  176. window.location = url + Url;
  177. document.getElementById('objective').value = type;
  178. }
  179.  
  180. function updateMissionStatus() {
  181. var missions = mission_notes[2].split("\n");
  182. mnum = mnum != undefined ? mnum : mission_notes[1];
  183. mnum = (missions[mnum] && missions[mnum].indexOf('restart') == 8) ? 0 : mnum;
  184. var current_mission = missions[mnum] ? missions[mnum] : 'done';
  185.  
  186. if (current_mission.indexOf('done') == 0) {
  187. b.value = "Restart";
  188. } else if (current_mission.indexOf('mission') == 0) {
  189. b.value = "Launch";
  190. } else if (current_mission.indexOf('trade') == 0) {
  191. b.value = "Trade";
  192. }
  193.  
  194. t.value = current_mission;
  195. }
  196.  
  197. function missionDo(mission) {
  198. // Do Mission stuff
  199.  
  200. var type;
  201. var subtype;
  202. var marray = mission.split(';');
  203.  
  204. for (var i = 0, len = marray.length; i < len; i++) {
  205. console.log(marray);
  206. var m = marray[i].split(':');
  207.  
  208. // Step
  209. if (m[0] == 'step') {
  210. mnum = m[1] - 1;
  211. saveMission(mnum);
  212. updateMission();
  213. return;
  214. }
  215. // Done
  216. if (m[0] == 'done') {
  217. mnum = 0;
  218. updateMission();
  219. return;
  220. }
  221.  
  222. // Mission
  223. if (type == 'mission') {
  224. switch (m[0]) {
  225. case 'planet':
  226. runPlanetMission(m[1]);
  227. //url += '&tworld2=' + m[1];
  228. break;
  229. case 'system':
  230. url += '&tsystem=' + m[1];
  231. runMissionbyUrl(url);
  232. break;
  233. case 'colony':
  234. url += '&tcolony2=' + m[1];
  235. runMissionbyUrl(url);
  236. break;
  237. case 'global':
  238. url += '&tpos=global&rawcoords=' + encodeURIComponent(m[1]);
  239. break;
  240. case 'local':
  241. url += '&tpos=local&rawcoords=' + encodeURIComponent(m[1]);
  242. break;
  243. default:
  244. break;
  245. }
  246.  
  247. }
  248. if (m[0] == 'mission') {
  249. url += 'fleet_navigation.php?fleet=' + fleetid;
  250. switch (m[1]) {
  251. case 'transfer':
  252. case 'explore':
  253. case 'transport':
  254. case 'colonize':
  255. case 'jump':
  256. break;
  257. default:
  258. m[1] = 'transfer';
  259. }
  260. url += '&verify=1&mtype=' + m[1];
  261. type = 'mission';
  262. }
  263.  
  264. // Trade
  265. if (m[0] == 'trade') {
  266. var outpost;
  267. var colony;
  268. var ship;
  269. switch (m[1]) {
  270. case 'refuel':
  271. type = 'refuel';
  272. case 'buy':
  273. case 'buyall':
  274. case 'sell':
  275. case 'sellall':
  276. break;
  277. default:
  278. s.value = "Syntax Error!";
  279. return;
  280. }
  281. subtype = m[1];
  282. }
  283.  
  284. if ((outpost && ship) || colony) {
  285. type = 'trade';
  286. }
  287.  
  288. if (type != 'trade') {
  289. switch (m[0]) {
  290. case 'outpost':
  291. outpost = m[1];
  292. url += 'outposttrade.php?fleet=' + fleetid + '&outpost=' + m[1];
  293. outpost = 1;
  294. if (type == 'refuel') {
  295. execMission('trade', url + '&refuel=1');
  296. return;
  297. }
  298. break;
  299. case 'ship':
  300. ship = m[1];
  301. /*url += '&shipselect='+m[1]; ship=1;*/
  302. break;
  303. case 'colony':
  304. colony = m[1];
  305. break;
  306. }
  307. }
  308.  
  309. if (((outpost && ship) || colony) && subtype && subtype != 'load' && subtype != 'unload') {
  310. if (outpost) {
  311. url += 'outposttrade.php?fleet=' + fleetid + '&outpost=' + outpost + '&shipselect=' + ship;
  312. } else if (colony) {
  313. url += 'cargo_fleet.php?posted=1&fleet=' + fleetid + '&colony=' + colony;
  314. }
  315.  
  316. if (mission.indexOf('all') != -1 || mission.indexOf('%') != -1) {
  317. getResources(subtype, url, mission);
  318. return;
  319. }
  320.  
  321. switch (subtype) {
  322. case 'refuel':
  323. break;
  324. case 'sell':
  325. subtype = 'unload';
  326. break;
  327. case 'buy':
  328. subtype = 'load';
  329. break;
  330. //case 'sellall': getResources('sell',url,mission);return;
  331. //case 'buyall': getResources('buy',url,mission);return;
  332. default:
  333. s.value = 'Syntax Error!';
  334. return;
  335. }
  336. }
  337.  
  338. if (type == 'trade') {
  339. if (subtype == 'load' || subtype == 'unload') {
  340. switch (m[0]) {
  341. case 'iron':
  342. case 'copper':
  343. case 'silver':
  344. case 'titanium':
  345. case 'gold':
  346. case 'uranium':
  347. case 'platinum':
  348. case 'diamonds':
  349. case 'oil':
  350. case 'water':
  351. case 'food':
  352. break;
  353. default:
  354. s.value = 'Syntax Error!';
  355. return;
  356. }
  357.  
  358. // Grab trade URL in the background.
  359. if (outpost) {
  360. execMission(type, url + '&cargo_type=' + m[0] + '&lu' + m[0] + '=' + subtype + '&' + m[0] + '=' + m[1]);
  361. } else if (colony) {
  362. url += '&lu' + m[0] + '=' + subtype + '&' + m[0] + '=' + m[1];
  363. }
  364. }
  365. }
  366. }
  367. // Mission Stuff
  368.  
  369. // Go to mission URL
  370. // execMission(type, url);
  371. }
  372.  
  373. function main() {
  374. if (document.evaluate("//b[text()='In Transit']", document, null, XPathResult.BOOLEAN_TYPE, null).booleanValue) {
  375. return;
  376. }
  377.  
  378. hasNotes = parseNote();
  379. if (hasNotes == undefined)
  380. {
  381. console.log('No Mission Log')
  382. return;
  383. }
  384.  
  385. setTimeout(loadButtons, 50);
  386.  
  387. return;
  388.  
  389.  
  390. //var note_submit = notes.parentNode.getElementsByTagName('input')[2];
  391. var t;
  392. var b;
  393. var s;
  394. var p;
  395. var form2;
  396. var gets = 0;
  397.  
  398. var restypes = new Array('iron', 'copper', 'silver', 'titanium', 'gold', 'uranium', 'platinum', 'diamonds', 'oil', 'water', 'food');
  399.  
  400.  
  401.  
  402. var mission_notes = notes.value.match(/<MISSION step=(\d+)>\n([\s\S]+?)\n<\/MISSION>/);
  403. //console.log(mission_notes);
  404.  
  405. if (mission_notes) {
  406. t = document.createElement('input');
  407. b = document.createElement('input');
  408. s = document.createElement('input');
  409. t.setAttribute('class', 'text');
  410. t.setAttribute('type', 'text');
  411. t.setAttribute('id', 'missionbox');
  412. t.setAttribute('size', '50');
  413. b.setAttribute('class', 'warn');
  414. b.setAttribute('type', 'button');
  415. b.setAttribute('id', 'missiondo');
  416. b.setAttribute('onclick', 'missionDo(document.getElementById("missionbox").value)');
  417. s.setAttribute('class', 'text');
  418. s.setAttribute('type', 'text');
  419. s.setAttribute('id', 'statusbox');
  420. s.setAttribute('size', '60');
  421. s.disabled = true;
  422. var fleetFrame = document.getElementById('fleetNote');
  423. form2 = fleetFrame.getElementsByClassName('highlight fullwidth padding5')[0];
  424. p = document.createElement('p');
  425. p.appendChild(t);
  426. p.appendChild(b);
  427. p.appendChild(document.createElement('br'));
  428. p.appendChild(s);
  429. form2.appendChild(p);
  430. updateMissionStatus();
  431. }
  432.  
  433.  
  434.  
  435. exportFunction(missionDo, unsafeWindow, {defineAs: "missionDo"});
  436.  
  437. window.saveMission = wrap(function (mnum) {
  438. mnum++;
  439. //s.value += 'Saving Mission... ';
  440. var new_notes = notes.value.replace(/step=\d+/, 'step=' + mnum);
  441. var url = 'http://' + window.location.hostname + '/notemanager.php';
  442. var pars = 'type=fleet&id=' + fleetid + '&note=' + encodeURIComponent(new_notes);
  443. GM_xmlhttpRequest({
  444. method : 'POST',
  445. url : url,
  446. headers : {
  447. 'User-agent' : 'Mozilla/4.0 (compatible) Greasemonkey',
  448. 'Accept' : 'application/atom+xml,application/xml,text/xml,text/html',
  449. 'Content-Type' : 'application/x-www-form-urlencoded'
  450. },
  451. data : pars,
  452. onload : function () {
  453. notes.value = new_notes
  454. }
  455. });
  456. });
  457.  
  458. window.execMission = wrap(function (type, url) {
  459. if (type == 'mission') {
  460. mnum++;
  461. saveMission(mnum);
  462. /*updateMission();*/
  463. s.value = 'Launching...';
  464. window.location = url;
  465. } else if (type == 'trade') {
  466. pars = url.split('?')[1];
  467. url = url.split('?')[0];
  468. gets++;
  469. s.value = 'Background Processes: ' + gets;
  470.  
  471. GM_xmlhttpRequest({
  472. method : 'POST',
  473. url : url,
  474. data : pars,
  475. headers : {
  476. 'User-agent' : 'Mozilla/4.0 (compatible) Greasemonkey',
  477. 'Accept' : 'application/atom+xml,application/xml,text/xml,text/html',
  478. 'Content-Type' : 'application/x-www-form-urlencoded'
  479. },
  480. onload : function (responseDetails) {
  481. gets--;
  482. s.value = 'Background Processes: ' + gets;
  483. if (gets == 0) {
  484. mnum++;
  485. s.value = '';
  486. updateMission();
  487. saveMission(mnum);
  488. }
  489. }
  490. });
  491. }
  492. });
  493.  
  494. window.getResources = wrap(function (buysell, url, mission) {
  495. s.value = 'Obtaining Data...';
  496. mission = mission.replace('buyall', 'buy').replace('sellall', 'sell');
  497. var resources = new Array();
  498. var marray = mission.split(';');
  499. var new_mission = new Array();
  500. for (var i = 0, len = marray.length; i < len; i++) {
  501. var res = marray[i].split(':');
  502. if (restypes.indexOf(res[0]) != -1) {
  503. if (res[1].indexOf('%') != -1) {
  504. res[1] = res[1].replace('%', '') / 100;
  505. }
  506. resources[res[0]] = res[1];
  507. } else {
  508. new_mission.push(res[0] + ':' + res[1]);
  509. }
  510. }
  511.  
  512. mission = new_mission.join(';');
  513.  
  514. if (buysell == 'buyall' || buysell == 'sellall') {
  515. for (i in restypes) {
  516. resources[i] = 'all';
  517. }
  518. }
  519.  
  520. GM_xmlhttpRequest({
  521. method : 'GET',
  522. url : url,
  523. headers : {
  524. 'User-agent' : 'Mozilla/4.0 (compatible) Greasemonkey',
  525. 'Accept' : 'application/atom+xml,application/xml,text/xml,text/html',
  526. 'Content-Type' : 'application/x-www-form-urlencoded'
  527. },
  528. onload : function (responseDetails) {
  529. var text = responseDetails.responseText;
  530. //GM_log(text);
  531. var station = text.match(/document\.getElementById\('lo\w+'\)\.checked = true; document\.getElementById\('\w+'\)\.value = \d+/g);
  532. var cargo = text.match(/document\.getElementById\('ul\w+'\)\.checked = true; document\.getElementById\('\w+'\)\.value = \d+/g);
  533.  
  534. var res = (buysell.indexOf('buy') != -1) ? station : cargo;
  535.  
  536. if (res) {
  537. //var resources = new Array();
  538. for (var i = 0, len = res.length; i < len; i++) {
  539. var thisres = res[i].match(/document\.getElementById\('(\w+)'\)\.value = (\d+)/);
  540. if (buysell == 'buyall' || buysell == 'sellall' || resources[thisres[1]] == 'all') {
  541. resources[thisres[1]] = thisres[2];
  542. }
  543. if (resources[thisres[1]] < 1 && resources[thisres[1]] > 0) {
  544. resources[thisres[1]] = Math.round(resources[thisres[1]] * thisres[2]);
  545. }
  546. }
  547.  
  548. res = new Array();
  549. for (i in resources) {
  550. if (resources[i] > 0) {
  551. res.push(i + ':' + resources[i]);
  552. }
  553. }
  554.  
  555. if (res == '') {
  556. s.value = 'The station seems to be empty! ';
  557. mnum++;
  558. updateMission();
  559. saveMission(mnum);
  560. return;
  561. }
  562.  
  563. mission += ';' + res.join(';');
  564.  
  565. // Finally recall missionDo()
  566. window.missionDo(mission);
  567.  
  568. } else {
  569. s.value = 'Your cargo holds are empty! ';
  570. mnum++;
  571. updateMission();
  572. saveMission(mnum);
  573. return
  574. }
  575. }
  576. });
  577. });
  578. }
  579.  
  580. main();