tobias88 / Pmanager.org Club Team Players

// ==UserScript==
// @name          Pmanager.org Club Team Players
// @namespace     http://tampermonkey.net/
// @description   Fetch All of the Club Team Players
// @include       https://www.pmanager.org/ver_equipa.asp?equipa=*&vjog=1
// @require	      http://code.jquery.com/jquery-latest.js
// @version       2.03
// @license MIT
// @grant         GM_xmlhttpRequest
// ==/UserScript==

let $ = window.$;

let button;
let tbody;
let players;
const originalColumnCount = 13;
let originalTeamPlayersFontCount;
const teamPlayersFontPerPlayer = 8;
let fetched = false;

$(document).ready(function()
{
	tbody = $("#feature").find("tbody").eq(1);
	players = tbody.find(".list1, .list2");
    if(players.eq(0).find("td").size() == originalColumnCount)
        CreateButton();
}); // ready

async function FetchPlayers()
{
	if (fetched || false == confirm("Ne használd gyakran, mert az ingyenes tárhelyünk nem szereti!\nBiztosan szeretnéd?"))
		return;
	fetched = true;
	button.find("img").attr("src","https://style-cdn.ravelrycache.com/images/progress.gif");
	originalTeamPlayersFontCount = players.find("font.team_players").size();
	while (originalTeamPlayersFontCount != players.find("font.team_players").size() + teamPlayersFontPerPlayer * players.size())
	{
		DownloadPlayerData();
		await Sleep(2000);
	}
	ModifyHeader();
	console.log("DONE!");
	button.find("img").attr("src","http://www.pmanagertutorial.nhely.hu/images/other/ok.png");
}

function Sleep(ms)
{
	return new Promise(resolve => setTimeout(resolve, ms));
}

async function DownloadPlayerData()
{
	console.log("DownloadPlayerData");
	// gathering data
	const playerLinks = players.find("a[href*='jog_id']");
	for (let playerNumber = 0; playerNumber < playerLinks.size(); ++playerNumber)
	{
		if (players.eq(playerNumber).find("td").size() != originalColumnCount)
		{
			console.log(playerNumber + " already downloaded");
			continue;
		}
		const href = playerLinks.eq(playerNumber).attr("href");
		const afterEqualityPos = href.indexOf("=") + 1;
		const playerId = href.substr(afterEqualityPos, href.length - afterEqualityPos);
		console.log(playerId + " download");
		(function(playerNumber)
		{ // save playerNumber, because ajax is asyncronous
			GM_xmlhttpRequest({
				method: "GET",
				url: "http://www.pmanagertutorial.nhely.hu/pm_piac/json.php?name=json_name&password=json_password&id=" + playerId,
				timeout: 5000,
				onload: function (xhr)
				{
					if (xhr.responseText.charAt(0) == "[")
					{
						let data;
						try
						{
							data = JSON.parse(xhr.responseText);
						}
						catch(e)
						{
							data = eval("(" + xhr.responseText + ")");
						}
						ShowPlayerData(data, playerNumber);
					}
				}
			});
		})(playerNumber);//*/
		// test for not found
		/*const responseText = '[{"Poszt":"?","Nev":"?","ID":"?","Kor":"?","Vedes":"?","Keresztezes":"?","Reflex":"?","Hatarozottsag":"?",\n' +
                              '"Szereles":"?","Fejjatek":"?","Atadas":"?","Helyezkedes":"?","Golszerzes":"?","Technika":"?","Gyorsasag":"?",\n' +
                              '"Ero":"?","Datum":"?","GS":"?","Orszag":"?","Adottsag":"?","Tehetseg":"?"}]';
        const data = JSON.parse(responseText);
        //ShowPlayer(data, playerNumber);
        // test for found
        const responseText2 = '[{"Poszt":"V JK","Nev":"A. Karaivanov","ID":"15558582","Kor":"31","Vedes":"8","Keresztezes":"1","Reflex":"8",' +
                               '"Hatarozottsag":"4","Szereles":"19","Fejjatek":"20","Atadas":"6","Helyezkedes":"1","Golszerzes":"2","Technika":"5",' +
                               '"Gyorsasag":"5","Ero":"20","Datum":"2018-02-12","GS":"73.0%","Orszag":"99","Adottsag":"Nincs","Tehetseg":"Pocsék"}]'
        const data2 = JSON.parse(responseText2);
        //ShowPlayer(data, playerNumber);
        ShowPlayerData(playerNumber % 2 ? data2 : data, playerNumber);//*/
	}
}

function ModifyHeader()
{
	// modify existed columns
	const header = tbody.find("td.cabecalhos");
	header.eq(0).replaceWith("<td class=\"cabecalhos\" width=\"3%\" align=\"center\">Poz</td>");
	header.eq(2).text("Kor");
	header.eq(4).replaceWith("<td class=\"cabecalhos\" width=\"2%\" align=\"center\"></td>");
	header.eq(5).replaceWith("<td class=\"cabecalhos\" width=\"3%\" align=\"center\">M</td>");
	// remove
	header.eq(9).remove();
	header.eq(8).remove();
	header.eq(7).remove();
	// the new column
	header.eq(6).replaceWith("<td class=\"cabecalhos\" width=\"100%\" align=\"center\">Adat</td>");
	header.eq(10).attr("align", "center");
	header.eq(11).attr("align", "center");
}

function ShowPlayerData(data, playerNumber)
{
	const myLineTd = players.eq(playerNumber).find("td");
	myLineTd.eq(10).remove();
	myLineTd.eq(9).remove();
	myLineTd.eq(8).remove();
	for (let column = 11; column < 13; ++column)
	{
		const cell = myLineTd.eq(column);
		const imgs = cell.find("img");
		const cellSrc = imgs.eq(1).attr("src");
		const color = cellSrc.substr(4, cellSrc.length - 4 - 7);
		imgs.remove();
		cell.attr("align", "center");
		cell.attr("style", "background-color:" + (color == "green" ? "LightGreen" : color == "red" ? "Tomato" : "LightSalmon ") + ";");
	}
	if (data[0].ID == "?")
	{
		myLineTd.eq(7).text("");
		return;
	}
	// add
	myLineTd.eq(3).append("(" + data[0].Kor + ")");
	myLineTd.eq(7).html(GetDataString(data));
	myLineTd.eq(12).append("(" + data[0].Tehetseg.trim() + ")");
}

function GetDataString(data)
{
	let ret = "<u>" + data[0].Datum + ":</u> ";
	if (data[0].Poszt.charAt(0) == "G")
	{
		ret += "<b><u> " + data[0].Vedes + "</u>-<u>" + data[0].Keresztezes + "</b></u> " +
            "<b> " + data[0].Reflex + "-" + data[0].Hatarozottsag + "</b> " +
            data[0].Gyorsasag + "-" + data[0].Ero +
            "<br>(Sz: " +data[0].Szereles + ", F: " + data[0].Fejjatek + ", Á: " + data[0].Atadas + ")";
	}
	else if (data[0].Poszt.charAt(0) == "V")
	{
		ret += "<b><u> " + data[0].Szereles + "</u>-<u>" + data[0].Fejjatek + "</b></u> " +
            "<b> " + data[0].Atadas + "-" + data[0].Helyezkedes + "</b> " +
            data[0].Gyorsasag + "-" + data[0].Ero +
            "<br>";
	}
	else if (data[0].Poszt.charAt(0) == "K")
	{
		ret += "<b><u> " + data[0].Atadas + "</u>-<u>" + data[0].Helyezkedes + "</b></u> " +
            "<b> " + data[0].Szereles + "-" + data[0].Technika + "</b> " +
            data[0].Gyorsasag + "-" + data[0].Ero +
            "<br>(F: " + data[0].Fejjatek + ", G: " + data[0].Golszerzes + ")";

	}
	else if (data[0].Poszt.charAt(0) == "T")
	{
		ret += "<b><u> " + data[0].Golszerzes + "</u>-<u>" + data[0].Technika + "</b></u> " +
            "<b> " + data[0].Fejjatek + "-" + data[0].Helyezkedes + "</b> " +
            data[0].Gyorsasag + "-" + data[0].Ero +
            "<br>(Sz: " + data[0].Szereles + ", Á: " + data[0].Atadas + ")";
	}
	ret += " <u>Adottság:</u> " + data[0].Adottsag;
	return ret;
}

function CreateButton()
{
	button = tbody.find("td").eq(0);
	button.html(
		"<a href=\"javascript:void(0);\"><img title=\"Adat Lekérés\" src=\"http://www.pmanagertutorial.nhely.hu/images/other/arrow_down.png\"></a>"
	);
	button.click(function()
	{
		FetchPlayers();
	});
}