TMVN Home HeadToHead

Trophymanager: check head to head record of any 2 clubs, including the club that ceased to exists

  1. // ==UserScript==
  2. // @name TMVN Home HeadToHead
  3. // @namespace https://trophymanager.com
  4. // @version 3
  5. // @description Trophymanager: check head to head record of any 2 clubs, including the club that ceased to exists
  6. // @match https://trophymanager.com/home
  7. // @match https://trophymanager.com/home/
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. const APPLICATION_COLOR = {
  15. FIRST_CLUB: 'Darkred',
  16. SECOND_CLUB: 'Blue',
  17. TITLE: 'Yellow',
  18. SEASON: 'Yellow'
  19. }
  20.  
  21. const MATCH_TYPE = {
  22. LEAGUE: '<label style="color:White;">League</label>',
  23. PLAYOFF: '<label style="color:White;">Playoff</label>',
  24. CUP: '<label style="color:Orange;">Cup</label>',
  25. INTER_CUP: '<label style="color:Black;">International Cup</label>',
  26. FRIEND: '<label style="color:Aqua;">Friend</label>',
  27. FRIEND_LEAGUE: '<label style="color:Aqua;">Friend League</label>'
  28. }
  29.  
  30. var leagueRecord = {
  31. firstClubWinCount: 0,
  32. secondClubWinCount: 0,
  33. drawCount: 0,
  34. firstClubGoal: 0,
  35. secondClubGoal: 0
  36. }
  37. var playoffRecord = {
  38. firstClubWinCount: 0,
  39. secondClubWinCount: 0,
  40. drawCount: 0,
  41. firstClubGoal: 0,
  42. secondClubGoal: 0
  43. }
  44. var cupRecord = {
  45. firstClubWinCount: 0,
  46. secondClubWinCount: 0,
  47. drawCount: 0,
  48. firstClubGoal: 0,
  49. secondClubGoal: 0
  50. }
  51. var interCupRecord = {
  52. firstClubWinCount: 0,
  53. secondClubWinCount: 0,
  54. drawCount: 0,
  55. firstClubGoal: 0,
  56. secondClubGoal: 0
  57. }
  58. var friendRecord = {
  59. firstClubWinCount: 0,
  60. secondClubWinCount: 0,
  61. drawCount: 0,
  62. firstClubGoal: 0,
  63. secondClubGoal: 0
  64. }
  65. var friendLeagueRecord = {
  66. firstClubWinCount: 0,
  67. secondClubWinCount: 0,
  68. drawCount: 0,
  69. firstClubGoal: 0,
  70. secondClubGoal: 0
  71. }
  72. var matchMap = new Map();
  73. var sortMap = new Map();
  74.  
  75. if ($('.banner_placeholder.rectangle').length > 0) {
  76. $('.banner_placeholder.rectangle').remove();
  77. }
  78.  
  79. $('.column2_a')[0].innerHTML += '<div class="box"><div class="box_head"><h2 class="std">Head to Head</h2></div><div class="box_body" id="tm_script_head_to_head_body_id"><div id="tm_script_head_to_head_input_area_id"></div><div id="tm_script_head_to_head_button_area_id" align="center"></div><div id="tm_script_head_to_head_result_area_id"></div></div><div class="box_footer"><div/></div></div>';
  80.  
  81. let inputFirstClubId = document.createElement("span");
  82. inputFirstClubId.style = "display: inline-block;";
  83. inputFirstClubId.innerHTML = '<input id="tm_script_first_club_id" type="text" class="embossed" style="line-height: 95%; padding: 3px 3px 4px 3px;" placeholder="#1 Club Id">';
  84.  
  85. let inputSecondClubId = document.createElement("span");
  86. inputSecondClubId.style = "display: inline-block;";
  87. inputSecondClubId.innerHTML = '<input id="tm_script_second_club_id" type="text" class="embossed" style="line-height: 95%; padding: 3px 3px 4px 3px;" placeholder="#2 Club Id">';
  88.  
  89. let btnCheck = document.createElement("span");
  90. btnCheck.id = "tm_script_button_check";
  91. btnCheck.className = "button";
  92. btnCheck.style = "margin-left: 3px;";
  93. btnCheck.innerHTML = '<span class="button_border">Check</span>';
  94.  
  95. let divInput = $('#tm_script_head_to_head_input_area_id')[0];
  96. divInput.insertBefore(inputSecondClubId, divInput.firstChild);
  97. divInput.insertBefore(inputFirstClubId, divInput.firstChild);
  98. let divButton = $('#tm_script_head_to_head_button_area_id')[0];
  99. divButton.insertBefore(btnCheck, divButton.firstChild);
  100.  
  101. document.getElementById('tm_script_button_check').addEventListener('click', (e) => {
  102. checkHeadToHead();
  103. });
  104.  
  105. function checkHeadToHead() {
  106. $.ajaxSetup({
  107. async: false
  108. });
  109. let firstClubId,
  110. secondClubId;
  111. firstClubId = $('#tm_script_first_club_id')[0].value;
  112. secondClubId = $('#tm_script_second_club_id')[0].value;
  113. if (firstClubId == '' || secondClubId == '') {
  114. alert('Please input club\'s id');
  115. return;
  116. }
  117. firstClubId = firstClubId.trim();
  118. secondClubId = secondClubId.trim();
  119. if (isNaN(firstClubId) || isNaN(secondClubId)) {
  120. alert('Club\'s id must be a number');
  121. return;
  122. }
  123.  
  124. let firstClubName,
  125. secondClubName;
  126. firstClubName = getClubName(firstClubId);
  127. secondClubName = getClubName(secondClubId);
  128.  
  129. if (firstClubName == '' || secondClubName == '') {
  130. alert('Not found club. Please check club\'s id again.');
  131. return;
  132. }
  133.  
  134. let noMatchFound = false;
  135. let headToHeadUrl = 'https://trophymanager.com/ajax/match_h2h.ajax.php?home_team=' + firstClubId + '&away_team=' + secondClubId;
  136.  
  137. $.ajax(headToHeadUrl, {
  138. type: "GET",
  139. dataType: 'json',
  140. crossDomain: true,
  141. success: function (response) {
  142. let matches = response.matches;
  143. if (matches.length == 0) {
  144. noMatchFound = true;
  145. } else {
  146. resetObject(leagueRecord);
  147. resetObject(playoffRecord);
  148. resetObject(cupRecord);
  149. resetObject(friendRecord);
  150. resetObject(friendLeagueRecord);
  151. resetObject(interCupRecord);
  152. matchMap = new Map();
  153. sortMap = new Map();
  154.  
  155. Object.keys(matches).forEach(function (key, index) {
  156. let seasonArr = matches[key];
  157. for (let i = 0; i < seasonArr.length; i++) {
  158. let match = seasonArr[i];
  159. if (match.matchtype == 'l') {
  160. statistic(match, firstClubId, leagueRecord);
  161. } else if (match.matchtype.startsWith('lq')) {
  162. statistic(match, firstClubId, playoffRecord);
  163. } else if (match.matchtype.startsWith('p')) {
  164. statistic(match, firstClubId, cupRecord);
  165. } else if (match.matchtype == 'f') {
  166. statistic(match, firstClubId, friendRecord);
  167. } else if (match.matchtype == 'fl') {
  168. statistic(match, firstClubId, friendLeagueRecord);
  169. } else {
  170. statistic(match, firstClubId, interCupRecord);
  171. }
  172. }
  173. });
  174. }
  175. },
  176. error: function (e) {}
  177. });
  178.  
  179. var headToHead_content = "<table>";
  180. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.FIRST_CLUB + ';">#1: </span>' + firstClubName + '</td></tr>';
  181. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.SECOND_CLUB + ';">#2: </span>' + secondClubName + '</td></tr>';
  182. if (noMatchFound) {
  183. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.TITLE + ';">No match found</span></td></tr>';
  184. } else {
  185. let firstClubWinCount = 0,
  186. secondClubWinCount = 0,
  187. drawCount = 0,
  188. firstClubGoal = 0,
  189. secondClubGoal = 0;
  190. let firstClubWinRatio,
  191. secondClubWinRatio,
  192. drawRatio;
  193. let firstClubGoalAvg,
  194. secondClubGoalAvg;
  195.  
  196. firstClubWinCount = leagueRecord.firstClubWinCount + playoffRecord.firstClubWinCount + cupRecord.firstClubWinCount + friendRecord.firstClubWinCount + friendLeagueRecord.firstClubWinCount + interCupRecord.firstClubWinCount;
  197. secondClubWinCount = leagueRecord.secondClubWinCount + playoffRecord.secondClubWinCount + cupRecord.secondClubWinCount + friendRecord.secondClubWinCount + friendLeagueRecord.secondClubWinCount + interCupRecord.secondClubWinCount;
  198. drawCount = leagueRecord.drawCount + playoffRecord.drawCount + cupRecord.drawCount + friendRecord.drawCount + friendLeagueRecord.drawCount + interCupRecord.drawCount;
  199. firstClubGoal = leagueRecord.firstClubGoal + playoffRecord.firstClubGoal + cupRecord.firstClubGoal + friendRecord.firstClubGoal + friendLeagueRecord.firstClubGoal + interCupRecord.firstClubGoal;
  200. secondClubGoal = leagueRecord.secondClubGoal + playoffRecord.secondClubGoal + cupRecord.secondClubGoal + friendRecord.secondClubGoal + friendLeagueRecord.secondClubGoal + interCupRecord.secondClubGoal;
  201.  
  202. firstClubWinRatio = Math.round(firstClubWinCount / (firstClubWinCount + drawCount + secondClubWinCount) * 100);
  203. if (drawCount == 0) {
  204. secondClubWinRatio = 100 - firstClubWinRatio;
  205. drawRatio = 0;
  206. } else {
  207. secondClubWinRatio = Math.round(secondClubWinCount / (firstClubWinCount + drawCount + secondClubWinCount) * 100);
  208. drawRatio = 100 - firstClubWinRatio - secondClubWinRatio;
  209. }
  210.  
  211. firstClubGoalAvg = Math.round(firstClubGoal / (firstClubWinCount + drawCount + secondClubWinCount));
  212. secondClubGoalAvg = Math.round(secondClubGoal / (firstClubWinCount + drawCount + secondClubWinCount));
  213.  
  214. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.TITLE + ';">All</span></td></tr>';
  215. headToHead_content += '<tr><td><span style="color:Orange;">#1 - Draw - #2: </span>' + firstClubWinCount + ' - ' + drawCount + ' - ' + secondClubWinCount + ' [' + firstClubWinRatio + '% - ' + drawRatio + '% - ' + secondClubWinRatio + '%]' + '</td></tr>';
  216. headToHead_content += '<tr><td><span style="color:Orange;">Goal #1 - #2: </span>' + firstClubGoal + ' - ' + secondClubGoal + '</td></tr>';
  217. headToHead_content += '<tr><td><span style="color:Orange;">Goal average: </span>' + firstClubGoalAvg + ' - ' + secondClubGoalAvg + '</td></tr>';
  218.  
  219. if (leagueRecord.firstClubWinCount + leagueRecord.drawCount + leagueRecord.secondClubWinCount > 0) {
  220. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.TITLE + ';">League</span></td></tr>';
  221. headToHead_content = setRecord(headToHead_content, leagueRecord);
  222. }
  223. if (playoffRecord.firstClubWinCount + playoffRecord.drawCount + playoffRecord.secondClubWinCount > 0) {
  224. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.TITLE + ';">Playoff</span></td></tr>';
  225. headToHead_content = setRecord(headToHead_content, playoffRecord);
  226. }
  227. if (cupRecord.firstClubWinCount + cupRecord.drawCount + cupRecord.secondClubWinCount > 0) {
  228. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.TITLE + ';">Cup</span></td></tr>';
  229. headToHead_content = setRecord(headToHead_content, cupRecord);
  230. }
  231. if (interCupRecord.firstClubWinCount + interCupRecord.drawCount + interCupRecord.secondClubWinCount > 0) {
  232. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.TITLE + ';">Internaltional Cup</span></td></tr>';
  233. headToHead_content = setRecord(headToHead_content, interCupRecord);
  234. }
  235. if (friendLeagueRecord.firstClubWinCount + friendLeagueRecord.drawCount + friendLeagueRecord.secondClubWinCount > 0) {
  236. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.TITLE + ';">Friend League</span></td></tr>';
  237. headToHead_content = setRecord(headToHead_content, friendLeagueRecord);
  238. }
  239. if (friendRecord.firstClubWinCount + friendRecord.drawCount + friendRecord.secondClubWinCount > 0) {
  240. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.TITLE + ';">Friend</span></td></tr>';
  241. headToHead_content = setRecord(headToHead_content, friendRecord);
  242. }
  243.  
  244. sortMap[Symbol.iterator] = function * () {
  245. yield * [...this.entries()].sort((a, b) => b[1] - a[1]);
  246. }
  247. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.TITLE + ';">Match:</span></td></tr>';
  248. for (let[key, value]of sortMap) {
  249. let match = matchMap.get(key);
  250. if (match.hometeam == '#1') {
  251. if (match.homegoal > match.awaygoal) {
  252. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.FIRST_CLUB + ';">[' + match.hometeam + ']</span> <span style="color:' + APPLICATION_COLOR.FIRST_CLUB + ';">' + match.homegoal + '</span> - ' + match.awaygoal + ' <span style="color:' + APPLICATION_COLOR.SECOND_CLUB + ';">[' + match.awayteam + ']</span>' + ' [<span onclick = \"window.open(\'https:\/\/trophymanager.com\/matches\/' + match.id + '\')\">' + match.matchtype + '</span>] [' + match.season + '] [' + match.date + ']</td></tr>';
  253. } else if (match.homegoal < match.awaygoal) {
  254. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.FIRST_CLUB + ';">[' + match.hometeam + ']</span> ' + match.homegoal + ' - <span style="color:' + APPLICATION_COLOR.SECOND_CLUB + ';">' + match.awaygoal + '</span> <span style="color:' + APPLICATION_COLOR.SECOND_CLUB + ';">[' + match.awayteam + ']</span>' + ' [<span onclick = \"window.open(\'https:\/\/trophymanager.com\/matches\/' + match.id + '\')\">' + match.matchtype + '</span>] [' + match.season + '] [' + match.date + ']</td></tr>';
  255. } else {
  256. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.FIRST_CLUB + ';">[' + match.hometeam + ']</span> ' + match.homegoal + ' - ' + match.awaygoal + ' <span style="color:' + APPLICATION_COLOR.SECOND_CLUB + ';">[' + match.awayteam + ']</span>' + ' [<span onclick = \"window.open(\'https:\/\/trophymanager.com\/matches\/' + match.id + '\')\">' + match.matchtype + '</span>] [' + match.season + '] [' + match.date + ']</td></tr>';
  257. }
  258. } else {
  259. if (match.homegoal > match.awaygoal) {
  260. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.SECOND_CLUB + ';">[' + match.hometeam + ']</span> <span style="color:' + APPLICATION_COLOR.SECOND_CLUB + ';">' + match.homegoal + '</span> - ' + match.awaygoal + ' <span style="color:' + APPLICATION_COLOR.FIRST_CLUB + ';">[' + match.awayteam + ']</span>' + ' [<span onclick = \"window.open(\'https:\/\/trophymanager.com\/matches\/' + match.id + '\')\">' + match.matchtype + '</span>] [' + match.season + '] [' + match.date + ']</td></tr>';
  261. } else if (match.homegoal < match.awaygoal) {
  262. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.SECOND_CLUB + ';">[' + match.hometeam + ']</span> ' + match.homegoal + ' - <span style="color:' + APPLICATION_COLOR.FIRST_CLUB + ';">' + match.awaygoal + '</span> <span style="color:' + APPLICATION_COLOR.FIRST_CLUB + ';">[' + match.awayteam + ']</span>' + ' [<span onclick = \"window.open(\'https:\/\/trophymanager.com\/matches\/' + match.id + '\')\">' + match.matchtype + '</span>] [' + match.season + '] [' + match.date + ']</td></tr>';
  263. } else {
  264. headToHead_content += '<tr><td><span style="color:' + APPLICATION_COLOR.SECOND_CLUB + ';">[' + match.hometeam + ']</span> ' + match.homegoal + ' - ' + match.awaygoal + ' <span style="color:' + APPLICATION_COLOR.FIRST_CLUB + ';">[' + match.awayteam + ']</span>' + ' [<span onclick = \"window.open(\'https:\/\/trophymanager.com\/matches\/' + match.id + '\')\">' + match.matchtype + '</span>] [' + match.season + '] [' + match.date + ']</td></tr>';
  265. }
  266. }
  267. }
  268. }
  269. headToHead_content += "</table>";
  270.  
  271. $("#tm_script_head_to_head_result_area_id")[0].innerText = '';
  272. $("#tm_script_head_to_head_result_area_id").append(headToHead_content);
  273. $.ajaxSetup({
  274. async: true
  275. });
  276. }
  277.  
  278. function statistic(match, firstClubId, record) {
  279. let result = match.result.split('-');
  280. if (match.hometeam == firstClubId) {
  281. record.firstClubGoal += parseInt(result[0]);
  282. record.secondClubGoal += parseInt(result[1]);
  283. } else {
  284. record.secondClubGoal += parseInt(result[0]);
  285. record.firstClubGoal += parseInt(result[1]);
  286. }
  287. if (result[0] == result[1]) {
  288. record.drawCount++;
  289. } else if (result[0] > result[1]) {
  290. if (match.hometeam == firstClubId) {
  291. record.firstClubWinCount++;
  292. } else {
  293. record.secondClubWinCount++;
  294. }
  295. } else {
  296. if (match.hometeam == firstClubId) {
  297. record.secondClubWinCount++;
  298. } else {
  299. record.firstClubWinCount++;
  300. }
  301. }
  302. let matchType = '';
  303. if (record == leagueRecord) {
  304. matchType = MATCH_TYPE.LEAGUE;
  305. } else if (record == playoffRecord) {
  306. matchType = MATCH_TYPE.PLAYOFF;
  307. } else if (record == cupRecord) {
  308. matchType = MATCH_TYPE.CUP;
  309. } else if (record == friendRecord) {
  310. matchType = MATCH_TYPE.FRIEND;
  311. } else if (record == friendLeagueRecord) {
  312. matchType = MATCH_TYPE.FRIEND_LEAGUE;
  313. } else {
  314. matchType = MATCH_TYPE.INTER_CUP;
  315. }
  316.  
  317. let hometeam,
  318. awayteam;
  319. if (match.hometeam == firstClubId) {
  320. hometeam = '#1';
  321. awayteam = '#2';
  322. } else {
  323. hometeam = '#2';
  324. awayteam = '#1';
  325. }
  326. matchMap.set(match.id, {
  327. 'id': match.id,
  328. 'matchtype': matchType,
  329. 'season': '<span style="color:' + APPLICATION_COLOR.SEASON + ';">' + match.season + '</span>',
  330. 'date': match.date,
  331. 'hometeam': hometeam,
  332. 'awayteam': awayteam,
  333. 'homegoal': result[0],
  334. 'awaygoal': result[1]
  335. });
  336. sortMap.set(match.id, new Date(match.date));
  337. }
  338.  
  339. function setRecord(headToHead_content, record) {
  340. let firstClubGoalAvg,
  341. secondClubGoalAvg;
  342. let firstClubWinRatio,
  343. secondClubWinRatio,
  344. drawRatio;
  345.  
  346. firstClubGoalAvg = Math.round(record.firstClubGoal / (record.firstClubWinCount + record.drawCount + record.secondClubWinCount));
  347. secondClubGoalAvg = Math.round(record.secondClubGoal / (record.firstClubWinCount + record.drawCount + record.secondClubWinCount));
  348.  
  349. firstClubWinRatio = Math.round(record.firstClubWinCount / (record.firstClubWinCount + record.drawCount + record.secondClubWinCount) * 100);
  350. if (record.drawCount == 0) {
  351. secondClubWinRatio = 100 - firstClubWinRatio;
  352. drawRatio = 0;
  353. } else {
  354. secondClubWinRatio = Math.round(record.secondClubWinCount / (record.firstClubWinCount + record.drawCount + record.secondClubWinCount) * 100);
  355. drawRatio = 100 - firstClubWinRatio - secondClubWinRatio;
  356. }
  357.  
  358. headToHead_content += '<tr><td><span style="color:Orange;">#1 - Draw - #2: </span>' + record.firstClubWinCount + ' - ' + record.drawCount + ' - ' + record.secondClubWinCount + ' [' + firstClubWinRatio + '% - ' + drawRatio + '% - ' + secondClubWinRatio + '%]' + '</td></tr>';
  359. headToHead_content += '<tr><td><span style="color:Orange;">Goal #1 - #2: </span>' + record.firstClubGoal + ' - ' + record.secondClubGoal + '</td></tr>';
  360. headToHead_content += '<tr><td><span style="color:Orange;">Goal average: </span>' + firstClubGoalAvg + ' - ' + secondClubGoalAvg + '</td></tr>';
  361. return headToHead_content;
  362. }
  363.  
  364. function resetObject(obj) {
  365. for (var key in obj) {
  366. obj[key] = 0;
  367. }
  368. }
  369.  
  370. function getClubName(clubId) {
  371. let clubName = '';
  372. $.ajax('https://trophymanager.com/club/' + clubId, {
  373. type: "GET",
  374. dataType: 'html',
  375. crossDomain: true,
  376. success: function (response) {
  377. let element = $('.column2_a div.box_sub_header.align_center a[club_link="' + clubId + '"]', response)[0];
  378. if (element) {
  379. clubName = element.innerText;
  380. } else {
  381. clubName = clubId; //club doesn't exists
  382. }
  383. },
  384. error: function (e) {}
  385. });
  386. return clubName;
  387. }
  388. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址