Ansolley

Plugin - Live Scoreboard Sorter

May 10th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 8.58 KB | None | 0 0
  1. /**
  2. * BF4 Live-Scoreboard Sorter - Sort the live scoreboard by your needs.
  3. *
  4. * @author I-MrFixIt-I
  5. * @version 0.1
  6. * @url https://getbblog.com
  7. */
  8.  
  9. // initialize your plugin
  10. BBLog.handle("add.plugin", {
  11.  
  12.     // default values by battlelog
  13.     sortedRowIndex : 4,
  14.     sortedRowReverse : true,
  15.  
  16.     /**
  17.     * The unique, lowercase id of my plugin
  18.     * Allowed chars: 0-9, a-z, -
  19.     */
  20.     id : "mrfixit-scoreboard-sorter",
  21.  
  22.     /**
  23.     * The name of my plugin, used to show config values in bblog options
  24.     * Could also be translated with the translation key "plugin.name" (optional)
  25.     *
  26.     * @type String
  27.     */
  28.     name : "BF4 Live-Scoreboard Sorter",
  29.  
  30.     /**
  31.     * Some translations for this plugins
  32.     * For every config flag must exist a corresponding EN translation
  33.     *   otherwise the plugin will no be loaded
  34.     *
  35.     * @type Object
  36.     */
  37.     translations : {
  38.         "en" : {
  39.             "use.scoreboard-sorter" : "Use Live-Scoreboard Sorter",
  40.             "use.synchronize-tables" : "Synchronize both tables",
  41.             "use.include-tag" : "Consider clan-tag"
  42.         },
  43.         "de" : {
  44.             "use.scoreboard-sorter" : "Live-Scoreboard Sorter verwenden",
  45.             "use.synchronize-tables" : "Beide Tabellen synchronisieren",
  46.             "use.include-tag" : "Clan-Tag berücksichtigen"
  47.         }
  48.     },
  49.  
  50.     /**
  51.     * Configuration Options that appears in the BBLog Menu
  52.     * Every option must be an object with properties as shown bellow
  53.     * Properties available:
  54.     *   key : The name for your config flag - The user can toggle this option
  55.     *         and you can retreive the users choice with instance instance.storage(YOUR_KEY_NAME) (0 or 1 will be returned)
  56.     *   init : Can be 0 or 1 - Represent the initial status of the option when the user load the plugin for the first time
  57.     *          If you want that this option is enabled on first load (opt-out) than set it to 1, otherwise to 0 (opt-in)
  58.     *   handler(optional): When set as a function this config entry turns into a button (like the plugins button you see in the bblog menu)
  59.     *                       The function well be executed when the user clicks the button
  60.     */
  61.     configFlags : [
  62.         {"key" : "use.scoreboard-sorter", "init" : 1},
  63.         {"key" : "use.synchronize-tables", "init" : 1},
  64.         {"key" : "use.include-tag", "init" : 0}
  65.     ],
  66.  
  67.     compare : function (a, b, reverse){
  68.         var retValue = 0; // values have the same size
  69.        
  70.         if (a > b) retValue = 1;
  71.         else if (a < b) retValue = -1;
  72.        
  73.         if (reverse) retValue *= -1; // reverse the order
  74.        
  75.         return retValue;
  76.     },
  77.    
  78.     sortScoreboard : function (instance, table, rowIndex, reverse){
  79.         rows = $(table).find("tbody tr");
  80.  
  81.         $(table).find("thead tr th").removeAttr("data-sort"); // remove all data-sort attributes, so there would be only one
  82.        
  83.         // add sort direction to clicked row header (used for detections sort direction)
  84.         th = $(table).find("thead tr th").eq(rowIndex - 1);
  85.         if (reverse)
  86.         {
  87.             $(th).attr("data-sort", "desc");
  88.         }
  89.         else
  90.         {
  91.             $(th).attr("data-sort", "asc");
  92.         }
  93.  
  94.         // the "real" sorting part (using JS function sort)
  95.         rows.sort(function(a, b) {
  96.             cmp = instance.compare(a, b, false);
  97.  
  98.             // get the necessary compare values (like name and numbers)
  99.             switch (rowIndex)
  100.             {
  101.                 // Soldiername
  102.                 case 1:
  103.                     if (!instance.storage("use.include-tag"))
  104.                     {
  105.                         // get clan tag from player
  106.                         tagA = $(a.cells[rowIndex]).find(".user-info div .bblog-tag").text();
  107.                         tagB = $(b.cells[rowIndex]).find(".user-info div .bblog-tag").text();
  108.                        
  109.                         // get playername and cut the clantag from it
  110.                         valA = $(a.cells[rowIndex]).find(".user-info div > span").text().substr(tagA.length).toLowerCase();
  111.                         valB = $(b.cells[rowIndex]).find(".user-info div > span").text().substr(tagB.length).toLowerCase();
  112.                     }
  113.                     else
  114.                     {
  115.                         // get playername
  116.                         valA = $(a.cells[rowIndex]).find(".user-info div > span").text().toLowerCase();
  117.                         valB = $(b.cells[rowIndex]).find(".user-info div > span").text().toLowerCase();
  118.                     }
  119.                    
  120.                     cmp = instance.compare(valA, valB, reverse);
  121.                     break;
  122.                 // Kills and Deaths
  123.                 case 2:
  124.                 case 3:
  125.                     valA = parseInt(a.cells[rowIndex].innerHTML, 10);
  126.                     valB = parseInt(b.cells[rowIndex].innerHTML, 10);
  127.                    
  128.                     cmp = instance.compare(valA, valB, reverse);
  129.                     break;
  130.                 // Score
  131.                 case 4:
  132.                     // replace "," and "." from number for the real value and sorting
  133.                     valA = parseInt(a.cells[rowIndex].innerHTML.replace(',','').replace('.',''), 10);
  134.                     valB = parseInt(b.cells[rowIndex].innerHTML.replace(',','').replace('.',''), 10);
  135.                    
  136.                     cmp = instance.compare(valA, valB, reverse);
  137.                     break;
  138.             }
  139.            
  140.             return cmp;
  141.    
  142.         });
  143.  
  144.         // rebuild the numbering in the first column
  145.         $.each(rows, function(index) {
  146.             $(this).find("td:first span").text(index + 1);
  147.         });
  148.        
  149.         table.find("tbody").append(rows);
  150.     },
  151.  
  152.     /**
  153.     * 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
  154.     * 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
  155.     * This make sure that all actions in battlelog been finished before this function been called
  156.     * This is how BBLog track Battlelog for any change, like url, content or anything
  157.     *
  158.     * @param object instance The instance of your plugin which is the whole plugin object
  159.     *    Always use "instance" to access any plugin related function, not use "this" because it's not working properly
  160.     *    For example: If you add a new function to your addon, always pass the "instance" object
  161.     */
  162.     domchange : function(instance){
  163.         if (instance.storage("use.scoreboard-sorter"))
  164.         {
  165.             if(window.location.href.match('battlelog.battlefield.com/bf4/'))
  166.             { // this plugin is only supporting bf4
  167.                 if ($("#server-players-list").length) // the server-players-list is available
  168.                 {
  169.                     $("#server-players-list table thead th").off("click.scoreboardclick").on("click.scoreboardclick", function() { // bind and unbind the click handler to every header on the scoreboard table
  170.                         var rowIndex = $(this).index() + 1; // get clicked row Index
  171.                        
  172.                         // get sort direction => reversed = true means desc
  173.                         reversed = false;
  174.                         if ($(this).attr("data-sort") == "asc") // if the row is already sorted (asc) then do it now desc, otherwise asc
  175.                         {
  176.                             reversed = true;
  177.                         }
  178.                        
  179.                         // save the new state (needed later for the live-scoreboard / live data)
  180.                         instance.sortedRowIndex = rowIndex;
  181.                         instance.sortedRowReverse = reversed;
  182.                        
  183.                         if (instance.storage("use.synchronize-tables"))
  184.                         {
  185.                             $.each($("#server-players-list table"), function() {
  186.                                 instance.sortScoreboard(instance, $(this), rowIndex, reversed);
  187.                             });
  188.                         }
  189.                         else
  190.                         {
  191.                             instance.sortScoreboard(instance, $(this).closest("table"), rowIndex, reversed);
  192.                         }
  193.                        
  194.                         /*$.each($("#server-players-list table"), function() {
  195.                             instance.sortScoreboard(instance, $(this), rowIndex, reversed);
  196.                         });*/
  197.                     });
  198.                    
  199.                     if ($("#serverbrowser-edit-livescoreboard").prop('checked')) // live-scoreboard is enabled
  200.                     {
  201.                         if ($("#server-players-list table thead tr th[data-sort]").length === 0) // no element is sorted (happens on page refresh or live data)
  202.                         {
  203.                             $.each($("#server-players-list table"), function() {
  204.                                 instance.sortScoreboard(instance, $(this), instance.sortedRowIndex, instance.sortedRowReverse);
  205.                             });
  206.                         }
  207.                     }
  208.                 }
  209.             }
  210.         }
  211.     }
  212. });
Add Comment
Please, Sign In to add comment