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