// ==UserScript==
// @name MatchResult-Review Script
// @namespace https://greasyfork.org/users/562206
// @version 0.0.1
// @description View match result in advance (under 50 minutes before the match start)
// @author TM supporter (unkown) + Jhonatan Bianchi (Goal scorers)
// @include http://trophymanager.com/matches/*
// @include https://trophymanager.com/matches/*
// @downloadURL none
// ==/UserScript==
function insertBefore(el, referenceNode) {
referenceNode.parentNode.insertBefore(el, referenceNode);
}
function insertAfter(el, referenceNode) {
referenceNode.parentNode.insertBefore(el, referenceNode.nextSibling);
}
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;
var goalScorerId;
var goalFound = false;
if (paramArr) {
for (var j = 0; j < paramArr.length; j++) {
var paramObj = paramArr[j];
if (paramObj.goal) {
goalsReport.push({
minute: key,
scorer: paramObj.goal.player
});
}
}
}
}
});
return goalsReport;
}
function getGoalsScorers(homeGoals, awayGoals) {
var goals = [];
homeGoals.forEach(function (goal) {
goal.minute.forEach(function (minute) {
minute = minute.replace(/\D/g,'');
goals.push({
minute: minute,
scorerName: goal.scorer.name,
home: true
});
});
});
awayGoals.forEach(function (goal) {
goal.minute.forEach(function (minute) {
minute = minute.replace(/\D/g,'');
goals.push({
minute: minute,
scorerName: goal.scorer.name,
home: false
});
});
});
goals.sort((a,b) => a.minute-b.minute )
return goals;
}
function formatGoalScorersReport(goalScorers) {
var homeGoals = 0;
var awayGoals = 0;
var html = '';
goalScorers.forEach(function (goal) {
console.log(goal);
if (goal.home == true) {
homeGoals++;
html += '
';
html += '
' + goal.scorerName + '
';
html += '
' + homeGoals + ' - ' + awayGoals + '
';
html += '
';
} else {
awayGoals++;
html += '';
html += '
';
html += '
' + homeGoals + ' - ' + awayGoals + '
';
html += '
' + goal.scorerName + '
';
html += '
';
}
});
html += '';
return html;
}
function mergeMinutes(data) {
var seen = {};
data = data.filter(function(entry) {
var previous;
if (seen.hasOwnProperty(entry.scorer)) {
previous = seen[entry.scorer];
previous.minute.push(' '+ entry.minute + '\'');
return false;
}
if (!Array.isArray(entry.minute)) {
entry.minute = [entry.minute + '\''];
}
seen[entry.scorer] = entry;
return true;
});
return data;
}
function formatReport(goalsRp, homeLineup, awayLineup) {
var rps = mergeMinutes(goalsRp);
var homeReport = [];
var awayReport = [];
rps.forEach(function (rp) {
if (homeLineup.hasOwnProperty(rp.scorer)) {
rp.scorer = homeLineup[rp.scorer];
homeReport.push(rp);
}
if (awayLineup.hasOwnProperty(rp.scorer)) {
rp.scorer = awayLineup[rp.scorer];
awayReport.push(rp);
}
});
return {
homeReport: homeReport,
awayReport: awayReport
}
}
function showMatchResult() {
var resultDiv = document.createElement('div');
var matchID = location.href.match(/([^\/]*)\/*$/)[1];
var xhr = new XMLHttpRequest();
var url = 'https://trophymanager.com/ajax/match.ajax.php?id=' + matchID;
resultDiv.className = 'main_center';
xhr.open('GET', url, true);
xhr.send();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(this.responseText);
var homeClub = data.club.home.club_name;
var awayClub = data.club.away.club_name;
var report = data.report;
var homeLineup = data.lineup.home;
var awayLineup = data.lineup.away;
var scoreData = report[Object.keys(report).sort().pop()];
var finalText = scoreData[0].chance.text[0];
var finalScore = 'vs';
var goalsReport = [];
var goalsRpDiv = '';
var homeGoalDiv = '
'
+'
';
var awayGoalDiv = ''
+ '
';
var centerDiv = '';
var homeGoals = [];
var awayGoals = [];
var goalsScorers = [];
var homeScores = 0;
var awayScores = 0;
if (true) {
goalsReport = getGoalsReport(report);
var finalReport = formatReport(goalsReport, homeLineup, awayLineup);
homeGoals = finalReport.homeReport;
awayGoals = finalReport.awayReport;
homeGoals.forEach(function (obj) {
homeScores += parseInt(obj.minute.length);
});
awayGoals.forEach(function (obj) {
awayScores += parseInt(obj.minute.length);
});
finalScore = homeScores + ' - ' + awayScores;
goalsScorers = getGoalsScorers(homeGoals, awayGoals);
}
var htmlTxt = '';
htmlTxt += '
' + homeClub + '
'
+ '
' + finalScore + '
'
+ '
' + awayClub + '
'
resultDiv.innerHTML = htmlTxt + goalsRpDiv;
var mainCenters = document.getElementsByClassName('main_center');
var lastMainDiv = mainCenters[mainCenters.length - 1];
var goalsScorersDiv = formatGoalScorersReport(goalsScorers);
if (lastMainDiv) {
resultDiv.innerHTML = resultDiv.innerHTML + goalsScorersDiv;
}
if (lastMainDiv) {
insertBefore(resultDiv, lastMainDiv);
}
}
};
}
(function() {
'use strict';
showMatchResult();
})();