jpk100 / DT16

// ==UserScript==
// @name        DT16
// @namespace   123
// @include     https://www.dreamteamfc.com/g/*
// @version     1.13
// @require     https://www.dreamteamfc.com/g/js/app.min.js
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant       none
// ==/UserScript==
console.log('waiting')
var Players;
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(leagueid)
}
if (leagueid != '0')
{ 
  $.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;
    }
  });
}
$.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(Pls)
  }
});
setTimeout(function ()
{
  console.log('10 seconds')
  console.log($('.clearfix.team-hub-item.page-hub-item.js-go-to-view').length)
  $('.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);
    addLineup('https://backend-nuk.fanhubmedia.com.au/api/season_transfer/show_my?team_id=',box, teamid, sid);
  });
  //if league table showing
  console.log($('.ladder-table  > tbody  > tr').length) //$('#tblOne > tbody  > tr').each(function() 
  var cnt = 0
  console.log(ladder)
  var gw=$('.js-gw-select :selected').val()
  $('.ladder-table  > tbody  > tr').each(function (key, value) {
    var box = this
    var teamid = ladder[cnt].id;
    console.log(teamid);
    addLineup('https://backend-nuk.fanhubmedia.com.au/api/season_team/show?gw='+gw+'&id=',box, teamid, sid);
    cnt++;
  });
  $('.load-more-btn').click(function () {
    setTimeout(function ()
    {
      $('.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);
        addLineup(box, teamid, sid);
      });
    }, 2500);
  });
}, 10000);
function addLineup(my, box, teamid, sid)
{
  $.ajax({
    async: false,
    url: my + teamid + '&sid=' + sid,
    dataType: 'json',     
    success: function (data)
    {
    Teams = data.result;
      console.log(Teams)
      $(box).append(Pls[Teams.lineup[1]].last_name + ' ');
      var plen = Teams.lineup[2].length;
      while (plen--) {
        $(box).append(Pls[Teams.lineup[2][plen]].last_name + ' ')
      }
      plen = Teams.lineup[3].length;
      while (plen--) {
        $(box).append(Pls[Teams.lineup[3][plen]].last_name + ' ')
      }
      plen = Teams.lineup[4].length;
      while (plen--) {
        $(box).append(Pls[Teams.lineup[4][plen]].last_name + ' ')
      } // $(box).append(Teams.name)

    }
  });
}
var stylc = 'font-size : 80% !important; height:15px; padding: 1px 2px; text-align:center'

main()
function main() {
  $(document.body).append('<div id="helloDiv"></div>');
  $(document).ready(function () {
    console.log('ready!');
    var sid = 'https://backend-nuk.fanhubmedia.com.au/api/season_team/show_my?sid=' + APP.getJStorage('session_id')
    console.log(sid) //gets the teams and stores them
       console.log( APP.User) //gets the teams and stores them
 var Players = null
    var Teams = null    //  console.log( $('#header-menu'))
    div = $('<div>');
    var table = $('<table></table>').attr('id', 'detailTable').attr('class','l10n-community-overview')
    var thead = $('<thead></thead>')
    $('<th></th>').attr('style', stylc).text('ID').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('NAME').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('Points').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('1').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('2').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('3').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('4').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('5').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('6').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('7').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('8').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('9').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('10').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('11').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('Value').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('Budget').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('StartGW').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('In Leagues').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('League names').appendTo(thead);
    $('<th></th>').attr('style', stylc).text('Transfers Left').appendTo(thead);
    table.append(thead);
    var tbody= $('<tbody></tbody>');
        table.append(tbody)

      $('body').prepend(table);

  var x=0
  
  while(x<=100)
    {
    $.ajax({
      async: false,
      url: sid+'&offset=' + x.toString(),
      dataType: 'json',
      success: function (data)
      {
        Teams = data.result;
        $.ajax({
          url: 'https://nuk-data.s3-eu-west-1.amazonaws.com/json/players.json',
          dataType: 'json',
          success: function (data)
          {
            Players = data;
            var Pls = new Object
            $.each(Players, function (i, player) {
              Pls[player.id] = player
            })
            console.log(Teams)
            console.log(Players)
            $.each(Teams, function (i, team) {
              var row = $('<tr id="row_' + i + '"></tr>').attr('style', stylc);
              //https://www.dreamteamfc.com/g/#season/team-hub/team/show/1821500
              //  $('<td></td>').html('<a href=https://www.dreamteamfc.com' + eur + '/team/view/' + i + '>' + i + '</a>').attr('style', stylc).appendTo(row);
              $('<td></td>').html('<a href=/g/#season/team-hub/team/show/' + team.id + '>' + team.id+'</a>').attr('style', stylc).appendTo(row);
              $('<td></td>').html(team.name).attr('style', stylc).appendTo(row);
              $('<td></td>').html(team.points).attr('style', stylc).appendTo(row);
              $('<td></td>').html(Pls[team.lineup[1]].last_name).attr('style', stylc).appendTo(row);
              var plen = team.lineup[2].length;
              while (plen--) {
                $('<td></td>').html(Pls[team.lineup[2][plen]].last_name).attr('style', stylc).appendTo(row);
              }
              plen = team.lineup[3].length;
              while (plen--) {
                $('<td></td>').html(Pls[team.lineup[3][plen]].last_name).attr('style', stylc).appendTo(row);
              }
              plen = team.lineup[4].length;
              while (plen--) {
                $('<td></td>').html(Pls[team.lineup[4][plen]].last_name).attr('style', stylc).appendTo(row);
              }
              $('<td></td>').html(team.value/1000000).attr('style', stylc).appendTo(row);
              $('<td></td>').html(team.salary_cap/1000000).attr('style', stylc).appendTo(row);
              $('<td></td>').html(team.start_gw).attr('style', stylc).appendTo(row);
              $('<td></td>').html(team.num_leagues).attr('style', stylc).appendTo(row);
              $('<td></td>').html(team.league_name).attr('style', stylc).appendTo(row);
              $('<td></td>').html(team.month_transfers_left).attr('style', stylc).appendTo(row);
              table.append(row)
            })
          },
          error: function (jqXHR, textStatus, errorThrown)
          {
            console.log(jqXHR)
            console.log(errorThrown);
          }
        });
      },
      error: function (jqXHR, textStatus, errorThrown)
      {
        console.log(jqXHR)
        console.log(errorThrown);
      }
    });
 x=x+10
    }
 
    
    $('th').click(function(){
    var table = $(this).parents('table').eq(0)
    var rows = table.find('tr:gt(0)').toArray().sort(comparer($(this).index()))
    this.asc = !this.asc
    if (!this.asc){rows = rows.reverse()}
    for (var i = 0; i < rows.length; i++){table.append(rows[i])}
})
function comparer(index) {
    return function(a, b) {
        var valA = getCellValue(a, index), valB = getCellValue(b, index)
        return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.localeCompare(valB)
    }
}
function getCellValue(row, index){ return $(row).children('td').eq(index).html() }
    var View = APP.module('View')
    var _MYteams = View.TeamHub.getTeamPlayers()
  });
}