// ==UserScript==
// @name TMVN League Result
// @version 1
// @description Trophymanager: get match's infos like possession, average rate, attendance, scorer... Include last round and next round (50 min before match).
// @namespace https://trophymanager.com
// @include https://trophymanager.com/league*
// @downloadURL none
// ==/UserScript==
(function () {
'use strict';
const lastRound = 'last_round_table';
const nextRound = 'next_round_table';
var clubMap = new Map();
var rankOrder = 0;
$('#overall_table td').each(function () {
var clubId = $(this).children('a').attr('club_link');
if (clubId) {
clubMap.set(clubId, ++rankOrder);
}
});
getMatchInfo(nextRound);
getMatchInfo(lastRound);
function parseMatchIds(tableId) {
var matchIds = [];
$('#' + tableId + ' td').each(function () {
var hrefVal = $(this).children('a').attr('href');
if (hrefVal) {
var matchID = hrefVal.substr(hrefVal.lastIndexOf('matches/') + 8, hrefVal.length - 10);
matchIds.push(matchID);
}
});
return matchIds;
}
function getGoalsReport(report) {
var goalsReport = [];
Object.keys(report).forEach(function (key, index) {
var minuteArr = report[key];
for (var i = 0; i < minuteArr.length; i++) {
var paramArr = minuteArr[i].parameters;
if (paramArr) {
for (var j = 0; j < paramArr.length; j++) {
var paramObj = paramArr[j];
if (paramObj.goal) {
goalsReport.push({
minute: key,
playerId: paramObj.goal.player
});
}
}
}
}
});
return goalsReport;
}
function getMatchInfo(tableId) {
var matchIds = parseMatchIds(tableId);
var scoreArr = [];
matchIds.forEach(function (matchId) {
var xhr = new XMLHttpRequest();
var url = 'https://trophymanager.com/ajax/match.ajax.php?id=' + matchId;
xhr.open('GET', url, true);
xhr.send();
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(this.responseText);
var report = data.report;
if (Object.keys(report).length <= 3) {
return; //because don't have datas of match
}
var matchData = data.match_data;
var attendance,
possession;
attendance = matchData.attendance;
if (attendance !== "") {
attendance = 'Attendance: ' + attendance.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
possession = 'Possession: ' + matchData.possession.home + " - " + matchData.possession.away;
var clubData = data.club;
var homeForm = "",
awayForm = "";
clubData.home.form.forEach((match) => {
homeForm += match.result;
});
clubData.away.form.forEach((match) => {
awayForm += match.result;
});
var performance = 'Form: ' + homeForm + " - " + awayForm;
var homeRank = clubMap.get(clubData.home.id);
var awayRank = clubMap.get(clubData.away.id);
var rank = 'Rank: ' + homeRank + " - " + awayRank;
var homeLineup = data.lineup.home;
var awayLineup = data.lineup.away;
var homePlayerIds = Object.getOwnPropertyNames(homeLineup);
var awayPlayerIds = Object.getOwnPropertyNames(awayLineup);
var homeRate = 0,
awayRate = 0,
homeRateCount = 0,
awayRateCount = 0;
var homePlayer = new Map(),
awayPlayer = new Map();
homePlayerIds.forEach((playerId) => {
homePlayer.set(playerId, homeLineup[playerId].name);
if (homeLineup[playerId].rating > 0) {
homeRate += homeLineup[playerId].rating;
homeRateCount++;
}
});
awayPlayerIds.forEach((playerId) => {
awayPlayer.set(playerId, awayLineup[playerId].name);
if (awayLineup[playerId].rating > 0) {
awayRate += awayLineup[playerId].rating;
awayRateCount++;
}
});
if (homeRateCount > 0) {
homeRate = (homeRate / homeRateCount).toFixed(1);
}
if (awayRateCount > 0) {
awayRate = (awayRate / awayRateCount).toFixed(1);
}
var rate = 'Rate: ' + homeRate + " - " + awayRate;
var goalsReport = getGoalsReport(report);
var score,
scorer = "
";
if (goalsReport.length == 0) {
score = 'Score: 0 - 0';
} else {
var homeGoal = 0,
awayGoal = 0;
goalsReport.forEach((goal) => {
if (homePlayer.has(goal.playerId)) {
homeGoal++;
scorer += "" + goal.minute + ". " + homePlayer.get(goal.playerId) + " |
";
} else {
awayGoal++;
scorer += "" + awayPlayer.get(goal.playerId) + " ." + goal.minute + " |
";
}
});
score = 'Score: ' + homeGoal + " - " + awayGoal + '';
scorer += "
";
}
//replace content
var replaceContent =
'' + score + ' |
' + rank + ' |
' + possession + ' |
' + attendance + ' |
' + rate + ' |
' + performance + ' |
';
if (goalsReport.length > 0) {
replaceContent += 'Scorer: |
';
replaceContent += '';
replaceContent += scorer;
replaceContent += ' |
';
}
replaceContent += '
';
$('#' + tableId + ' a[href="\/matches\/' + matchId + '\/"]')[0].parentElement.style.width = "60%";
$('#' + tableId + ' a[href="\/matches\/' + matchId + '\/"]')[0].parentElement.innerHTML = replaceContent;
}
};
});
}
})();