Advertisement
Guest User

FPL get team info 24022018

a guest
Feb 24th, 2018
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        FPL get team info
  3. // @namespace   nickchild
  4. // @include     https://fantasy.premierleague.com/a/leagues/standings/*
  5. // @version     2.1.1
  6. // @grant    GM_getValue
  7. // @grant    GM_setValue
  8. // ==/UserScript==
  9.  
  10.  
  11. var stIsIE = /*@cc_on!@*/false;
  12. var mytimeout;
  13.  
  14. sorttable = {
  15.   init: function() {
  16.     // quit if this function has already been called
  17.     if (arguments.callee.done) return;
  18.     // flag this function so we don't do the same thing twice
  19.     arguments.callee.done = true;
  20.     // kill the timer
  21.     if (_timer) clearInterval(_timer);
  22.  
  23.     if (!document.createElement || !document.getElementsByTagName) return;
  24.  
  25.     sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;
  26.  
  27.     forEach(document.getElementsByTagName('table'), function(table) {
  28.       if (table.className.search(/\bsortable\b/) != -1) {
  29.         sorttable.makeSortable(table);
  30.       }
  31.     });
  32.  
  33.   },
  34.  
  35.   makeSortable: function(table) {
  36.     if (table.getElementsByTagName('thead').length == 0) {
  37.       // table doesn't have a tHead. Since it should have, create one and
  38.       // put the first table row in it.
  39.       the = document.createElement('thead');
  40.       the.appendChild(table.rows[0]);
  41.       table.insertBefore(the,table.firstChild);
  42.     }
  43.     // Safari doesn't support table.tHead, sigh
  44.     if (table.tHead == null) table.tHead = table.getElementsByTagName('thead')[0];
  45.  
  46.     if (table.tHead.rows.length != 1) return; // can't cope with two header rows
  47.  
  48.     // Sorttable v1 put rows with a class of "sortbottom" at the bottom (as
  49.     // "total" rows, for example). This is B&R, since what you're supposed
  50.     // to do is put them in a tfoot. So, if there are sortbottom rows,
  51.     // for backwards compatibility, move them to tfoot (creating it if needed).
  52.     sortbottomrows = [];
  53.     for (var i=0; i<table.rows.length; i++) {
  54.       if (table.rows[i].className.search(/\bsortbottom\b/) != -1) {
  55.         sortbottomrows[sortbottomrows.length] = table.rows[i];
  56.       }
  57.     }
  58.     if (sortbottomrows) {
  59.       if (table.tFoot == null) {
  60.         // table doesn't have a tfoot. Create one.
  61.         tfo = document.createElement('tfoot');
  62.         table.appendChild(tfo);
  63.       }
  64.       for (var i=0; i<sortbottomrows.length; i++) {
  65.         tfo.appendChild(sortbottomrows[i]);
  66.       }
  67.       delete sortbottomrows;
  68.     }
  69.  
  70.     // work through each column and calculate its type
  71.     headrow = table.tHead.rows[0].cells;
  72.     for (var i=0; i<headrow.length; i++) {
  73.       // manually override the type with a sorttable_type attribute
  74.       if (!headrow[i].className.match(/\bsorttable_nosort\b/)) { // skip this col
  75.         mtch = headrow[i].className.match(/\bsorttable_([a-z0-9]+)\b/);
  76.         if (mtch) { override = mtch[1]; }
  77.           if (mtch && typeof sorttable["sort_"+override] == 'function') {
  78.             headrow[i].sorttable_sortfunction = sorttable["sort_"+override];
  79.           } else {
  80.             headrow[i].sorttable_sortfunction = sorttable.guessType(table,i);
  81.           }
  82.           // make it clickable to sort
  83.           headrow[i].sorttable_columnindex = i;
  84.           headrow[i].sorttable_tbody = table.tBodies[0];
  85.           dean_addEvent(headrow[i],"click", sorttable.innerSortFunction = function(e) {
  86.  
  87.           if (this.className.search(/\bsorttable_sorted\b/) != -1) {
  88.             // if we're already sorted by this column, just
  89.             // reverse the table, which is quicker
  90.             sorttable.reverse(this.sorttable_tbody);
  91.             this.className = this.className.replace('sorttable_sorted',
  92.                                                     'sorttable_sorted_reverse');
  93.             this.removeChild(document.getElementById('sorttable_sortfwdind'));
  94.             sortrevind = document.createElement('span');
  95.             sortrevind.id = "sorttable_sortrevind";
  96.             sortrevind.innerHTML = stIsIE ? '&nbsp<font face="webdings">5</font>' : '&nbsp;&#x25B4;';
  97.             this.appendChild(sortrevind);
  98.             return;
  99.           }
  100.           if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) {
  101.             // if we're already sorted by this column in reverse, just
  102.             // re-reverse the table, which is quicker
  103.             sorttable.reverse(this.sorttable_tbody);
  104.             this.className = this.className.replace('sorttable_sorted_reverse',
  105.                                                     'sorttable_sorted');
  106.             this.removeChild(document.getElementById('sorttable_sortrevind'));
  107.             sortfwdind = document.createElement('span');
  108.             sortfwdind.id = "sorttable_sortfwdind";
  109.             sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
  110.             this.appendChild(sortfwdind);
  111.             return;
  112.           }
  113.  
  114.           // remove sorttable_sorted classes
  115.           theadrow = this.parentNode;
  116.           forEach(theadrow.childNodes, function(cell) {
  117.             if (cell.nodeType == 1) { // an element
  118.               cell.className = cell.className.replace('sorttable_sorted_reverse','');
  119.               cell.className = cell.className.replace('sorttable_sorted','');
  120.             }
  121.           });
  122.           sortfwdind = document.getElementById('sorttable_sortfwdind');
  123.           if (sortfwdind) { sortfwdind.parentNode.removeChild(sortfwdind); }
  124.           sortrevind = document.getElementById('sorttable_sortrevind');
  125.           if (sortrevind) { sortrevind.parentNode.removeChild(sortrevind); }
  126.  
  127.           this.className += ' sorttable_sorted';
  128.           sortfwdind = document.createElement('span');
  129.           sortfwdind.id = "sorttable_sortfwdind";
  130.           sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
  131.           this.appendChild(sortfwdind);
  132.  
  133.             // build an array to sort. This is a Schwartzian transform thing,
  134.             // i.e., we "decorate" each row with the actual sort key,
  135.             // sort based on the sort keys, and then put the rows back in order
  136.             // which is a lot faster because you only do getInnerText once per row
  137.             row_array = [];
  138.             col = this.sorttable_columnindex;
  139.             rows = this.sorttable_tbody.rows;
  140.             for (var j=0; j<rows.length; j++) {
  141.               row_array[row_array.length] = [sorttable.getInnerText(rows[j].cells[col]), rows[j]];
  142.             }
  143.             /* If you want a stable sort, uncomment the following line */
  144.             //sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
  145.             /* and comment out this one */
  146.             row_array.sort(this.sorttable_sortfunction);
  147.  
  148.             tb = this.sorttable_tbody;
  149.             for (var j=0; j<row_array.length; j++) {
  150.               tb.appendChild(row_array[j][1]);
  151.             }
  152.  
  153.             delete row_array;
  154.           });
  155.         }
  156.     }
  157.   },
  158.  
  159.   guessType: function(table, column) {
  160.     // guess the type of a column based on its first non-blank row
  161.     sortfn = sorttable.sort_alpha;
  162.     for (var i=0; i<table.tBodies[0].rows.length; i++) {
  163.       text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]);
  164.       if (text != '') {
  165.         if (text.match(/^-?[£$¤]?[\d,.]+%?$/)) {
  166.           return sorttable.sort_numeric;
  167.         }
  168.         // check for a date: dd/mm/yyyy or dd/mm/yy
  169.         // can have / or . or - as separator
  170.         // can be mm/dd as well
  171.         possdate = text.match(sorttable.DATE_RE);
  172.         if (possdate) {
  173.           // looks like a date
  174.           first = parseInt(possdate[1]);
  175.           second = parseInt(possdate[2]);
  176.           if (first > 12) {
  177.             // definitely dd/mm
  178.             return sorttable.sort_ddmm;
  179.           } else if (second > 12) {
  180.             return sorttable.sort_mmdd;
  181.           } else {
  182.             // looks like a date, but we can't tell which, so assume
  183.             // that it's dd/mm (English imperialism!) and keep looking
  184.             sortfn = sorttable.sort_ddmm;
  185.           }
  186.         }
  187.       }
  188.     }
  189.     return sortfn;
  190.   },
  191.  
  192.   getInnerText: function(node) {
  193.     // gets the text we want to use for sorting for a cell.
  194.     // strips leading and trailing whitespace.
  195.     // this is *not* a generic getInnerText function; it's special to sorttable.
  196.     // for example, you can override the cell text with a customkey attribute.
  197.     // it also gets .value for <input> fields.
  198.  
  199.     if (!node) return "";
  200.  
  201.     hasInputs = (typeof node.getElementsByTagName == 'function') &&
  202.                  node.getElementsByTagName('input').length;
  203.  
  204.     if (node.getAttribute("sorttable_customkey") != null) {
  205.       return node.getAttribute("sorttable_customkey");
  206.     }
  207.     else if (typeof node.textContent != 'undefined' && !hasInputs) {
  208.       return node.textContent.replace(/^\s+|\s+$/g, '');
  209.     }
  210.     else if (typeof node.innerText != 'undefined' && !hasInputs) {
  211.       return node.innerText.replace(/^\s+|\s+$/g, '');
  212.     }
  213.     else if (typeof node.text != 'undefined' && !hasInputs) {
  214.       return node.text.replace(/^\s+|\s+$/g, '');
  215.     }
  216.     else {
  217.       switch (node.nodeType) {
  218.         case 3:
  219.           if (node.nodeName.toLowerCase() == 'input') {
  220.             return node.value.replace(/^\s+|\s+$/g, '');
  221.           }
  222.         case 4:
  223.           return node.nodeValue.replace(/^\s+|\s+$/g, '');
  224.           break;
  225.         case 1:
  226.         case 11:
  227.           var innerText = '';
  228.           for (var i = 0; i < node.childNodes.length; i++) {
  229.             innerText += sorttable.getInnerText(node.childNodes[i]);
  230.           }
  231.           return innerText.replace(/^\s+|\s+$/g, '');
  232.           break;
  233.         default:
  234.           return '';
  235.       }
  236.     }
  237.   },
  238.  
  239.   reverse: function(tbody) {
  240.     // reverse the rows in a tbody
  241.     newrows = [];
  242.     for (var i=0; i<tbody.rows.length; i++) {
  243.       newrows[newrows.length] = tbody.rows[i];
  244.     }
  245.     for (var i=newrows.length-1; i>=0; i--) {
  246.        tbody.appendChild(newrows[i]);
  247.     }
  248.     delete newrows;
  249.   },
  250.  
  251.   /* sort functions
  252.      each sort function takes two parameters, a and b
  253.      you are comparing a[0] and b[0] */
  254.   sort_numeric: function(a,b) {
  255.     aa = parseFloat(a[0].replace(/[^0-9.-]/g,''));
  256.     if (isNaN(aa)) aa = 0;
  257.     bb = parseFloat(b[0].replace(/[^0-9.-]/g,''));
  258.     if (isNaN(bb)) bb = 0;
  259.     return bb-aa;
  260.   },
  261.   sort_alpha: function(a,b) {
  262.     if (a[0]==b[0]) return 0;
  263.     if (a[0]<b[0]) return -1;
  264.     return 1;
  265.   },
  266.   sort_ddmm: function(a,b) {
  267.     mtch = a[0].match(sorttable.DATE_RE);
  268.     y = mtch[3]; m = mtch[2]; d = mtch[1];
  269.     if (m.length == 1) m = '0'+m;
  270.     if (d.length == 1) d = '0'+d;
  271.     dt1 = y+m+d;
  272.     mtch = b[0].match(sorttable.DATE_RE);
  273.     y = mtch[3]; m = mtch[2]; d = mtch[1];
  274.     if (m.length == 1) m = '0'+m;
  275.     if (d.length == 1) d = '0'+d;
  276.     dt2 = y+m+d;
  277.     if (dt1==dt2) return 0;
  278.     if (dt1<dt2) return -1;
  279.     return 1;
  280.   },
  281.   sort_mmdd: function(a,b) {
  282.     mtch = a[0].match(sorttable.DATE_RE);
  283.     y = mtch[3]; d = mtch[2]; m = mtch[1];
  284.     if (m.length == 1) m = '0'+m;
  285.     if (d.length == 1) d = '0'+d;
  286.     dt1 = y+m+d;
  287.     mtch = b[0].match(sorttable.DATE_RE);
  288.     y = mtch[3]; d = mtch[2]; m = mtch[1];
  289.     if (m.length == 1) m = '0'+m;
  290.     if (d.length == 1) d = '0'+d;
  291.     dt2 = y+m+d;
  292.     if (dt1==dt2) return 0;
  293.     if (dt1<dt2) return -1;
  294.     return 1;
  295.   },
  296.  
  297.   shaker_sort: function(list, comp_func) {
  298.     // A stable sort function to allow multi-level sorting of data
  299.     // see: http://en.wikipedia.org/wiki/Cocktail_sort
  300.     // thanks to Joseph Nahmias
  301.     var b = 0;
  302.     var t = list.length - 1;
  303.     var swap = true;
  304.  
  305.     while(swap) {
  306.         swap = false;
  307.         for(var i = b; i < t; ++i) {
  308.             if ( comp_func(list[i], list[i+1]) > 0 ) {
  309.                 var q = list[i]; list[i] = list[i+1]; list[i+1] = q;
  310.                 swap = true;
  311.             }
  312.         } // for
  313.         t--;
  314.  
  315.         if (!swap) break;
  316.  
  317.         for(var i = t; i > b; --i) {
  318.             if ( comp_func(list[i], list[i-1]) < 0 ) {
  319.                 var q = list[i]; list[i] = list[i-1]; list[i-1] = q;
  320.                 swap = true;
  321.             }
  322.         } // for
  323.         b++;
  324.  
  325.     } // while(swap)
  326.   }
  327. };
  328.  
  329. /*
  330.   SortTable
  331.   version 2
  332.   7th April 2007
  333.   Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/
  334.   Instructions:
  335.   Download this file
  336.   Add <script src="sorttable.js"></script> to your HTML
  337.   Add class="sortable" to any table you'd like to make sortable
  338.   Click on the headers to sort
  339.   Thanks to many, many people for contributions and suggestions.
  340.   Licenced as X11: http://www.kryogenix.org/code/browser/licence.html
  341.   This basically means: do what you want with it.
  342. */
  343.  
  344. /* ******************************************************************
  345.    Supporting functions: bundled here to avoid depending on a library
  346.    ****************************************************************** */
  347.  
  348. // Dean Edwards/Matthias Miller/John Resig
  349.  
  350. /* for Mozilla/Opera9 */
  351. if (document.addEventListener) {
  352.     document.addEventListener("DOMContentLoaded", sorttable.init, false);
  353. }
  354.  
  355. /* for Internet Explorer */
  356. /*@cc_on @*/
  357. /*@if (@_win32)
  358.     document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
  359.     var script = document.getElementById("__ie_onload");
  360.     script.onreadystatechange = function() {
  361.         if (this.readyState == "complete") {
  362.             sorttable.init(); // call the onload handler
  363.         }
  364.     };
  365. /*@end @*/
  366.  
  367. /* for Safari */
  368. if (/WebKit/i.test(navigator.userAgent)) { // sniff
  369.     var _timer = setInterval(function() {
  370.         if (/loaded|complete/.test(document.readyState)) {
  371.             sorttable.init(); // call the onload handler
  372.         }
  373.     }, 10);
  374. }
  375.  
  376. /* for other browsers */
  377. window.onload = sorttable.init;
  378.  
  379. // written by Dean Edwards, 2005
  380. // with input from Tino Zijdel, Matthias Miller, Diego Perini
  381.  
  382. // http://dean.edwards.name/weblog/2005/10/add-event/
  383.  
  384. function dean_addEvent(element, type, handler) {
  385.     if (element.addEventListener) {
  386.         element.addEventListener(type, handler, false);
  387.     } else {
  388.         // assign each event handler a unique ID
  389.         if (!handler.$$guid) handler.$$guid = dean_addEvent.guid++;
  390.         // create a hash table of event types for the element
  391.         if (!element.events) element.events = {};
  392.         // create a hash table of event handlers for each element/event pair
  393.         var handlers = element.events[type];
  394.         if (!handlers) {
  395.             handlers = element.events[type] = {};
  396.             // store the existing event handler (if there is one)
  397.             if (element["on" + type]) {
  398.                 handlers[0] = element["on" + type];
  399.             }
  400.         }
  401.         // store the event handler in the hash table
  402.         handlers[handler.$$guid] = handler;
  403.         // assign a global event handler to do all the work
  404.         element["on" + type] = handleEvent;
  405.     }
  406. };
  407. // a counter used to create unique IDs
  408. dean_addEvent.guid = 1;
  409.  
  410. function removeEvent(element, type, handler) {
  411.     if (element.removeEventListener) {
  412.         element.removeEventListener(type, handler, false);
  413.     } else {
  414.         // delete the event handler from the hash table
  415.         if (element.events && element.events[type]) {
  416.             delete element.events[type][handler.$$guid];
  417.         }
  418.     }
  419. };
  420.  
  421. function handleEvent(event) {
  422.     var returnValue = true;
  423.     // grab the event object (IE uses a global event object)
  424.     event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
  425.     // get a reference to the hash table of event handlers
  426.     var handlers = this.events[event.type];
  427.     // execute each event handler
  428.     for (var i in handlers) {
  429.         this.$$handleEvent = handlers[i];
  430.         if (this.$$handleEvent(event) === false) {
  431.             returnValue = false;
  432.         }
  433.     }
  434.     return returnValue;
  435. };
  436.  
  437. function fixEvent(event) {
  438.     // add W3C standard event methods
  439.     event.preventDefault = fixEvent.preventDefault;
  440.     event.stopPropagation = fixEvent.stopPropagation;
  441.     return event;
  442. };
  443. fixEvent.preventDefault = function() {
  444.     this.returnValue = false;
  445. };
  446. fixEvent.stopPropagation = function() {
  447.   this.cancelBubble = true;
  448. };
  449.  
  450. // Dean's forEach: http://dean.edwards.name/base/forEach.js
  451. /*
  452.     forEach, version 1.0
  453.     Copyright 2006, Dean Edwards
  454.     License: http://www.opensource.org/licenses/mit-license.php
  455. */
  456.  
  457. // array-like enumeration
  458. if (!Array.forEach) { // mozilla already supports this
  459.     Array.forEach = function(array, block, context) {
  460.         for (var i = 0; i < array.length; i++) {
  461.             block.call(context, array[i], i, array);
  462.         }
  463.     };
  464. }
  465.  
  466. // generic enumeration
  467. Function.prototype.forEach = function(object, block, context) {
  468.     for (var key in object) {
  469.         if (typeof this.prototype[key] == "undefined") {
  470.             block.call(context, object[key], key, object);
  471.         }
  472.     }
  473. };
  474.  
  475. // character enumeration
  476. String.forEach = function(string, block, context) {
  477.     Array.forEach(string.split(""), function(chr, index) {
  478.         block.call(context, chr, index, string);
  479.     });
  480. };
  481.  
  482. // globally resolve forEach enumeration
  483. var forEach = function(object, block, context) {
  484.     if (object) {
  485.         var resolve = Object; // default
  486.         if (object instanceof Function) {
  487.             // functions have a "length" property
  488.             resolve = Function;
  489.         } else if (object.forEach instanceof Function) {
  490.             // the object implements a custom forEach method so use that
  491.             object.forEach(block, context);
  492.             return;
  493.         } else if (typeof object == "string") {
  494.             // the object is a string
  495.             resolve = String;
  496.         } else if (typeof object.length == "number") {
  497.             // the object is array-like
  498.             resolve = Array;
  499.         }
  500.         resolve.forEach(object, block, context);
  501.     }
  502. };
  503.  
  504. //sleep function
  505. function sleep(miliseconds) {
  506.    var currentTime = new Date().getTime();
  507.  
  508.    while (currentTime + miliseconds >= new Date().getTime()) {
  509.    }
  510. };
  511.  
  512. function wait(ms){
  513.    var start = new Date().getTime();
  514.    var end = start;
  515.    while(end < start + ms) {
  516.      end = new Date().getTime();
  517.   }
  518. };
  519.  
  520. //get gameweek id
  521. links = document.getElementsByTagName("a");
  522. gwnum = 0;
  523. for(i=0; i<links.length; i++) {
  524.     if(links[i].innerHTML == "Points") {
  525.         tmpurl = links[i].href;
  526.         tmparr = tmpurl.split("/");
  527.         gwnum = tmparr[tmparr.length-1];
  528.         break;
  529.     }
  530. }
  531.  
  532. config_items = new Array("teamval", "bankval", "totalval", "tt", "gwt", "hitpts", "wc", "chips", "captain", "h2h", "livepoints", "livetotal", "wide", "played", "bonus", "autosubs");
  533. config_values = {};
  534. config_strs = {};
  535. config_strs["teamval"] = "Team value";
  536. config_strs["bankval"] = "Bank value";
  537. config_strs["totalval"] = "Total value";
  538. config_strs["tt"] = "Total transfers";
  539. config_strs["gwt"] = "Gameweek transfers";
  540. config_strs["wc"] = "Wildcard";
  541. config_strs["chips"] = "Chips";
  542. config_strs["captain"] = "Captain";
  543. config_strs["h2h"] = "H2H league position";
  544. config_strs["livepoints"] = "Live gameweek points";
  545. config_strs["livetotal"] = "Live total points";
  546. config_strs["wide"] = "Extra wide";
  547. config_strs["played"] = "Playing stats";
  548. config_strs["hitpts"] = "Transfer points hit";
  549. config_strs["bonus"] = "Bonus points";
  550. config_strs["autosubs"] = "Automatic subs points";
  551. for(i=0; i<config_items.length; i++) {
  552.     c = config_items[i];
  553.     tmpval = GM_getValue(c);
  554.     if(tmpval == undefined) {
  555.         GM_setValue(c, 1);
  556.         tmpval = 1;
  557.     }
  558.     config_values[c] = tmpval;
  559. }
  560.  
  561. optionWidth = "220px";
  562. if(config_values["wide"] == 1) {
  563.     refreshWidth = "1150px";
  564. } else {
  565.     refreshWidth = "1150px";
  566. }
  567.  
  568. function GetXmlHttpObject()
  569. {
  570.     var xmlHttp=null;
  571.     try
  572.       {
  573.       // Firefox, Opera 8.0+, Safari
  574.       xmlHttp=new XMLHttpRequest();
  575.       }
  576.     catch (e)
  577.       {
  578.       // Internet Explorer
  579.       try
  580.         {
  581.         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  582.         }
  583.       catch (e)
  584.         {
  585.         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  586.         }
  587.       }
  588.     return xmlHttp;
  589. }
  590.  
  591. function numberWithCommas(x) {
  592.     var parts = x.toString().split(".");
  593.     parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  594.     return parts.join(".");
  595. }
  596.  
  597. //var window.leagueName;
  598.  
  599. function getData() {
  600.     //alert('go');
  601.     if(!document.getElementById("ismr-classic-standings")) {
  602.         setTimeout(function(){
  603.             getData();
  604.         }, 2000);
  605.         return;
  606.     }
  607.  
  608.     // get data from fixtures
  609.     url = "https://fantasy.premierleague.com/drf/fixtures/?event="+gwnum;
  610.     var xmlHttp=GetXmlHttpObject();
  611.     xmlHttp.onreadystatechange=stateChangedFixturesEvent;
  612.     xmlHttp.open("GET",url,true);
  613.     xmlHttp.send(null);
  614.  
  615.  
  616.     standingsDiv = document.getElementById("ismr-classic-standings");
  617.     //alert(standingsDiv.innerHTML);
  618.     tables = standingsDiv.getElementsByTagName("table");
  619.     //alert(tables.length);
  620.  
  621.     leagueNameDiv = document.getElementById("ismr-main");
  622.     leagueNameH2 = leagueNameDiv.getElementsByTagName("h2");
  623.     window.leagueName = leagueNameH2[0].innerHTML;
  624.  
  625.     table = tables[0];
  626.     trs = table.getElementsByTagName("tr");
  627.     for(j=0; j<trs.length; j++) {
  628.  
  629.         ancs = trs[j].getElementsByTagName("a");
  630.         tds = trs[j].getElementsByTagName("td");
  631.         if(ancs.length > 0) {
  632.             anc = tds[1].getElementsByTagName("a");
  633.             arr1 = anc[0].href.split("team/");
  634.             teamID = arr1[1];
  635.             idpn = "row" + teamID;
  636.             trs[j].id = idpn;
  637.  
  638.         rowstr = "";
  639.         tds[2].style.position = "relative";
  640.         tds[2].innerHTML = tds[2].innerHTML + "<div id='squaddiv"+teamID+"' style='display: none; background-color: #fff; color: #000; padding-left: 20px; font-size: 8pt; position: absolute; left: 0; top: 0; width: "+refreshWidth+"; height: 48px; line-height:48px'><div>";
  641.  
  642.         tds[0].innerHTML = tds[0].innerHTML + "<a style='padding-left: 10px' title='Show Team' href='javascript:void(0)'; onclick='if(document.getElementById(\"squaddiv"+teamID+"\").style.display != \"block\"){document.getElementById(\"squaddiv"+teamID+"\").style.display = \"block\"}else{document.getElementById(\"squaddiv"+teamID+"\").style.display = \"none\"}'>T</a>";
  643.  
  644.         if(config_values["teamval"] == 1) {
  645.             rowstr += "<td id='teamval"+teamID+"' align='right' nowrap>&nbsp;</td>";
  646.         }
  647.         if(config_values["bankval"] == 1) {
  648.             rowstr += "<td id='bankval"+teamID+"' align='right' nowrap>&nbsp;</td>";
  649.         }
  650.         if(config_values["totalval"] == 1) {
  651.             rowstr += "<td id='totalval"+teamID+"' align='right' nowrap>&nbsp;</td>";
  652.         }
  653.         if(config_values["tt"] == 1) {
  654.             rowstr += "<td id='tt"+teamID+"'>&nbsp;</td>";
  655.         }
  656.         if(config_values["gwt"] == 1) {
  657.             rowstr += "<td id='gwt"+teamID+"'>&nbsp;</td>";
  658.         }
  659.         if(config_values["hitpts"] == 1) {
  660.             rowstr += "<td id='hitpts"+teamID+"'>&nbsp;</td>";
  661.         }
  662.         if(config_values["wc"] == 1) {
  663.             rowstr += "<td id='wc"+teamID+"' align='center'>&nbsp;</td>";
  664.         }
  665.         if(config_values["chips"] == 1) {
  666.             rowstr += "<td id='chips"+teamID+"' align='center'>&nbsp;</td>";
  667.         }
  668.         if(config_values["captain"] == 1) {
  669.             rowstr += "<td id='captain"+teamID+"'>&nbsp;</td>";
  670.         }
  671.         if(config_values["h2h"] == 1) {
  672.             rowstr += "<td id='h2h"+teamID+"'><span style='display: none'>-&nbsp;</span>&nbsp;</td>";
  673.         }
  674.         if(config_values["livepoints"] == 1) {
  675.             rowstr += "<td id='livepoints"+teamID+"'>&nbsp;</td>";
  676.         }
  677.         if(config_values["livetotal"] == 1) {
  678.             rowstr += "<td id='livetotal"+teamID+"' nowrap align='right'>&nbsp;</td>";
  679.         }
  680.         if(config_values["played"] == 1) {
  681.             rowstr += "<td id='played_p"+teamID+"' title='&nbsp;' nowrap>&nbsp;</td>";
  682.             rowstr += "<td id='played_tp"+teamID+"' title='&nbsp;' nowrap>&nbsp;</td>";
  683.             rowstr += "<td id='played_dnp"+teamID+"' title='&nbsp;' nowrap>&nbsp;</td>";
  684.         }
  685.         if(config_values["bonus"] == 1) {
  686.             rowstr += "<td id='bonus"+teamID+"' title='&nbsp;' nowrap>&nbsp;</td>";
  687.         }
  688.         if(config_values["autosubs"] == 1) {
  689.             rowstr += "<td id='autosubs"+teamID+"' title='&nbsp;' nowrap>&nbsp;</td>";
  690.         }
  691.         document.getElementById("row"+teamID).innerHTML = document.getElementById("row"+teamID).innerHTML + rowstr;
  692.  
  693.             url = "https://fantasy.premierleague.com/drf/entry/"+teamID;
  694.             var xmlHttp=GetXmlHttpObject();
  695.             xmlHttp.onreadystatechange=stateChangedEntry;
  696.             xmlHttp.open("GET",url,true);
  697.             xmlHttp.send(null);
  698.            
  699.             //sleep(50);
  700.             //wait(50);
  701.  
  702.  
  703.             url = "https://fantasy.premierleague.com/drf/entry/"+teamID+"/event/"+gwnum+"/picks";
  704.             var xmlHttp=GetXmlHttpObject();
  705.             xmlHttp.onreadystatechange=stateChangedEvent;
  706.             xmlHttp.open("GET",url,true);
  707.             xmlHttp.send(null);
  708.            
  709.  
  710.             url = "https://fantasy.premierleague.com/drf/entry/"+teamID+"/history";
  711.             var xmlHttp=GetXmlHttpObject();
  712.             xmlHttp.onreadystatechange=stateChangedHistory;
  713.             xmlHttp.open("GET",url,true);
  714.             xmlHttp.send(null);
  715.  
  716.             url = "https://fantasy.premierleague.com/drf/entry/"+teamID+"/transfers";
  717.             var xmlHttp=GetXmlHttpObject();
  718.             xmlHttp.onreadystatechange=stateChangedTransfers;
  719.             xmlHttp.open("GET",url,true);
  720.             xmlHttp.send(null);
  721.  
  722.  
  723.  
  724.         } else if(j==0) {
  725.             headstr = "";
  726.             if(config_values["teamval"] == 1) {
  727.                 headstr += "<th title='Team value'>Value</th>";
  728.             }
  729.             if(config_values["bankval"] == 1) {
  730.                 headstr += "<th title='Bank value'>Bank</th>";
  731.             }
  732.             if(config_values["totalval"] == 1) {
  733.                 headstr += "<th title='Total value'>Total</th>";
  734.             }
  735.             if(config_values["tt"] == 1) {
  736.                 headstr += "<th><abbr title='Total transfers'>TT</abbr></th>";
  737.             }
  738.             if(config_values["gwt"] == 1) {
  739.                 headstr += "<th><abbr title='Gameweek transfers'>GWT</abbr></th>";
  740.             }
  741.             if(config_values["hitpts"] == 1) {
  742.                 headstr += "<th><abbr title='Transfer points hit'>TPH</abbr></th>";
  743.             }
  744.             if(config_values["wc"] == 1) {
  745.                 headstr += "<th><abbr title='Wildcard available'>WC</abbr></th>";
  746.             }
  747.             if(config_values["chips"] == 1) {
  748.                 headstr += "<th><abbr title='Chips played'>Chips</abbr></th>";
  749.             }
  750.             if(config_values["captain"] == 1) {
  751.                 headstr += "<th>Captain</th>";
  752.             }
  753.             if(config_values["h2h"] == 1) {
  754.                 headstr += "<th><abbr title='Associated H2H league position'>H2H</abbr></th>";
  755.             }
  756.             if(config_values["livepoints"] == 1) {
  757.                 headstr += "<th><abbr title='Live gameweek points'>Live</abbr></th>";
  758.             }
  759.             if(config_values["livetotal"] == 1) {
  760.                 headstr += "<th><abbr title='Live points total'>Total</abbr></th>";
  761.             }
  762.             if(config_values["played"] == 1) {
  763.                 headstr += "<th><abbr title='Players played'>P</abbr></th>";
  764.                 headstr += "<th><abbr title='Players to play'>TP</abbr></th>";
  765.                 headstr += "<th><abbr title='Players who did not play'>DNP</abbr></th>";
  766.             }
  767.             if(config_values["bonus"] == 1) {
  768.                 headstr += "<th><abbr title='Live Bonus points (today&apos;s matches only)'>BAPS</abbr></th>";
  769.             }
  770.             if(config_values["autosubs"] == 1) {
  771.                 headstr += "<th><abbr title='Automatic subs points'>Subs</abbr></th>";
  772.             }
  773.             ths = trs[j].getElementsByTagName("th");
  774.             ths[0].className = "sorttable_nosort";
  775.             ths[0].innerHTML = '<abbr onclick="divs=document.getElementsByTagName(\'div\');onoff=\'\';for(i=0;i<divs.length;i++){tmpid=divs[i].id;if(tmpid.lastIndexOf(\'squaddiv\', 0) === 0){if(onoff==\'\'){if(divs[i].style.display == \'none\'){onoff=\'on\'}else{onoff=\'off\'}}if(onoff==\'on\'){divs[i].style.display=\'\'}else{divs[i].style.display=\'none\'}}}" title="Toggle teams">T</abbr>';
  776.             ths[1].className = "sorttable_nosort";
  777.             trs[j].innerHTML = trs[j].innerHTML + headstr;
  778.         }
  779.     }
  780.         table.innerHTML = table.innerHTML + "<tr><td colspan=21><div id='configrow'></div></td></tr>";
  781.         configrow = document.getElementById("configrow");
  782.  
  783.         for(i=0; i<config_items.length; i++) {
  784.             c = config_items[i];
  785.             //add captain config
  786.             var div = document.createElement('div');
  787.             div.style.display = "inline-block";
  788.             div.style.float = "left";
  789.             div.style.width = optionWidth;
  790.             var span = document.createElement('span');
  791.             span.appendChild(document.createTextNode(config_strs[c]+': '));
  792.             div.appendChild(span);
  793.  
  794.             var a = document.createElement('a');
  795.             if(config_values[c] == 0) {
  796.                 a.style.color = "#bbb";
  797.             }
  798.             a.id = c+'_on';
  799.             a.appendChild(document.createTextNode('On'));
  800.             a.href = 'javascript:null(0)';
  801.             eval("a.addEventListener('click', function(){document.getElementById(\""+c+"_on\").style.color='#000'; document.getElementById(\""+c+"_off\").style.color='#bbb'; GM_setValue(\""+c+"\",1);}, false);");
  802.             div.appendChild(a);
  803.  
  804.             var span = document.createElement('span');
  805.             span.appendChild(document.createTextNode(' | '));
  806.             div.appendChild(span);
  807.  
  808.             var a = document.createElement('a');
  809.             if(config_values[c] == 1) {
  810.                 a.style.color = "#bbb";
  811.             }
  812.             a.id = c+'_off';
  813.             a.appendChild(document.createTextNode('Off'));
  814.             a.href = 'javascript:null(0)';
  815.             eval("a.addEventListener('click', function(){document.getElementById(\""+c+"_off\").style.color='#000'; document.getElementById(\""+c+"_on\").style.color='#bbb'; GM_setValue(\""+c+"\",0);}, false);");
  816.             div.appendChild(a);
  817.             configrow.appendChild(div);
  818.             //end captain config
  819.         }
  820.  
  821.         var div = document.createElement('div');
  822.         div.style.display = "inline-block";
  823.         div.style.float = "left";
  824.         div.style.width = optionWidth;
  825.         var span = document.createElement('span');
  826.         span.appendChild(document.createTextNode('All: '));
  827.         div.appendChild(span);
  828.  
  829.         var a = document.createElement('a');
  830.         a.id = 'all_on';
  831.         a.appendChild(document.createTextNode('On'));
  832.         a.href = 'javascript:null(0)';
  833.         a.addEventListener('click', function(){
  834.             for(i=0; i<config_items.length; i++) {
  835.                 c = config_items[i];
  836.                 document.getElementById(c+"_on").style.color='#000';
  837.                 document.getElementById(c+"_off").style.color='#bbb';
  838.                 GM_setValue(c, 1);
  839.             }
  840.         }, false);
  841.         div.appendChild(a);
  842.  
  843.         var span = document.createElement('span');
  844.         span.appendChild(document.createTextNode(' | '));
  845.         div.appendChild(span);
  846.  
  847.         var a = document.createElement('a');
  848.         a.id = 'all_off';
  849.         a.appendChild(document.createTextNode('Off'));
  850.         a.href = 'javascript:null(0)';
  851.         a.addEventListener('click', function(){
  852.             for(i=0; i<config_items.length; i++) {
  853.                 c = config_items[i];
  854.                 document.getElementById(c+"_off").style.color='#000';
  855.                 document.getElementById(c+"_on").style.color='#bbb';
  856.                 GM_setValue(c, 0);
  857.             }
  858.         }, false);
  859.         div.appendChild(a);
  860.         configrow.appendChild(div);
  861.  
  862.         var div = document.createElement('div');
  863.         div.style.display = "inline-block";
  864.         div.style.float = "left";
  865.         div.style.width = optionWidth;
  866.         var span = document.createElement('span');
  867.         span.appendChild(document.createTextNode('Quick picks: '));
  868.         div.appendChild(span);
  869.  
  870.         var a = document.createElement('a');
  871.         a.id = 'all_on';
  872.         a.appendChild(document.createTextNode('Team Info'));
  873.         a.href = 'javascript:null(0)';
  874.         a.addEventListener('click', function(){
  875.             for(i=0; i<config_items.length; i++) {
  876.                 //live stats
  877.                 if(i<8) {
  878.                     c = config_items[i];
  879.                     document.getElementById(c+"_on").style.color='#000';
  880.                     document.getElementById(c+"_off").style.color='#bbb';
  881.                     GM_setValue(c, 1);
  882.                 } else {
  883.                     c = config_items[i];
  884.                     document.getElementById(c+"_off").style.color='#000';
  885.                     document.getElementById(c+"_on").style.color='#bbb';
  886.                     GM_setValue(c, 0);
  887.                 }
  888.             }
  889.         }, false);
  890.         div.appendChild(a);
  891.  
  892.         var span = document.createElement('span');
  893.         span.appendChild(document.createTextNode(' | '));
  894.         div.appendChild(span);
  895.  
  896.         var a = document.createElement('a');
  897.         a.id = 'all_off';
  898.         a.appendChild(document.createTextNode('Live Data'));
  899.         a.href = 'javascript:null(0)';
  900.         a.addEventListener('click', function(){
  901.             for(i=0; i<config_items.length; i++) {
  902.                 //live stats
  903.                 if(i==7 || i==8 || i==10 || i==11 || i==13) {
  904.                     c = config_items[i];
  905.                     document.getElementById(c+"_on").style.color='#000';
  906.                     document.getElementById(c+"_off").style.color='#bbb';
  907.                     GM_setValue(c, 1);
  908.                 } else {
  909.                     c = config_items[i];
  910.                     document.getElementById(c+"_off").style.color='#000';
  911.                     document.getElementById(c+"_on").style.color='#bbb';
  912.                     GM_setValue(c, 0);
  913.                 }
  914.             }
  915.         }, false);
  916.         div.appendChild(a);
  917.         configrow.appendChild(div);
  918.  
  919.         var div = document.createElement('div');
  920.         div.style.display = "inline-block";
  921.         div.style.width = refreshWidth;
  922.         div.style.float = "left";
  923.         div.style.paddingTop = "10px";
  924.         var span = document.createElement('span');
  925.         var a = document.createElement('a');
  926.         a.appendChild(document.createTextNode('Reload'));
  927.         a.href = 'javascript:location.reload()';
  928.         a.style.border = "1px solid #000";
  929.         a.style.padding = "3px";
  930.         a.style.borderRadius = "6px";
  931.         span.appendChild(a);
  932.         div.appendChild(span);
  933.         configrow.appendChild(div);
  934.  
  935.         var div = document.createElement('div');
  936.         div.style.display = "inline-block";
  937.         div.style.width = refreshWidth;
  938.         div.style.float = "left";
  939.         div.style.paddingTop = "10px";
  940.         var img = document.createElement('img');
  941.         var img2 = document.createElement('img');
  942.         var span = document.createElement('div');
  943.         span.innerHTML = '<br /><b>You can now download my FPL Live app on iOS and see the live data for all your leagues!<br /><br />See live points and positions, full team view, captain and vice-captain, chips and players playing stats for every team...</b><br /><br />';
  944.         span.style.width = refreshWidth;
  945.         img.style.width = "150px";
  946.         img2.style.width = "75px";
  947.         img.src = "http://www.enicma.co.uk/fpl/images/app_store.png";
  948.         img2.src = "http://www.enicma.co.uk/fpl/images/lion_launch.png";
  949.         var a = document.createElement('a');
  950.         var a2 = document.createElement('a');
  951.         a.href = 'https://itunes.apple.com/gb/app/fpl-live/id1039666919';
  952.         a2.href = 'http://www.enicma.co.uk/fpl/';
  953.         a.target = "_blank";
  954.         a2.target = "_blank";
  955.         a.style.padding = "3px";
  956.         a.style.borderRadius = "6px";
  957.         a.appendChild(img);
  958.         a2.style.padding = "3px";
  959.         a2.style.borderRadius = "6px";
  960.         a2.appendChild(img2);
  961.         div.appendChild(span);
  962.         div.appendChild(a);
  963.         div.appendChild(a2);
  964.  
  965.         configrow.appendChild(div);
  966.  
  967. }
  968.  
  969. setTimeout(function(){
  970.     getData();
  971. }, 2000);
  972.  
  973.  
  974.  
  975. function sortByProperty(objArray, prop, direction){
  976.     if (arguments.length<2) throw new Error("ARRAY, AND OBJECT PROPERTY MINIMUM ARGUMENTS, OPTIONAL DIRECTION");
  977.     if (!Array.isArray(objArray)) throw new Error("FIRST ARGUMENT NOT AN ARRAY");
  978.     const clone = objArray.slice(0);
  979.     const direct = arguments.length>2 ? arguments[2] : 1; //Default to ascending
  980.     const propPath = (prop.constructor===Array) ? prop : prop.split(".");
  981.     clone.sort(function(a,b){
  982.         for (let p in propPath){
  983.                 if (a[propPath[p]] && b[propPath[p]]){
  984.                     a = a[propPath[p]];
  985.                     b = b[propPath[p]];
  986.                 }
  987.         }
  988.         // convert numeric strings to integers
  989.         a = a.match(/^\d+$/) ? +a : a;
  990.         b = b.match(/^\d+$/) ? +b : b;
  991.         return ( (a < b) ? -1*direct : ((a > b) ? 1*direct : 0) );
  992.     });
  993.     return clone;
  994. }
  995.  
  996. var playersObj;
  997.  
  998. function stateChangedPlayers() {
  999.     if (this.readyState==4) {
  1000.         retval = this.responseText;
  1001.         playersObj = JSON.parse(retval);
  1002.         playersObj.elements.sort(function(a, b){return a.id-b.id;});
  1003.     }
  1004. }
  1005.  
  1006. url = "https://fantasy.premierleague.com/drf/bootstrap-static";
  1007. var xmlHttp=GetXmlHttpObject();
  1008. xmlHttp.onreadystatechange=stateChangedPlayers;
  1009. xmlHttp.open("GET",url,true);
  1010. xmlHttp.send(null);
  1011.  
  1012. var liveObj;
  1013.  
  1014. function stateChangedLive() {
  1015.     if (this.readyState==4) {
  1016.         retval = this.responseText;
  1017.         liveObj = JSON.parse(retval);
  1018.     }
  1019. }
  1020.  
  1021. url = "https://fantasy.premierleague.com/drf/event/"+gwnum+"/live";
  1022. var xmlHttp=GetXmlHttpObject();
  1023. xmlHttp.onreadystatechange=stateChangedLive;
  1024. xmlHttp.open("GET",url,true);
  1025. xmlHttp.send(null);
  1026.  
  1027. function stateChangedHistory() {
  1028.     if (this.readyState==4) {
  1029.         retval = this.responseText;
  1030.         teamObj = JSON.parse(retval);
  1031.         team_id = teamObj.entry.id;
  1032.         lastGW = teamObj.entry.current_event;
  1033.  
  1034.         numChips = teamObj.chips.length;
  1035.         chipsdetail = "";
  1036.         chipname = "";
  1037.         livechip = 0;
  1038.  
  1039.         wildcardplayed = false;
  1040.  
  1041.         for(i=0; i<teamObj.chips.length; i++) {
  1042.             chip = teamObj.chips[i];
  1043.             if(chip.name == "3xc") {
  1044.                 chipname = "Triple Captain";
  1045.             } else if(chip.name == "attack") {
  1046.                 chipname = "All Out Attack";
  1047.             } else if(chip.name == "bboost") {
  1048.                 chipname = "Bench Boost";
  1049.             } else if(chip.name == "freehit") {
  1050.                 chipname = "Free Hit";
  1051.             } else if(chip.name == "wildcard") {
  1052.                 chipname = "Wildcard";
  1053.                 wildcardplayed = true;
  1054.             } else {
  1055.                 chipname = chip.name;
  1056.             }
  1057.             chipsdetail += chipname + " (GW" + chip.event + ")\n";
  1058.             if(chip.event == lastGW) {
  1059.                 livechip = 1;
  1060.             }
  1061.         }
  1062.  
  1063.         document.getElementById("chips"+team_id).innerHTML = numChips;
  1064.         document.getElementById("chips"+team_id).title = chipsdetail;
  1065.         if(livechip) {
  1066.             document.getElementById("chips"+team_id).style.backgroundColor = "pink";
  1067.         }
  1068.         if(!wildcardplayed) {
  1069.             wcimg = "http://upload.wikimedia.org/wikipedia/commons/thumb/0/03/Green_check.svg/13px-Green_check.svg.png";
  1070.             wctxt = "Available";
  1071.         } else {
  1072.             wcimg = "http://upload.wikimedia.org/wikipedia/en/thumb/b/ba/Red_x.svg/13px-Red_x.svg.png";
  1073.             wctxt = "Played";
  1074.         }
  1075.         cell = document.getElementById("wc"+team_id);
  1076.         cell.innerHTML = "<span id='wcs"+teamID+"' style='display: none;'>" + wctxt + "</span><img id='wci"+teamID+"' title='" + wctxt + "' src='" + wcimg + "'>";
  1077.  
  1078.     }
  1079.     standingsDiv = document.getElementById("ismr-classic-standings");
  1080.     //alert(standingsDiv.innerHTML);
  1081.     tables = standingsDiv.getElementsByTagName("table");
  1082.     //alert(tables.length);
  1083.  
  1084.     table = tables[0];
  1085.  
  1086.     clearTimeout(mytimeout);
  1087.     mytimeout = setTimeout(function() {sorttable.makeSortable(table)}, 1000);
  1088. }
  1089.  
  1090.  
  1091.  
  1092. var transfersObj;
  1093.  
  1094. function stateChangedTransfers() {
  1095.     if (this.readyState==4) {
  1096.         retval = this.responseText;
  1097.         transfersObj = JSON.parse(retval);
  1098.         team_id = transfersObj.entry.id;
  1099.         lastGW = transfersObj.entry.current_event;
  1100.         transfersHistory = transfersObj.history;
  1101.         numOfTransfers = transfersHistory.length;
  1102.  
  1103.         playersIN = "GW" + lastGW + " transfers \nIN: ";
  1104.         playersOUT= "OUT: ";
  1105.  
  1106.         for(i=0; i<numOfTransfers; i++) {
  1107.             transfer = transfersObj.history[i];
  1108.             if(transfer.event == lastGW){
  1109.                     if(playersIN != "GW" + lastGW + " transfers \nIN: ") {
  1110.                     playersIN += ", ";
  1111.                 }
  1112.  
  1113.                 if(playersOUT != "OUT: ") {
  1114.                     playersOUT += ", ";
  1115.                 }
  1116.                 playersIN += playersObj.elements[transfer.element_in-1].web_name ;
  1117.                 playersOUT += playersObj.elements[transfer.element_out-1].web_name;
  1118.             }
  1119.         }
  1120.         document.getElementById("gwt"+team_id).title = playersIN + "\n" + playersOUT;
  1121.  
  1122.     }
  1123.  
  1124.     standingsDiv = document.getElementById("ismr-classic-standings");
  1125.     //alert(standingsDiv.innerHTML);
  1126.     tables = standingsDiv.getElementsByTagName("table");
  1127.     //alert(tables.length);
  1128.  
  1129.     table = tables[0];
  1130.  
  1131.     clearTimeout(mytimeout);
  1132.     mytimeout = setTimeout(function() {sorttable.makeSortable(table)}, 1000);
  1133. }
  1134. //bonus var
  1135. var bonusArr = [];
  1136.  
  1137.  
  1138. function stateChangedFixturesEvent() {
  1139.     if (this.readyState==4) {
  1140.         retval = this.responseText;
  1141.         fixturesArr = JSON.parse(retval);
  1142.         event_day = 0;
  1143.  
  1144.         indy = 0;
  1145.  
  1146.         for(k=0;k<fixturesArr.length;k++) {
  1147.             if (fixturesArr[k].started && !fixturesArr[k].finished) {
  1148.                 bpsArr = [];
  1149.                 indx = 0;
  1150.                 stats = fixturesArr[k].stats;
  1151.                 event_day = fixturesArr[k].event_day;
  1152.                 for(i=0; i<stats.length; i++) {
  1153.                     if (Object.keys(stats[i]) == "bps") {
  1154.                         // away team
  1155.                         bps_a = stats[i].bps.a;
  1156.                         for(j=0; j<bps_a.length; j++){
  1157.                             if (bps_a[j].value > 10) {
  1158.                                 bpsArr[indx] = {playerID: bps_a[j].element, bpsPTS: bps_a[j].value};
  1159.                                 indx++;
  1160.                             }
  1161.                         }
  1162.                         // home team
  1163.                         bps_h = stats[i].bps.h;
  1164.                         for(j=0; j<bps_h.length; j++){
  1165.                             if (bps_h[j].value > 10) {
  1166.                                 bpsArr[indx] = {playerID: bps_h[j].element, bpsPTS: bps_h[j].value};
  1167.                                 indx++;
  1168.                             }
  1169.                         }
  1170.                     }
  1171.                 }
  1172.                 // sorting
  1173.                 bpsArr.sort(function(a, b){return b.bpsPTS - a.bpsPTS});
  1174.                 // add bonuses
  1175.                 minBP = 3;
  1176.                 bonus_pt = 3;
  1177.                 empty_pt = 0;
  1178.                 for(i=0; i<minBP; i++) {
  1179.                     if (bpsArr[i].bpsPTS == bpsArr[i+1].bpsPTS) {
  1180.                         bonusArr[indy] = {playerID: bpsArr[i].playerID, bonusPTS: bonus_pt+empty_pt-i};
  1181.                         empty_pt++;
  1182.                         minBP++;
  1183.                     } else {
  1184.                         bonusArr[indy] = {playerID: bpsArr[i].playerID, bonusPTS: bonus_pt+empty_pt-i};
  1185.                         empty_pt = 0;
  1186.                     }
  1187.                     indy++;
  1188.                 }
  1189.             }
  1190.         }
  1191.     }
  1192. }
  1193.  
  1194. var entryObj = {};
  1195.  
  1196. function stateChangedEntry() {
  1197.     if (this.readyState==4) {
  1198.         retval = this.responseText;
  1199.         entryObj = JSON.parse(retval);
  1200.         changed_entry = entryObj.entry.id;
  1201.         if (changed_entry != teamID) {
  1202.             //alert("teamID is " + teamID);
  1203.             //alert("entry is " + changed_entry);
  1204.             stateChangedEntry();
  1205.         }
  1206. /*
  1207.         entry = entryObj.entry;
  1208.         value = (entry.value*100)/1000;
  1209.         bank = (entry.bank*100)/1000;
  1210.         total_value = value + bank;
  1211.         value = value.toFixed(1);
  1212.         bank = bank.toFixed(1);
  1213.         total_value = total_value.toFixed(1);
  1214.  
  1215.         total_transfers = entry.total_transfers;
  1216.         gw_transfers = entry.event_transfers;
  1217.         transfers_hit = entry.event_transfers_cost;
  1218.  
  1219.  
  1220.         lastGW = entry.current_event;
  1221.  
  1222.         current_points = entry.summary_overall_points;
  1223.         week_points = entry.summary_event_points;
  1224. */
  1225.         clearTimeout(mytimeout);
  1226.         mytimeout = setTimeout(function() {sorttable.makeSortable(table)}, 1000);
  1227.     }
  1228. }
  1229.  
  1230.  
  1231. function stateChangedEvent() {
  1232.     if (this.readyState==4) {
  1233.         retval = this.responseText;
  1234.         teamObj = JSON.parse(retval);
  1235.  
  1236.         entry = entryObj.entry;
  1237.         value = (entry.value*100)/1000;
  1238.         bank = (entry.bank*100)/1000;
  1239.         total_value = value + bank;
  1240.         value = value.toFixed(1);
  1241.         bank = bank.toFixed(1);
  1242.         total_value = total_value.toFixed(1);
  1243.  
  1244.         total_transfers = entry.total_transfers;
  1245.         gw_transfers = entry.event_transfers;
  1246.         transfers_hit = entry.event_transfers_cost;
  1247.  
  1248.  
  1249.         lastGW = entry.current_event;
  1250.  
  1251.         live_total_str = "";
  1252.  
  1253.  
  1254.         current_points = entry.summary_overall_points;
  1255.         week_points = entry.summary_event_points;
  1256.  
  1257.         live_total_str += "week_points after " + week_points + ";";
  1258.  
  1259.         entry_history = teamObj.entry_history;
  1260.         auto_subs = teamObj.automatic_subs;
  1261.  
  1262.         team_id = entry_history.entry;
  1263.         transfers_cost = entry_history.event_transfers_cost;
  1264.         picks = teamObj.picks;
  1265.         event = teamObj.event;
  1266.  
  1267.  
  1268.         live_total_str += "picks id " + team_id + ";";
  1269.         live_total_str += "entry id " + entry.id + ";";
  1270.  
  1271.         live_score = 0;
  1272.         bonus_pts = 0;
  1273.         bench_pts = 0;
  1274.         bench_bonus_pts = 0;
  1275.         autosubs_pts = 0;
  1276.         autosubs_bonus_pts = 0;
  1277.         autosubs_str = "";
  1278.         bonus_str = "";
  1279.  
  1280.  
  1281.         playedstr = "";
  1282.         playednum = 0;
  1283.         toplaystr = "";
  1284.         toplaynum = 0;
  1285.         didntplaystr = "";
  1286.         didntplaynum = 0;
  1287.  
  1288.         firstnum = 0;
  1289.         subsnum = 0;
  1290.  
  1291.         captain_dnp = false;
  1292.         subs_made = false;
  1293.  
  1294.         firstxistr = "";
  1295.         subsstr = "";
  1296.  
  1297.         numfirstxicount = 0;
  1298.         numfirstxidefcount = 0;
  1299.         numnonplayingdefcount = 0;
  1300.         numplayingdefcount = 0;
  1301.         numsubscount = 0;
  1302.         dnpArr = [];
  1303.         subsArr = [];
  1304.  
  1305.  
  1306.  
  1307.         player_minutes = 0;
  1308.         player_fixture = "";
  1309.         player_type = 0;
  1310.  
  1311.  
  1312.  
  1313.         for(i=0; i<picks.length; i++) {
  1314.             is_captain = false;
  1315.             is_vice = false;
  1316.             pick_points = playersObj.elements[picks[i].element-1].event_points;
  1317.             player_type = playersObj.elements[picks[i].element-1].element_type;
  1318.             player_minutes = 0;
  1319.             player_fixture = "";
  1320.  
  1321.             if(picks[i].is_captain) {
  1322.                 captain_id = picks[i].element;
  1323.                 captain = playersObj.elements[captain_id-1].web_name;
  1324.                 is_captain = true;
  1325.                 captain_points = pick_points;
  1326.             }
  1327.             if(picks[i].is_vice_captain) {
  1328.                 vice_captain_id = picks[i].element;
  1329.                 vice_captain = playersObj.elements[vice_captain_id-1].web_name;
  1330.                 is_vice = true;
  1331.                 vice_points = pick_points;
  1332.             }
  1333.             if(i<11) {
  1334.                 numfirstxicount++;
  1335.  
  1336.                 if (player_type == 2) {
  1337.                    numfirstxidefcount++;
  1338.                 }
  1339.  
  1340.                 firstxistr += "<player title=\"" + pick_points + "\">";
  1341.                 firstxistr += playersObj.elements[picks[i].element-1].web_name;
  1342.  
  1343.                 if(is_captain) {
  1344.                     firstxistr += "(C)";
  1345.                 } else if(is_vice) {
  1346.                     firstxistr += "(vc)";
  1347.                 }
  1348.  
  1349.                 if(numfirstxicount !== 11) {
  1350.                     firstxistr += ", ";
  1351.                 }
  1352.                 firstxistr += "</player>" ;
  1353.  
  1354.  
  1355.  
  1356.                 pts = pick_points * picks[i].multiplier;
  1357.                 live_score = live_score + pts;
  1358.                 //bonus
  1359.                 for (j=0; j<bonusArr.length; j++) {
  1360.                     //bonus for finished matche
  1361.                     if(bonusArr[j].playerID == picks[i].element) {
  1362.                         bonus_pts = bonus_pts + bonusArr[j].bonusPTS * picks[i].multiplier;
  1363.                         bonus_str += playersObj.elements[picks[i].element-1].web_name + "(" + bonusArr[j].bonusPTS * picks[i].multiplier + ")\n";
  1364.                     }
  1365.                 }
  1366.              player_minutes = liveObj.elements[picks[i].element].stats.minutes;
  1367.  
  1368.              if (Object.keys(liveObj.elements[picks[i].element].explain).length>0) {
  1369.                  player_fixture = liveObj.elements[picks[i].element].explain[0][1];
  1370.              }
  1371.              fixtures = liveObj.fixtures;
  1372.              has_played = 0;
  1373.  
  1374.  
  1375.                 for(fix=0; fix<fixtures.length; fix++) {
  1376.                     if(fixtures[fix].id == player_fixture && player_fixture!=""){
  1377.                        if(fixtures[fix].started) {
  1378.                           has_played = 1;
  1379.                        }
  1380.                     }
  1381.                 }
  1382.  
  1383.  
  1384.                 if(has_played == 1) {
  1385.                     if(player_minutes > 0) {
  1386.                         //has played
  1387.                         if(playedstr != "") {
  1388.                             playedstr += ", ";
  1389.                         }
  1390.                         playednum++;
  1391.                         playedstr += playersObj.elements[picks[i].element-1].web_name;
  1392.                     } else {
  1393.                         //didnt play
  1394.                         if(didntplaystr != "") {
  1395.                             didntplaystr += ", ";
  1396.                         }
  1397.  
  1398.                         dnpArr[didntplaynum] = {playerID: picks[i].element-1, playerType: player_type, autosubbedOut: false};
  1399.        
  1400.  
  1401.                         didntplaynum++;
  1402.                         didntplaystr += playersObj.elements[picks[i].element-1].web_name;
  1403.  
  1404.  
  1405.  
  1406.                         if(is_captain) {
  1407.                             captain_dnp = true;
  1408.                         }
  1409.                     }
  1410.                 } else {
  1411.                     //to play
  1412.                     if(toplaystr != "") {
  1413.                         toplaystr += ", ";
  1414.                     }
  1415.                     toplaynum++;
  1416.                     toplaystr += playersObj.elements[picks[i].element-1].web_name;
  1417.                 }
  1418.             } else {
  1419.                 subsArr[numsubscount] = {playerID: picks[i].element-1, playerType: player_type, autosubbedIn: false};
  1420.  
  1421.                 if (player_type == 2){
  1422.                    numnonplayingdefcount++;
  1423.                 }
  1424.  
  1425.                 numsubscount++;
  1426.                 subsstr += "<player title=\"" + pick_points + "\">";
  1427.                 subsstr += playersObj.elements[picks[i].element-1].web_name;
  1428.  
  1429.                 pts = pick_points * picks[i].multiplier;
  1430.                 bench_pts = bench_pts + pts;
  1431.  
  1432.                 for (j=0; j<bonusArr.length; j++) {
  1433.                     //bonus for finished matches
  1434.                     if(bonusArr[j].playerID == picks[i].element) {
  1435.                         bench_bonus_pts = bench_bonus_pts + bonusArr[j].bonusPTS * picks[i].multiplier;
  1436.                     }
  1437.                 }
  1438.  
  1439.                 if(is_captain) {
  1440.                     subsstr += "(C)";
  1441.                 } else if(is_vice) {
  1442.                     subsstr += "(vc)";
  1443.                 }
  1444.  
  1445.                 if(numsubscount !== 4) {
  1446.                     subsstr += ", ";
  1447.                 }
  1448.  
  1449.                 subsstr += "</player>";
  1450.  
  1451.             }
  1452.         }
  1453.         rowstr = "";
  1454.  
  1455.   if (dnpArr.length > 0) {
  1456.         //Sub goalkeepers
  1457.         for (indx=0; indx<dnpArr.length; indx++) {
  1458.             //if its a goalkeeper and isn't already subbed out
  1459.             if (dnpArr[indx].playerType==1 && !dnpArr[indx].autosubbedOut) {
  1460.                 //and isn't already subbed in and has played minutes already
  1461.                if (!subsArr[0].autosubbedIn && liveObj.elements[subsArr[0].playerID+1].stats.minutes>0) {
  1462.                    //sub player
  1463.                    //autosubs_pts += liveObj.elements[subsArr[0].playerID+1].stats.total_points;
  1464.                    autosubs_str += playersObj.elements[subsArr[0].playerID].web_name + "(" + liveObj.elements[subsArr[0].playerID+1].stats.total_points + ")\n";
  1465.                    subsArr[0].autosubbedIn = true;
  1466.                    dnpArr[indx].autosubbedOut = true;
  1467.                }
  1468.             }
  1469.         }
  1470.  
  1471.         //Sub outfield players
  1472.         numplayingdefcount = numfirstxidefcount - numnonplayingdefcount;
  1473.         numofdefssubstituted = 0;
  1474.  
  1475.         if  (numplayingdefcount < 3) {
  1476.         //do {
  1477.             //do only 2 changes because 2 is max places on bench for defenders
  1478.             for (z=1; z<3; z++) {
  1479.             index_subin = -1;
  1480.             index_subout = -1;
  1481.  
  1482.                 for (x=0; x<dnpArr.length; x++) {
  1483.                  //if its a defender and isn't already subbed out
  1484.                  if (dnpArr[x].playerType==2 && !dnpArr[x].autosubbedOut) {
  1485.                      index_subout = x;
  1486.                      break;
  1487.                  }
  1488.                 }
  1489.  
  1490.                 for (y=1; y<=3; y++) {
  1491.                     if (subsArr[y].playerType==2 && !subsArr[y].autosubbedIn && liveObj.elements[subsArr[y].playerID+1].stats.minutes>0) {
  1492.                         index_subin = y;
  1493.                         break;
  1494.                     }
  1495.                 }
  1496.  
  1497.             if(index_subout>=0 && index_subin>0) {
  1498.  
  1499.                subsArr[index_subin].autosubbedIn = true;
  1500.                dnpArr[index_subout].autosubbedOut = true;
  1501.                autosubs_str += playersObj.elements[subsArr[index_subin].playerID].web_name + "(" + liveObj.elements[subsArr[index_subin].playerID+1].stats.total_points + ")\n";
  1502.                numplayingdefcount++;
  1503.  
  1504.             }
  1505.              numofdefssubstituted++;
  1506.              if (numplayingdefcount == 3) {
  1507.                  break;
  1508.              }
  1509.           }
  1510.         }
  1511.       //do other subs in order
  1512.        for (w=0; w<dnpArr.length; w++) {
  1513.            //don't sub goalkeepers
  1514.            if (dnpArr[w].playerType!=1) {
  1515.            //if already subsituted 2 defenders then skip defenders
  1516.              if ((!dnpArr[w].autosubbedOut) && ((dnpArr[w].playerType!=2) || ((dnpArr[w].playerType==2) && (numofdefssubstituted !=2)))) {
  1517.                  for (v=1; v<=3; v++) {
  1518.                     if (!subsArr[v].autosubbedIn && liveObj.elements[subsArr[v].playerID+1].stats.minutes>0) {
  1519.                          subsArr[v].autosubbedIn = true;
  1520.                          dnpArr[w].autosubbedOut = true;
  1521.                          autosubs_str += playersObj.elements[subsArr[v].playerID].web_name + "(" + liveObj.elements[subsArr[v].playerID+1].stats.total_points + ")\n";
  1522.                          break;
  1523.                          }
  1524.                  }
  1525.                 }
  1526.            }
  1527.        }
  1528.  
  1529.       //calculate autosubs points
  1530.       for (m=0; m<subsArr.length; m++) {
  1531.           if (subsArr[m].autosubbedIn) {
  1532.               autosubs_pts += liveObj.elements[subsArr[m].playerID+1].stats.total_points;
  1533.               for (n=0; n<bonusArr.length; n++) {
  1534.                     //add bonus for finished matches
  1535.                     if(bonusArr[n].playerID == subsArr[m].playerID+1) {
  1536.                         autosubs_pts += bonusArr[n].bonusPTS;
  1537.                     }
  1538.                 }
  1539.           }
  1540.       }
  1541.  
  1542.  
  1543.  
  1544.   }
  1545.          live_total_str += "captain id is " + captain_id + ";";
  1546.         for(q=0; q<auto_subs.length; q++) {
  1547.                 subs_made = true;
  1548.         }
  1549.  
  1550.         if ((captain_dnp) && !(subs_made)){
  1551.             if (teamObj.active_chip == "3xc"){
  1552.             autosubs_pts += vice_points*2;
  1553.             autosubs_str += vice_captain + " ("+ vice_points*2 + ")(vc)";
  1554.         } else {
  1555.             autosubs_pts += vice_points;
  1556.             autosubs_str += vice_captain + " ("+ vice_points + ")(vc)";}
  1557.         }
  1558.  
  1559.         if(teamObj.active_chip == "bboost") {
  1560.                     live_score = live_score + bench_pts + bench_bonus_pts;
  1561.                 } else {
  1562.                     live_score += autosubs_pts + bonus_pts;
  1563.                 }
  1564.  
  1565.  
  1566.  
  1567.         live_total = (current_points - week_points) + live_score;
  1568.         live_total_str += "(current_points/" + current_points + "/ - week_points/" + week_points + "/) + live_score/" + live_score + "/";
  1569.  
  1570.         if (event_day == 1){
  1571.                     live_total -= transfers_cost;
  1572.                 }
  1573.  
  1574.  
  1575.         h2hpos = 0;
  1576.  
  1577.         if(config_values["teamval"] == 1) {
  1578.             document.getElementById("teamval"+team_id).innerHTML = value;
  1579.         }
  1580.         if(config_values["bankval"] == 1) {
  1581.             document.getElementById("bankval"+team_id).innerHTML = bank;
  1582.         }
  1583.         if(config_values["totalval"] == 1) {
  1584.             document.getElementById("totalval"+team_id).innerHTML = total_value;
  1585.         }
  1586.         if(config_values["tt"] == 1) {
  1587.             document.getElementById("tt"+team_id).innerHTML = total_transfers;
  1588.         }
  1589.         if(config_values["gwt"] == 1) {
  1590.             document.getElementById("gwt"+team_id).innerHTML = gw_transfers;
  1591.         }
  1592.         if(config_values["hitpts"] == 1) {
  1593.             document.getElementById("hitpts"+team_id).innerHTML = transfers_hit;
  1594.         }
  1595.         if(config_values["captain"] == 1) {
  1596.             if (team_id != entry.id) {
  1597.             //live_total_str = "<b>" + live_total_str + "</b>";
  1598.             document.getElementById("captain"+team_id).innerHTML = "<b>" + captain + "</b>";
  1599.             } else {
  1600.             document.getElementById("captain"+team_id).innerHTML = captain;
  1601.             }
  1602.             document.getElementById("captain"+team_id).title = vice_captain;
  1603.         }
  1604.         if(config_values["livepoints"] == 1) {
  1605.             if (transfers_cost>0 && event_day == 1){
  1606.                 document.getElementById("livepoints"+team_id).innerHTML = live_score + "(-" +transfers_cost + ")" ;
  1607.             } else {
  1608.                 document.getElementById("livepoints"+team_id).innerHTML = live_score;
  1609.             }
  1610.         }
  1611.         if(config_values["livetotal"] == 1) {
  1612.             //live_total_str
  1613.             document.getElementById("livetotal"+team_id).innerHTML = numberWithCommas(live_total);
  1614.             //document.getElementById("livetotal"+team_id).title = live_total_str;
  1615.         }
  1616.         if(config_values["h2h"] == 1) {
  1617.             h2hpos = "-";
  1618.             /*for(li=0; li<teamObj.leagues.h2h.length; li++) {
  1619.                 ln = teamObj.leagues.h2h[li].name;
  1620.                 if(ln.indexOf(window.leagueName) == 0) {
  1621.                     h2hpos = teamObj.leagues.h2h[li].entry_rank;
  1622.                 }
  1623.             }*/
  1624.             document.getElementById("h2h"+team_id).innerHTML = h2hpos;
  1625.         }
  1626.         if(config_values["played"] == 1) {
  1627.             document.getElementById("played_p"+team_id).innerHTML = playednum;
  1628.             document.getElementById("played_p"+team_id).title = playedstr;
  1629.             document.getElementById("played_tp"+team_id).innerHTML = toplaynum;
  1630.             document.getElementById("played_tp"+team_id).title = toplaystr;
  1631.             document.getElementById("played_dnp"+team_id).innerHTML = didntplaynum;
  1632.             document.getElementById("played_dnp"+team_id).title = didntplaystr;
  1633.         }
  1634.         if(config_values["bonus"] == 1) {
  1635.             document.getElementById("bonus"+team_id).innerHTML = bonus_pts;
  1636.             document.getElementById("bonus"+team_id).title = bonus_str;
  1637.  
  1638.         }
  1639.         if(config_values["autosubs"] == 1) {
  1640.             document.getElementById("autosubs"+team_id).innerHTML = autosubs_pts;
  1641.             document.getElementById("autosubs"+team_id).title = autosubs_str;
  1642.         }
  1643.         document.getElementById("squaddiv"+team_id).innerHTML = firstxistr + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>Subs: "+subsstr;
  1644.  
  1645.     }
  1646.  
  1647.     standingsDiv = document.getElementById("ismr-classic-standings");
  1648.     //alert(standingsDiv.innerHTML);
  1649.     tables = standingsDiv.getElementsByTagName("table");
  1650.     //alert(tables.length);
  1651.  
  1652.     table = tables[0];
  1653.  
  1654.     clearTimeout(mytimeout);
  1655.     mytimeout = setTimeout(function() {sorttable.makeSortable(table)}, 1000);
  1656.  
  1657. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement