jpk100 / DT17

// ==UserScript==
// @name        DT17
// @namespace   123
// @include     https://www.dreamteamfc.com/g/*
// @version     1.5
// @require  http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
// @license MIT
// @grant       none
// ==/UserScript==


var Players;
var Gameweeks = [
  3,
  6,
  9,
  13,
  17,
  21,
  24,
  29,
  33
];
var Pls = new Object;
var leagueid = '0';
var ladder;
var sid = APP.getJStorage('session_id');
if (window.location.href.indexOf('table') > - 1) {
  var pathArray = window.location.href.split('/');
  leagueid = pathArray[6];
  console.log('league');
  console.log(leagueid);
  $.ajax({
    url: 'https://backend-nuk.fanhubmedia.com.au/api/season_stats/ladder?league_id=' + leagueid + '&sid=' + sid,
    dataType: 'json',
    success: function (data)
    {
      ladder = data.result.teams;
    }
  });
}
if (window.location.href.indexOf('leaderboard') > - 1) {
  var pathArray = window.location.href.split('/');
  leagueid = pathArray[6];
  console.log('league');
  console.log(leagueid);
  $.ajax({
    url: 'https://backend-nuk.fanhubmedia.com.au/api/season_stats/global_ladder?sid=' + sid,
    dataType: 'json',
    success: function (data)
    {
      ladder = data.result.teams;
    }
  });
}
$.ajax({
  url: 'https://nuk-data.s3-eu-west-1.amazonaws.com/json/players.json',
  dataType: 'json',
  success: function (data)
  {
    Players = data;
    $.each(Players, function (i, player) {
      Pls[player.id] = player;
    });
    console.log('players');
    console.log(Pls);
  }
});
function main() {
  var Teams = null;
  $('.clearfix.team-hub-item.page-hub-item.js-go-to-view').each(function (key, value) {
    var box = this;
    var teamid = $(this).attr('data-team_id');
    //console.log(teamid);
    if(teamid=3001038)teamid=3001011
    addLineup('https://backend-nuk.fanhubmedia.com.au/api/season_transfer/show_my?team_id=', box, teamid, sid,0);
  });
  var cnt = 0;
  var gw = 1 ;//$('.js-gw-select :selected').val()
  $('.ladder-table  > tbody  > tr').each(function (key, value) {
    var box = this;
    $('.team-avatar').remove();
    var teamid = ladder[cnt].id;
    console.log(teamid);
    if(teamid-=3001038)teamid=3001011
    addLineup('https://backend-nuk.fanhubmedia.com.au/api/season_team/show?id=', box, teamid, sid,0);
    addLineup('https://backend-nuk.fanhubmedia.com.au/api/season_team/show?gw=1&id=', box, teamid, sid,1);
   
   cnt++;
  });
  /*
  $('.load-more-btn').click(function () {
    setTimeout(function ()
    {
      alert('klik')
      $('.ladder-table  > tbody  > tr').each(function (key, value) {
        var box = this //   $('.team-avatar').remove()
        var teamid = ladder[cnt].id;
        console.log(teamid);
        addLineup('https://backend-nuk.fanhubmedia.com.au/api/season_team/show?id=', box, teamid, sid);
        cnt++;
      });
    }, 2500);
  });
  */
}
$(window).on('load', function () {
  main();
});

function addLineup(my, box, teamid, sid,GW)
{
  $.ajax({
    async: true,
    url: my + teamid + '&sid=' + sid,
    dataType: 'json',
    success: function (data)
    {
      Teams = data.result;
      console.log(Teams);
      var teamprice = 0;
      var teamdetail = '';
      var sp = $('<p><span />').css({
        'font-size': '12px',
        'color': 'orange'
      });

      var spb = $('<p><span /').css({
        'font-size': '12px','font-weight': 'bold',
        'color': 'black'
      });
        var plen=0;
   if(GW==0)
     {
      spb.append(Pls[Teams.lineup[1]].last_name + ' ');
      teamprice += Pls[Teams.lineup[1]].cost;
       plen = Teams.lineup[2].length;
      while (plen--) {
        spb.append(Pls[Teams.lineup[2][plen]].last_name + ' ');
        teamprice += Pls[Teams.lineup[2][plen]].cost;
      }
      plen = Teams.lineup[3].length;
      while (plen--) {
        spb.append(Pls[Teams.lineup[3][plen]].last_name + ' ');
        teamprice += Pls[Teams.lineup[3][plen]].cost;
      }
      plen = Teams.lineup[4].length;
      while (plen--) {
        spb.append(Pls[Teams.lineup[4][plen]].last_name + ' ');
        teamprice += Pls[Teams.lineup[4][plen]].cost;

 }
     spb.append('<p>cost ' + teamprice / 1000000 + ' ');
     spb.append('budget ' + (Teams.salary_cap - Teams.value) / 1000000); }
     if(GW==1)
     {
      sp.append(Pls[Teams.lineup[1]].last_name + ' ');
       plen = Teams.lineup[2].length;
      while (plen--) {
        sp.append(Pls[Teams.lineup[2][plen]].last_name + ' ');
      }
      plen = Teams.lineup[3].length;
      while (plen--) {
        sp.append(Pls[Teams.lineup[3][plen]].last_name + ' ');
     }
      plen = Teams.lineup[4].length;
      while (plen--) {
        sp.append(Pls[Teams.lineup[4][plen]].last_name + ' ');
      }
     }
        var currentw = $('.js-list-gw option:nth-last-child(2)').val();
      if (typeof currentw === 'undefined') {
         currentw = $('.js-gw-select option:nth-last-child(2)').val();
      }

          if(GW==1)
     {
      for (i = 1; i <= currentw; i++)
      {
        numc = ListChanges(teamid, i, i + 1, sp);
      }
     }


       $(box).append(spb);
       $(box).append(sp);
    }
  });
}
function compareTeams(teamid, gwfrom, gwto, box)
{
  var numchanges = 0; //load team from both gameweeks
  var Team1 = null;
  var Team2 = null;
  $.ajax({
    async: true,
    url: 'https://backend-nuk.fanhubmedia.com.au/api/season_team/show?gw=' + gwfrom + '&id=' + teamid + '&sid=' + sid,
    dataType: 'json',
    success: function (data)
    {
      Team1 = data.result;
      $.ajax({
        async: true,
        url: 'https://backend-nuk.fanhubmedia.com.au/api/season_team/show?gw=' + gwto + '&id=' + teamid + '&sid=' + sid,
        dataType: 'json',
        success: function (data)
        {
          Team2 = data.result;
          //put teams into arrays
          var Lineup1 = [
          ];
          var Lineup2 = [
          ];
          var plen = 0;
          Lineup1.push(Team1.lineup[1][0]);
          plen = Team1.lineup[2].length;
          while (plen--) {
            Lineup1.push([Team1.lineup[2][plen]][0]);
          }
          plen = Team1.lineup[3].length;
          while (plen--) {
            Lineup1.push([Team1.lineup[3][plen]][0]);
          }
          plen = Team1.lineup[4].length;
          while (plen--) {
            Lineup1.push([Team1.lineup[4][plen]][0]);
          }
          plen = 0;
          Lineup2.push(Team2.lineup[1][0]);
          plen = Team2.lineup[2].length;
          while (plen--) {
            Lineup2.push([Team2.lineup[2][plen]][0]);
          }
          plen = Team2.lineup[3].length;
          while (plen--) {
            Lineup2.push([Team2.lineup[3][plen]][0]);
          }
          plen = Team2.lineup[4].length;
          while (plen--) {
            Lineup2.push([Team2.lineup[4][plen]][0]);
          }
          console.log(Lineup1); //compare gk
          console.log(Lineup2); //compare gk
          for (i = 0; i < Lineup1.length; i++) {
            var found = false;
            for (j = 0; j < Lineup2.length; j++) {
              if (Lineup1[i] == Lineup2[j]) {
                found = true;
              }
            }
            if (!found) numchanges++;
          }
          console.log(numchanges);
          $(box).append('<p>Week ' + gwfrom + ' to ' + gwto + ' ' + numchanges + ' transfers used');
        }
      });
    }
  });
  return numchanges;
}
function ListChanges(teamid, gwfrom, gwto, box)
{
  var numchanges = 0; //load team from both gameweeks
  var Team1 = null;
  var Team2 = null;
  $.ajax({
    async: true,
    url: 'https://backend-nuk.fanhubmedia.com.au/api/season_team/show?gw=' + gwfrom + '&id=' + teamid + '&sid=' + sid,
    dataType: 'json',
    success: function (data)
    {
      Team1 = data.result;
      $.ajax({
        async: true,
        url: 'https://backend-nuk.fanhubmedia.com.au/api/season_team/show?gw=' + gwto + '&id=' + teamid + '&sid=' + sid,
        dataType: 'json',
        success: function (data)
        {
          var trans = '';
          Team2 = data.result;
          //put teams into arrays
          var Lineup1 = [
          ];
          var Lineup2 = [
          ];
          var plen = 0;
          Lineup1.push(Team1.lineup[1][0]);
          plen = Team1.lineup[2].length;
          while (plen--) {
            Lineup1.push([Team1.lineup[2][plen]][0]);
          }
          plen = Team1.lineup[3].length;
          while (plen--) {
            Lineup1.push([Team1.lineup[3][plen]][0]);
          }
          plen = Team1.lineup[4].length;
          while (plen--) {
            Lineup1.push([Team1.lineup[4][plen]][0]);
          }
          plen = 0;
          Lineup2.push(Team2.lineup[1][0]);
          plen = Team2.lineup[2].length;
          while (plen--) {
            Lineup2.push([Team2.lineup[2][plen]][0]);
          }
          plen = Team2.lineup[3].length;
          while (plen--) {
            Lineup2.push([Team2.lineup[3][plen]][0]);
          }
          plen = Team2.lineup[4].length;
          while (plen--) {
            Lineup2.push([Team2.lineup[4][plen]][0]);
          }
          console.log(Lineup1); //compare gk
          console.log(Lineup2); //compare gk
          var len = 0;
          var diff = [
          ];
          var ij = 0;
          diff = arr_diff(Lineup2, Lineup1);
       /*   if (gwfrom == 1)
          {
            //put week 1 lineup
            trans = 'Week1 Lineup ';
            for (ij = 0, len = Lineup1.length; ij < len; ij++) {
             trans += Pls[Lineup1[ij]].last_name + ' ';
            }
          }*/
          if (diff.length === 0) {
            return 0;
          }
          for (ij = 0, len = diff.length; ij < len; ij++) {
            trans += Pls[diff[ij]].last_name + ' ';
          }
          console.log(diff);
          trans += 'to ';
          diff = arr_diff(Lineup1, Lineup2);
          for (ij = 0, len = diff.length; ij < len; ij++) {
            trans += Pls[diff[ij]].last_name + ' ';
          }
          console.log(diff);
          console.log(trans);
          $(box).append('<p>Week ' + gwfrom + ' to ' + gwto + ' ' + trans);
        }
      });
    }
  });
  return numchanges;
}
function arr_diff(a1, a2) {
  var foo = [
  ];
  var i = 0;
  jQuery.grep(a2, function (el) {
    if (jQuery.inArray(el, a1) == - 1) foo.push(el);
    i++;
  });
  return foo;
}