TMVN Match Event

Trophymanager: show main events of the match. So you can easy research tactics of other clubs and your tactics rules happened as expected or not.

  1. // ==UserScript==
  2. // @name TMVN Match Event
  3. // @version 5
  4. // @description Trophymanager: show main events of the match. So you can easy research tactics of other clubs and your tactics rules happened as expected or not.
  5. // @namespace https://trophymanager.com
  6. // @include https://trophymanager.com/matches/*
  7. // ==/UserScript==
  8.  
  9. (function () {
  10. 'use strict';
  11.  
  12. const MENTALITY_MAP = new Map()
  13. .set("1", "Very Defensive")
  14. .set("2", "Defensive")
  15. .set("3", "Slightly Defensive")
  16. .set("4", "Normal")
  17. .set("5", "Slightly Attacking")
  18. .set("6", "Attacking")
  19. .set("7", "Very Attacking");
  20.  
  21. const STYLE_MAP = new Map()
  22. .set("1", "Balanced")
  23. .set("2", "Direct")
  24. .set("3", "Wings")
  25. .set("4", "Shortpassing")
  26. .set("5", "Long Balls")
  27. .set("6", "Through Balls");
  28.  
  29. const GOAL_STYLE_MAP = new Map()
  30. .set("p_s", "Penalty")
  31. .set("kco", "GK Counter")
  32. .set("klo", "GK Kick")
  33. .set("doe", "Conner")
  34. .set("cou", "Counter/Direct")
  35. .set("dir", "Freekick")
  36. .set("win", "Wing Attack")
  37. .set("sho", "Short Pass")
  38. .set("lon", "Long Ball")
  39. .set("thr", "Through Ball");
  40.  
  41. const FOCUS_MAP = new Map()
  42. .set("1", "Balanced")
  43. .set("2", "Left")
  44. .set("3", "Center")
  45. .set("4", "Right");
  46.  
  47. const EVENT_TYPE = {
  48. GOAL: 'goal',
  49. YELLOW_CARD: 'yellow',
  50. RED_CARD: 'red',
  51. MENTALITY: 'mentality',
  52. STYLE: 'style',
  53. POSITION: 'position',
  54. SUBTITION: 'subtition',
  55. INJURY: 'injury'
  56. }
  57.  
  58. var mainEventHTML = "";
  59.  
  60. var matchUrl = location.href.split('/');
  61. var matchId,
  62. url;
  63. if (isNaN(matchUrl[4])) {
  64. matchId = matchUrl[5];
  65. url = 'https://trophymanager.com/ajax/match.ajax.php?id=nt' + matchId;
  66. } else {
  67. matchId = matchUrl[4];
  68. url = 'https://trophymanager.com/ajax/match.ajax.php?id=' + matchId;
  69. }
  70.  
  71. var xhr = new XMLHttpRequest();
  72. var homeStartStyle,
  73. homeStartMentality,
  74. homeFocus,
  75. awayStartStyle,
  76. awayStartMentality,
  77. awayFocus;
  78.  
  79. xhr.open('GET', url, true);
  80. xhr.send();
  81. xhr.onreadystatechange = function () {
  82. if (this.readyState == 4 && this.status == 200) {
  83. var data = JSON.parse(this.responseText);
  84.  
  85. var report = data.report;
  86. if (Object.keys(report).length <= 3) {
  87. return; //because don't have datas of match
  88. }
  89.  
  90. homeStartStyle = data.match_data.attacking_style.home;
  91. if (homeStartStyle == "0") {
  92. homeStartStyle = "1"; //bug of TM
  93. }
  94. awayStartStyle = data.match_data.attacking_style.away;
  95. if (awayStartStyle == "0") {
  96. awayStartStyle = "1"; //bug of TM
  97. }
  98. homeStartMentality = data.match_data.mentality.home.toString();
  99. awayStartMentality = data.match_data.mentality.away.toString();
  100. homeFocus = data.match_data.focus_side.home;
  101. awayFocus = data.match_data.focus_side.away;
  102.  
  103. var homeClubId = data.club.home.id;
  104. var awayClubId = data.club.away.id;
  105.  
  106. var homeLineup = data.lineup.home;
  107. var awayLineup = data.lineup.away;
  108. var homePlayerIds = Object.getOwnPropertyNames(homeLineup);
  109. var awayPlayerIds = Object.getOwnPropertyNames(awayLineup);
  110. var homePlayer = new Map(),
  111. awayPlayer = new Map();
  112. homePlayerIds.forEach((playerId) => {
  113. homePlayer.set(playerId, homeLineup[playerId].name);
  114. });
  115. awayPlayerIds.forEach((playerId) => {
  116. awayPlayer.set(playerId, awayLineup[playerId].name);
  117. });
  118.  
  119. var homeGoal = 0,
  120. awayGoal = 0;
  121. var eventReport = [];
  122. Object.keys(report).forEach(function (key, index) {
  123. var minuteArr = report[key];
  124. for (var i = 0; i < minuteArr.length; i++) {
  125. var paramArr = minuteArr[i].parameters;
  126. if (paramArr) {
  127. for (var j = 0; j < paramArr.length; j++) {
  128. var paramObj = paramArr[j];
  129. if (paramObj.goal) {
  130. if (homePlayer.has(paramObj.goal.player)) {
  131. homeGoal++;
  132. let goalStyle = "";
  133. let chanceType = minuteArr[i].type;
  134. if (chanceType) {
  135. chanceType = chanceType.substring(0, 3);
  136. if (GOAL_STYLE_MAP.has(chanceType)) {
  137. goalStyle = GOAL_STYLE_MAP.get(chanceType);
  138. }
  139. }
  140.  
  141. eventReport.push({
  142. minute: key,
  143. type: EVENT_TYPE.GOAL,
  144. home: true,
  145. content: "[Goal] " +
  146. homePlayer.get(paramObj.goal.player) +
  147. (paramObj.goal.assist ? " (" + homePlayer.get(paramObj.goal.assist) + ") " : " ") +
  148. (goalStyle != "" ? " [" + goalStyle + "] " : " ") +
  149. paramObj.goal.score[0] + " - " + paramObj.goal.score[1]
  150. });
  151. } else {
  152. awayGoal++;
  153. let goalStyle = "";
  154. let chanceType = minuteArr[i].type;
  155. if (chanceType) {
  156. chanceType = chanceType.substring(0, 3);
  157. if (GOAL_STYLE_MAP.has(chanceType)) {
  158. goalStyle = GOAL_STYLE_MAP.get(chanceType);
  159. }
  160. }
  161.  
  162. eventReport.push({
  163. minute: key,
  164. type: EVENT_TYPE.GOAL,
  165. home: false,
  166. content: "[Goal] " +
  167. awayPlayer.get(paramObj.goal.player) +
  168. (paramObj.goal.assist ? " (" + awayPlayer.get(paramObj.goal.assist) + ") " : " ") +
  169. (goalStyle != "" ? " [" + goalStyle + "] " : " ") +
  170. paramObj.goal.score[0] + " - " + paramObj.goal.score[1]
  171. });
  172. }
  173. } else if (paramObj.yellow) {
  174. if (homePlayer.has(paramObj.yellow)) {
  175. eventReport.push({
  176. minute: key,
  177. type: EVENT_TYPE.YELLOW_CARD,
  178. home: true,
  179. content: "[Yellow card] " +
  180. homePlayer.get(paramObj.yellow)
  181. });
  182. } else {
  183. eventReport.push({
  184. minute: key,
  185. type: EVENT_TYPE.YELLOW_CARD,
  186. home: false,
  187. content: "[Yellow card] " +
  188. awayPlayer.get(paramObj.yellow)
  189. });
  190. }
  191. } else if (paramObj.red) {
  192. if (homePlayer.has(paramObj.red)) {
  193. eventReport.push({
  194. minute: key,
  195. type: EVENT_TYPE.RED_CARD,
  196. home: true,
  197. content: "[Red card] " +
  198. homePlayer.get(paramObj.red)
  199. });
  200. } else {
  201. eventReport.push({
  202. minute: key,
  203. type: EVENT_TYPE.RED_CARD,
  204. home: false,
  205. content: "[Red card] " +
  206. awayPlayer.get(paramObj.red)
  207. });
  208. }
  209. } else if (paramObj.yellow_red) {
  210. if (homePlayer.has(paramObj.yellow_red)) {
  211. eventReport.push({
  212. minute: key,
  213. type: EVENT_TYPE.RED_CARD,
  214. home: true,
  215. content: "[Red card] (2 yellow cards) " +
  216. homePlayer.get(paramObj.yellow_red)
  217. });
  218. } else {
  219. eventReport.push({
  220. minute: key,
  221. type: EVENT_TYPE.RED_CARD,
  222. home: false,
  223. content: "[Red card] (2 yellow cards) " +
  224. awayPlayer.get(paramObj.yellow_red)
  225. });
  226. }
  227. } else if (paramObj.mentality_change) {
  228. if (paramObj.mentality_change.mentality) {
  229. if (paramObj.mentality_change.team == homeClubId) {
  230. eventReport.push({
  231. minute: key,
  232. type: EVENT_TYPE.MENTALITY,
  233. home: true,
  234. content: "[Tactics] Mentality change to " + MENTALITY_MAP.get(paramObj.mentality_change.mentality)
  235. });
  236. } else {
  237. eventReport.push({
  238. minute: key,
  239. type: EVENT_TYPE.MENTALITY,
  240. home: false,
  241. content: "[Tactics] Mentality change to " + MENTALITY_MAP.get(paramObj.mentality_change.mentality)
  242. });
  243. }
  244. } else if (paramObj.mentality_change.style) {
  245. if (paramObj.mentality_change.team == homeClubId) {
  246. eventReport.push({
  247. minute: key,
  248. type: EVENT_TYPE.STYLE,
  249. home: true,
  250. content: "[Tactics] Attacking style change to " + STYLE_MAP.get(paramObj.mentality_change.style)
  251. });
  252. } else {
  253. eventReport.push({
  254. minute: key,
  255. type: EVENT_TYPE.STYLE,
  256. home: false,
  257. content: "[Tactics] Attacking style change to " + STYLE_MAP.get(paramObj.mentality_change.style)
  258. });
  259. }
  260. }
  261. } else if (paramObj.player_change) {
  262. if (homePlayer.has(paramObj.player_change.player)) {
  263. eventReport.push({
  264. minute: key,
  265. type: EVENT_TYPE.POSITION,
  266. home: true,
  267. content: "[Position] " + homePlayer.get(paramObj.player_change.player) + " change to " + (paramObj.player_change.position !== undefined ? paramObj.player_change.position.toUpperCase() : "")
  268. });
  269. } else {
  270. eventReport.push({
  271. minute: key,
  272. type: EVENT_TYPE.POSITION,
  273. home: false,
  274. content: "[Position] " + awayPlayer.get(paramObj.player_change.player) + " change to " + (paramObj.player_change.position !== undefined ? paramObj.player_change.position.toUpperCase() : "")
  275. });
  276. }
  277. } else if (paramObj.sub && paramObj.sub.player_in && paramObj.sub.player_out) {
  278. if (homePlayer.has(paramObj.sub.player_in)) {
  279. eventReport.push({
  280. minute: key,
  281. type: EVENT_TYPE.SUBTITION,
  282. home: true,
  283. content: "[Subtition] " + homePlayer.get(paramObj.sub.player_in) + " replace " + homePlayer.get(paramObj.sub.player_out) + (paramObj.sub.player_position !== undefined ? " and play " + paramObj.sub.player_position.toUpperCase() : "")
  284. });
  285. } else {
  286. eventReport.push({
  287. minute: key,
  288. type: EVENT_TYPE.SUBTITION,
  289. home: false,
  290. content: "[Subtition] " + awayPlayer.get(paramObj.sub.player_in) + " replace " + awayPlayer.get(paramObj.sub.player_out) + (paramObj.sub.player_position !== undefined ? " and play " + paramObj.sub.player_position.toUpperCase() : "")
  291. });
  292. }
  293. } else if (paramObj.injury) {
  294. if (homePlayer.has(paramObj.injury)) {
  295. eventReport.push({
  296. minute: key,
  297. type: EVENT_TYPE.INJURY,
  298. home: true,
  299. content: "[Injury] " + homePlayer.get(paramObj.injury)
  300. });
  301. } else {
  302. eventReport.push({
  303. minute: key,
  304. type: EVENT_TYPE.INJURY,
  305. home: false,
  306. content: "[Injury] " + awayPlayer.get(paramObj.injury)
  307. });
  308. }
  309. }
  310. }
  311. }
  312. }
  313. });
  314.  
  315. if (eventReport.length > 0) {
  316. mainEventHTML += '<li style="color:burlywood"><table><tbody><tr><td align="left">' +
  317. '0. Mentality: ' + MENTALITY_MAP.get(homeStartMentality) +
  318. '. Attacking style: ' + STYLE_MAP.get(homeStartStyle) +
  319. '. Focus: ' + FOCUS_MAP.get(homeFocus) +
  320. '</td><td align="right">' +
  321. 'Mentality: ' + MENTALITY_MAP.get(awayStartMentality) +
  322. '. Attacking style: ' + STYLE_MAP.get(awayStartStyle) +
  323. '. Focus: ' + FOCUS_MAP.get(awayFocus) +
  324. ' .0' +
  325. '</td></tr></tbody></table></li>';
  326. eventReport.forEach((event) => {
  327. let color;
  328. switch (event.type) {
  329. case EVENT_TYPE.GOAL:
  330. color = "style='color:Aqua;'";
  331. break;
  332. case EVENT_TYPE.YELLOW_CARD:
  333. color = "style='color:Yellow;'";
  334. break;
  335. case EVENT_TYPE.RED_CARD:
  336. color = "style='color:Darkred;'";
  337. break;
  338. case EVENT_TYPE.MENTALITY:
  339. color = "style='color:Orange;'";
  340. break;
  341. case EVENT_TYPE.STYLE:
  342. color = "style='color:Blue;'";
  343. break;
  344. case EVENT_TYPE.POSITION:
  345. color = "style='color:GreenYellow;'";
  346. break;
  347. case EVENT_TYPE.INJURY:
  348. color = "style='color:Brown;'";
  349. break;
  350. default:
  351. color = "";
  352. }
  353. if (event.home) {
  354. mainEventHTML +=
  355. "<li align='left' " + color + ">" +
  356. event.minute +
  357. ". " +
  358. event.content +
  359. "</li>";
  360. } else {
  361. mainEventHTML +=
  362. "<li align='right' " + color + ">" +
  363. event.content +
  364. " ." +
  365. event.minute +
  366. "</li>";
  367. }
  368. });
  369. }
  370. }
  371. }
  372.  
  373. var myInterval = setInterval(display, 1000);
  374. var pause = false;
  375. function display() {
  376. if (mainEventHTML !== "" && $('.box_body.mv_bottom').length > 0) {
  377. if ($('.post_report').length > 0) {
  378. let divArea = $('.box_body.mv_bottom div.post_report')[0];
  379. divArea.innerHTML =
  380. '<div class="mega_headline tcenter report_section_header dark_bg">Main Event</div><div><ul class="clean underlined large_padding">' +
  381. mainEventHTML +
  382. '</ul></div>' +
  383. divArea.innerHTML;
  384. clearInterval(myInterval);
  385. } else if (!pause) {
  386. let divArea = $('.box_body.mv_bottom')[0];
  387. divArea.innerHTML =
  388. '<div class="mega_headline tcenter report_section_header dark_bg">Main Event</div><div><ul class="clean underlined large_padding">' +
  389. mainEventHTML +
  390. '</ul></div>' +
  391. divArea.innerHTML;
  392. pause = true;
  393. }
  394. }
  395. }
  396. })();