Advertisement
Guest User

battleog fix

a guest
Jul 16th, 2018
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.95 KB | None | 0 0
  1. /**
  2. * True Player Counts - Shows the true player count on the server (Not the ones in queue/cheating with the bots).
  3. *
  4. * Used I-MrFixIt-I's Friends Highlighter as a base.
  5. *
  6. * @author xfileFIN
  7. * @version 2.1
  8. * @url https://getbblog.com
  9. */
  10.  
  11. /*************/
  12. /* Changelog */
  13. /*************/
  14. /*
  15. Version: 2.1
  16. - Fix: Stop excessive request flooding (hopefully :))
  17. - Fix: Fix match info and scoreboard on battlelog (Thanks DICE for breaking them). And thanks PolloLoco for pointing out that https works even though http doesn't
  18. Version: 2.0
  19. - Change: Fetch data from another place
  20. Version: 1.4
  21. - Fix: Made ajax request async so it won't hang the whole site when the request doesn't work
  22. Version: 1.3
  23. - Added: Color coding on low, mid, high difference of the player count shown/the actual ones playing.
  24. - Added: Option to remove spectators/commanders if there are none. This is to trim down the view.
  25. Version: 1.1
  26. - Fixed a bug that prevented automatic loading on page load (Worked from the Editor but not when uploaded).
  27. Version: 1.0
  28. - Initial release
  29. */
  30.  
  31.  
  32. var instanssi;
  33.  
  34. // initialize your plugin
  35. BBLog.handle("add.plugin", {
  36.  
  37. /**
  38. * The unique, lowercase id of my plugin
  39. * Allowed chars: 0-9, a-z, -
  40. */
  41. id: "xfilefin-true-playercounts",
  42.  
  43. /**
  44. * The name of my plugin, used to show config values in bblog options
  45. * Could also be translated with the translation key "plugin.name" (optional)
  46. *
  47. * @type String
  48. */
  49. name: "True Player Counts",
  50.  
  51. /**
  52. * Some translations for this plugins
  53. * For every config flag must exist a corresponding EN translation
  54. * otherwise the plugin will no be loaded
  55. *
  56. * @type Object
  57. */
  58. translations: {
  59. "en": {
  60. "use.true-playercounts": "Use True Player Counts",
  61. "use.trim-view": "Trim Spectator/Commander",
  62. "change-color-high": "Change color (High)",
  63. "choose-color-high": "Choose a color of your choice. Example: #ff0000",
  64. "change-color-mid": "Change color (Mid)",
  65. "choose-color-mid": "Choose a color of your choice. Example: #99b839",
  66. "change-color-low": "Change color (Low)",
  67. "choose-color-low": "Choose a color of your choice. Example: #39b54a"
  68. },
  69. "de": {
  70. "use.true-playercounts": "Use True Player Counts",
  71. "use.trim-view": "Trim Spectator/Commander",
  72. "change-color-high": "Farbe ändern (High)",
  73. "choose-color-high": "Wähle eine Farbe deiner Wahl. Beispiel: #ff0000",
  74. "change-color-mid": "Farbe ändern (Mid)",
  75. "choose-color-mid": "Wähle eine Farbe deiner Wahl. Beispiel: #99b839",
  76. "change-color-low": "Farbe ändern (Low)",
  77. "choose-color-low": "Wähle eine Farbe deiner Wahl. Beispiel: #39b54a"
  78. }
  79. },
  80.  
  81. stdColorHigh: "#ff0000",
  82. stdColorMid: "#99b839",
  83. stdColorLow: "#39b54a",
  84.  
  85. /**
  86. * Configuration Options that appears in the BBLog Menu
  87. * Every option must be an object with properties as shown bellow
  88. * Properties available:
  89. * key : The name for your config flag - The user can toggle this option
  90. * and you can retreive the users choice with instance instance.storage(YOUR_KEY_NAME) (0 or 1 will be returned)
  91. * init : Can be 0 or 1 - Represent the initial status of the option when the user load the plugin for the first time
  92. * If you want that this option is enabled on first load (opt-out) than set it to 1, otherwise to 0 (opt-in)
  93. * handler(optional): When set as a function this config entry turns into a button (like the plugins button you see in the bblog menu)
  94. * The function well be executed when the user clicks the button
  95. */
  96. configFlags: [
  97. { "key": "use.true-playercounts", "init": 1 },
  98. { "key": "use.trim-view", "init": 0 },
  99. {
  100. "key": "change-color-high", "init": 0, "handler": function (instance) {
  101. var color = prompt(instance.t("choose-color-high"));
  102. if (color.charAt(0) != "#") {
  103. color = + "#";
  104. }
  105.  
  106. var isHexValue = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(color);
  107. if (isHexValue) {
  108. instance.storage("colorHigh", color);
  109. }
  110. }
  111. },
  112. {
  113. "key": "change-color-mid", "init": 0, "handler": function (instance) {
  114. var color = prompt(instance.t("choose-color-mid"));
  115. if (color.charAt(0) != "#") {
  116. color = + "#";
  117. }
  118.  
  119. var isHexValue = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(color);
  120. if (isHexValue) {
  121. instance.storage("colorMid", color);
  122. }
  123. }
  124. },
  125. {
  126. "key": "change-color-low", "init": 0, "handler": function (instance) {
  127. var color = prompt(instance.t("choose-color-low"));
  128. if (color.charAt(0) != "#") {
  129. color = + "#";
  130. }
  131.  
  132. var isHexValue = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(color);
  133. if (isHexValue) {
  134. instance.storage("colorLow", color);
  135. }
  136. }
  137. }
  138. ],
  139.  
  140. /**
  141. * A handler that be fired immediately (only once) after the plugin is loaded into bblog
  142. *
  143. * @param object instance The instance of your plugin which is the whole plugin object
  144. * Always use "instance" to access any plugin related function, not use "this" because it's not working properly
  145. * For example: If you add a new function to your addon, always pass the "instance" object
  146. */
  147. init: function (instance) {
  148. // some log to the console to show you how the things work
  149. /*console.log(
  150. "plugin."+instance.id+".init"
  151. );*/
  152. instanssi = instance;
  153. },
  154.  
  155. /**
  156. * A trigger that fires everytime when the dom is changing but at max only once each 200ms (5x per second) to prevent too much calls in a short time
  157. * Example Case: If 10 DOM changes happen in a period of 100ms than this function will only been called 200ms after the last of this 10 DOM changes
  158. * This make sure that all actions in battlelog been finished before this function been called
  159. * This is how BBLog track Battlelog for any change, like url, content or anything
  160. *
  161. * @param object instance The instance of your plugin which is the whole plugin object
  162. * Always use "instance" to access any plugin related function, not use "this" because it's not working properly
  163. * For example: If you add a new function to your addon, always pass the "instance" object
  164. */
  165. domchange: function (instance) {
  166. instanssi = instance;
  167.  
  168. S.globalContext.staticContext.keeperQueryEndpoint = "https://keeper.battlelog.com"
  169. },
  170. });
  171.  
  172. $( document ).ready(function() {
  173. S.globalContext.staticContext.keeperQueryEndpoint = "https://keeper.battlelog.com"
  174. });
  175.  
  176. // Create a closure
  177. (function () {
  178. // Your base, I'm in it!
  179. var originalAddClassMethod = jQuery.fn.addClass;
  180.  
  181. jQuery.fn.addClass = function () {
  182. if(jQuery.inArray("loading-info", arguments) !== -1){
  183. if (this.hasClass("bblog-serverbrowser-filters")) {
  184. this.removeClass("bblog-serverbrowser-filters");
  185. }
  186. }
  187. if(jQuery.inArray("bblog-serverbrowser-filters", arguments) !== -1){
  188. if (!this.hasClass("bblog-serverbrowser-filters")) {
  189. doTheMagic(this);
  190. }
  191. }
  192.  
  193. // Execute the original method.
  194. var result = originalAddClassMethod.apply(this, arguments);
  195.  
  196. // trigger a custom event
  197. jQuery(this).trigger('cssClassChanged');
  198.  
  199. // return the original result
  200. return result;
  201. }
  202. })();
  203.  
  204. function doTheMagic(row){
  205. if (!instanssi.storage("use.true-playercounts")) {
  206. return;
  207. }
  208.  
  209. if (BBLog.cache("mode") != "bf4" || !serverbrowserwarsaw || !serverbrowserwarsaw.table) {
  210. return;
  211. }
  212.  
  213. var data = $(row).data("server");
  214. if (!data) return true;
  215.  
  216. // True player count
  217. var url = "http://battlelog.battlefield.com/bf4/servers/show/pc/" + data.guid + "?json=1";
  218.  
  219. var $serverRow = $(row);
  220. function showTrueCounts(response) {
  221. if (response.type == "success" && response.message.SERVER_INFO && response.message.SERVER_PLAYERS) {
  222. //console.log("Current: " + response.message.SERVER_INFO.slots[2].max + "/" + response.message.SERVER_PLAYERS.length);
  223. var slotData = response.message.SERVER_INFO.slots;
  224. var totalPlayers = response.message.SERVER_PLAYERS.length;
  225.  
  226. if (slotData[2]) {
  227. if (!$serverRow.find(".bblog-slots.trueplayercount").length) {
  228. if ($serverRow.find(".bblog-slots.commander").length) {
  229. $serverRow.find(".bblog-slots.commander").before('<div class="bblog-slots trueplayercount">' + totalPlayers + "/" + slotData[2].max + '</div>');
  230. }
  231. else if ($serverRow.find(".bblog-slots.spectator").length) {
  232. $serverRow.find(".bblog-slots.spectator").before('<div class="bblog-slots trueplayercount">' + totalPlayers + "/" + slotData[2].max + '</div>');
  233. }
  234. else {
  235. $serverRow.find("td.players").append('<div class="bblog-slots trueplayercount">' + totalPlayers + "/" + slotData[2].max + '</div>');
  236. }
  237. }
  238. else{
  239. $serverRow.find(".bblog-slots.trueplayercount").html('<div class="bblog-slots trueplayercount">' + totalPlayers + "/" + slotData[2].max + '</div>');
  240. }
  241. var serverplayers = $serverRow.find(".bblog-slots.trueplayercount");
  242.  
  243. var difference = Math.abs(slotData[2].current - totalPlayers);
  244. if (difference <= 2) {
  245. if (instanssi.storage("change-color-low")) {
  246. var color = instanssi.storage("colorLow");
  247. if (color !== null) {
  248. $(serverplayers).css("color", color);
  249. }
  250. else {
  251. $(serverplayers).css("color", instanssi.stdColorLow);
  252. }
  253. }
  254. else {
  255. $(serverplayers).css("color", instanssi.stdColorLow);
  256. }
  257. }
  258. else if (difference <= 5) {
  259. if (instanssi.storage("change-color-mid")) {
  260. var color = instanssi.storage("colorMid");
  261. if (color !== null) {
  262. $(serverplayers).css("color", color);
  263. }
  264. else {
  265. $(serverplayers).css("color", instanssi.stdColorMid);
  266. }
  267. }
  268. else {
  269. $(serverplayers).css("color", instanssi.stdColorMid);
  270. }
  271. }
  272. else {
  273. if (instanssi.storage("change-color-high")) {
  274. var color = instanssi.storage("colorHigh");
  275. if (color !== null) {
  276. $(serverplayers).css("color", color);
  277. }
  278. else {
  279. $(serverplayers).css("color", instanssi.stdColorHigh);
  280. }
  281. }
  282. else {
  283. $(serverplayers).css("color", instanssi.stdColorHigh);
  284. }
  285. }
  286. $(serverplayers).css("font-size", "12px");
  287. }
  288.  
  289. // Remove the unneeded nodes to make the view a bit nicer/cleaner
  290. if (instanssi.storage("use.trim-view")) {
  291. if (slotData[4] && $serverRow.find(".bblog-slots.commander").length && slotData[4].current <= 0) {
  292. $serverRow.find(".bblog-slots.commander").css("display", "none");
  293. }
  294. if (slotData[8] && $serverRow.find(".bblog-slots.spectator").length && slotData[8].current <= 0) {
  295. $serverRow.find(".bblog-slots.spectator").css("display", "none");
  296. }
  297. }
  298. }
  299. }
  300.  
  301. // Fetch the current data
  302. $.ajax({
  303. async: true,
  304. url: url,
  305. error: function () {
  306. //console.log("Fetching: " + url + " timed out.");
  307. },
  308. success: function (result) {
  309. //console.log(result);
  310. if (result) {
  311. showTrueCounts(result);
  312. }
  313. },
  314. timeout: 5000 // sets timeout to 5 seconds
  315. });
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement