nuSweepFC

Planets.nu plugin for "FC sweep"

  1. /*
  2. Copyright (C) 2017-2019 Thomas Horn
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12.  
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. // ==UserScript==
  17. // @name nuSweepFC
  18. // @description Planets.nu plugin for "FC sweep"
  19. // @version 0.01.08
  20. // @date 2019-06-11
  21. // @author drgirasol
  22. // @include http://planets.nu/*
  23. // @include https://planets.nu/*
  24. // @include http://play.planets.nu/*
  25. // @include https://play.planets.nu/*
  26. // @include http://test.planets.nu/*
  27. // @include https://test.planets.nu/*
  28. // @supportURL https://github.com/drgirasol/nusweepfc/issues
  29. // @homepageURL https://github.com/drgirasol/nusweepfc/wiki
  30. // @grant none
  31.  
  32. // @namespace https://greasyfork.org/users/91175
  33. // ==/UserScript==
  34.  
  35. function wrapper () { // wrapper for injection
  36.  
  37. /*
  38. *
  39. * FC Sweeper
  40. *
  41. */
  42. let sweepfc = {
  43. getRaceAdjectives: function() {
  44. return vgap.races.map(function(r) {
  45. return r.adjective;
  46. });
  47. },
  48. getPotentialRaceAdjectives: function(raceAdjectives) {
  49. return vgap.relations.filter(function (r) {
  50. return r.relationfrom < 2 && r.playertoid !== r.playerid;
  51. }).map(function (r) {
  52. return raceAdjectives[r.playertoid];
  53. });
  54. },
  55. getScannerId: function(m) {
  56. let matchId = m.headline.match(/ID#(\d+)/);
  57. if (matchId) {
  58. return matchId[1];
  59. } else {
  60. return false;
  61. }
  62. },
  63. injectShipFC: function(m, ship) {
  64. let lines = m.body.split("<br/>");
  65. let newBody = [];
  66. lines.forEach(function (line) {
  67. newBody.push(line);
  68. if (line.match(/AT:\s\(/)) newBody.push("FC: " + ship.friendlycode);
  69. });
  70. m.body = newBody.join("<br/>");
  71. },
  72. scanReports: function() {
  73. console.warn("Scanning mine scan reports for 'friendly' enemies...");
  74. vgap.messageTypes.push("FC Sweep");
  75. //vgap.messageTypeCount.push(1);
  76. let raceAdjectives = sweepfc.getRaceAdjectives();
  77. let potEnemies = sweepfc.getPotentialRaceAdjectives(raceAdjectives);
  78. //console.log(potEnemies);
  79. let mineScanReports = vgap.messages.filter(function (m) {
  80. let match = m.body.match(/\(([A-Za-z]+)\) has granted us safe passage/);
  81. //if (m.messagetype === 19 && m.body.match(/has granted us safe passage/) !== null) console.log(m.body);
  82. return m.messagetype === 19 && match !== null && potEnemies.indexOf(match[1]) > -1;
  83. });
  84. //console.log(mineScanReports);
  85. let sfcReportIds = vgap.messages.filter(function (m) {
  86. return m.messagetype === 23;
  87. }).map(function (m) {
  88. return m.id;
  89. });
  90. if (mineScanReports.length > 0) {
  91. mineScanReports.forEach(function (m) {
  92. if (sfcReportIds.indexOf(m.id) === -1) {
  93. let shipId = sweepfc.getScannerId(m);
  94. if (shipId) {
  95. let curShip = vgap.getShip(shipId);
  96. if (curShip) {
  97. sweepfc.injectShipFC(m, curShip);
  98. } else {
  99. console.error("Ship data for s'%s' could not be accessed!", shipId);
  100. }
  101. } else {
  102. console.error("Ship ID could not be determined from report!", m);
  103. }
  104. vgap.messages.push({
  105. body: m.body,
  106. headline: m.headline,
  107. id: m.id,
  108. messagetype: 23,
  109. ownerid: m.ownerid,
  110. target: m.target,
  111. turn: m.turn,
  112. x: m.x,
  113. y: m.y
  114. });
  115. }
  116. });
  117. } else {
  118. if (sfcReportIds.indexOf(11223344) === -1) {
  119. vgap.messages.push({
  120. body: "Nothing to report",
  121. headline: "FC Sweep",
  122. id: 11223344,
  123. messagetype: 23,
  124. ownerid: vgap.player.id,
  125. target: false,
  126. turn: vgap.game.turn,
  127. x: 0,
  128. y: 0
  129. });
  130. }
  131. }
  132. },
  133. /*
  134. * DRAWING
  135. */
  136. // draw: executed on any click or drag on the starmap
  137. draw: function() {
  138. //console.log("Draw: plugin called.");
  139. },
  140. //
  141. /*
  142. * UI - Hooks
  143. */
  144. // processload: executed whenever a turn is loaded: either the current turn or an older turn through time machine
  145. processload: function() {
  146. //console.log(vgap);
  147. sweepfc.scanReports(); // check reports for destroyed vessels
  148. },
  149. // loaddashboard: executed to rebuild the dashboard content after a turn is loaded
  150. loaddashboard: function() {
  151. //console.log("LoadDashboard: plugin called.");
  152. },
  153. // showdashboard: executed when switching from starmap to dashboard
  154. showdashboard: function() {
  155. //console.log("ShowDashboard: plugin called.");
  156. },
  157. // showsummary: executed when returning to the main screen of the dashboard
  158. showsummary: function() {
  159. //console.log("ShowSummary: plugin called.");
  160. },
  161. // loadmap: executed after the first turn has been loaded to create the map
  162. loadmap: function() {
  163. //console.log("LoadMap: plugin called.");
  164. },
  165. // showmap: executed when switching from dashboard to starmap
  166. showmap: function() {
  167. //console.log("ShowMap: plugin called.");
  168. },
  169. // loadplanet: executed when a planet is selected on dashboard or starmap
  170. loadplanet: function() {
  171. //console.log("LoadPlanet: plugin called.");
  172. },
  173. // loadstarbase: executed when a starbase is selected on dashboard or starmap
  174. loadstarbase: function() {
  175. //console.log("LoadStarbase: plugin called.");
  176. },
  177. // loadship: executed when a planet is selected on dashboard or starmap
  178. loadship: function() {
  179. //console.log("LoadShip: plugin called.");
  180. }
  181. };
  182.  
  183.  
  184. // register your plugin with NU
  185. vgap.registerPlugin(sweepfc, "sweepfcPlugin");
  186. console.log("nuSweepFC plugin registered");
  187. } //wrapper for injection
  188.  
  189. var script = document.createElement("script");
  190. script.type = "application/javascript";
  191. script.textContent = "(" + wrapper + ")();";
  192.  
  193. document.body.appendChild(script);