// ==UserScript==
// @name TMVN Home HeadToHead
// @namespace https://trophymanager.com
// @version 2
// @description Trophymanager: check head to head record of any 2 clubs, including the club that ceased to exists
// @match https://trophymanager.com/home
// @match https://trophymanager.com/home/
// @grant none
// @downloadURL none
// ==/UserScript==
(function () {
'use strict';
const APPLICATION_COLOR = {
FIRST_CLUB: 'Darkred',
SECOND_CLUB: 'Blue',
TITLE: 'Yellow',
SEASON: 'Yellow'
}
const MATCH_TYPE = {
LEAGUE: 'League ',
CUP: 'Cup ',
INTER_CUP: 'International Cup ',
FRIEND: 'Friend ',
FRIEND_LEAGUE: 'Friend League '
}
var leagueRecord = {
firstClubWinCount: 0,
secondClubWinCount: 0,
drawCount: 0,
firstClubGoal: 0,
secondClubGoal: 0
}
var cupRecord = {
firstClubWinCount: 0,
secondClubWinCount: 0,
drawCount: 0,
firstClubGoal: 0,
secondClubGoal: 0
}
var interCupRecord = {
firstClubWinCount: 0,
secondClubWinCount: 0,
drawCount: 0,
firstClubGoal: 0,
secondClubGoal: 0
}
var friendRecord = {
firstClubWinCount: 0,
secondClubWinCount: 0,
drawCount: 0,
firstClubGoal: 0,
secondClubGoal: 0
}
var friendLeagueRecord = {
firstClubWinCount: 0,
secondClubWinCount: 0,
drawCount: 0,
firstClubGoal: 0,
secondClubGoal: 0
}
var matchMap = new Map();
var sortMap = new Map();
if ($('.banner_placeholder.rectangle').length > 0) {
$('.banner_placeholder.rectangle').remove();
}
$('.column2_a')[0].innerHTML += '
';
let inputFirstClubId = document.createElement("span");
inputFirstClubId.style = "display: inline-block;";
inputFirstClubId.innerHTML = ' ';
let inputSecondClubId = document.createElement("span");
inputSecondClubId.style = "display: inline-block;";
inputSecondClubId.innerHTML = ' ';
let btnCheck = document.createElement("span");
btnCheck.id = "tm_script_button_check";
btnCheck.className = "button";
btnCheck.style = "margin-left: 3px;";
btnCheck.innerHTML = 'Check ';
let divInput = $('#tm_script_head_to_head_input_area_id')[0];
divInput.insertBefore(inputSecondClubId, divInput.firstChild);
divInput.insertBefore(inputFirstClubId, divInput.firstChild);
let divButton = $('#tm_script_head_to_head_button_area_id')[0];
divButton.insertBefore(btnCheck, divButton.firstChild);
document.getElementById('tm_script_button_check').addEventListener('click', (e) => {
checkHeadToHead();
});
function checkHeadToHead() {
$.ajaxSetup({
async: false
});
let firstClubId,
secondClubId;
firstClubId = $('#tm_script_first_club_id')[0].value;
secondClubId = $('#tm_script_second_club_id')[0].value;
if (firstClubId == '' || secondClubId == '') {
alert('Please input club\'s id');
return;
}
firstClubId = firstClubId.trim();
secondClubId = secondClubId.trim();
if (isNaN(firstClubId) || isNaN(secondClubId)) {
alert('Club\'s id must be a number');
return;
}
let firstClubName,
secondClubName;
firstClubName = getClubName(firstClubId);
secondClubName = getClubName(secondClubId);
if (firstClubName == '' || secondClubName == '') {
alert('Not found club. Please check club\'s id again.');
return;
}
let noMatchFound = false;
let headToHeadUrl = 'https://trophymanager.com/ajax/match_h2h.ajax.php?home_team=' + firstClubId + '&away_team=' + secondClubId;
$.ajax(headToHeadUrl, {
type: "GET",
dataType: 'json',
crossDomain: true,
success: function (response) {
let matches = response.matches;
if (matches.length == 0) {
noMatchFound = true;
} else {
resetObject(leagueRecord);
resetObject(cupRecord);
resetObject(friendRecord);
resetObject(friendLeagueRecord);
resetObject(interCupRecord);
matchMap = new Map();
sortMap = new Map();
Object.keys(matches).forEach(function (key, index) {
let seasonArr = matches[key];
for (let i = 0; i < seasonArr.length; i++) {
let match = seasonArr[i];
if (match.matchtype == 'l') {
statistic(match, firstClubId, leagueRecord);
} else if (match.matchtype.startsWith('p')) {
statistic(match, firstClubId, cupRecord);
} else if (match.matchtype == 'f') {
statistic(match, firstClubId, friendRecord);
} else if (match.matchtype == 'fl') {
statistic(match, firstClubId, friendLeagueRecord);
} else {
statistic(match, firstClubId, interCupRecord);
}
}
});
}
},
error: function (e) {}
});
var headToHead_content = "";
headToHead_content += '#1: ' + firstClubName + ' ';
headToHead_content += '#2: ' + secondClubName + ' ';
if (noMatchFound) {
headToHead_content += 'No match found ';
} else {
let firstClubWinCount = 0,
secondClubWinCount = 0,
drawCount = 0,
firstClubGoal = 0,
secondClubGoal = 0;
let firstClubWinRatio,
secondClubWinRatio,
drawRatio;
let firstClubGoalAvg,
secondClubGoalAvg;
firstClubWinCount = leagueRecord.firstClubWinCount + cupRecord.firstClubWinCount + friendRecord.firstClubWinCount + friendLeagueRecord.firstClubWinCount + interCupRecord.firstClubWinCount;
secondClubWinCount = leagueRecord.secondClubWinCount + cupRecord.secondClubWinCount + friendRecord.secondClubWinCount + friendLeagueRecord.secondClubWinCount + interCupRecord.secondClubWinCount;
drawCount = leagueRecord.drawCount + cupRecord.drawCount + friendRecord.drawCount + friendLeagueRecord.drawCount + interCupRecord.drawCount;
firstClubGoal = leagueRecord.firstClubGoal + cupRecord.firstClubGoal + friendRecord.firstClubGoal + friendLeagueRecord.firstClubGoal + interCupRecord.firstClubGoal;
secondClubGoal = leagueRecord.secondClubGoal + cupRecord.secondClubGoal + friendRecord.secondClubGoal + friendLeagueRecord.secondClubGoal + interCupRecord.secondClubGoal;
firstClubWinRatio = Math.round(firstClubWinCount / (firstClubWinCount + drawCount + secondClubWinCount) * 100);
if (drawCount == 0) {
secondClubWinRatio = 100 - firstClubWinRatio;
drawRatio = 0;
} else {
secondClubWinRatio = Math.round(secondClubWinCount / (firstClubWinCount + drawCount + secondClubWinCount) * 100);
drawRatio = 100 - firstClubWinRatio - secondClubWinRatio;
}
firstClubGoalAvg = Math.round(firstClubGoal / (firstClubWinCount + drawCount + secondClubWinCount));
secondClubGoalAvg = Math.round(secondClubGoal / (firstClubWinCount + drawCount + secondClubWinCount));
headToHead_content += 'All ';
headToHead_content += '#1 - Draw - #2: ' + firstClubWinCount + ' - ' + drawCount + ' - ' + secondClubWinCount + ' [' + firstClubWinRatio + '% - ' + drawRatio + '% - ' + secondClubWinRatio + '%]' + ' ';
headToHead_content += 'Goal #1 - #2: ' + firstClubGoal + ' - ' + secondClubGoal + ' ';
headToHead_content += 'Goal average: ' + firstClubGoalAvg + ' - ' + secondClubGoalAvg + ' ';
if (leagueRecord.firstClubWinCount + leagueRecord.drawCount + leagueRecord.secondClubWinCount > 0) {
headToHead_content += 'League ';
headToHead_content = setRecord(headToHead_content, leagueRecord);
}
if (cupRecord.firstClubWinCount + cupRecord.drawCount + cupRecord.secondClubWinCount > 0) {
headToHead_content += 'Cup ';
headToHead_content = setRecord(headToHead_content, cupRecord);
}
if (interCupRecord.firstClubWinCount + interCupRecord.drawCount + interCupRecord.secondClubWinCount > 0) {
headToHead_content += 'Internaltional Cup ';
headToHead_content = setRecord(headToHead_content, interCupRecord);
}
if (friendLeagueRecord.firstClubWinCount + friendLeagueRecord.drawCount + friendLeagueRecord.secondClubWinCount > 0) {
headToHead_content += 'Friend League ';
headToHead_content = setRecord(headToHead_content, friendLeagueRecord);
}
if (friendRecord.firstClubWinCount + friendRecord.drawCount + friendRecord.secondClubWinCount > 0) {
headToHead_content += 'Friend ';
headToHead_content = setRecord(headToHead_content, friendRecord);
}
sortMap[Symbol.iterator] = function * () {
yield * [...this.entries()].sort((a, b) => b[1] - a[1]);
}
headToHead_content += 'Match: ';
for (let[key, value]of sortMap) {
let match = matchMap.get(key);
if (match.hometeam == '#1') {
if (match.homegoal > match.awaygoal) {
headToHead_content += '[' + match.hometeam + '] ' + match.homegoal + ' - ' + match.awaygoal + ' [' + match.awayteam + '] ' + ' [' + match.matchtype + ' ] [' + match.season + '] [' + match.date + '] ';
} else if (match.homegoal < match.awaygoal) {
headToHead_content += '[' + match.hometeam + '] ' + match.homegoal + ' - ' + match.awaygoal + ' [' + match.awayteam + '] ' + ' [' + match.matchtype + ' ] [' + match.season + '] [' + match.date + '] ';
} else {
headToHead_content += '[' + match.hometeam + '] ' + match.homegoal + ' - ' + match.awaygoal + ' [' + match.awayteam + '] ' + ' [' + match.matchtype + ' ] [' + match.season + '] [' + match.date + '] ';
}
} else {
if (match.homegoal > match.awaygoal) {
headToHead_content += '[' + match.hometeam + '] ' + match.homegoal + ' - ' + match.awaygoal + ' [' + match.awayteam + '] ' + ' [' + match.matchtype + ' ] [' + match.season + '] [' + match.date + '] ';
} else if (match.homegoal < match.awaygoal) {
headToHead_content += '[' + match.hometeam + '] ' + match.homegoal + ' - ' + match.awaygoal + ' [' + match.awayteam + '] ' + ' [' + match.matchtype + ' ] [' + match.season + '] [' + match.date + '] ';
} else {
headToHead_content += '[' + match.hometeam + '] ' + match.homegoal + ' - ' + match.awaygoal + ' [' + match.awayteam + '] ' + ' [' + match.matchtype + ' ] [' + match.season + '] [' + match.date + '] ';
}
}
}
}
headToHead_content += "
";
$("#tm_script_head_to_head_result_area_id")[0].innerText = '';
$("#tm_script_head_to_head_result_area_id").append(headToHead_content);
$.ajaxSetup({
async: true
});
}
function statistic(match, firstClubId, record) {
let result = match.result.split('-');
if (match.hometeam == firstClubId) {
record.firstClubGoal += parseInt(result[0]);
record.secondClubGoal += parseInt(result[1]);
} else {
record.secondClubGoal += parseInt(result[0]);
record.firstClubGoal += parseInt(result[1]);
}
if (result[0] == result[1]) {
record.drawCount++;
} else if (result[0] > result[1]) {
if (match.hometeam == firstClubId) {
record.firstClubWinCount++;
} else {
record.secondClubWinCount++;
}
} else {
if (match.hometeam == firstClubId) {
record.secondClubWinCount++;
} else {
record.firstClubWinCount++;
}
}
let matchType = '';
if (record == leagueRecord) {
matchType = MATCH_TYPE.LEAGUE;
} else if (record == cupRecord) {
matchType = MATCH_TYPE.CUP;
} else if (record == friendRecord) {
matchType = MATCH_TYPE.FRIEND;
} else if (record == friendLeagueRecord) {
matchType = MATCH_TYPE.FRIEND_LEAGUE;
} else {
matchType = MATCH_TYPE.INTER_CUP;
}
let hometeam,
awayteam;
if (match.hometeam == firstClubId) {
hometeam = '#1';
awayteam = '#2';
} else {
hometeam = '#2';
awayteam = '#1';
}
matchMap.set(match.id, {
'id': match.id,
'matchtype': matchType,
'season': '' + match.season + ' ',
'date': match.date,
'hometeam': hometeam,
'awayteam': awayteam,
'homegoal': result[0],
'awaygoal': result[1]
});
sortMap.set(match.id, new Date(match.date));
}
function setRecord(headToHead_content, record) {
let firstClubGoalAvg,
secondClubGoalAvg;
let firstClubWinRatio,
secondClubWinRatio,
drawRatio;
firstClubGoalAvg = Math.round(record.firstClubGoal / (record.firstClubWinCount + record.drawCount + record.secondClubWinCount));
secondClubGoalAvg = Math.round(record.secondClubGoal / (record.firstClubWinCount + record.drawCount + record.secondClubWinCount));
firstClubWinRatio = Math.round(record.firstClubWinCount / (record.firstClubWinCount + record.drawCount + record.secondClubWinCount) * 100);
if (record.drawCount == 0) {
secondClubWinRatio = 100 - firstClubWinRatio;
drawRatio = 0;
} else {
secondClubWinRatio = Math.round(record.secondClubWinCount / (record.firstClubWinCount + record.drawCount + record.secondClubWinCount) * 100);
drawRatio = 100 - firstClubWinRatio - secondClubWinRatio;
}
headToHead_content += '#1 - Draw - #2: ' + record.firstClubWinCount + ' - ' + record.drawCount + ' - ' + record.secondClubWinCount + ' [' + firstClubWinRatio + '% - ' + drawRatio + '% - ' + secondClubWinRatio + '%]' + ' ';
headToHead_content += 'Goal #1 - #2: ' + record.firstClubGoal + ' - ' + record.secondClubGoal + ' ';
headToHead_content += 'Goal average: ' + firstClubGoalAvg + ' - ' + secondClubGoalAvg + ' ';
return headToHead_content;
}
function resetObject(obj) {
for (var key in obj) {
obj[key] = 0;
}
}
function getClubName(clubId) {
let clubName = '';
$.ajax('https://trophymanager.com/club/' + clubId, {
type: "GET",
dataType: 'html',
crossDomain: true,
success: function (response) {
let element = $('.column2_a div.box_sub_header.align_center a[club_link="' + clubId + '"]', response)[0];
if (element) {
clubName = element.innerText;
} else {
clubName = clubId; //club doesn't exists
}
},
error: function (e) {}
});
return clubName;
}
})();