krisztiantobias / Pmanager.org Tranfer List Abilitys

// ==UserScript==
// @name          Pmanager.org Tranfer List Abilitys
// @namespace     http://tampermonkey.net/
// @description   That script highligts the talented players what I need
// @include       https://www.pmanager.org/procurar.asp?action=proc_jog*
// @require       http://code.jquery.com/jquery-latest.js
// @version       2.01
// @license       MIT
// ==/UserScript==

let $ = window.$;

let startOfSeason = new Date("2014-09-01");
const abilityString = "Tehetség</b>: ";
const talentString = "<b>Adottság</b>: <font class=comentarios>";
const currentDate = new Date();
const dayPerSeason = 77; // 11 weeks
const abilityReducePerSeason = 0.75;
const attributeYoungMinimum = 60;
const attributeAdultMinimum = 84;
const attributeExcelentMinimum = 90;
const priceMaximumHUF = 1500000;
const lastMaxAbilityYear = 20;
const matchesPerSeason = 24;
const lastGoodYear = 24;
const columnPosition = 0;
const columnAge = columnPosition + 2;
const columnHandling = columnAge + 2;
const columnSpeed = columnHandling + 10;
const columnStrength = columnSpeed + 1;
const columnPrice = columnStrength + 2;
const columnInfo = columnPrice + 1;
const bestTalents = [
	"Magas labda specialista", "Ellentámadás specialista",
	"Teljes letámadás specialista", "Távoli lövés specialista",
	"Kapás-lövés specialista"
];

$(document).ready(function()
{
	startOfSeason = startOfSeason.addDays((currentDate.getSeasonDiffTo(startOfSeason) + 1) * dayPerSeason);
	// iterate on the table with title
	let title;
	$.each($("tbody").eq(1).find("tr"), function(index)
	{
		if (index == 0)
		{
			title = $(this);
		}
		else
		{
			analysePlayer($(this));
		}
	});
	// highlight header at the END to see the script worked
	highlightTitle(title);
	createHintBelow();
});

function highlightTitle(title)
{
	title.css("background-color","#FF7F50");
}

function highlightBestTalents(elements)
{
	let infoImg = elements.eq(columnInfo).find("img");
	const infoTitle = infoImg.attr("title");
	let infoTalentStart = infoTitle.indexOf(talentString);
	if (infoTalentStart == -1)
    {
		return;
    }
	infoTalentStart += talentString.length;
	const infoTalentEnd = infoTitle.indexOf("</font>", infoTalentStart);
	const talentStr = infoTitle.substr(infoTalentStart, infoTalentEnd - infoTalentStart);
	if (bestTalents.indexOf(talentStr) != -1)
    {
		infoImg.attr("src", "http://www.pmanagertutorial.nhely.hu/images/other/black_star.png");
    }
}

function getPrice(elements)
{
	return parseInt(elements.eq(columnPrice).text().replace(/\./g,""));
}

function getAttributeCountNow(elements)
{
	let attrAtNow = 0;
	for (let column = columnHandling; column < columnStrength; ++column)
	{
		const boldVal = elements.eq(column).find("b");
		if (boldVal.size())
        {
			attrAtNow += parseInt(boldVal.text());
        }
	}
	const neededPhys = parseInt(elements.eq(columnSpeed).text()) + parseInt(elements.eq(columnStrength).text());
	return attrAtNow + neededPhys;
}

function getChangeUntil24(ability, startAge)
{
	let dayThisSeason = startOfSeason.getDayDiffTo(currentDate);
	let positiveChange = 0;
	let age = startAge;
	while(age < lastGoodYear)
	{
		positiveChange += ability * dayThisSeason / dayPerSeason * matchesPerSeason; // circa...
		dayThisSeason = dayPerSeason;
		++age;
		if (age > lastMaxAbilityYear)
		{
			ability *= abilityReducePerSeason;
		}
	}
	return positiveChange;
}

function getAbility(elements)
{
	const infoTitle = elements.eq(columnInfo).find("img").attr("title");
	const infoAbilityStart = infoTitle.indexOf(abilityString) + abilityString.length;
	const infoAbilityEnd = infoTitle.indexOf("<br />", infoAbilityStart);
	return getAbilityMin(infoTitle.substr(infoAbilityStart, infoAbilityEnd - infoAbilityStart));
}

function analysePlayer(row)
{
	const elements = row.find("td");
	const startAge = parseInt(elements.eq(columnAge).text());
	if (startAge < lastGoodYear)
	{
		// collect base data
		const price = getPrice(elements);
		const attrAtNow = getAttributeCountNow(elements);
		const positiveChange = getChangeUntil24(getAbility(elements), startAge);
		let attrAt24 = attrAtNow + positiveChange;
		// analyse
		elements.eq(columnPosition).find("font").append(" <small " +
														(attrAt24 > attributeExcelentMinimum ? "style='color:Red'" :
															(attrAt24 > attributeAdultMinimum ? "style='color:Coral'" : "" ))
														+ ">|" + Math.floor(attrAt24) + "|</small>");
		if (positiveChange)
		{
			elements.eq(columnAge).find("font").append(" <small>|+" + Math.floor(positiveChange) + "|</small>");
			// 20 20 20 0 18 x  // 1.500.000 HUF // I do not want young players also
			if (attrAt24 > attributeAdultMinimum && price < priceMaximumHUF && attrAtNow > attributeYoungMinimum)
			{
				row.css("background-color", attrAt24 >= attributeExcelentMinimum ? "#FFA0A0" : "#AFFFAF");
			}
		}
	}
	highlightBestTalents(elements);
}

function createHintBelow()
{
	const bottomP = $("tbody").eq(2).parent().parent().find("p").eq(1);
	bottomP.after("<p>A pozició mellett a mostani vagy 24 éves 0 napos össz érték, az életkor mellett a fejlődés található.</p>");
	bottomP.after("<p>A script 24 éves nulla napos korig számol.</p>");
	bottomP.after("<p>Zölddel színezi a sort, ha " + attributeAdultMinimum + " felett van a várható érték 24 évesen és 1.5m HUF alatt van az ár.</p>");
	bottomP.after("<p>Pirossal színezi a sort, ha " + attributeExcelentMinimum + " felett van a várható érték 24 évesen és 1.5m HUF alatt van az ár.</p>");
}

function getAbilityLevel(abilityString)
{
	if (abilityString == "Pocsék")
	{
		return 0;
	}
	if (abilityString == "Csapnivaló")
	{
		return 1;
	}
	if (abilityString == "Nagyon gyenge")
	{
		return 2;
	}
	if (abilityString == "Gyenge")
	{
		return 3;
	}
	if (abilityString == "Megfelelo" || abilityString == "Megfelelő")
	{
		return 4;
	}
	if (abilityString == "Jó")
	{
		return 5;
	}
	if (abilityString == "Nagyon jó")
	{
		return 6;
	}
	if (abilityString == "Remek")
	{
		return 7;
	}
	if (abilityString == "Nagyszeru" || abilityString == "Nagyszerű")
	{
		return 8;
	}
	if (abilityString == "Világklasszis")
	{
		return 9;
	}
	console.log("ERROR, unknown Ability!");
	return 0;
}
function getAbilityMin(abilityString)
{
	return /*0.04 + */0.05 * getAbilityLevel(abilityString);
}

Date.prototype.addDays = function(days)
{
	let date = new Date(this.valueOf());
	date.setDate(date.getDate() + days);
	return date;
};
Date.prototype.getDayDiffTo = function(days)
{
	return Math.floor(this.valueOf() - days) / (1000 * 60 * 60 * 24);
};
Date.prototype.getSeasonDiffTo = function(days)
{
	return Math.floor(this.getDayDiffTo(days) / dayPerSeason);
};