apajohnson

Untitled

Aug 7th, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.86 KB | None | 0 0
  1. // ==UserScript==
  2. // @name TagPro Rank Userscript
  3. // @namespace http://www.reddit.com/user/-omicron-/
  4. // @description Displays ranks on users ingame based on tagpro-stats.com data
  5. // @include http://tagpro-*.koalabeast.com:*
  6. // @include http://tangent.jukejuice.com:*
  7. // @include http://maptest.newcompte.fr:*
  8. // @include http://justletme.be:*
  9. // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  10. // @author OmicroN
  11. // @version 0.5
  12. // ==/UserScript==
  13. (function() {
  14.  
  15. function contentEval(source)
  16. {
  17. // Check for function input.
  18. if ('function' == typeof source) {
  19. // Execute this function with no arguments, by adding parentheses.
  20. // One set around the function, required for valid syntax, and a
  21. // second empty set calls the surrounded function.
  22. source = '(' + source + ')();'
  23. }
  24.  
  25. // Create a script node holding this source code.
  26. var script = document.createElement('script');
  27. script.setAttribute("type", "application/javascript");
  28. script.textContent = source;
  29.  
  30. // Insert the script node into the page, so it will run, and immediately
  31. // remove it to clean up.
  32. document.body.appendChild(script);
  33. document.body.removeChild(script);
  34. }
  35.  
  36. // The actual script functionality
  37. function rankScript()
  38. {
  39. // Stores the playerid/names of players who have already been parsed/ranked shown
  40. ranked = [];
  41.  
  42. // Simple function to get the length of an object
  43. function objectLength(obj)
  44. {
  45. var result = 0;
  46.  
  47. for(var prop in obj) {
  48. if (obj.hasOwnProperty(prop)) {
  49. result++;
  50. }
  51. }
  52.  
  53. return result;
  54. }
  55.  
  56. // Stores the options for user to customize the script
  57. rankoptions = []
  58.  
  59. /*
  60. The identifier to use to pull either the actual number
  61. or rank for the various different stats available
  62.  
  63. See http://www.tagpro-stats.com/profile.php?userid=20338
  64.  
  65. Number Rank
  66. Win Percent | win-percent | win-percent-rank
  67. Wins | wins | wins-rank
  68. Losses | losses | losses-rank
  69. Games | games | games-rank
  70. Hours |
  71. Minutes/Game | minute-game | minute-game-rank
  72. Disconnects | dcs | dcs-rank
  73.  
  74. Grabs | grabs | grabs-rank
  75. Grabs/Game | grab_game | grab-game-rank
  76. Grabs/Hour | grab-hour | grab-hour-rank
  77. Drops | drops | drops-rank
  78. Drops/Game | drop-game | drop-game-rank
  79. Drops/Hour | drop-hour | drop-hour-rank
  80. Popped | popped | popped-rank
  81. Pops/Game | pop-game | pop-game-rank
  82. Pops/Hour | pop-hour | pop-hour-rank
  83.  
  84. Tags | tags | tags-rank
  85. Tags/Game | tag-game | tag-game-rank
  86. Tags/Hour | tag-hour | tag-hour-rank
  87. Returns | returns | returns-rank
  88. Returns/Game | return-game | return-game-rank
  89. Returns/Hours | return-hour | return-hour-rank
  90. Captures | captures | captures-rank
  91. Captures/Game | cap-game | cap-game-rank
  92. Captures/Hour | cap-hour | cap-hour-rank
  93.  
  94. Captures/Grab | caps-grab | caps-grab-rank
  95. Tags/Pop | tag-pop | tag-pop-rank
  96. Non-Return Tags | nrtags | nrtags-rank
  97.  
  98. Support | support | support-rank
  99. Hold | hold | hold-rank
  100. Prevent | prevent | prevent-rank
  101. */
  102. rankoptions['query'] = 'win-percent';
  103.  
  104. // Timespan to pull data from 'all' for all time or 'month' for monthly stats.
  105. rankoptions['timespan'] = 'month';
  106.  
  107. // HTML HEX color of the injected stats text for authorized/non authorized users
  108. rankoptions['color-auth'] = '#FDD017'; // Default #FDD017 (gold)
  109. rankoptions['color-nonauth'] = '#CCCCCC'; // Default #FFFFFF (white)
  110.  
  111. // Position to place stat text relative to top left corner of player name
  112. // limited to a x/width of 200 and y/height of 45
  113. rankoptions['position'] = {x: 50, y: 27};
  114.  
  115. // Function to wait until game is loaded before pulling stats for players
  116. function waitUntilGameLoaded()
  117. {
  118. // Check if game is loaded and players are inside
  119. if(typeof tagpro.players !== "undefined" && objectLength(tagpro.players) > 0)
  120. {
  121. // Variable to hold the name/value pair playerids and names
  122. var querystring = "";
  123.  
  124. // Loop through all current players in the game and build querystring variable
  125. for (var pID in tagpro.players)
  126. {
  127. // Add player to ranked array so we don't pull stats for them again this game
  128. ranked[pID] = tagpro.players[pID].name;
  129.  
  130. querystring += "player[" + pID + "]=" + encodeURIComponent(tagpro.players[pID].name) + "&";
  131. }
  132.  
  133. // Use the options and the list of players in the game and make an AJAX get request to stats query page/site
  134. $.get('http://reanimated.net/stats2.php?query=' + rankoptions['query'] + '&timespan=' + rankoptions['timespan'] + '&' + querystring, function(data) {
  135. // Parse returned results as JSON, results are returned key/value of playerid/requested stat
  136. var results = $.parseJSON(data);
  137.  
  138. // Loop through the results and draw the requested stat next to each players ball using the options set above in the script
  139. for (pID in results)
  140. {
  141. tagpro.prettyText(results[pID].replace('N/A', ''), rankoptions['position'].x, rankoptions['position'].y, tagpro.players[pID].auth ? rankoptions['color-auth'] : rankoptions['color-nonauth'], !1, !1, tagpro.players[pID].cache.context);
  142. }
  143. });
  144.  
  145. // Create a listener on the tagpro socket object for the 'p' event which is used for
  146. // various aspects of the game but also used when a new player joins the game
  147. tagpro.socket.on('p', function(data) {
  148. // Check if the u object exist in the data sent over the p event
  149. if (typeof data.u !== "undefined")
  150. {
  151. // Another check to see if the u object contains a name property which should be enough
  152. // to indicate we have the object we want for when a new person joins the match
  153. if (typeof data.u[0].name !== "undefined")
  154. {
  155. // For some reason when a user joins the users info is passed 3 times through the p event so we don't want waste resources and
  156. // time pulling stats for a user that has already had stats pulled from the first time the object passed through the pipe, so
  157. // we check if our ranked object which we use to store players that have been parsed doesn't contain this player that joined
  158. if (typeof ranked[data.u[0].id] === "undefined")
  159. {
  160. // Player just joine game and hasn't had stats pulled so add them to the list of users already parsed
  161. ranked[data.u[0].id] = data.u[0].name;
  162.  
  163. // Use the options and new players id/name and make an AJAX get request to stats query page/site
  164. $.get('http://reanimated.net/stats2.php?query=' + rankoptions['query'] + '&timespan=' + rankoptions['timespan'] + '&player[' + data.u[0].id + ']=' + encodeURIComponent(data.u[0].name), function(data2) {
  165. // Parse returned results as JSON, results are returned key/value of playerid/requested stat
  166. var results = $.parseJSON(data2);
  167.  
  168. // Loop through the results and draw the requested stat next to each players ball using the options set above in the script
  169. for (pID in results)
  170. {
  171. tagpro.prettyText(results[pID].replace('N/A', ''), rankoptions['position'].x, rankoptions['position'].y, tagpro.players[pID].auth ? rankoptions['color-auth'] : rankoptions['color-nonauth'], !1, !1, tagpro.players[pID].cache.context);
  172. }
  173. });
  174. }
  175. }
  176. }
  177.  
  178. // This whole code block is similar to the one above except when your spectating a game a new player joins for some reason there
  179. // is no u object assocated with the passed data and instead is passed as an array in the first key so we check that first key to
  180. if (typeof data[0] !== "undefined")
  181. {
  182. // See if a player name property exist inside of it to identify if we have the right object we're looking for
  183. if (typeof data[0].name !== "undefined")
  184. {
  185. // For some reason when a user joins the users info is passed 3 times through the p event so we don't want waste resources and
  186. // time pulling stats for a user that has already had stats pulled from the first time the object passed through the pipe, so
  187. // we check if our ranked object which we use to store players that have been parsed doesn't contain this player that joined
  188. if (typeof ranked[data[0].id] === "undefined")
  189. {
  190. // Player just joine game and hasn't had stats pulled so add them to the list of users already parsed
  191. ranked[data[0].id] = data[0].name;
  192.  
  193. $.get('http://reanimated.net/stats2.php?query=' + rankoptions['query'] + '&timespan=' + rankoptions['timespan'] + '&player[' + data[0].id + ']=' + encodeURIComponent(data[0].name), function(data2) {
  194. // Parse returned results as JSON, results are returned key/value of playerid/requested stat
  195. var results = $.parseJSON(data2);
  196.  
  197. for (pID in results)
  198. {
  199. // Loop through the results and draw the requested stat next to each players ball using the options set above in the script
  200. tagpro.prettyText(results[pID].replace('N/A', ''), rankoptions['position'].x, rankoptions['position'].y, tagpro.players[pID].auth ? rankoptions['color-auth'] : rankoptions['color-nonauth'], !1, !1, tagpro.players[pID].cache.context);
  201. }
  202. });
  203. }
  204. }
  205. }
  206. });
  207. }
  208. else
  209. {
  210. // Loop the function to check for game to be loaded every second
  211. setTimeout(function(){
  212. waitUntilGameLoaded();
  213. }, 1000);
  214. }
  215. }
  216.  
  217. // Call the function to check if game is loaded, players are inside, and execute our custom code
  218. waitUntilGameLoaded();
  219. }
  220.  
  221. // Inject script into page
  222. contentEval(rankScript);
  223. })();
Add Comment
Please, Sign In to add comment