// ==UserScript==
// @name MatchResult_GoalScorers
// @namespace https://greasyfork.org/users/562206
// @version 0.0.6
// @description View match result in advance (under 50 minutes before the match start)
// @author Jhonatan Bianchi
// @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);
}
Object.filter = (obj, predicate) =>
Object.keys(obj)
.filter(key => predicate(obj[key]))
.reduce((res, key) => (res[key] = obj[key], res), {});
function getEventImage(match, event) {
let src = '';
if (getEventIs(event, 'goal')) {
src = 'https://trophymanager.com/pics/icons/ball.gif';
}
if (getEventIs(event, 'yellow')) {
src = 'https://trophymanager.com/pics/icons/yellow_card.gif';
}
if (getEventIs(event, 'red')) {
src = 'https://trophymanager.com/pics/icons/red_card.gif';
}
if (getEventIs(event, 'injury')) {
src = 'https://trophymanager.com/pics/icons/injury.gif';
}
const playerId = getPlayerIdFromEvent(event);
if (isPlayerFromHomeTeam(match, playerId)) {
return '';
} else {
return '
'
}
}
function getEventIs(event, eventNameToCheck) {
return event[0].parameters.filter(e => e[eventNameToCheck]).length;
}
function getGoalDetailsFromGoalEvent(goalEvent) {
return goalEvent[0].parameters.filter(e => e.goal)[0].goal;
}
function getMatchCurrentScoreAfterGoalEvent(goalEvent) {
const goal = getGoalDetailsFromGoalEvent(goalEvent);
if (goal) {
return {
homeGoals: goal.score[0],
awayGoals: goal.score[1],
}
}
}
function isPlayerFromHomeTeam(match, playerId) {
return Object.keys(match.lineup.home).includes(playerId);
}
function getPlayerName(match, playerId) {
return isPlayerFromHomeTeam(match, playerId) ? match.lineup.home[playerId].name : match.lineup.away[playerId].name;
}
function eventIsGoalCardInjury(event) {
return event.parameters && event.parameters.filter(event => event.goal || event.yellow || event.red || event.injury).length;
}
function getWantedEvents(match) {
return Object.filter(match.report, (event) => {
return event.filter(e => {
return eventIsGoalCardInjury(e);
}).length;
})
}
function getGoalsEvents(match) {
return Object.filter(match.report, (event) => {
return event.filter(e => {
return e.parameters && e.parameters.filter(e => e.goal).length;
}).length;
})
}
function getMatchResult(match) {
const goalsEvents = getGoalsEvents(match);
if (Object.keys(goalsEvents).length === 0) {
return {
homeGoals: 0,
awayGoals: 0
};
}
const { [Object.keys(goalsEvents).pop()]: lastGoal } = goalsEvents;
return getMatchCurrentScoreAfterGoalEvent(lastGoal);
}
function getHomeTeamName(match) {
return match.club.home.club_name;
}
function getAwayTeamName(match) {
return match.club.away.club_name;
}
function getPlayerIdFromEvent(event) {
if (getEventIs(event, 'goal')) {
return event[0].parameters.filter(e => e.goal)[0].goal.player;
}
if (getEventIs(event, 'yellow')) {
return event[0].parameters.filter(e => e.yellow)[0].yellow;
}
if (getEventIs(event, 'red')) {
return event[0].parameters.filter(e => e.red)[0].red;
}
if (getEventIs(event, 'injury')) {
return event[0].parameters.filter(e => e.injury)[0].injury;
}
}
function getFinalResultHtml(match) {
const matchResult = getMatchResult(match);
return '