Kongregate Portal Report

Find out who has the portals you need

  1. // ==UserScript==
  2. // @name Kongregate Portal Report
  3. // @namespace tag://kongregate.com
  4. // @description Find out who has the portals you need
  5. // @include http://www.kongregate.com/games/*
  6. // @version 0.0.1.20160306135333
  7. // ==/UserScript==
  8.  
  9.  
  10. try{
  11. if(unsafeWindow && unsafeWindow.holodeck){
  12. dom = unsafeWindow;
  13. } else {
  14. dom = this;
  15. }
  16. }catch(e){
  17. dom = this;
  18. }
  19.  
  20. if (window.undefined === unsafeWindow.console)
  21. {
  22. console = new function()
  23. {
  24. this.log = function(message){};
  25. this.info = function(message){};
  26. this.warn = function(message){};
  27. this.error = function(message){};
  28. };
  29. }
  30. else
  31. {
  32. console = unsafeWindow.console;
  33. }
  34.  
  35.  
  36. var KPRVersion = 0.01;
  37.  
  38. function init_portalReport(){
  39. // If no version of the script is running, write down this one
  40. if (!window.KPRMaxVersion)
  41. {
  42. window.KPRMaxVersion = KPRVersion;
  43. setTimeout(init_portalReport, 1000);
  44. }
  45. // If the max version is less than this version, kill it
  46. else if (window.KPRMaxVersion < KPRVersion)
  47. {
  48. window.KPRMaxVersion = KPRVersion;
  49. setTimeout(init_portalReport, 1000);
  50. }
  51. // If this version is less than the max, kill this one
  52. else if (window.KPRMaxVersion > KPRVersion)
  53. {
  54. console.warn("KPR: Version " + KPRVersion + " is still installed, but is older than another version installed");
  55. return;
  56. }
  57. // If no version of the script is running, this one is
  58. if (!window.KPRScriptRunning)
  59. {
  60. window.KPRScriptRunning = true;
  61. }
  62. // If a version of the script is already running, kill this one.
  63. else
  64. {
  65. if (KPRVersion == window.KPRMaxVersion)
  66. {
  67. // TODO: Is a dupe of the script running on the frame of the game, also?
  68. console.warn("KPR: Another instance of Version " + KPRVersion + " attempted to run, but was cancelled.");
  69. }
  70. else
  71. {
  72. console.warn("KPR: Version " + KPRVersion + " failed to run because Version " + window.KPRMaxVersion + " was already running.");
  73. }
  74. return;
  75. }
  76. console.info("KPR: Kongregate Portal Report loaded! v" + KPRVersion);
  77.  
  78. GM_registerMenuCommand("Run Portal Report", portalReport);
  79. dom.holodeck.addChatCommand("portals", portalReport);
  80. }
  81.  
  82. window.setTimeout(init_portalReport,0);
  83.  
  84. function portalReport()
  85. {
  86.  
  87. if (!dom.holodeck)
  88. {
  89. // console.log("no holodeck");
  90. window.setTimeout(portalReport, 1000);
  91. return;
  92. }
  93. if (! dom.holodeck._chat_window)
  94. {
  95. // console.log("no chat window");
  96. window.setTimeout(portalReport, 1000);
  97. return;
  98. }
  99. if (! dom.holodeck._chat_window._active_room)
  100. {
  101. // console.log("no active room");
  102. window.setTimeout(portalReport, 1000);
  103. return;
  104. }
  105. if (! dom.holodeck._chat_window._active_room._users)
  106. {
  107. // console.log("no users");
  108. window.setTimeout(portalReport, 1000);
  109. return;
  110. }
  111. var users = dom.holodeck._chat_window._active_room._users;
  112. var groupMap = {"atlas":[],"p-body":[]};
  113. for (var username in users)
  114. {
  115. var user = users[username];
  116. if (user)
  117. {
  118. var group = user.variables.dueling_group;
  119. if (group)
  120. {
  121. var need = user.variables.portals_needed;
  122. var has = user.variables.portals_to_give;
  123. groupMap[group][username] = {"name":username,"need":need, "has":has};
  124. }
  125. }
  126. }
  127.  
  128. var reportString = "";
  129. var currentUser = dom.holodeck._active_user._attributes._object;
  130. var userGroup = currentUser.dueling_group;
  131. var iNeed = currentUser.portals_needed;
  132. var iHave = currentUser.portals_to_give;
  133. if (userGroup)
  134. {
  135. reportString += "<h3>You need " + iNeed + " " + getOtherPortalColor(userGroup) + " portal" + s(iNeed) + ", and you have " +
  136. iHave + " " + getPortalColor(userGroup) + " portal" + s(iHave) + " left to give.</h3>";
  137. }
  138. else
  139. {
  140. reportString += "<h3>You are not currently playing the portal game.</h3>"
  141. }
  142. if (userGroup && iNeed > 0)
  143. {
  144. var otherGroup = (userGroup == "atlas")?"p-body":"atlas";
  145. var usersString = "";
  146. var sameTeamString = "";
  147. var foundAny = false;
  148. var foundSameTeam = false;
  149. for (var username in groupMap[otherGroup])
  150. {
  151. var user = groupMap[otherGroup][username];
  152. if (user && user.has > 0)
  153. {
  154. usersString += "\t";
  155. usersString += "<a href=\"#\" onClick=\"var texts = document.getElementsByClassName('chat_input'); for (var i in texts){var inp = texts[i]; inp.value='/w " + username + " Could I trouble you for " +
  156. ((user.has>1)?"one of your":"your last") + " " + getPortalColor(otherGroup) + " portal" + s(user.has) + "?'}; return false;\" title=\"Click to whisper\">" + username + "</a>";
  157. usersString += " has " + user.has + " " + getPortalColor(otherGroup) + " and needs " + user.need + " " + getOtherPortalColor(otherGroup) + "<br />";
  158. foundAny = true;
  159. groupMap[otherGroup][username] = null;
  160. }
  161. }
  162. for (var username in groupMap[userGroup])
  163. {
  164. var user = groupMap[userGroup][username];
  165. if (user && user.need > 0)
  166. {
  167. sameTeamString += "\t" + username + " has " + user.has + " " + getPortalColor(userGroup) + " and needs " + user.need + " " + getOtherPortalColor(userGroup) + "<br />";
  168. foundSameTeam = true;
  169. }
  170. }
  171. if (foundAny)
  172. {
  173. reportString += "<br /><br /><h3>Peaple with " + getOtherPortalColor(userGroup) + " portals:</h3>";
  174. reportString += usersString;
  175. }
  176. else
  177. {
  178. reportString += "<br /><br /><h3>Nobody in this room has the " + getOtherPortalColor(userGroup) + " portal" + s(iNeed) + " you need. :-(</h3>";
  179. }
  180. if (foundSameTeam)
  181. {
  182. reportString += "<br /><br /><h3>People competing for " + getOtherPortalColor(userGroup) + " portals:</h3>";
  183. reportString += sameTeamString;
  184. }
  185. else
  186. {
  187. reportString += "<br /><br /><h3>You have no competition for " + getOtherPortalColor(userGroup) + " portals.</h3>";
  188. }
  189. }
  190. else
  191. {
  192. var groups = ["atlas","p-body"];
  193. for (var i in groups)
  194. {
  195. var group = groups[i];
  196. var anyInGroup = false;
  197. var groupString = "";
  198. for (var username in groupMap[group])
  199. {
  200. var user = groupMap[group][username];
  201. if (user && (user.has > 0 || user.need > 0))
  202. {
  203. groupString += "\t" + username + " has " + user.has + " " + getPortalColor(group) + " and needs " + user.need + " " + getOtherPortalColor(group) + "<br />";
  204. groupMap[group][username] = null;
  205. anyInGroup = true;
  206. }
  207. }
  208. if (anyInGroup)
  209. {
  210. reportString += "<h3>People on team <span style='background-color: " + getPortalColor(group) + ";'>" + group + "</span> who still have/need portals:</h3>";
  211. reportString += groupString;
  212. }
  213. else
  214. {
  215. reportString += "<h3>No one on team <span style='background-color: " + getPortalColor(group) + ";'>" + group + "</span> still has or needs portals.</h3>";
  216. }
  217. }
  218. }
  219.  
  220. dom.holodeck.activeDialogue().displayUnsanitizedMessage(
  221. "PortalBot: ",
  222. reportString,
  223. {"class":"whisper received_whisper"},
  224. {non_user:true}
  225. );
  226. return false;
  227. }
  228.  
  229. function s(count)
  230. {
  231. if (count != 1)
  232. {
  233. return "s";
  234. }
  235. else
  236. {
  237. return "";
  238. }
  239. }
  240.  
  241. function getPortalColor(groupName)
  242. {
  243. if (groupName == "atlas")
  244. {
  245. return "blue";
  246. }
  247. else if (groupName == "p-body")
  248. {
  249. return "orange";
  250. }
  251. else
  252. {
  253. return "no portal color"
  254. }
  255. }
  256.  
  257. function getOtherPortalColor(groupName)
  258. {
  259. if (groupName == "atlas")
  260. {
  261. return "orange";
  262. }
  263. else if (groupName == "p-body")
  264. {
  265. return "blue";
  266. }
  267. else
  268. {
  269. return "no portal color"
  270. }
  271. }