// ==UserScript== // @name TMVN League ALI // @namespace https://trophymanager.com // @version 4 // @description Trophymanager: calculate ALI (Average League Indicator) and the number of championships by teams. ALI show the average rank/division of the team. The smaller the ALI, the better the team's league record. // @match https://trophymanager.com/league/* // @grant none // @downloadURL none // ==/UserScript== (function () { 'use strict'; const APP_COLOR = { LEVEL_1: "Darkred", LEVEL_2: "Black", LEVEL_3: "Yellow", LEVEL_4: "Blue", LEVEL_5: "Aqua", LEVEL_6: "White" }; const CONTROL_ID = { INPUT_SEASON_COUNT: 'tmvn_league_ali_input_season_count', BUTTON_SEASON_COUNT: 'tmvn_league_ali_button_season_count_set', } const APPLICATION_PARAM = { DEFAULT_SEASON_COUNT: 0, SEASON_COUNT_LOCAL_STORAGE_KEY: "TMVN_LEAGUE_ALI_SEASON_COUNT" } var seasonCount = localStorage.getItem(APPLICATION_PARAM.SEASON_COUNT_LOCAL_STORAGE_KEY); if (seasonCount == null || seasonCount == "") { seasonCount = APPLICATION_PARAM.DEFAULT_SEASON_COUNT; } try { $('.banner_placeholder.rectangle')[0].parentNode.removeChild($('.banner_placeholder.rectangle')[0]); } catch (err) {} var clubLeagueMap = new Map(); var clubMap = new Map(); $('#overall_table td').each(function () { let clubId = $(this).children('a').attr('club_link'); if (clubId) { let clubName = $(this).children('a')[0].innerHTML; clubMap.set(clubId, clubName); } }); clubMap.forEach((value, key) => { $.ajax('https://trophymanager.com/history/club/league/' + key, { type: "GET", dataType: 'html', crossDomain: true, success: function (response) { var trArr = $('.zebra.hover.sortable tr', response); var totalAli = 0; var count = 0; var championMap = new Map(); for (var i = 1; i < trArr.length; i++) { var rank = trArr[i].lastElementChild.innerText; if (rank !== "") { count++; var division = trArr[i].children[1].innerText.substring(0, 1); if (rank == 1) { if (championMap.has(division)) { var championCount = championMap.get(division); championCount++; championMap.set(division, championCount); } else { championMap.set(division, 1); } } totalAli = totalAli + 18 * (parseInt(division) - 1) + parseInt(rank); if (seasonCount > 0 && seasonCount == count) { break; } } } var champion = ""; if (championMap.size > 0) { var championDivision = [...championMap.keys()].sort(); championDivision.forEach((div) => { if (div == 1) { champion += ("" + div + "." + championMap.get(div) + " "); } else if (div == 2) { champion += ("" + div + "." + championMap.get(div) + " "); } else if (div == 3) { champion += ("" + div + "." + championMap.get(div) + " "); } else if (div == 4) { champion += ("" + div + "." + championMap.get(div) + " "); } else if (div == 5) { champion += ("" + div + "." + championMap.get(div) + " "); } else { champion += ("" + div + "." + championMap.get(div) + " "); } }); } var ali = ""; if (count > 0) { var aliAverage = Math.round(totalAli / count); var divisionAverage = Math.ceil(aliAverage / 18); var rankAverage = aliAverage - (18 * (divisionAverage - 1)); if (divisionAverage == 1) { ali = ("" + aliAverage + " (" + divisionAverage + "." + rankAverage + ") "); } else if (divisionAverage == 2) { ali = ("" + aliAverage + " (" + divisionAverage + "." + rankAverage + ") "); } else if (divisionAverage == 3) { ali = ("" + aliAverage + " (" + divisionAverage + "." + rankAverage + ") "); } else if (divisionAverage == 4) { ali = ("" + aliAverage + " (" + divisionAverage + "." + rankAverage + ") "); } else if (divisionAverage == 5) { ali = ("" + aliAverage + " (" + divisionAverage + "." + rankAverage + ") "); } else { ali = ("" + aliAverage + " (" + divisionAverage + "." + rankAverage + ") "); } } clubLeagueMap.set(key, { "ALI": ali, "Champion": champion, "SeasonCount": count }); }, error: function (e) {} }); }); var myInterval = setInterval(append, 1000); function append() { if (clubLeagueMap.size < 18) { return; } clearInterval(myInterval); /*APPEND ALI TABLE*/ let ali = "
" + "
" + "

Average League Indicator

" + "
" + "
" + "
" + "" + "Season Count" + "
" + "
" + "
" + "
" + "
" + "
"; $(".column3_a").append(ali); document.getElementById(CONTROL_ID.BUTTON_SEASON_COUNT).addEventListener('click', (e) => { setSeasonCount(); }); $('#' + CONTROL_ID.INPUT_SEASON_COUNT).val(seasonCount); let ali_content = "" + ""; let rowCount = 0; clubMap.forEach((value, key) => { rowCount++; let classOdd = ""; if ((rowCount % 2) == 1) { classOdd = "class='odd'"; } if (clubLeagueMap.has(key)) { let clubLeague = clubLeagueMap.get(key); value = '' + clubLeague.SeasonCount + '.' + value; ali_content += ""; } else { ali_content += ""; } }); ali_content += "
ClubALI#1
" + value + "" + clubLeague.ALI + "" + clubLeague.Champion + "
" + value + "
"; $("#ali_content").append(ali_content); } function setSeasonCount() { let seasonCount = $('#' + CONTROL_ID.INPUT_SEASON_COUNT)[0].value; let valid = true; if (seasonCount == '') { localStorage.removeItem(APPLICATION_PARAM.SEASON_COUNT_LOCAL_STORAGE_KEY); } else if (isNaN(seasonCount) || seasonCount < 0) { alert('Season count must be positive integer. Season count = 0 means all seasons.'); valid = false; } else { localStorage.setItem(APPLICATION_PARAM.SEASON_COUNT_LOCAL_STORAGE_KEY, seasonCount); } if (valid) { alert('Set successful, please refresh'); } } })();