// ==UserScript==
// @name TMVN Club HeadToHead (Test)
// @namespace https://trophymanager.com
// @version 3
// @description Trophymanager: check head to head record of club with your team
// @include https://trophymanager.com/club/*
// @include https://trophymanager.com/club/*/
// @exclude https://trophymanager.com/club/
// @exclude https://trophymanager.com/club/*/squad/
// @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 ',
PLAYOFF: 'Playoff ',
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 playoffRecord = {
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();
var firstClubId,
secondClubId,
firstClubName,
secondClubName;
var checkClubInfoSuccess = false;
var checkClubInfoInterval = setInterval(checkClubInfo, 500);
var checkHeadToHeadInterval = setInterval(checkHeadToHead, 1000);
function checkHeadToHead() {
if (!checkClubInfoSuccess) {
return;
} else {
clearInterval(checkHeadToHeadInterval);
}
if (firstClubId == secondClubId) {
return;
}
$.ajaxSetup({
async: false
});
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(playoffRecord);
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('lq')) {
statistic(match, firstClubId, playoffRecord);
} 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) {}
});
let headToHead =
"
" +
"
" +
"
Head to Head " +
"" +
"
" +
"" +
"
";
$(".column3_a").append(headToHead);
let 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 + playoffRecord.firstClubWinCount + cupRecord.firstClubWinCount + friendRecord.firstClubWinCount + friendLeagueRecord.firstClubWinCount + interCupRecord.firstClubWinCount;
secondClubWinCount = leagueRecord.secondClubWinCount + playoffRecord.secondClubWinCount + cupRecord.secondClubWinCount + friendRecord.secondClubWinCount + friendLeagueRecord.secondClubWinCount + interCupRecord.secondClubWinCount;
drawCount = leagueRecord.drawCount + playoffRecord.drawCount + cupRecord.drawCount + friendRecord.drawCount + friendLeagueRecord.drawCount + interCupRecord.drawCount;
firstClubGoal = leagueRecord.firstClubGoal + playoffRecord.firstClubGoal + cupRecord.firstClubGoal + friendRecord.firstClubGoal + friendLeagueRecord.firstClubGoal + interCupRecord.firstClubGoal;
secondClubGoal = leagueRecord.secondClubGoal + playoffRecord.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 (playoffRecord.firstClubWinCount + playoffRecord.drawCount + playoffRecord.secondClubWinCount > 0) {
headToHead_content += 'Playoff ';
headToHead_content = setRecord(headToHead_content, playoffRecord);
}
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").append(headToHead_content);
try {
$('.banner_placeholder.rectangle')[0].parentNode.removeChild($('.banner_placeholder.rectangle')[0]);
} catch (err) {}
$.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 == playoffRecord) {
matchType = MATCH_TYPE.PLAYOFF;
} 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 checkClubInfo() {
if (checkClubInfoSuccess) {
clearInterval(checkClubInfoInterval);
} else {
try {
firstClubId = $('.club.faux_link').attr('club');
firstClubName = $('.club.faux_link')[0].innerText.trim();
let url = location.href;
secondClubId = url.split('/')[4];
try {
secondClubName = $('.column2_a div.box_sub_header.align_center a[club_link="' + secondClubId + '"]')[0].innerText;
} catch (e) {
secondClubName = secondClubId; //club doesn't exists
}
if (firstClubId != '' && firstClubName != '' && secondClubId != '' && secondClubName != '') {
checkClubInfoSuccess = true;
}
} catch (e) {
checkClubInfoSuccess = false;
}
}
}
})();