// ==UserScript==
// @name TM Routine Line Sharing
// @namespace https://trophymanager.com/
// @version 0.3
// @description Display routine of players on field in tactics page
// @iconURL https://static.trophymanager.com/pics/icons/mini_field.png
// @author Matteo Tomassetti (Polverigi FC)
// @include https://trophymanager.com/tactics/*
// @grant none
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
// ****************************
// *** Constants definition ***
// ****************************
var share_bonus = 0.25;
var routine_cap = 40.0;
var def_line_idx = [0, 6]; // Defensive positions are 0 to 5
var mid_line_idx = [6, 16]; // Midfield positions are 6 to 15
var off_line_idx = [16, 24]; // Offensive positions are 16 to 23
// ************************
// *** Script main code ***
// ************************
var players_on_field = {};
// Attach click event handler function to the tactics field:
// when the user clicks on the field, routine values are updated
$("#tactics_field").click(function() {
updatePlayersRoutine();
displayPlayersRoutine();
});
// ****************************
// *** Functions definition ***
// ****************************
/*
* This function displays routine value of each player below his name
*/
function displayPlayersRoutine() {
$("div.field_player").each(function(index, el) {
if ($(el).attr("player_set") === "false") {
$(el).find("div.field_player_routine").remove();
} else {
var id = $(el).attr("player_id");
var no = players_on_field[id]["no"];
var routine = players_on_field[id]["routine"];
var rou_div = $(el).find("div.field_player_routine");
if (rou_div.length) {
// div already exists
rou_div.text(routine);
} else {
// create new div to display routine value
$(this).append('
'+routine+'
');
}
}
});
}
/*
* This function updates the routine of all players on the field
* applying the routine bonus
*/
function updatePlayersRoutine() {
updateLineRoutine(def_line_idx);
updateLineRoutine(mid_line_idx);
updateLineRoutine(off_line_idx);
}
/*
* This function applies the routine bonus to the player with least routine
* in the given line and saves the updated routine value
*/
function updateLineRoutine(line_idx) {
var players_ar = [];
// for each position in the line
for (var i = line_idx[0]; i < line_idx[1]; i++) {
var id = formation_by_pos[i];
// check if there is a player in that position
if (id !== "0" && id !== null) {
// retrieve player info
var player = players_by_id[id];
var name = player["name"];
var no = player["no"];
var routine = parseFloat(player["routine"]);
// create new player object and add it to the array
var p = {"id": id, "no": no, "routine": routine};
players_ar.push(p);
}
}
// sort players array by routine in ascending order
players_ar.sort(compareByRoutineAsc);
var line_size = players_ar.length; // players in the line
// if line consist of two or more players
if (line_size > 1) {
var min = players_ar[0]["routine"];
// player must have less than 40 routine to obtain bonus
if (min < routine_cap) {
var min2 = players_ar[1]["routine"];
var max = players_ar[line_size - 1]["routine"];
// calculate new routine value applying routine bonus
var bonus = max * share_bonus;
var new_routine = min + bonus;
new_routine = (new_routine < min2 ? new_routine : min2);
new_routine = (new_routine < routine_cap ? new_routine : routine_cap);
new_routine = parseFloat(new_routine.toFixed(1));
// update player routine
players_ar[0]["routine"] = new_routine;
}
// insert players into players_on_field object by id
for (var i = 0; i < players_ar.length; i++) {
var player = players_ar[i];
var id = player["id"];
var no = player["no"];
var routine = player["routine"];
players_on_field[id] = {"no": no, "routine": routine};
}
}
}
/*
* Compare function to sort objects by their routine property in ascending order
*/
function compareByRoutineAsc(a, b) {
return a.routine - b.routine;
}
/*
* Compare function to sort objects by their routine property in descending order
*/
function compareByRoutineDes(a, b) {
return b.routine - a.routine;
}
})();